Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 3050cc7

Browse files
committed
Merge pull request #1354 from yazd/dwarf-debug
Add file and line info to backtraces under Linux
2 parents 15cde50 + 2f5ff93 commit 3050cc7

File tree

8 files changed

+800
-19
lines changed

8 files changed

+800
-19
lines changed

mak/SRCS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ SRCS=\
130130
src\rt\trace.d \
131131
src\rt\tracegc.d \
132132
\
133+
src\rt\backtrace\dwarf.d \
134+
src\rt\backtrace\elf.d \
135+
\
133136
src\rt\util\array.d \
134137
src\rt\util\hash.d \
135138
src\rt\util\random.d \

src/core/runtime.d

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,9 @@ Throwable.TraceInfo defaultTraceHandler( void* ptr = null )
531531
stackPtr < stackBottom &&
532532
numframes < MAXFRAMES; )
533533
{
534-
callstack[numframes++] = *(stackPtr + 1);
534+
enum CALL_INSTRUCTION_SIZE = 1; // it may not be 1 but it is good enough to get
535+
// in CALL instruction address range for backtrace
536+
callstack[numframes++] = *(stackPtr + 1) - CALL_INSTRUCTION_SIZE;
535537
stackPtr = cast(void**) *stackPtr;
536538
}
537539
}
@@ -548,39 +550,64 @@ Throwable.TraceInfo defaultTraceHandler( void* ptr = null )
548550

549551
override int opApply( scope int delegate(ref size_t, ref const(char[])) dg ) const
550552
{
551-
version( Posix )
553+
version(Posix)
552554
{
553-
// NOTE: The first 5 frames with the current implementation are
555+
// NOTE: The first 4 frames with the current implementation are
554556
// inside core.runtime and the object code, so eliminate
555557
// these for readability. The alternative would be to
556558
// exclude the first N frames that are in a list of
557559
// mangled function names.
558-
static enum FIRSTFRAME = 5;
560+
enum FIRSTFRAME = 4;
559561
}
560-
else version( Windows )
562+
else version(Windows)
561563
{
562564
// NOTE: On Windows, the number of frames to exclude is based on
563565
// whether the exception is user or system-generated, so
564566
// it may be necessary to exclude a list of function names
565567
// instead.
566-
static enum FIRSTFRAME = 0;
568+
enum FIRSTFRAME = 0;
567569
}
568-
int ret = 0;
569570

570-
const framelist = backtrace_symbols( callstack.ptr, numframes );
571-
scope(exit) free(cast(void*) framelist);
571+
version(linux)
572+
{
573+
import core.internal.traits : externDFunc;
574+
575+
alias traceHandlerOpApplyImpl = externDFunc!(
576+
"rt.backtrace.dwarf.traceHandlerOpApplyImpl",
577+
int function(const void*[], scope int delegate(ref size_t, ref const(char[])))
578+
);
572579

573-
for( int i = FIRSTFRAME; i < numframes; ++i )
580+
if (numframes >= FIRSTFRAME)
581+
{
582+
return traceHandlerOpApplyImpl(
583+
callstack[FIRSTFRAME .. numframes],
584+
dg
585+
);
586+
}
587+
else
588+
{
589+
return 0;
590+
}
591+
}
592+
else
574593
{
575-
char[4096] fixbuf;
576-
auto buf = framelist[i][0 .. strlen(framelist[i])];
577-
auto pos = cast(size_t)(i - FIRSTFRAME);
578-
buf = fixline( buf, fixbuf );
579-
ret = dg( pos, buf );
580-
if( ret )
581-
break;
594+
const framelist = backtrace_symbols( callstack.ptr, numframes );
595+
scope(exit) free(cast(void*) framelist);
596+
597+
int ret = 0;
598+
for( int i = FIRSTFRAME; i < numframes; ++i )
599+
{
600+
char[4096] fixbuf;
601+
auto buf = framelist[i][0 .. strlen(framelist[i])];
602+
auto pos = cast(size_t)(i - FIRSTFRAME);
603+
buf = fixline( buf, fixbuf );
604+
ret = dg( pos, buf );
605+
if( ret )
606+
break;
607+
}
608+
return ret;
582609
}
583-
return ret;
610+
584611
}
585612

586613
override string toString() const

0 commit comments

Comments
 (0)