我有一个程序有超过 300 个线程,我附加了 gdb。我需要确定一个特定的线程,它的调用堆栈有一个帧,其中包含一个变量,我想将其值用于匹配。我可以在 gdb 中编写脚本吗?
(gdb) thread 3
[Switching to thread 3 (Thread 0x7f16c1eeb700 (LWP 18833))]
#4 0x00007f17f3a3bdd5 in start_thread () from /lib64/libpthread.so.0
(gdb) backtrace
#0 0x00007f17f3a3fd12 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00007f17e72838be in __afr_shd_healer_wait (healer=healer@entry=0x7f17e05203d0) at afr-self-heald.c:101
#2 0x00007f17e728392d in afr_shd_healer_wait (healer=healer@entry=0x7f17e05203d0) at afr-self-heald.c:125
#3 0x00007f17e72848e8 in afr_shd_index_healer (data=0x7f17e05203d0) at afr-self-heald.c:572
#4 0x00007f17f3a3bdd5 in start_thread () from /lib64/libpthread.so.0
#5 0x00007f17f3302ead in clone () from /lib64/libc.so.6
(gdb) frame 3
#3 0x00007f17e72848e8 in afr_shd_index_healer (data=0x7f17e05203d0) at afr-self-heald.c:572
572 afr_shd_healer_wait (healer);
(gdb) p this->name
$6 = 0x7f17e031b910 "testvol-replicate-0"
例如,我可以运行一个宏来遍历每个线程,转到每个线程中的第 3 帧,检查变量 this->name
并仅在值匹配时打印 thead 编号testvol-replicate-0
?
最佳答案
可以将 Python 集成到 GDB 中。然后,使用 Python GDB API,您可以遍历线程并搜索匹配项。下面是使用 GDB 和 Python 调试线程的两个示例。
https://www.linuxjournal.com/article/11027
https://fy.blackhats.net.au/blog/html/2017/08/04/so_you_want_to_script_gdb_with_python.html
关于c - gdb 脚本根据变量值隔离线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56578747/