Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 288
The Vivandiere Avatar asked Mar 24 '17 19:03

The Vivandiere


People also ask

What is LWP in GDB?

The operating system's lightweight process (LWP) ID value for the thread. This ID is used in part for the OS to keep track of this thread for scheduling purposes. The GDB ID for the thread. This is the ID to use when specifying a specific thread in GDB commands.

What does thread ID mean?

Thread Id is a long positive integer that is created when the thread was created. During the entire lifecycle of a thread, the thread ID is unique and remains unchanged. It can be reused when the thread is terminated.

What is Backtrace in GDB?

A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the currently executing frame (frame zero), followed by its caller (frame one), and on up the stack.

What is thread debugging?

A thread is a sequence of instructions to which the operating system grants processor time. Every process that is running in the operating system consists of at least one thread.


2 Answers

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))]
like image 121
nos Avatar answered Sep 20 '22 00:09

nos


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
like image 33
user2399321 Avatar answered Sep 19 '22 00:09

user2399321