Exception Hierarchy [WAS: Re: Top 5]
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Sat Oct 18 08:54:43 PDT 2008
Brad Roberts wrote:
> For anything running on top of glibc (ie, every linux distribution) it's
> fairly trivial via backtrace() and backtrace_symbols() found in
> execinfo.h. The symbols are the mangled form, but that's a simple matter
> of code to translate back to more meaningful names. An incomplete version
> of that is available in phobos already (and probably somewhere in Tango
> too) -- and it should really be in the core runtime, probably.
>
> Given the 2 or 3 implementations floating around, I imagine it wouldn't be
> hard to integrate for both windows and linux.
Hey, those are a handy pair of functions.
I just implemented a backtrace handler for Tango using those.
The source is attached.
Features:
* Produces stacktraces (obviously).
* Easy to use: just make sure it gets linked in (e.g. by importing
trace.backtrace and using DSSS). The static constructor will then
register as a stack trace provider with the runtime, and every exception
will contain a stack trace.
* Uses a slightly modified std.demangle (just enough to get it to
compile with Tango) to demangle function names.
* Filters out initial frames from its own module since they're not
particularly interesting.
* Lazily converts a stack trace to strings. Traces that are never
printed only allocate the trace object and a list of return addresses.
Stack traces that are printed multiple times only allocate the strings once.
* Shouldn't be hard to port to Phobos.
Less desirable behavior:
* toString() returns a fresh string every time. This could be prevented
with some more bookkeeping but I'm not sure it's worth it.
Everyone: feel free to use this any way you want. In particular, feel
free to commit this to the Tango, Phobos and/or druntime repositories :).
Example:
=====
$ cat test.d
module test;
import trace.backtrace; // just so DSSS picks it up.
void main() {
foo();
}
void foo() {
bar();
}
void bar() {
throw new Exception("Just showing off");
}
$ dsss build
[[[snip]]]
$ ./test
object.Exception: Just showing off
----------------
./test(class Exception object.Exception._ctor(char[], class
Exception)+0x24) [0x42b174]
./test(void test.bar()+0x37) [0x4186ba]
./test(void test.foo()+0x9) [0x418681]
./test(_Dmain+0x9) [0x418671]
./test [0x42dece]
./test [0x42dfae]
./test [0x42e540]
./test [0x42dfae]
./test(_d_run_main+0xbb) [0x42e42b]
/lib/libc.so.6(__libc_start_main+0xf4) [0x7fd1aacff1c4]
./test [0x4185d9]
=====
Note: the trace may be wrapped in your newsreader.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: trace.zip
Type: application/zip
Size: 6261 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20081018/ca948981/attachment.zip>
More information about the Digitalmars-d
mailing list