Gdb debugging cli

From findscripter/everything-skills · View interactive version

Gdb debugging cli is an AI agent skill from the GitHub repository findscripter/everything-skills. 当需要用 GDB(经 gdb-cli)分析 C/C++ 程序的核心转储、附加调试运行进程、排查崩溃/死锁/内存问题时使用;做源码与运行时状态关联分析并产出根因定位结论;不适用于非 C/C++ 调试、无 GDB(需 9.0+ 带 Python)或非 Linux 环境;触发词:core dump、崩溃、死锁、gdb attach、backtrace、段错误

How to use this skill

Skill file

何时使用

适用场景:

不该用(负边界):

前置准备:

# 安装 gdb-cli
pip install gdb-cli
# 或从 GitHub 安装
pip install git+https://github.com/Cerdore/gdb-cli.git

# 验证 GDB 已启用 Python 支持
gdb -nx -q -batch -ex "python print('OK')"

步骤

  1. 初始化会话:加载 core 或 attach 进程,记下返回的 session_id。
  2. 收集初始信息:线程列表、带局部变量的回溯、寄存器。
  3. 关联源码(关键):对每个栈帧读取崩溃点 ±20 行源码,结合局部变量值分析逻辑。
  4. 深入排查:求值变量/字段、查类型、查内存、反汇编、遍历全部线程(死锁)。
  5. 会话管理:查看状态、用完即 stop 清理。

指令

初始化会话:

# 加载核心转储
gdb-cli load --binary <binary_path> --core <core_path> [--gdb-path <gdb_path>]

# 附加运行进程
gdb-cli attach --pid <pid> [--binary <binary_path>]

返回形如 "session_id": "a1b2c3",后续命令复用:

SESSION="<session_id>"

# 线程列表
gdb-cli threads -s $SESSION
# 回溯(含局部变量)
gdb-cli bt -s $SESSION --full
# 寄存器
gdb-cli registers -s $SESSION
# 指定帧的局部变量
gdb-cli locals-cmd -s $SESSION --frame <N>

深入排查:

# 求值变量 / 指针字段
gdb-cli eval-cmd -s $SESSION "variable_name"
gdb-cli eval-cmd -s $SESSION "ptr->field"
# 查看类型定义
gdb-cli ptype -s $SESSION "struct_name"
# 内存检查
gdb-cli memory -s $SESSION "0x7fffffffe000" --size 64
# 反汇编
gdb-cli disasm -s $SESSION --count 20
# 遍历全部线程回溯(死锁分析)
gdb-cli thread-apply -s $SESSION bt --all
# 共享库
gdb-cli sharedlibs -s $SESSION

会话管理:

gdb-cli sessions             # 列出活动会话
gdb-cli status -s $SESSION   # 查看会话状态
gdb-cli stop -s $SESSION     # 停止并清理

常见模式速查:

示例

源码与运行时关联(核心方法):

Frame #0: process_data() at src/worker.c:87
源码:
  85: Node* node = get_node(id);
  86: if (node == NULL) return;
  87: node->data = value;  <- 崩溃在此

变量:
  node = 0x0 (NULL)

结论:第 86 行的 NULL 检查未拦住该问题。

核心转储分析:

gdb-cli load --binary ./myapp --core /tmp/core.1234
gdb-cli bt -s a1b2c3 --full          # 崩溃位置
gdb-cli locals-cmd -s a1b2c3 --frame 0

运行进程调试:

gdb-cli attach --pid 12345
gdb-cli threads -s b2c3d4
gdb-cli thread-apply -s b2c3d4 bt --all

注意事项

最佳实践:

安全与权限:

互见


采编自 sickn33/antigravity-awesome-skills(MIT)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.