[ACCEPTED]-Getting a backtrace of other thread-backtrace

Accepted answer
Score: 11

I implemented that myself here.

Initially, I 25 wanted to implement something similar as 24 suggested here, i.e. getting somehow the top 23 frame pointer of the thread and unwinding 22 it manually (the linked source is derived 21 from Apples backtrace implementation, thus might 20 be Apple-specific, but the idea is generic).

However, to 19 have that safe (and the source above is 18 not and may even be broken anyway), you 17 must suspend the thread while you access 16 its stack. I searched around for different 15 ways to suspend a thread and found this, this and 14 this. Basically, there is no really good way. The 13 common hack, also used by the Hotspot JAVA VM, is to use signals and 12 sending a custom signal to your thread via 11 pthread_kill.

So, as I would need such signal-hack anyway, I 10 can have it a bit simpler and just use backtrace inside 9 the called signal handler which is executed 8 in the target thread (as also suggested 7 here by sandeep). This is basically what my implementation 6 is doing.

If you are also interested in printing 5 the backtrace, i.e. get some useful debugging 4 information (function name, source code 3 filename, source code line number, ...), read 2 here about an extended backtrace_symbols based on libbfd. Or 1 just see the source here.

Score: 9

Signal Handling with the help of backtrace 7 can solve your purpose.

I mean if you have 6 a PID of the Thread, you can raise a signal 5 for that thread. and in the handler you 4 can use the backtrace. since the handler 3 would be executing in that partucular thread, the 2 backtrace there would be the output what 1 you are needed.

Score: 1

gdb provides these facilities for debugging 4 multi-thread programs:

  • automatic notification of new threads
  • ‘thread thread-id’, a command to switch among threads
  • ‘info threads’, a command to inquire about existing threads
  • ‘thread apply [thread-id-list] [all] args’, a command to apply a command to a list of threads
  • thread-specific breakpoints
  • ‘set print thread-events’, which controls printing of messages on thread start and exit.
  • ‘set libthread-db-search-path path’, which lets the user specify which libthread_db to use if the default choice isn't compatible with the program.

So just goto required 3 thread in GDB by cmd: 'thread thread-id'. Then 2 do 'bt' in that thread context to print 1 the thread backtrace.

More Related questions