Does anyone know about a code snippet that can be used
to output a backtrace just like gdb's "backtrace" command
does. One that can be called on a segmentation fault.
(without resolving symbol names, just simple list of frames
until the main() stack frame)
This cannot be done portably. GDB does it with code that includes
special cases -- as in, code written especially for every single
machine architecture for which gdb does it. There is *some*
code-sharing, e.g., via the "bfd" library, but it really, truly
requires that you write new code every time you port gdb to a new
machine. Sometimes that code is simple, but sometimes it is
horribly complex, and requires cooperation with the compiler.
(In particular, on architectures in which there are different
methods for returning from different function classifications,
one might need to look in different registers or stack locations
or similar depending on the return type and/or "leaf-ness" of
any particular function.)
In a few cases (which one might hope are rare), it cannot be done
at all, because by the time you get something like a "segmentation
fault", the system has removed critical information that the program
that encountered the problem would need. (For a debugger to work
at all on these systems, they have to provide a special, outside-the-
"segmentation-fault" communications channel. Systems with no
debugging facilities at all do exist as well, though.)