[LLVMdev] Printing a stack trace?

Talin viridia at gmail.com
Fri Feb 22 17:00:36 PST 2008


I'd like to be able to catch an exception and print a stack trace,
showing the call stack with source file and line number (using
embedded debugging information I would guess.) Obviously, I want the
call stack to be evaluated as lazily as possible - the actual throwing
of the exception should do as little up-front work as possible, with
the bulk of the work only being done if the the exception handler
actually chooses to print a stack trace (instead of handling the
exception silently, which will be the more common case.)

What's the general strategy for achieving this? At the site where the
exception is caught, the stack will already have been unwound, and the
frames destroyed. So the stack trace has to be captured before this.
But if the exception handler decides not to print a stack trace, then
all the work that went into capturing the frame information will be
wasted.

The only approach I can think of is to construct a list of frame
pointers as I unwind, and then turn them into human-readable form if
requested. This has two drawbacks: First, it means that *every* call
instruction has to be an invoke, and second it means that even if the
exception is handled without printing, I still pay the cost of
building the frame pointer list (although not the entire stack - the
cost is proportional to the number of frames between the throw site
and the catch site.)

-- 
-- Talin



More information about the llvm-dev mailing list