Welcome to Web-News
A Web-based News Reader
Subject Re: Exception Hierarchy [WAS: Re: Top 5]
From Frits van Bommel <fvbommel@REMwOVExCAPSs.nl>
Date Sat, 18 Oct 2008 18:03:20 +0200
Newsgroups digitalmars.D
Attachment(s) trace.zip

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.stacktrace; // 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.


Recent messages in this thread
 
-# Re: Exception Hierarchy [WAS: Re: Top 5] Benji Smith 17-Oct-2008 07:48 pm
|-# Re: Exception Hierarchy [WAS: Re: Top 5] Sean Kelly 17-Oct-2008 08:35 pm
|.-# Re: Exception Hierarchy [WAS: Re: Top 5] Benji Smith 17-Oct-2008 09:00 pm
|..-# Re: Exception Hierarchy [WAS: Re: Top 5] Brad Roberts 17-Oct-2008 09:39 pm
|...-# Re: Exception Hierarchy [WAS: Re: Top 5] (Current message) Frits van Bommel 18-Oct-2008 12:03 pm
|....-# Re: Exception Hierarchy [WAS: Re: Top 5] Sean Kelly 18-Oct-2008 12:30 pm
|.....|# Re: Exception Hierarchy [WAS: Re: Top 5] Robert Fraser 18-Oct-2008 12:56 pm
|.....\# Re: Exception Hierarchy [WAS: Re: Top 5] Frits van Bommel 18-Oct-2008 01:47 pm
-# Re: Exception Hierarchy [WAS: Re: Top 5] Jarrett Billingsley 18-Oct-2008 12:36 pm
.|# Re: Exception Hierarchy [WAS: Re: Top 5] Sean Kelly 18-Oct-2008 02:16 pm
.\# Re: Exception Hierarchy [WAS: Re: Top 5] bearophile 18-Oct-2008 02:39 pm