www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Thinking about stack traces in D

reply Myron Alexander <someone somewhere.com> writes:
I have been thinking about exception design as a whole and for D 
specifically. The lack of stack traces make certain types of bug tracing 
very difficult so I went a googling looking for the answer and came 
across this article in Dr. Dobbs:

http://www.ddj.com/184405270

This paragraph, in particular, has me interested:

 Hooking Entry and Exit
 
 This version uses an explicit code to report the function name (as a string).
An alternative, used by code profilers (such as Numega TrueTime), is to parse
the binary code, and inject hooks to it. The best alternative is to use
compiler supplied hooks. In this hook—_penter() and _pexit()—the function
address is written to the registrar. A symbol engine can then convert the
address to a well-formed function name. This requires compilation with debug
symbols. The technique is thoroughly described by John Robbins in his article
on creating the Smooth Working Set tool (MSDN Magazine, October and December
2000; http://msdn.microsoft.com/msdnmag/
issues/1000/bugslayer/bugslayer1000.asp). This technique seems to be the ideal
solution, but since I use VC 6, the compiler supplies only the _penter(). I
tried injecting my own _pexit() from it but failed. A full explanation is
beyond the scope of this article.
 
Does D have entry and exit hooks already and, if not, would Walter add them? Regards, Myron.
Sep 16 2007
next sibling parent reply Jascha Wetzel <firstname mainia.de> writes:
Myron Alexander wrote:
 I have been thinking about exception design as a whole and for D 
 specifically. The lack of stack traces make certain types of bug tracing 
 very difficult so I went a googling looking for the answer and came 
 across this article in Dr. Dobbs:
 
 http://www.ddj.com/184405270
 
 This paragraph, in particular, has me interested:
 
 Hooking Entry and Exit

 This version uses an explicit code to report the function name (as a 
 string). An alternative, used by code profilers (such as Numega 
 TrueTime), is to parse the binary code, and inject hooks to it. The 
 best alternative is to use compiler supplied hooks. In this 
 hook—_penter() and _pexit()—the function address is written to the 
 registrar. A symbol engine can then convert the address to a 
 well-formed function name. This requires compilation with debug 
 symbols. The technique is thoroughly described by John Robbins in his 
 article on creating the Smooth Working Set tool (MSDN Magazine, 
 October and December 2000; http://msdn.microsoft.com/msdnmag/ 
 issues/1000/bugslayer/bugslayer1000.asp). This technique seems to be 
 the ideal solution, but since I use VC 6, the compiler supplies only 
 the _penter(). I tried injecting my own _pexit() from it but failed. A 
 full explanation is beyond the scope of this article.
Does D have entry and exit hooks already and, if not, would Walter add them? Regards, Myron.
as a side note, on windows you can use ddbg (http://ddbg.mainia.de) to get full stack traces.
Sep 16 2007
parent Myron Alexander <someone somewhere.com> writes:
Jascha Wetzel wrote:
 as a side note, on windows you can use ddbg (http://ddbg.mainia.de) to 
 get full stack traces.
Thanks Jascha, I didn't know that.
Sep 16 2007
prev sibling next sibling parent reply kris <foo bar.com> writes:
Myron Alexander wrote:
 I have been thinking about exception design as a whole and for D 
 specifically. The lack of stack traces make certain types of bug tracing 
 very difficult so I went a googling looking for the answer and came 
 across this article in Dr. Dobbs:
 
 http://www.ddj.com/184405270
 
 This paragraph, in particular, has me interested:
 
 Hooking Entry and Exit

 This version uses an explicit code to report the function name (as a 
 string). An alternative, used by code profilers (such as Numega 
 TrueTime), is to parse the binary code, and inject hooks to it. The 
 best alternative is to use compiler supplied hooks. In this 
 hook—_penter() and _pexit()—the function address is written to the 
 registrar. A symbol engine can then convert the address to a 
 well-formed function name. This requires compilation with debug 
 symbols. The technique is thoroughly described by John Robbins in his 
 article on creating the Smooth Working Set tool (MSDN Magazine, 
 October and December 2000; http://msdn.microsoft.com/msdnmag/ 
 issues/1000/bugslayer/bugslayer1000.asp). This technique seems to be 
 the ideal solution, but since I use VC 6, the compiler supplies only 
 the _penter(). I tried injecting my own _pexit() from it but failed. A 
 full explanation is beyond the scope of this article.
Does D have entry and exit hooks already and, if not, would Walter add them? Regards, Myron.
All Tango exceptions (apart from OutOfMemoryException) are derived from one which can be hooked by an external backtrace facility. This means that, for example, when you link Tango with the Flectioned library you will get a backtrace for the standard Tango exceptions also
Sep 16 2007
parent reply Myron Alexander <someone somewhere.com> writes:
kris wrote:
 All Tango exceptions (apart from OutOfMemoryException) are derived from 
 one which can be hooked by an external backtrace facility. This means 
 that, for example, when you link Tango with the Flectioned library you 
 will get a backtrace for the standard Tango exceptions also
Kris, You guys have done some excellent work. I had a look at the Flectioned library and that might just be the thing I need but I would have to work with it before I get a better idea. Just FYI, I have been experimenting with exception design and came up with the idea for exception properties. Details are here http://preparingforwinter.blogspot.com/ . I would like to know what you think of the idea. Thanks for the help, Myron.
Sep 16 2007
parent reply kris <foo bar.com> writes:
Myron Alexander wrote:
 kris wrote:
 All Tango exceptions (apart from OutOfMemoryException) are derived 
 from one which can be hooked by an external backtrace facility. This 
 means that, for example, when you link Tango with the Flectioned 
 library you will get a backtrace for the standard Tango exceptions also
Kris, You guys have done some excellent work.
That's nice of you to say so ... thanks from all of us: http://www.dsource.org/projects/tango/wiki/Contributors :)
 Just FYI, I have been experimenting with exception design and came up 
 with the idea for exception properties. Details are here 
 http://preparingforwinter.blogspot.com/ . I would like to know what you 
 think of the idea.
Interesting idea! I wonder if such detail might perhaps belong in a logging layer instead of an exception layer? It is provocative, regardless :) - Kris (and, I enjoyed your "prep for winter" analogy)
Sep 16 2007
parent Myron Alexander <someone somewhere.com> writes:
kris wrote:
 That's nice of you to say so ... thanks from all of us: 
 http://www.dsource.org/projects/tango/wiki/Contributors
 
 :)
