Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ get backtrace of a different thread [duplicate]

I am looking to understand what is the state of a specific thread in my software, doing it from another thread. Specifically I'd like to know if it's I/O stuck. I was thinking of doing it by getting the backtrace(unless someone has another idea?), since I know what function it's supposed to be stuck on.. but I can't figure out how to get the backtrace of that specific thread, without calling the SEGFAULT handler... but gdb is able to do it(I doubt he creates SEGFAULTS..)

Can anyone help? any idea?

[Edit] all 3 answers refer to gdb, I KNOW I can do it from gdb, I wanted to know how to do it from a software(even linking to gdb libs somehow would be an answer, but how ? )

like image 433
Alon Avatar asked Aug 25 '14 13:08

Alon


2 Answers

I know what function it's supposed to be stuck on.. but I can't figure out how to get the backtrace of that specific thread

You can get backtraces of all threads and try to find function which is supposed to be stuck on in backtraces output. Here is how to get all backtraces in gdb:

(gdb) thread apply all bt
like image 190
ks1322 Avatar answered Oct 13 '22 18:10

ks1322


(gdb) info threads [will list all the threads and also indicate the thread you are currently backtracing on]

(gdb) thread apply all bt [will show backtrace of all threads so that you can see which thread is stuck on the function you are interested in before switching to that thread]

(gdb) thread #threadno [will switch the backtrace to the specific thread you are interested in and a bt will show its backtrace.]

Ref http://www.delorie.com/gnu/docs/gdb/gdb_25.html

like image 1
jayadev Avatar answered Oct 13 '22 17:10

jayadev