debugger
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDebugger
调试指南
An advanced debugging specialist that helps diagnose and resolve code issues systematically.
这是一个高级调试专家,可系统地帮助诊断和解决代码问题。
When This Skill Activates
该技能的触发场景
Activates when you:
- Report an error or bug
- Mention "debug this" or "help debug"
- Describe unexpected behavior
- Ask why something isn't working
当你出现以下情况时,该技能会激活:
- 报告错误或bug
- 提及“调试这个”或“帮忙调试”
- 描述意外行为
- 询问某功能为何无法正常工作
Debugging Process
调试流程
Phase 1: Understand the Problem
阶段1:理解问题
-
Reproduce the issue
- What are the exact steps to reproduce?
- What is the expected behavior?
- What is the actual behavior?
- What error messages appear?
-
Gather contextbash
# Check recent changes git log --oneline -10 # Check error logs tail -f logs/error.log # Check environment env | grep -i debug
-
复现问题
- 复现问题的确切步骤是什么?
- 预期行为是什么?
- 实际行为是什么?
- 出现了哪些错误信息?
-
收集上下文信息bash
# 查看最近的变更 git log --oneline -10 # 查看错误日志 tail -f logs/error.log # 查看环境变量 env | grep -i debug
Phase 2: Isolate the Issue
阶段2:定位问题
-
Locate the error source
- Stack trace analysis
- Error code lookup
- Log correlation
-
Narrow down scope
- Binary search (comment out half)
- Minimize reproduction case
- Identify affected components
-
定位错误来源
- 堆栈跟踪分析
- 错误代码查询
- 日志关联分析
-
缩小问题范围
- 二分法排查(注释掉一半代码)
- 简化复现案例
- 确定受影响的组件
Phase 3: Analyze the Root Cause
阶段3:分析根本原因
Common Error Categories
常见错误类别
| Category | Symptoms | Investigation Steps |
|---|---|---|
| Null/Undefined | "Cannot read X of undefined" | Trace the variable origin |
| Type Errors | "X is not a function" | Check actual vs expected type |
| Async Issues | Race conditions, timing | Check promise handling, async/await |
| State Issues | Stale data, wrong state | Trace state mutations |
| Network | Timeouts, connection refused | Check endpoints, CORS, auth |
| Environment | Works locally, not in prod | Compare env vars, versions |
| Memory | Leaks, OOM | Profile memory usage |
| Concurrency | Deadlocks, race conditions | Check locks, shared state |
| 类别 | 症状 | 排查步骤 |
|---|---|---|
| 空值/未定义 | "Cannot read X of undefined" | 追踪变量的来源 |
| 类型错误 | "X is not a function" | 检查实际类型与预期类型是否一致 |
| 异步问题 | 竞态条件、时序问题 | 检查Promise处理、async/await使用 |
| 状态问题 | 数据过期、状态异常 | 追踪状态变更过程 |
| 网络问题 | 超时、连接被拒绝 | 检查端点、CORS、认证信息 |
| 环境问题 | 本地正常但生产环境异常 | 对比环境变量、版本差异 |
| 内存问题 | 内存泄漏、内存不足(OOM) | 分析内存使用情况 |
| 并发问题 | 死锁、竞态条件 | 检查锁机制、共享状态 |
Phase 4: Form Hypotheses
阶段4:提出假设
For each potential cause:
- Form a hypothesis
- Create a test to validate
- Run the test
- Confirm or reject
针对每个潜在原因:
- 提出假设
- 设计测试方案验证假设
- 执行测试
- 确认或推翻假设
Phase 5: Fix and Verify
阶段5:修复与验证
- Implement the fix
- Add logging if needed
- Test the fix
- Add regression test
- 实施修复方案
- 必要时添加日志
- 测试修复效果
- 添加回归测试
Debugging Commands
调试命令
General Debugging
通用调试命令
bash
undefinedbash
undefinedFind recently modified files
查找最近修改的文件
find . -type f -mtime -1 -name ".js" -o -name ".ts" -o -name "*.py"
find . -type f -mtime -1 -name ".js" -o -name ".ts" -o -name "*.py"
Grep for error patterns
搜索错误模式
grep -r "ERROR|FATAL|Exception" logs/
grep -r "ERROR|FATAL|Exception" logs/
Search for suspicious patterns
搜索可疑标记
grep -r "TODO|FIXME|XXX" src/
grep -r "TODO|FIXME|XXX" src/
Check for console.log left in code
检查代码中遗留的console.log
grep -r "console.log|debugger" src/
undefinedgrep -r "console.log|debugger" src/
undefinedLanguage-Specific
语言专属命令
JavaScript/TypeScript:
bash
undefinedJavaScript/TypeScript:
bash
undefinedRun with debug output
启用调试输出运行
NODE_DEBUG=* node app.js
NODE_DEBUG=* node app.js
Check syntax
检查语法
node -c file.js
node -c file.js
Run tests in debug mode
以调试模式运行测试
npm test -- --inspect-brk
**Python:**
```bashnpm test -- --inspect-brk
**Python:**
```bashRun with pdb
使用pdb运行脚本
python -m pdb script.py
python -m pdb script.py
Check syntax
检查语法
python -m py_compile script.py
python -m py_compile script.py
Verbose mode
详细模式运行
python -v script.py
**Go:**
```bashpython -v script.py
**Go:**
```bashRace detection
竞态检测
go run -race main.go
go run -race main.go
Debug build
编译调试版本
go build -gcflags="-N -l"
go build -gcflags="-N -l"
Profile
性能分析
go test -cpuprofile=cpu.prof
undefinedgo test -cpuprofile=cpu.prof
undefinedCommon Debugging Patterns
常见调试模式
Pattern 1: Divide and Conquer
模式1:分治排查
python
undefinedpython
undefinedWhen you don't know where the bug is:
当不知道bug位置时:
def process():
step1()
step2()
step3()
step4()
def process():
step1()
step2()
step3()
step4()
Comment out half:
注释掉一半代码:
def process():
step1()
# step2()
# step3()
# step4()
def process():
step1()
# step2()
# step3()
# step4()
If bug disappears, uncomment half of commented:
如果bug消失,取消注释一半代码:
def process():
step1()
step2()
# step3()
# step4()
def process():
step1()
step2()
# step3()
# step4()
Continue until you isolate the bug
重复此过程直到定位bug
undefinedundefinedPattern 2: Add Logging
模式2:添加日志
typescript
// Before (mysterious failure):
async function getUser(id: string) {
const user = await db.find(id);
return transform(user);
}
// After (with logging):
async function getUser(id: string) {
console.log('[DEBUG] getUser called with id:', id);
const user = await db.find(id);
console.log('[DEBUG] db.find returned:', user);
const result = transform(user);
console.log('[DEBUG] transform returned:', result);
return result;
}typescript
// 修复前(故障原因不明):
async function getUser(id: string) {
const user = await db.find(id);
return transform(user);
}
// 修复后(添加日志):
async function getUser(id: string) {
console.log('[DEBUG] getUser called with id:', id);
const user = await db.find(id);
console.log('[DEBUG] db.find returned:', user);
const result = transform(user);
console.log('[DEBUG] transform returned:', result);
return result;
}Pattern 3: Minimal Reproduction
模式3:简化复现案例
typescript
// Complex code with bug:
function processBatch(items, options) {
// 100 lines of complex logic
}
// Create minimal reproduction:
function processBatch(items, options) {
console.log('Items:', items.length);
console.log('Options:', options);
// Test with minimal data
return processBatch([items[0]], options);
}typescript
// 存在bug的复杂代码:
function processBatch(items, options) {
// 100行复杂逻辑
}
// 创建简化的复现案例:
function processBatch(items, options) {
console.log('Items:', items.length);
console.log('Options:', options);
// 使用最小数据集测试
return processBatch([items[0]], options);
}Error Message Analysis
错误信息分析
Common Error Messages
常见错误信息
| Error | Likely Cause | Solution |
|---|---|---|
| Accessing property on null/undefined | Add null check, use optional chaining |
| Wrong type, shadowing | Check typeof, verify import |
| Syntax error | Check line before error, validate syntax |
| Import path wrong | Check relative path, verify file exists |
| Port already in use | Kill existing process, use different port |
| Service not running | Start service, check port |
| Request too slow | Increase timeout, check network |
| 错误信息 | 可能原因 | 解决方案 |
|---|---|---|
| 访问了null/undefined对象的属性 | 添加空值检查,使用可选链操作符 |
| 类型错误、变量被遮蔽 | 检查类型,验证导入是否正确 |
| 语法错误 | 检查错误行的上一行代码,验证语法 |
| 导入路径错误 | 检查相对路径,验证文件是否存在 |
| 端口已被占用 | 终止现有进程,使用其他端口 |
| 服务未运行 | 启动服务,检查端口是否正确 |
| 请求过慢 | 增加超时时间,检查网络状况 |
Debugging Checklist
调试检查清单
- I can reproduce the issue consistently
- I have identified the exact error location
- I understand the root cause
- I have a proposed fix
- The fix doesn't break existing functionality
- I've added a test to prevent regression
- 我能稳定复现该问题
- 我已定位到确切的错误位置
- 我理解问题的根本原因
- 我有可行的修复方案
- 修复不会破坏现有功能
- 我已添加回归测试以防止问题复发
Scripts
脚本
Generate a debug report:
bash
python scripts/debug_report.py <error-message>生成调试报告:
bash
python scripts/debug_report.py <error-message>References
参考资料
- - Debugging checklist
references/checklist.md - - Common debugging patterns
references/patterns.md - - Error message reference
references/errors.md
- - 调试检查清单
references/checklist.md - - 常见调试模式
references/patterns.md - - 错误信息参考手册
references/errors.md