Unfolding the stack

Ryan Johnson ryan.johnson@cs.utoronto.ca
Mon Mar 12 12:18:00 GMT 2012


On 12/03/2012 5:39 AM, Michel Bardiaux wrote:
> To complete the port of some library to Cygwin, I need a way to produce
> a traceback with as much info as possible. Currently I have something
> that works but not that well.  There are basically 3 parts:
>
> * Gather all the stack frames; see below.
> * Assign the PCs in each frame to some executable image; done via dlfcn.
> To be discussed later.
> * Convert the PCs to function+source+line; done via addr2line. Ditto.
>
> Despite a lot of web digging I have not (yet?) found a better way than
> this classic unixish song-and-dance:
>
> [...
>       snipped source that manually parses output
>       of forked child's call to cygwin_stackdump()
> ...]
>
> Now this is acceptable for a traceback following the raising of some
> error condition, but seems much too heavy for using in a leak detector.
> Does anyone know of an equivalent API under cygwin? Or could we consider
> adding such a variation of cygwin_stackdump to the cygwin DLL?
This is hard in C, and harder in code compiled by gcc due to things like 
-fomit-frame-pointer and gcc's tendency to use alloca under the hood for 
large stack allocations. This is nothing unique to cygwin, but it does 
mean that cygwin's stack dumper is unsafe to use because it may get 
something wrong and go to la-la-land. This seems to work ok-ish during 
error-before-abort but not for code that hopes to continue running 
reliably after the call.

If you're using C++, and if exceptions are enabled, then you could use 
gcc's <unwind.h> to non-destructively unwind any stack frame that an 
exception might propagate through/into. Unfortunately, interesting 
functions don't always get unwind info, and traversing one of those can 
cause seg faults during unwinding. Also unfortunately, this trick seems 
broken in the latest version of cygwin/gcc on my machine (the example I 
developed a while back under linux seg faults).

Alternatively, you could compile with -g and try to traverse the debug 
info tables gdb uses to work around everything nasty gcc does, but 
there's no clean API there that I know of.

Out of curiosity, what is your library currently using to generate 
backtraces? There's a backtrace facility in glibc (man backtrace), but 
it's got a long list of caveats as well, including death by 
-fomit-frame-pointer (it doesn't use any debug/unwind info emitted by 
the compiler).

Ryan




--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list