views:

87

answers:

1

Hello.

I am compiling C++ on *nix and I would like to generate a stack dump a) at an arbitrary point in the program, b) during any signal, particularly during SIGSEGV.

Google tells me that ptrace is probably the tool for the job, but I can't find any comprehensible examples of walking the stack. Getting the return address, yeah, but what about the NEXT return address? And what about extracting the symbolic name of the function at that point? Something to do with DWARF?

Many thanks if you can tell me where to go from here.

+2  A: 

If you are using glibc, then the GNU functions backtrace() and backtrace_symbols() are the best way to do this. Walking the stack is going to be environment-specific anyway, so there's no downside to using the non-portable native functions on each platform to do it.

caf