line numbers for linux exception traces

Adam D. Ruppe via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Wed Aug 5 08:57:44 PDT 2015


If you just download this little file:

http://arsdnet.net/dcode/linetrace.d

and add it to your build, when compiling in debug mode, it will 
translate the addresses into line numbers (by simply piping out 
to addr2line on the command line)

No modification to your program is required.


before: dmd yourapp.d -g

object.Exception at x.d(4): test
----------------
./x(_Dmain+0xb) [0x808276b]
./x(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x12) [0x809215a]
./x(void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x18) 
[0x80920d0]
./x(void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll()+0x27) [0x809211f]
./x(void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x18) 
[0x80920d0]
./x(_d_run_main+0x166) [0x8092066]
./x(main+0x14) [0x8085b9c]
/lib/libc.so.6(__libc_start_main+0xf3) [0xf7321773]


after: dmd yourapp.d linetrace.d -g

object.Exception at x.d(4): test
----------------
./x(void x.t()+0x3e) [/home/me/test/x.d:5]
./x(_Dmain+0xb) [/home/me/test/x.d:11]
./x(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x12) [0x80b993a]
./x(void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x18) 
[0x80b98b0]
./x(void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll()+0x27) [0x80b98ff]
./x(void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x18) 
[0x80b98b0]
./x(_d_run_main+0x166) [0x80b9846]
./x(main+0x14) [0x80ac71c]
/lib/libc.so.6(__libc_start_main+0xf3) [0xf7318773]




Notice the line numbers on the first two lines.


I also added a -version=hide_names that will cut out the function 
names (and the druntime lines) with the logic being that the line 
number in your own code is probably the most relevant part to you 
anyway and you don't need any distractiions:

dmd yourapp.d linetrace.d -g -version=hide_names

object.Exception at x.d(4): test
----------------
/home/me/test/x.d:5
/home/me/test/x.d:11


My code isn't too complex: it wraps the default handler then 
manipulates the string using plain phobos techniques. Inefficient 
surely, but since the program is probably dying anyway when this 
is called, I don't mind it. addr2line does need to be available 
on the system for this to work though. If it isn't, the old 
address behavior should still work.

I suspect this will also work on other posix systems but I 
haven't tried.


But the simple code there ought to be easy enough for you to 
customize to your liking too.


More information about the Digitalmars-d-announce mailing list