c - 基于变量值隔离线程的 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
?
解决方案
可以将 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
推荐阅读
- bintray - 如何将 maven 工件从 jcenter 同步到 maven Central?
- git - 如何将新项目与现有 Gitlab 存储库进行比较
- json - "error" : "无效数据;无法解析 JSON 对象。您是否发送了具有有效键名的 JSON 对象?"
- python-3.x - 在运行时将自定义小部件添加到屏幕
- graphql - 如何使用不同的字段生成相同的 graphql 查询
- java - 远程主机上的 NiFi API SocketException
- r - 将 ifelse() 应用于多列,在另一列上具有动态偶然性
- r - 带有 # 或 % 符号的列名 - R
- php - PHP Get Age 函数在 Codeigniter 上不起作用
- pandas - 在 Jupyter notebook 上显示所有可能的方法