5

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 ? )

Alon
  • 1,776
  • 13
  • 31
  • Well on Linux you can easily open `/proc//stat` (or `/proc//task//stat`) and read the stack and instruction pointers. What are your platform requirements? – Useless Aug 25 '14 at 13:37
  • If you know what function you think you may be stuck in -- could you not just set and clear a flag as you enter and leaves that function, and make that a indication to the other threads? – Soren Aug 25 '14 at 14:08
  • For which platform do you need this? – alk Aug 26 '14 at 06:34
  • @soren, not for high multi-threading fault detection. – Alon Sep 01 '14 at 08:03
  • You won't be able to do that reliably if the inspected thread is running. – Basile Starynkevitch Sep 01 '14 at 08:05
  • I have knowledge of where it should be halted, and that is what I want to inspect, if it's not halted, it's an error and that is what I'm trying to find out. – Alon Sep 01 '14 at 12:50

3 Answers3

4

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
ks1322
  • 33,961
  • 14
  • 109
  • 164
  • While interesting (+1 for that), the OP specifically asked for how to do it in software and not from gdb – Soren Aug 25 '14 at 14:12
  • As Soren commented, I need to know how to do it from the software, and not the gdb. – Alon Sep 01 '14 at 08:01
  • @Alon, gdb itself uses libthread_db library to list threads, so you can do the same in your app. First, you will need to load it (it is not very obvious to do). See this blog on how to load it: http://timetobleed.com/notes-about-an-odd-esoteric-yet-incredibly-useful-library-libthread_db/ . – ks1322 Sep 02 '14 at 09:49
1

(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

jayadev
  • 3,576
  • 25
  • 13
0

Since you know which function you think you are getting stuck on, you could set a break point at the begining of that function. GDB allows you to attach a series of commands to a break point that are automatically executed when the breakpoint is hit, allowing you to print the backtrace for the thread that was executing when the breakpoint was hit.

(gdb) break filename:line
(gdb) commands
Type commands for breakpoint(s) 1, one per line
End with a line saying just "end"
>info threads
>bt
>continue
>end

The above will give you the list of threads, with the * by the active thread for the breakpoint, followed by the backtrace.

diverscuba23
  • 2,165
  • 18
  • 32