Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get stack backtrace of a running process

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?

like image 316
jsc Avatar asked Jan 06 '10 10:01

jsc


People also ask

How do I get stack trace on Linux?

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.

How do I get backtrace on Linux?

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.


1 Answers

You need libunwind

like image 151
vitaly.v.ch Avatar answered Oct 19 '22 00:10

vitaly.v.ch