|
| 1 | +# SDD Spec: E2E Testing with Team Collaboration |
| 2 | + |
| 3 | +## 0. Open Questions |
| 4 | +- [x] Q1: 测试数据生成队友是否必要?→ **是,新增 test-data-gen 角色** |
| 5 | +- [x] Q2: coredump 文件生成方式 → **Option B: 编译崩溃程序** |
| 6 | +- [x] Q3: E2E 测试范围 → **第一阶段最小验证集** |
| 7 | +- [x] Q4: GDB RPC Server 事件处理机制 → **已解决(见 §5.3 根因分析与修复)** |
| 8 | + |
| 9 | +## 1. Requirements (Context) |
| 10 | + |
| 11 | +- **Goal**: 创建团队协作环境,生成真实 coredump 测试数据,并完成第一阶段端到端测试 |
| 12 | +- **In-Scope**: |
| 13 | + - 根据 team-config.md 创建 SDD-DEV-TEAM 团队 |
| 14 | + - 新增 test-data-gen 角色 |
| 15 | + - 生成真实 coredump 文件(用于 E2E 测试) |
| 16 | + - 执行端到端测试验证 gdb-cli 功能 |
| 17 | +- **Out-of-Scope**: |
| 18 | + - 完整的压力测试和性能测试 |
| 19 | + - 生产环境的 GDB 调试场景 |
| 20 | + |
| 21 | +## 1.1 Context Sources |
| 22 | +- Requirement Source: 用户对话 |
| 23 | +- Team Config: `mydocs/team-config.md` |
| 24 | +- Project Spec: `mydocs/specs/2026-03-19_17-30_GDB-CLI-for-AI.md` |
| 25 | +- Codebase: `src/gdb_cli/` — 已完成的 GDB CLI 实现 |
| 26 | + |
| 27 | +## 2. Research Findings |
| 28 | + |
| 29 | +### 2.1 团队配置 |
| 30 | + |
| 31 | +**最终配置** (6 角色): |
| 32 | +| 角色 | 模型 | 职责 | |
| 33 | +|------|------|------| |
| 34 | +| team-lead | glm-5 | 协调、Review 决策 | |
| 35 | +| architect | opus | 架构设计、Innovate | |
| 36 | +| developer | opus | Execute 实现 | |
| 37 | +| tester | haiku | 测试验证 | |
| 38 | +| reviewer | sonnet | 代码审查 | |
| 39 | +| test-data-gen | haiku | 测试数据生成 | |
| 40 | + |
| 41 | +### 2.2 环境发现 |
| 42 | + |
| 43 | +**GDB 版本**: |
| 44 | +- 系统 GDB `/usr/local/bin/gdb` 13.2 — **无 Python 支持** |
| 45 | +- GCC Toolset GDB `/opt/rh/gcc-toolset-13/root/usr/bin/gdb` 13.2 — **有 Python 3.6.8 支持** |
| 46 | + |
| 47 | +**Python 版本兼容性问题**: |
| 48 | +- GDB 内置 Python 3.6.8 不支持 `from __future__ import annotations`(需要 3.7+) |
| 49 | +- 需要使用 `typing` 模块的 `List`, `Dict`, `Tuple` 代替 `list[]`, `dict[]`, `tuple[]` |
| 50 | + |
| 51 | +### 2.3 崩溃程序设计 |
| 52 | + |
| 53 | +**文件**: `tests/crash_test/crash_test_c.c` (~120行纯 C) |
| 54 | + |
| 55 | +**数据结构**: |
| 56 | +- `Database` → `tables[]` → `Table` → `columns[]` → `Column` |
| 57 | +- 支持嵌套访问验证 `g_database.tables[0].columns[1].name` |
| 58 | + |
| 59 | +**多线程**: 主线程 + 3 worker + 1 crash 线程 |
| 60 | + |
| 61 | +**崩溃类型**: NULL 指针解引用 |
| 62 | + |
| 63 | +## 3. Innovate (Skipped) |
| 64 | +- 跳过原因:需求明确,无需多方案对比 |
| 65 | + |
| 66 | +## 4. Plan (Contract) |
| 67 | + |
| 68 | +### 4.1 任务分配 |
| 69 | + |
| 70 | +| Task ID | 任务 | Owner | 状态 | |
| 71 | +|---------|------|-------|------| |
| 72 | +| #7 | 设计崩溃测试程序 | architect | ✅ 完成 | |
| 73 | +| #9 | 编译程序并生成 coredump | test-data-gen | ✅ 完成 | |
| 74 | +| #11 | 修复 Python 3.6.8 兼容性 | developer | ✅ 完成 | |
| 75 | +| #6 | 执行端到端测试 | tester | ⏳ 进行中 | |
| 76 | +| #8 | 审查 E2E 测试结果 | reviewer | ⏳ 待定 | |
| 77 | + |
| 78 | +### 4.2 E2E 测试 Checklist |
| 79 | +- [x] 1. architect 设计崩溃程序 ✅ |
| 80 | +- [x] 2. test-data-gen 编译并生成 coredump ✅ |
| 81 | +- [x] 3. developer 修复 Python 兼容性问题 ✅ |
| 82 | +- [x] 4. tester 执行 E2E 测试 ✅ **全部通过 (2026-03-20)** |
| 83 | + - [x] 4.1 gdb-cli load — 进程启动 + session 持久化 |
| 84 | + - [x] 4.2 gdb-cli sessions — 正确列出活跃会话 |
| 85 | + - [x] 4.3 gdb-cli threads — 5 线程,含 current_thread |
| 86 | + - [x] 4.4 gdb-cli bt — 3 帧 backtrace |
| 87 | + - [x] 4.5 gdb-cli eval-cmd — `(int)5+3 = 8` |
| 88 | + - [x] 4.6 gdb-cli status — 线程数、当前帧信息 |
| 89 | + - [x] 4.7 gdb-cli exec — `info threads` 原始输出 |
| 90 | + - [x] 4.8 gdb-cli stop — 会话正常停止 |
| 91 | + |
| 92 | +## 5. Execute Log |
| 93 | + |
| 94 | +### 5.1 执行记录 |
| 95 | + |
| 96 | +| 时间 | 任务 | 状态 | 备注 | |
| 97 | +|------|------|------|------| |
| 98 | +| 21:00 | 创建团队 | ✅ | `sdd-dev-team` | |
| 99 | +| 21:07 | 设计崩溃程序 | ✅ | `tests/crash_test/crash_test_c.c` | |
| 100 | +| 21:14 | 编译崩溃程序 | ✅ | `crash_test_c` binary | |
| 101 | +| 21:25 | 生成 coredump | ✅ | `core-crash_test_c-*` | |
| 102 | +| 21:30 | 发现 GDB Python 问题 | 🔴 | 系统 GDB 无 Python 支持 | |
| 103 | +| 22:00 | 切换到 GCC Toolset GDB | ✅ | `/opt/rh/gcc-toolset-13/root/usr/bin/gdb` | |
| 104 | +| 22:05 | 发现 Python 3.6.8 兼容性问题 | 🔴 | `annotations` 需要 3.7+ | |
| 105 | +| 22:10 | 修复 gdb_server 兼容性 | ✅ | 移除 annotations,使用 typing 模块 | |
| 106 | +| 22:20 | 修复相对导入问题 | ✅ | 使用动态加载 handlers/value_formatter | |
| 107 | +| 22:30 | GDB 进程保持运行 | ✅ | 使用管道保持 stdin | |
| 108 | +| 23:00 | RPC Server 事件处理问题 | 🔴 | `gdb.post_event` 未被处理 | |
| 109 | + |
| 110 | +### 5.2 代码修改摘要 |
| 111 | + |
| 112 | +**修复的文件**: |
| 113 | +1. `src/gdb_cli/cli.py` — 添加 `--gdb-path` 选项 |
| 114 | +2. `src/gdb_cli/launcher.py` — |
| 115 | + - 修复 sock_path 类型(Path 转换) |
| 116 | + - 添加 stdin 管道保持 GDB 运行 |
| 117 | + - 修改 server 启动命令(避免导入整个 gdb_cli 包) |
| 118 | +3. `src/gdb_cli/gdb_server/gdb_rpc_server.py` — |
| 119 | + - 移除 `from __future__ import annotations` |
| 120 | + - 使用 `Dict` 代替 `dict[]` |
| 121 | + - 动态加载 handlers 模块 |
| 122 | +4. `src/gdb_cli/gdb_server/handlers.py` — |
| 123 | + - 移除 `from __future__ import annotations` |
| 124 | + - 使用 `List`, `Tuple` 代替 `list[]`, `tuple[]` |
| 125 | + - 动态加载 value_formatter 模块 |
| 126 | +5. `src/gdb_cli/gdb_server/value_formatter.py` — 移除 annotations |
| 127 | +6. `src/gdb_cli/gdb_server/__init__.py` — 移除 annotations |
| 128 | +7. `src/gdb_cli/gdb_server/heartbeat.py` — 移除 annotations |
| 129 | + |
| 130 | +**新增文件**: |
| 131 | +- `tests/crash_test/crash_test_c.c` — 纯 C 崩溃测试程序 |
| 132 | + |
| 133 | +## 6. Review Verdict |
| 134 | + |
| 135 | +### 6.1 当前状态 |
| 136 | + |
| 137 | +| 测试类别 | 状态 | 详情 | |
| 138 | +|----------|------|------| |
| 139 | +| 单元测试 | ✅ PASS | 214 tests passed | |
| 140 | +| GDB 启动 | ✅ PASS | 进程常驻(FIFO stdin) | |
| 141 | +| RPC Server | ✅ PASS | Socket 通信正常 | |
| 142 | +| Session 管理 | ✅ PASS | 创建/查询/停止 | |
| 143 | +| threads | ✅ PASS | 5 线程,支持分页 | |
| 144 | +| bt | ✅ PASS | 支持指定 thread_id | |
| 145 | +| eval | ✅ PASS | 表达式求值正常 | |
| 146 | +| exec | ✅ PASS | 原始 GDB 命令执行 | |
| 147 | +| stop | ✅ PASS | 会话正常停止 | |
| 148 | + |
| 149 | +### 6.2 修复的问题(2026-03-20) |
| 150 | + |
| 151 | +修复了 3 层嵌套 Bug,最终 E2E 测试全部通过: |
| 152 | + |
| 153 | +#### Bug 1: Socket 通信协议死锁 |
| 154 | +- **根因**: Client `sendall()` 后不关闭写端 → Server recv 循环等待 EOF → 双方互等 → 超时 |
| 155 | +- **修复**: `client.py` — 在 `sendall()` 后调用 `socket.shutdown(SHUT_WR)`,每次 call 后 close 重连 |
| 156 | + |
| 157 | +#### Bug 2: GDB 进程无法常驻 |
| 158 | +- **根因**: Launcher 用 `os.pipe()` 保持 GDB stdin,但 CLI 进程退出后 pipe write fd 关闭 → GDB 收到 EOF 退出 |
| 159 | +- **修复**: `launcher.py` — 用 named FIFO (O_RDWR) 替代 pipe,GDB 阻塞在 FIFO 读端,不依赖父进程 |
| 160 | + |
| 161 | +#### Bug 3: Session 元数据未持久化 gdb_pid |
| 162 | +- **根因**: `_start_gdb_process()` 设置 `session.gdb_pid` 后未回写 `meta.json` → 后续读取 session 判定为 dead → 被清理 |
| 163 | +- **修复**: `launcher.py` — 在设置 gdb_pid 后调用 `_write_meta(session)` |
| 164 | + |
| 165 | +#### Bug 4: `gdb.Value` 的 `str()` 从后台线程触发 GDB SEGFAULT |
| 166 | +- **根因**: `val.type.is_unsigned()` 在 GDB 12.1 不存在 → `_format_int` 的 except 分支调用 `str(val)` → 触发 Guile pretty printer(非线程安全)→ SEGFAULT |
| 167 | +- **修复**: `value_formatter.py` — 添加 `_safe_str()` 辅助函数(用 `format_string(raw=True)` 绕过 pretty printer),替换所有 `str(val)` 调用;`is_unsigned()` 改为 `hasattr` 保护 |
| 168 | + |
| 169 | +#### 架构决策:放弃 `gdb.post_event()` |
| 170 | +- GDB 12.1 在非交互模式下不处理 `post_event` 队列 |
| 171 | +- 对于 core dump 分析(只读场景),后台线程直接 dispatch 是正确的务实选择 |
| 172 | +- 避免调用 `str(gdb.Value)` 即可规避大部分线程安全问题 |
| 173 | + |
| 174 | +## 7. Plan-Execution Diff |
| 175 | + |
| 176 | +| 计划项 | 计划状态 | 实际状态 | 差异说明 | |
| 177 | +|--------|----------|----------|----------| |
| 178 | +| 团队创建 | pending | completed | ✅ 6 角色 | |
| 179 | +| 崩溃程序 | pending | completed | ✅ 纯 C 版本 | |
| 180 | +| Python 兼容性 | 未计划 | completed | ⚠️ 额外工作 | |
| 181 | +| E2E 测试 | pending | **completed** | ✅ 8/8 全部通过 | |
| 182 | +| Socket 死锁修复 | 未计划 | completed | ⚠️ 额外工作 | |
| 183 | +| GDB 常驻修复 | 未计划 | completed | ⚠️ 额外工作 | |
| 184 | +| 线程安全修复 | 未计划 | completed | ⚠️ 额外工作 | |
| 185 | + |
| 186 | +**总结**: |
| 187 | +- E2E 测试全部通过:load → sessions → threads → bt → eval → status → exec → stop |
| 188 | +- 修复了 4 个阻塞性 Bug:Socket 死锁、GDB 进程不常驻、Session 元数据丢失、gdb.Value 线程安全 |
| 189 | +- 架构决策:放弃 `gdb.post_event()`,core dump 场景下后台线程直接 dispatch |
| 190 | +- 代码改动文件:`client.py`、`launcher.py`、`value_formatter.py` |
| 191 | + |
| 192 | +## 8. 远程真实环境 E2E 测试(2026-03-20) |
| 193 | + |
| 194 | +### 8.1 测试目标 |
| 195 | + |
| 196 | +在远程测试机上使用真实 Database core dump 验证 gdb-cli 的分发安装和功能完整性。 |
| 197 | + |
| 198 | +### 8.2 测试环境 |
| 199 | + |
| 200 | +| 项目 | 详情 | |
| 201 | +|------|------| |
| 202 | +| 测试机 | `work@6.12.234.100` | |
| 203 | +| GDB | `/usr/bin/gdb` 9.2,内嵌 Python 3.6.8 | |
| 204 | +| 系统 Python | 3.8.17 | |
| 205 | +| binary | `/data/1/backup/myback.2026-03-20.10.55.32/server` (6.1G) | |
| 206 | +| core dump | `/data/1/backup/myback.2026-03-20.10.55.32/core-T1002_L0_G0-101182-1773975204` (5.6G) | |
| 207 | +| 线程数 | 779 | |
| 208 | +| crash 信号 | SIGSEGV (sig=11) | |
| 209 | +| crash 函数 | `sql::LibCacheMissReason::from_recorder` | |
| 210 | + |
| 211 | +### 8.3 分发安装测试 |
| 212 | + |
| 213 | +**安装方式**: `pip install git+http://gitlab.internal.example.com/tools/gdb_cli.git` |
| 214 | + |
| 215 | +#### 遇到的问题与修复 |
| 216 | + |
| 217 | +| # | 问题 | 根因 | 修复 | 提交 | |
| 218 | +|---|------|------|------|------| |
| 219 | +| 5 | `NameError: name 'Path' is not defined` | `cli.py` env_check 使用 `Path` 但未导入 | 添加 `from pathlib import Path` | `560bc4c` | |
| 220 | +| 6 | `SyntaxError: future feature annotations is not defined` | GDB 内嵌 Python 3.6.8 不支持 PEP 563 `from __future__ import annotations` | 移除全部 9 个文件的 `from __future__ import annotations` | `90535ba` | |
| 221 | +| 7 | `setuptools>=61.0` 在 Python 3.6 上不可用 | pip/setuptools 版本太旧 | 降为 `setuptools>=40.0` | `0f9dc6f` | |
| 222 | +| 8 | 小写泛型 `list[str]`、`tuple[...]` | Python 3.9+ 语法 | 改为 `typing.List`、`typing.Tuple` | `0f9dc6f` | |
| 223 | +| 9 | `capture_output=True` 不可用 | Python 3.7+ 参数 | 改为 `stdout=subprocess.PIPE, stderr=subprocess.PIPE` | `0f9dc6f` | |
| 224 | +| 10 | Socket 等待超时 (30s) | 5.6G core + 6.1G binary 加载远超 30s | 超时改为 300s | `e8471dd` | |
| 225 | + |
| 226 | +#### pyproject.toml 兼容性调整 |
| 227 | + |
| 228 | +``` |
| 229 | +requires-python = ">=3.6.8" # 原 >=3.8 |
| 230 | +setuptools >= 40.0 # 原 >=61.0 |
| 231 | +click >= 7.0 # 原 >=8.0.0 |
| 232 | +dataclasses >= 0.6 (仅 python_version < 3.7) |
| 233 | +``` |
| 234 | + |
| 235 | +### 8.4 功能测试结果 |
| 236 | + |
| 237 | +| 命令 | 状态 | 响应 | 说明 | |
| 238 | +|------|------|------|------| |
| 239 | +| `env-check` | ✅ PASS | GDB 9.2, Python 3.8.17 | — | |
| 240 | +| `load` | ✅ PASS | session `b243b42e` 创建 | 大文件加载约 60s | |
| 241 | +| `threads` | ✅ PASS | 779 线程,支持分页 | `total_count: 779, truncated: true` | |
| 242 | +| `bt --thread 1` | ✅ PASS | 23 帧完整栈帧 | crash: `coredump_cb → LibCacheMissReason::from_recorder` | |
| 243 | +| `bt --thread 1 --limit 5` | ✅ PASS | 前 5 帧 + `truncated: true` | — | |
| 244 | +| `status` | ✅ PASS | 779 线程,当前帧 `syscall` | — | |
| 245 | +| `eval-cmd` | ⚠️ 受限 | 表达式求值可用,GDB 命令不可用 | `p/x $rip` 的 `$` 需要 shell 转义 | |
| 246 | +| `frame` | ❌ CRASH | `gdb.execute()` 触发 GDB SEGFAULT | GDB 9.2 后台线程调用 `gdb.execute()` 不安全 | |
| 247 | +| `exec` | ❌ CRASH | 同上 | `gdb.execute()` 线程安全问题 | |
| 248 | +| `stop` | ✅ PASS | `force_stopped` | — | |
| 249 | + |
| 250 | +### 8.5 关键发现:`gdb.execute()` 后台线程 SEGFAULT 根因分析 |
| 251 | + |
| 252 | +#### 8.5.1 GDB 官方文档的线程安全约束 |
| 253 | + |
| 254 | +根据 GDB 官方文档("Basic Python" / "Threading in GDB"): |
| 255 | + |
| 256 | +> **GDB is not thread-safe.** If your Python program uses multiple threads, you must be careful to only call GDB-specific functions in the GDB thread. |
| 257 | +
|
| 258 | +> `gdb.post_event(event)` — This is the **only thread-safe way** to call into GDB from another thread. |
| 259 | +
|
| 260 | +即:**所有** GDB Python API(包括 `gdb.execute()`、`gdb.parse_and_eval()`、`gdb.selected_frame()` 等)从非主线程调用均属于 undefined behavior。 |
| 261 | + |
| 262 | +#### 8.5.2 实际测试:哪些 API 在后台线程真正 crash |
| 263 | + |
| 264 | +在 `work@6.12.234.100` (GDB 9.2) 上对真实 DB core dump (6.1G binary, 779 线程) 做了逐项验证: |
| 265 | + |
| 266 | +| API 调用 | 小程序 (1 线程) | DB core (779 线程) | 说明 | |
| 267 | +|----------|:-:|:-:|------| |
| 268 | +| `gdb.selected_inferior()` | ✅ | ✅ | 纯 Python 对象访问 | |
| 269 | +| `inferior.threads()` | ✅ | ✅ | 读取线程列表 | |
| 270 | +| `gdb.selected_thread()` | ✅ | ✅ | 读当前线程 | |
| 271 | +| `thread.switch()` | ✅ | ✅ | 切换线程上下文 | |
| 272 | +| `gdb.selected_frame()` | ✅ | ✅ | 读当前帧 | |
| 273 | +| `gdb.newest_frame()` | ✅ | ✅ | 获取最新帧 | |
| 274 | +| `frame.older()` | ✅ | ✅ | 遍历栈帧链 | |
| 275 | +| `frame.name()` | ✅ | ✅ | 读函数名 | |
| 276 | +| `frame.select()` | ✅ | ✅ | **Python API 选帧,安全** | |
| 277 | +| `frame.block()` + `sym.value()` | ✅ | ✅ | **遍历局部变量,安全** | |
| 278 | +| `gdb.parse_and_eval("1+1")` | ✅ | ✅ | 简单表达式求值 | |
| 279 | +| `gdb.parse_and_eval("this")` | ✅ | ✅ | 复杂指针求值也安全(求值本身不 crash) | |
| 280 | +| `str(gdb.Value)` 复杂对象 | ✅ | **❌ SEGFAULT** | 触发 pretty printer(Guile/Python),非线程安全 | |
| 281 | +| `val.format_string(raw=True)` | ✅ | ✅ | **绕过 pretty printer,安全** | |
| 282 | +| `gdb.execute("print 1+1")` | ✅ | **❌ SEGFAULT** | 即使最简单的命令也 crash | |
| 283 | +| `gdb.execute("info threads")` | ✅ | **❌ SEGFAULT** | 同上 | |
| 284 | +| `gdb.execute("frame 3")` | ✅ | **❌ SEGFAULT** | 同上 | |
| 285 | + |
| 286 | +#### 8.5.3 两类 crash 的根因对比 |
| 287 | + |
| 288 | +**Crash 类型 A: `gdb.execute()` — CLI 解释器竞争** |
| 289 | + |
| 290 | +``` |
| 291 | +后台线程 → gdb.execute("frame 3") → GDB CLI interpreter |
| 292 | + → 命令解析 → 输出重定向 → 上下文切换 → 全局状态修改 |
| 293 | + → 与主线程的 batch-mode 事件循环竞争 → SEGFAULT |
| 294 | +``` |
| 295 | + |
| 296 | +- `gdb.execute()` 走 GDB 的 **CLI command interpreter** 完整路径 |
| 297 | +- 涉及命令解析、输出缓冲区分配、pager 状态等大量全局状态 |
| 298 | +- 小程序碰巧能用(符号表简单,竞争窗口极小) |
| 299 | +- 大型 DB binary (779 线程, 6.1G) **100% 复现** |
| 300 | + |
| 301 | +**Crash 类型 B: `str(gdb.Value)` — Pretty Printer 竞争** |
| 302 | + |
| 303 | +``` |
| 304 | +后台线程 → str(val) → Python __str__ → gdb valpy_str() |
| 305 | + → 查找 pretty printer → 触发 Guile SCM pretty printer |
| 306 | + → scm_gc_protect_object() → GC 竞争 → SEGFAULT |
| 307 | +``` |
| 308 | + |
| 309 | +- `str(val)` 内部调用 `valpy_str()` → 自动查找 pretty printer |
| 310 | +- DB 注册了 Guile pretty printer,Guile GC 不是线程安全的 |
| 311 | +- **修复**: 使用 `val.format_string(raw=True)` 绕过 pretty printer |
| 312 | + |
| 313 | +#### 8.5.4 验证实验记录 |
| 314 | + |
| 315 | +**实验 1**: `frame.select()` + `parse_and_eval("sig")` (简单 int 参数) |
| 316 | +``` |
| 317 | +BG: frame.select OK, func=common::coredump_cb |
| 318 | +BG: sig = 11 |
| 319 | +MAIN: done ✅ |
| 320 | +``` |
| 321 | + |
| 322 | +**实验 2**: `frame.select()` + `parse_and_eval("this")` + `str(val)` → SEGFAULT |
| 323 | +``` |
| 324 | +BG: frame 3 = sql::LibCacheMissReason::from_recorder |
| 325 | +(GDB process crashed, no further output) ❌ |
| 326 | +``` |
| 327 | + |
| 328 | +**实验 3**: `frame.select()` + `parse_and_eval("this")` + `format_string(raw=True)` |
| 329 | +``` |
| 330 | +BG: frame 3 = sql::LibCacheMissReason::from_recorder |
| 331 | +BG: this (safe) = 0x7f89add1c948 |
| 332 | +MAIN: done ✅ |
| 333 | +``` |
| 334 | + |
| 335 | +**实验 4**: `frame.block()` + `sym.value(frame)` + `format_string(raw=True)` (完整局部变量遍历) |
| 336 | +``` |
| 337 | +BG: frame 1 = common::coredump_cb |
| 338 | +BG: vars=['sig=11', 'sig_code=1', 'sig_addr=0x20', 'context=<optimized out>', ...] |
| 339 | +MAIN: done ✅ |
| 340 | +``` |
| 341 | + |
| 342 | +#### 8.5.5 结论:后台线程安全的 GDB Python API 边界 |
| 343 | + |
| 344 | +在 core dump 只读场景下,后台线程可安全调用的 API: |
| 345 | + |
| 346 | +| 安全 ✅ | 不安全 ❌ | |
| 347 | +|---------|----------| |
| 348 | +| `gdb.selected_inferior()` | `gdb.execute()` — 任何命令 | |
| 349 | +| `inferior.threads()` | `str(gdb.Value)` — 触发 pretty printer | |
| 350 | +| `thread.switch()` | | |
| 351 | +| `gdb.newest_frame()` / `frame.older()` / `frame.select()` | | |
| 352 | +| `frame.name()` / `frame.sal()` / `frame.block()` | | |
| 353 | +| `gdb.parse_and_eval(expr)` | | |
| 354 | +| `sym.value(frame)` | | |
| 355 | +| `val.format_string(raw=True)` | | |
| 356 | +| `int(val)` / `float(val)` | | |
| 357 | + |
| 358 | +**核心规则**: |
| 359 | +1. **永远不用 `gdb.execute()`** — 用对应的 Python API 替代 |
| 360 | +2. **永远不用 `str(gdb.Value)`** — 用 `val.format_string(raw=True)` 替代 |
| 361 | + |
| 362 | +#### 8.5.6 受影响的 handler 及修复方案 |
| 363 | + |
| 364 | +| handler | 当前实现 | crash 原因 | 修复方案 | |
| 365 | +|---------|---------|-----------|---------| |
| 366 | +| `handle_frame_select()` L386 | `gdb.execute(f"frame {number}")` | crash 类型 A | 遍历帧链 + `frame.select()` | |
| 367 | +| `handle_locals()` L436 | `gdb.execute(f"frame {frame}")` | crash 类型 A | 同上 | |
| 368 | +| `handle_exec()` L530 | `gdb.execute(command)` | crash 类型 A | **无法替换**,exec 本质就是执行 GDB CLI 命令;标记为不稳定或通过 `gdb.post_event()` 调度 | |
| 369 | +| `handle_thread_apply()` L710 | `gdb.execute(command)` | crash 类型 A | 同 exec | |
| 370 | +| `value_formatter._format_*()` | 部分路径走 `str(val)` | crash 类型 B | 已修复(`_safe_str()` + `format_string(raw=True)`),需全面审查确认无遗漏 | |
| 371 | + |
| 372 | +#### 8.5.5 大文件加载性能 |
| 373 | + |
| 374 | +- 5.6G core + 6.1G binary → GDB 加载约 60 秒 |
| 375 | +- 原 30s 超时不足,已调整为 300s |
| 376 | +- 建议后续添加 `--timeout` CLI 参数供用户自定义 |
| 377 | + |
| 378 | +### 8.6 另一台测试机 (11.124.5.104) 的排除记录 |
| 379 | + |
| 380 | +| 项目 | 详情 | |
| 381 | +|------|------| |
| 382 | +| GDB | `/usr/local/gdb-13/bin/gdb` 13.2,编译时 `--without-python` | |
| 383 | +| 系统 Python | 3.6.8,pip 版本旧 | |
| 384 | +| 结论 | **不可用** — GDB 无 Python 支持,无法启动 RPC Server | |
| 385 | +| 建议 | 需运维安装 `gcc-toolset-13-gdb` 或重新编译 GDB `--with-python=python3` | |
| 386 | + |
| 387 | +### 8.7 待改进项 |
| 388 | + |
| 389 | +1. **`frame`/`locals` 消除 `gdb.execute()` (P0)**: 将 `gdb.execute(f"frame {N}")` 替换为帧链遍历 + `frame.select()`,已验证可行 |
| 390 | +2. **`str(gdb.Value)` 全面审查 (P0)**: 确认 handlers.py 和 value_formatter.py 中所有 `str(val)` 调用均已替换为 `format_string(raw=True)`,防止 pretty printer SEGFAULT |
| 391 | +3. **`exec`/`thread-apply` 标记为不稳定 (P1)**: 这两个命令必须走 `gdb.execute()`,在大型 binary 上 100% crash。选项:(a) 文档标注限制,(b) 通过 `gdb.post_event()` 调度到主线程(需 GDB 处于事件循环) |
| 392 | +4. **`--timeout` 参数 (P2)**: `load` 命令应支持用户指定超时 |
| 393 | +5. **`env-check --gdb-path` (P2)**: env-check 命令不支持 `--gdb-path` |
| 394 | +6. **安装文档 (P2)**: 明确说明 GDB 需要 `--with-python` 编译选项 |
| 395 | + |
| 396 | +--- |
| 397 | + |
| 398 | +*最后更新: 2026-03-20 15:00* |
0 commit comments