The picture is very scary. Please don't hurt me :) Is that really a picture of the team? The plastic clothing is done very well although I would not be able to stop myself popping the bubbles :)
 Interesting idea! I wonder if such detail might perhaps belong in a 
 logging layer instead of an exception layer? It is provocative, 
 regardless :)
 
I am looking at code structure from many different angles. Exception design use-cases and methodology is my current mad experiment as I have not been happy with the way they work in the frameworks and platforms I tried. Java is a case in point. I do a lot of Java development but find most library exceptions to be cryptic and very difficult to use programmatically; especially the JDBC exceptions. Every attempt to build fault tolerant systems that work with JDBC brings forth cussing to obliterate entire forests of cuss-burs. Then a promise to show "them" how a database interface should be done. As it turns out, promises are easier said than done :)
 
 (and, I enjoyed your "prep for winter" analogy)
Thanks. My current favourite animal is the saber-tooth squirrel from Ice Age. Whenever I code, I think of that squirrel and his nut. All he wants is the most simple thing but even the simplest things can get complex. Somehow, thinking about the squirrel and his antics led me to the analogy but I cannot stop thinking about the one scene where he tries to defrost the nut and ends up with popcorn; reminds me of a significant portion of my code :) Keep up the good work with Tango. Myron.
Sep 16 2007
prev sibling parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
If you use DMD on win32, there's the Phobos backtrace hack by shinh: 
http://shinh.skr.jp/d/backtrace.zip

I've tweaked it a little and updated to DMD 1.020. It can be dloaded 
from http://team0xf.com/download/phoboshack/PhobosHackDmd1.20.zip , just 
replace your phobos.lib

If you need a version for DMD 1.021, let me know and I'll recompile it.


If you're a dmd+win32+Tango user, I've just committed a patch into 
Tango's SVN repo, 'patches/BacktraceHack-DMD-Win32.diff', which is a 
port of the original hack with numerous tweaks. I've also created a 
patching program, which can be found at 
http://h3.team0xf.com/tangoTrace.7z and should allow patching of Tango 
installed through DSSS or other means, as long as it's a revision > 2565.

How to use it? Just compile with -g (and in case of Tango, make sure to 
pass tango.lib to the compilation cmd), do not catch the exception at 
the end of the program and you should be given a backtrace when 
something gets thrown. Especially nice is the fact that it can catch 
access violations.

That'd be my 2 cents ;)


-- 
Tomasz Stachowiak
http://h3.team0xf.com/
h3/h3r3tic on #D freenode
Sep 16 2007
parent reply Myron Alexander <someone somewhere.com> writes:
Tom,

Thanks. Are you willing to release the source? I am curious as to how it 
works.

Regards,

Myron.
Sep 16 2007
parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Myron Alexander wrote:
 Tom,
 
 Thanks. Are you willing to release the source? I am curious as to how it 
 works.
Ah, right... I forgot that the phobos hack's 7z only contains the final lib. Here's what I use to create it: http://h3.team0xf.com/phobosTrace.7z The .bat is configured for my system and requires msys, but it shouldn't be hard to tweak it a bit, like I did with the port for Tango. Cheers! -- Tomasz Stachowiak http://h3.team0xf.com/ h3/h3r3tic on #D freenode
Sep 17 2007