[LLVMdev] Printing a stack trace?

Gordon Henriksen gordonhenriksen at mac.com
Fri Feb 22 18:23:08 PST 2008


On 2008-02-22, at 20:00, Talin wrote:

> 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.) [...]
>
> 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.)


Hi Talin,

Using invokes as you suggest will only capture frames from the callee  
to the catch block. Rather than embedding this logic throughout your  
output program, I think you might prefer to augment your runtime with  
a routine which captures the return addresses from the stack. This  
does avoid the cost of rummaging through source information, although  
it's probably still rather expensive. Perhaps you can use gcc's  
unwinder as a basis for this algorithm.

Surely it would also be useful for GC. :)

— Gordon





More information about the llvm-dev mailing list