I'm trying to get the stack backtrace of a running process (PID and binary path are known) from another program. The program in question is written in C++ and compiled with g++ with gdb debugging symbols.
I'm looking for a C or C++ library that works across Linux, HP-UX and Solaris that produces an output similar to the output of calling popen() with something like this:
gdb -batch -x /dev/stdin <BINARY> <PID> << EOF
thread apply all bt
EOF
I have found lsstack ( http://sourceforge.net/projects/lsstack/ ), which is Linux only and know the pstack program for Solaris.
Does anyone know of a reliable way of doing this cross Unix / POSIX?
In order to get a stack trace on Linux or Windows one must have either the GNU Debugger, gdb, or the LLVM Debugger, lldb, installed. Linux users should their package manager to install one or the other if necessary.
backtrace() returns a backtrace for the calling program, in the array pointed to by buffer. A backtrace is the series of currently active function calls for the program. Each item in the array pointed to by buffer is of type void *, and is the return address from the corresponding stack frame.
You need libunwind
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With