首页 > 解决方案 > 基于变量值隔离线程的 gdb 脚本

问题描述

我有一个程序有超过 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并仅在值匹配时打印头号testvol-replicate-0

标签: cmultithreadingdebugginggdb

解决方案


可以将 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


推荐阅读