The Vivandiere
The Vivandiere

Reputation: 3201

GDB - What thread am I on?

When I hit a breakpoint in GDB, and I need to find out what thread this is on, I do info thr. This prints out the list of all the threads in my program, and the current thread is marked with a *

Instead of having GDB dump the entire list of threads, and then manually reading what thread has the *, is there a command in gdb which simply prints out the current thread?

I need this because I am logging some behavior in my program. In other words, I have something like this -

(gdb) command 12
    >> p " xyz just happpened"
    >> whatThreadIsThis // I would like this
    >> c
    >> end

If GDB implemented something like the whatThreadIsThis command, then I could use GDB to log all occurrences of xyz with the threads they happened on.

Upvotes: 14

Views: 10445

Answers (2)

user2399321
user2399321

Reputation: 637

gdb also has Convenience Variables. One of the them is:

$_thread

    The thread number of the current thread.

You can print it with:

(gdb) p $_thread
$2 = 2

Also it can be used in conditions:

condition 1 $_thread != 1

Upvotes: 6

nos
nos

Reputation: 229284

You can use the "thread thread-id" command to switch to another thread as the docs mentions. What the docs doesn't seem to mention is that without any argument, it just prints the current thread:

 (gdb) thread
 [Current thread is 1 (Thread 0x7ffff7fc2700 (LWP 4641))]

Upvotes: 15

Related Questions