Reputation: 83
What I want is to get stacktrace of all threads for running process using c/c++.
The different ways which I know to get stacktrace :
we have backtrace() api but problem with this is that it only gives stacktrace of current thread. Does any one know how to associate it with each running thread?
Second way I tried is using pstack command. pstack takes input as pid of running process shows all stack of all threads. But problem with this is that it is not a C/C++ api so we can't use it in our code. (When I study) pstack is a shell file which in turn used gdb's bt command.
Does anyone know different ways which will help me to get stacktrace of all threads for running process?
Upvotes: 3
Views: 4352
Reputation: 30010
Maybe you can use ptrace
. Attach to all your threads (except for the thread that prints the stacktrace), and you can get register values with PTRACE_GETREGS
. Then you can do stack unwinding (maybe you will need information stored in the .elf file to do this reliably). Note, that you have to take special care, if a thread is just creating/destroying its stack frame. And you may need debug information from the elf to do this reliably (you'd surely need that if your code compiled with omitted frame pointers).
Not an easy task to do this by hand, but surely can be done.
Upvotes: 2