gdb how to get thread name displayed
Asked Answered
S

4

12

There are many threads created in my application. some of the threads name are visible in the gdb while i execute the command 'info threads', others are not displayed. How to get all the thread name itself instead of the hex value like 0xb7fe1424

4 Thread 0xb68ffb70 (LWP 18377)  0xb7fe1424 in __kernel_vsyscall ()
* 3 Thread 0xb7291b70 (LWP 18375)  JKMainT (arg=0x0) at mylib.cpp:482
2 Thread 0xb7a92b70 (LWP 18374)  0xb7fe1424 in __kernel_vsyscall ()
1 Thread 0xb7a94730 (LWP 18371)  0xb7fe1424 in __kernel_vsyscall ()
Supernatant answered 20/1, 2012 at 16:1 Comment(2)
To add more.. I put break point at mylib.cpp:482, and down the line it creates a thread.Supernatant
That is not the thread name, that is the name of the function which that thread is currently executing.Dearth
D
10

If you upgrade to gdb 7.3 or later, "info thread" will show thread names; at least on native (not remote) Linux.

Dacia answered 1/7, 2014 at 16:11 Comment(0)
L
9

You can set the thread name via non-standard POSIX api calls. GDB (and other debuggers) will display these names.

On Linux

// watch out, 16 char limit on the name
pthread_setname_np(pthread_self(), "My thread name");

On Mac

pthread_setname_np("My thread name");
Lodge answered 29/12, 2014 at 2:29 Comment(1)
Related: #2370238Constantina
E
8

Threads don't have names by default - the JKMainT string there is the name of the current function.

Try selecting one of the threads and viewing the backtrace - that might give you a good idea which thread it is. Otherwise, you could try prctl with PR_SET_NAME if it's available.

Exhalant answered 20/1, 2012 at 16:37 Comment(1)
an easier way to do this is pthread_setname_np(pthread_t , const char)Lodge
W
0
  • (gdb) info threads - will display all the threads

  • (gdb) thread will switch to the thread you selected and display the thread name

  • (gdb) thread - will display current thread name

Walloping answered 27/9, 2021 at 12:47 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Educate

© 2022 - 2024 — McMap. All rights reserved.