Using gdb, I can type 'info threads' or 'thread apply all backtrace' to get a listing of all my threads and what they're doing.
I would like to write a gdb macro which will instead list all running threads. This macro would retrieve the thread number (i.e. the argument to the gdb 'thread' command) and then would get the name of the thread that I have defined and stored in thread-local storage.
In pseudocode, this would look something like:
for each thread in threads
t thread
f 1
set $name = my_name
f top
printf "Thread %d has name %s and is currently doing %s\n", thread, $name, curr_frame
next
There may be some small complexity around getting the string for curr_frame, but I'm willing to elide that for right now if I could get a good framework going for the rest of this bit.
I have a list of threads in my program, and for it I can get the pthread_t which corresponds to the thread pointer displayed as part of 'info thread' output:
(gdb) info thread
....
30 Thread 0x5221c940 (LWP 25304) 0x00000031c5a0aee9 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
....
(gdb) p *(_thread_t*)my_threads[0].pid
$4 = {
t_id = 0x5221c940,
As you can see, the t_id member is the same as the thread address in the output, but I can't really find any way to take programmatic advanta开发者_开发知识库ge of this fact.
Anybody know a way to do this?
Starting with version 7.0, GDB is scriptable in python, so you could pretty much implement your pseudo-code as is. See the documentation for accessing threads here and for getting the stack frame here.
I think easiest is to write your program in a source.py
file and then run in GDB:
source source.py
精彩评论