debugging-techniques
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDebugging Techniques
调试技术
Purpose
用途
Provides systematic debugging workflows for local, remote, container, and production environments across Python, Go, Rust, and Node.js. Covers interactive debuggers, container debugging with ephemeral containers, and production-safe techniques using correlation IDs and distributed tracing.
为Python、Go、Rust和Node.js提供本地、远程、容器及生产环境下的系统化调试工作流。涵盖交互式调试器、基于ephemeral containers的容器调试,以及使用correlation IDs和分布式追踪的生产环境安全调试技术。
When to Use This Skill
何时使用该技能
Trigger this skill for:
- Setting breakpoints in Python, Go, Rust, or Node.js code
- Debugging running containers or Kubernetes pods
- Setting up remote debugging connections
- Safely debugging production issues
- Inspecting goroutines, threads, or async tasks
- Analyzing core dumps or stack traces
- Choosing the right debugging tool for a scenario
在以下场景中使用该技能:
- 在Python、Go、Rust或Node.js代码中设置断点
- 调试运行中的容器或Kubernetes Pod
- 搭建远程调试连接
- 安全调试生产环境问题
- 检查goroutines、线程或异步任务
- 分析核心转储或堆栈跟踪
- 为特定场景选择合适的调试工具
Quick Reference by Language
按语言分类快速参考
Python Debugging
Python调试
Built-in: pdb
python
undefined内置工具:pdb
python
undefinedPython 3.7+
Python 3.7+
def buggy_function(x, y):
breakpoint() # Stops execution here
return x / y
def buggy_function(x, y):
breakpoint() # Stops execution here
return x / y
Older Python
Older Python
import pdb
pdb.set_trace()
**Essential pdb commands:**
- `list` (l) - Show code around current line
- `next` (n) - Execute current line, step over functions
- `step` (s) - Execute current line, step into functions
- `continue` (c) - Continue until next breakpoint
- `print var` (p) - Print variable value
- `where` (w) - Show stack trace
- `quit` (q) - Exit debugger
**Enhanced tools:**
- `ipdb` - Enhanced pdb with tab completion, syntax highlighting (`pip install ipdb`)
- `pudb` - Terminal GUI debugger (`pip install pudb`)
- `debugpy` - VS Code integration (included in Python extension)
**Debugging tests:**
```bash
pytest --pdb # Drop into debugger on test failureFor detailed Python debugging patterns, see .
references/python-debugging.mdimport pdb
pdb.set_trace()
**pdb核心命令:**
- `list` (l) - 显示当前行附近的代码
- `next` (n) - 执行当前行,步过函数
- `step` (s) - 执行当前行,步入函数
- `continue` (c) - 继续执行至下一个断点
- `print var` (p) - 打印变量值
- `where` (w) - 显示堆栈跟踪
- `quit` (q) - 退出调试器
**增强工具:**
- `ipdb` - 带自动补全和语法高亮的增强版pdb(`pip install ipdb`)
- `pudb` - 终端GUI调试器(`pip install pudb`)
- `debugpy` - 与VS Code集成(包含在Python扩展中)
**调试测试:**
```bash
pytest --pdb # Drop into debugger on test failure如需了解详细的Python调试模式,请参阅。
references/python-debugging.mdGo Debugging
Go调试
Delve - Official Go debugger
Installation:
bash
go install github.com/go-delve/delve/cmd/dlv@latestBasic usage:
bash
dlv debug main.go # Debug main package
dlv test github.com/me/pkg # Debug test suite
dlv attach <pid> # Attach to running process
dlv debug -- --config prod.yaml # Pass argumentsEssential commands:
- (b) - Set breakpoint at function
break main.main - (b) - Set breakpoint at line
break file.go:10 - (c) - Continue execution
continue - (n) - Step over
next - (s) - Step into
step - (p) - Print variable
print x - (gr) - Show current goroutine
goroutine - (grs) - List all goroutines
goroutines - - Show goroutine stacktraces
goroutines -t - (bt) - Show stack trace
stack
Goroutine debugging:
bash
(dlv) goroutines # List all goroutines
(dlv) goroutines -t # Show stacktraces
(dlv) goroutines -with user # Filter user goroutines
(dlv) goroutine 5 # Switch to goroutine 5For detailed Go debugging patterns, see .
references/go-debugging.mdDelve - 官方Go调试器
安装方式:
bash
go install github.com/go-delve/delve/cmd/dlv@latest基本用法:
bash
dlv debug main.go # Debug main package
dlv test github.com/me/pkg # Debug test suite
dlv attach <pid> # Attach to running process
dlv debug -- --config prod.yaml # Pass arguments核心命令:
- (b) - 在函数处设置断点
break main.main - (b) - 在指定行设置断点
break file.go:10 - (c) - 继续执行
continue - (n) - 步过
next - (s) - 步入
step - (p) - 打印变量
print x - (gr) - 显示当前goroutine
goroutine - (grs) - 列出所有goroutines
goroutines - - 显示goroutine堆栈跟踪
goroutines -t - (bt) - 显示堆栈跟踪
stack
Goroutine调试:
bash
(dlv) goroutines # List all goroutines
(dlv) goroutines -t # Show stacktraces
(dlv) goroutines -with user # Filter user goroutines
(dlv) goroutine 5 # Switch to goroutine 5如需了解详细的Go调试模式,请参阅。
references/go-debugging.mdRust Debugging
Rust调试
LLDB - Default Rust debugger
Compilation:
bash
cargo build # Debug build includes symbols by defaultUsage:
bash
rust-lldb target/debug/myapp # LLDB wrapper for Rust
rust-gdb target/debug/myapp # GDB wrapper (alternative)Essential LLDB commands:
- - Set breakpoint at line
breakpoint set -f main.rs -l 10 - - Set breakpoint at function
breakpoint set -n main - (r) - Start program
run - (c) - Continue execution
continue - (n) - Step over
next - (s) - Step into
step - (p) - Print variable
print variable - (fr v) - Show local variables
frame variable - (bt) - Show stack trace
backtrace - - List all threads
thread list
VS Code integration:
- Install CodeLLDB extension ()
vadimcn.vscode-lldb - Configure for Rust projects
launch.json
For detailed Rust debugging patterns, see .
references/rust-debugging.mdLLDB - 默认Rust调试器
编译:
bash
cargo build # Debug build includes symbols by default用法:
bash
rust-lldb target/debug/myapp # LLDB wrapper for Rust
rust-gdb target/debug/myapp # GDB wrapper (alternative)LLDB核心命令:
- - 在指定行设置断点
breakpoint set -f main.rs -l 10 - - 在函数处设置断点
breakpoint set -n main - (r) - 启动程序
run - (c) - 继续执行
continue - (n) - 步过
next - (s) - 步入
step - (p) - 打印变量
print variable - (fr v) - 显示局部变量
frame variable - (bt) - 显示堆栈跟踪
backtrace - - 列出所有线程
thread list
VS Code集成:
- 安装CodeLLDB扩展()
vadimcn.vscode-lldb - 为Rust项目配置
launch.json
如需了解详细的Rust调试模式,请参阅。
references/rust-debugging.mdNode.js Debugging
Node.js调试
Built-in: node --inspect
Basic usage:
bash
node --inspect-brk app.js # Start and pause immediately
node --inspect app.js # Start and run
node --inspect=0.0.0.0:9229 app.js # Specify host/portChrome DevTools:
- Open
chrome://inspect - Click "Open dedicated DevTools for Node"
- Set breakpoints, inspect variables
VS Code integration:
Configure :
launch.jsonjson
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js"
}Docker debugging:
dockerfile
EXPOSE 9229
CMD ["node", "--inspect=0.0.0.0:9229", "app.js"]For detailed Node.js debugging patterns, see .
references/nodejs-debugging.md内置工具:node --inspect
基本用法:
bash
node --inspect-brk app.js # Start and pause immediately
node --inspect app.js # Start and run
node --inspect=0.0.0.0:9229 app.js # Specify host/portChrome DevTools:
- 打开
chrome://inspect - 点击"Open dedicated DevTools for Node"
- 设置断点、检查变量
VS Code集成:
配置:
launch.jsonjson
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js"
}Docker调试:
dockerfile
EXPOSE 9229
CMD ["node", "--inspect=0.0.0.0:9229", "app.js"]如需了解详细的Node.js调试模式,请参阅。
references/nodejs-debugging.mdContainer & Kubernetes Debugging
容器与Kubernetes调试
kubectl debug with Ephemeral Containers
使用Ephemeral Containers进行kubectl debug
When to use:
- Container has crashed (kubectl exec won't work)
- Using distroless/minimal image (no shell, no tools)
- Need debugging tools without rebuilding image
- Debugging network issues
Basic usage:
bash
undefined适用场景:
- 容器已崩溃(kubectl exec无法使用)
- 使用无发行版/极简镜像(无Shell、无工具)
- 无需重新构建镜像即可使用调试工具
- 调试网络问题
基本用法:
bash
undefinedAdd ephemeral debugging container
Add ephemeral debugging container
kubectl debug -it <pod-name> --image=nicolaka/netshoot
kubectl debug -it <pod-name> --image=nicolaka/netshoot
Share process namespace (see other container processes)
Share process namespace (see other container processes)
kubectl debug -it <pod-name> --image=busybox --share-processes
kubectl debug -it <pod-name> --image=busybox --share-processes
Target specific container
Target specific container
kubectl debug -it <pod-name> --image=busybox --target=app
**Recommended debugging images:**
- `nicolaka/netshoot` (~380MB) - Network debugging (curl, dig, tcpdump, netstat)
- `busybox` (~1MB) - Minimal shell and utilities
- `alpine` (~5MB) - Lightweight with package manager
- `ubuntu` (~70MB) - Full environment
**Node debugging:**
```bash
kubectl debug node/<node-name> -it --image=ubuntuDocker container debugging:
bash
docker exec -it <container-id> shkubectl debug -it <pod-name> --image=busybox --target=app
**推荐调试镜像:**
- `nicolaka/netshoot` (~380MB) - 网络调试工具(包含curl、dig、tcpdump、netstat)
- `busybox` (~1MB) - 极简Shell及工具集
- `alpine` (~5MB) - 轻量级镜像,带包管理器
- `ubuntu` (~70MB) - 完整运行环境
**节点调试:**
```bash
kubectl debug node/<node-name> -it --image=ubuntuDocker容器调试:
bash
docker exec -it <container-id> shIf no shell available
If no shell available
docker run -it --pid=container:<container-id>
--net=container:<container-id>
busybox sh
--net=container:<container-id>
busybox sh
For detailed container debugging patterns, see `references/container-debugging.md`.docker run -it --pid=container:<container-id>
--net=container:<container-id>
busybox sh
--net=container:<container-id>
busybox sh
如需了解详细的容器调试模式,请参阅`references/container-debugging.md`。Production Debugging
生产环境调试
Production Debugging Principles
生产环境调试原则
Golden rules:
- Minimal performance impact - Profile overhead, limit scope
- No blocking operations - Use non-breaking techniques
- Security-aware - Avoid logging secrets, PII
- Reversible - Can roll back quickly (feature flags, Git)
- Observable - Structured logging, correlation IDs, tracing
黄金准则:
- 最小性能影响 - 分析开销,限制范围
- 无阻塞操作 - 使用非中断技术
- 安全优先 - 避免记录敏感信息、个人可识别信息(PII)
- 可回滚 - 可快速回退(功能开关、Git)
- 可观测 - 结构化日志、correlation IDs、追踪
Safe Production Techniques
生产环境安全调试技术
1. Structured Logging
python
import logging
import json
logger = logging.getLogger(__name__)
logger.info(json.dumps({
"event": "user_login_failed",
"user_id": user_id,
"error": str(e),
"correlation_id": request_id
}))2. Correlation IDs (Request Tracing)
go
func handleRequest(w http.ResponseWriter, r *http.Request) {
correlationID := r.Header.Get("X-Correlation-ID")
if correlationID == "" {
correlationID = generateUUID()
}
ctx := context.WithValue(r.Context(), "correlationID", correlationID)
log.Printf("[%s] Processing request", correlationID)
}3. Distributed Tracing (OpenTelemetry)
python
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def process_order(order_id):
with tracer.start_as_current_span("process_order") as span:
span.set_attribute("order.id", order_id)
span.add_event("Order validated")4. Error Tracking Platforms
- Sentry - Exception tracking with context
- New Relic - APM with error tracking
- Datadog - Logs, metrics, traces
- Rollbar - Error monitoring
Production debugging workflow:
- Detect - Error tracking alert, log spike, metric anomaly
- Locate - Find correlation ID, search logs, view distributed trace
- Reproduce - Try to reproduce in staging with production data (sanitized)
- Fix - Create feature flag, deploy to canary first
- Verify - Check error rates, review logs, monitor traces
For detailed production debugging patterns, see .
references/production-debugging.md1. 结构化日志
python
import logging
import json
logger = logging.getLogger(__name__)
logger.info(json.dumps({
"event": "user_login_failed",
"user_id": user_id,
"error": str(e),
"correlation_id": request_id
}))2. Correlation IDs(请求追踪)
go
func handleRequest(w http.ResponseWriter, r *http.Request) {
correlationID := r.Header.Get("X-Correlation-ID")
if correlationID == "" {
correlationID = generateUUID()
}
ctx := context.WithValue(r.Context(), "correlationID", correlationID)
log.Printf("[%s] Processing request", correlationID)
}3. 分布式追踪(OpenTelemetry)
python
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def process_order(order_id):
with tracer.start_as_current_span("process_order") as span:
span.set_attribute("order.id", order_id)
span.add_event("Order validated")4. 错误追踪平台
- Sentry - 带上下文的异常追踪
- New Relic - 包含错误追踪的APM工具
- Datadog - 日志、指标、追踪一体化平台
- Rollbar - 错误监控工具
生产环境调试工作流:
- 检测 - 错误追踪告警、日志激增、指标异常
- 定位 - 查找correlation ID、搜索日志、查看分布式追踪链路
- 复现 - 使用脱敏后的生产数据在预发布环境复现问题
- 修复 - 创建功能开关,先部署到金丝雀环境
- 验证 - 检查错误率、查看日志、监控追踪链路
如需了解详细的生产环境调试模式,请参阅。
references/production-debugging.mdDecision Framework
决策框架
Which Debugger for Which Language?
各语言对应的调试器?
| Language | Primary Tool | Installation | Best For |
|---|---|---|---|
| Python | pdb | Built-in | Simple scripts, server environments |
| ipdb | | Enhanced UX, IPython users | |
| debugpy | VS Code extension | IDE integration, remote debugging | |
| Go | delve | | All Go debugging, goroutines |
| Rust | rust-lldb | System package | Mac, Linux, MSVC Windows |
| rust-gdb | System package | Linux, prefer GDB | |
| Node.js | node --inspect | Built-in | All Node.js debugging, Chrome DevTools |
| 编程语言 | 主要工具 | 安装方式 | 最佳适用场景 |
|---|---|---|---|
| Python | pdb | 内置 | 简单脚本、服务器环境 |
| ipdb | | 增强交互体验、IPython用户 | |
| debugpy | VS Code扩展 | IDE集成、远程调试 | |
| Go | delve | | 所有Go调试场景、goroutine调试 |
| Rust | rust-lldb | 系统包管理器安装 | Mac、Linux、MSVC Windows环境 |
| rust-gdb | 系统包管理器安装 | Linux环境、偏好GDB的用户 | |
| Node.js | node --inspect | 内置 | 所有Node.js调试场景、Chrome DevTools集成 |
Which Technique for Which Scenario?
各场景对应的调试技术?
| Scenario | Recommended Technique | Tools |
|---|---|---|
| Local development | Interactive debugger | pdb, delve, lldb, node --inspect |
| Bug in test | Test-specific debugging | pytest --pdb, dlv test, cargo test |
| Remote server | SSH tunnel + remote attach | VS Code Remote, debugpy |
| Container (local) | docker exec -it | sh/bash + debugger |
| Kubernetes pod | Ephemeral container | kubectl debug --image=nicolaka/netshoot |
| Distroless image | Ephemeral container (required) | kubectl debug with busybox/alpine |
| Production issue | Log analysis + error tracking | Structured logs, Sentry, correlation IDs |
| Goroutine deadlock | Goroutine inspection | delve goroutines -t |
| Crashed process | Core dump analysis | gdb core, lldb -c core |
| Distributed failure | Distributed tracing | OpenTelemetry, Jaeger, correlation IDs |
| Race condition | Race detector + debugger | go run -race, cargo test |
| 场景 | 推荐技术 | 工具 |
|---|---|---|
| 本地开发 | 交互式调试器 | pdb, delve, lldb, node --inspect |
| 测试用例失败 | 测试专属调试 | pytest --pdb, dlv test, cargo test |
| 远程服务器 | SSH隧道+远程附加 | VS Code Remote, debugpy |
| 本地容器 | docker exec -it | sh/bash + 调试器 |
| Kubernetes Pod | Ephemeral容器 | kubectl debug --image=nicolaka/netshoot |
| 无发行版镜像 | Ephemeral容器(必需) | kubectl debug结合busybox/alpine |
| 生产环境问题 | 日志分析+错误追踪 | 结构化日志、Sentry、correlation IDs |
| Goroutine死锁 | Goroutine检查 | delve goroutines -t |
| 进程崩溃 | 核心转储分析 | gdb core, lldb -c core |
| 分布式故障 | 分布式追踪 | OpenTelemetry, Jaeger, correlation IDs |
| 竞态条件 | 竞态检测器+调试器 | go run -race, cargo test |
Production Debugging Safety Checklist
生产环境调试安全检查表
Before debugging in production:
- Will this impact performance? (Profile overhead)
- Will this block users? (Use non-breaking techniques)
- Could this expose secrets? (Avoid variable dumps)
- Is there a rollback plan? (Git branch, feature flag)
- Have we tried logs first? (Less invasive)
- Do we have correlation IDs? (Trace requests)
- Is error tracking enabled? (Sentry, New Relic)
- Can we reproduce in staging? (Safer environment)
在生产环境中调试前,请确认:
- 是否会影响性能?(分析开销)
- 是否会阻塞用户?(使用非中断技术)
- 是否会暴露敏感信息?(避免变量全量打印)
- 是否有回滚方案?(Git分支、功能开关)
- 是否已优先查看日志?(侵入性更低)
- 是否有correlation IDs?(追踪请求链路)
- 是否已启用错误追踪?(Sentry、New Relic)
- 是否可在预发布环境复现?(更安全的环境)
Common Debugging Workflows
常见调试工作流
Workflow 1: Local Development Bug
工作流1:本地开发Bug调试
- Insert breakpoint in code (language-specific)
- Start debugger (dlv debug, rust-lldb, node --inspect-brk)
- Execute to breakpoint (run, continue)
- Inspect variables (print, frame variable)
- Step through code (next, step, finish)
- Identify issue and fix
- 插入断点到代码中(对应编程语言的方式)
- 启动调试器(dlv debug、rust-lldb、node --inspect-brk)
- 执行至断点(run、continue)
- 检查变量(print、frame variable)
- 逐步执行代码(next、step、finish)
- 定位问题并修复
Workflow 2: Test Failure Debugging
工作流2:测试失败调试
Python:
bash
pytest --pdb # Drops into pdb on failureGo:
bash
dlv test github.com/user/project/pkg
(dlv) break TestMyFunction
(dlv) continueRust:
bash
cargo test --no-run
rust-lldb target/debug/deps/myapp-<hash>
(lldb) breakpoint set -n test_name
(lldb) run test_namePython:
bash
pytest --pdb # Drops into pdb on failureGo:
bash
dlv test github.com/user/project/pkg
(dlv) break TestMyFunction
(dlv) continueRust:
bash
cargo test --no-run
rust-lldb target/debug/deps/myapp-<hash>
(lldb) breakpoint set -n test_name
(lldb) run test_nameWorkflow 3: Kubernetes Pod Debugging
工作流3:Kubernetes Pod调试
Scenario: Pod with distroless image, network issue
bash
undefined场景:使用无发行版镜像的Pod出现网络问题
bash
undefinedStep 1: Check pod status
步骤1:检查Pod状态
kubectl get pod my-app-pod -o wide
kubectl get pod my-app-pod -o wide
Step 2: Check logs first
步骤2:先查看日志
kubectl logs my-app-pod
kubectl logs my-app-pod
Step 3: Add ephemeral container if logs insufficient
步骤3:若日志不足,添加临时调试容器
kubectl debug -it my-app-pod --image=nicolaka/netshoot
kubectl debug -it my-app-pod --image=nicolaka/netshoot
Step 4: Inside debug container, investigate
步骤4:在调试容器内排查问题
curl localhost:8080
netstat -tuln
nslookup api.example.com
undefinedcurl localhost:8080
netstat -tuln
nslookup api.example.com
undefinedWorkflow 4: Production Error Investigation
工作流4:生产环境错误排查
Scenario: API returning 500 errors
bash
undefined场景:API返回500错误
bash
undefinedStep 1: Check error tracking (Sentry)
步骤1:查看错误追踪平台(Sentry)
- Find error details, stack trace
- 查找错误详情、堆栈跟踪
- Copy correlation ID from error report
- 从错误报告中复制correlation ID
Step 2: Search logs for correlation ID
步骤2:根据correlation ID搜索日志
In log aggregation tool (ELK, Splunk):
在日志聚合工具(ELK、Splunk)中搜索:
correlation_id:"abc-123-def"
correlation_id:"abc-123-def"
Step 3: View distributed trace
步骤3:查看分布式追踪链路
In tracing tool (Jaeger, Datadog):
在追踪工具(Jaeger、Datadog)中:
Search by correlation ID, review span timeline
根据correlation ID搜索,查看Span时间线
Step 4: Reproduce in staging
步骤4:在预发布环境复现
Use production data (sanitized) if needed
如需,使用脱敏后的生产数据
Add additional logging if needed
如需,添加额外日志
Step 5: Fix and deploy
步骤5:修复并部署
Create feature flag for gradual rollout
创建功能开关用于灰度发布
Deploy to canary environment first
先部署到金丝雀环境
Monitor error rates closely
密切监控错误率
undefinedundefinedAdditional Resources
额外资源
For language-specific deep dives:
- - pdb, ipdb, pudb, debugpy detailed guide
references/python-debugging.md - - Delve CLI, goroutine debugging, conditional breakpoints
references/go-debugging.md - - LLDB vs GDB, ownership debugging, macro debugging
references/rust-debugging.md - - node --inspect, Chrome DevTools, Docker debugging
references/nodejs-debugging.md
For environment-specific patterns:
- - kubectl debug, ephemeral containers, node debugging
references/container-debugging.md - - Structured logging, correlation IDs, OpenTelemetry, error tracking
references/production-debugging.md
For decision support:
- - Expanded debugging decision frameworks
references/decision-trees.md
For hands-on examples:
- - Step-by-step debugging sessions for each language
examples/
针对各语言的深度指南:
- - pdb、ipdb、pudb、debugpy详细指南
references/python-debugging.md - - Delve命令行、goroutine调试、条件断点
references/go-debugging.md - - LLDB vs GDB、所有权调试、宏调试
references/rust-debugging.md - - node --inspect、Chrome DevTools、Docker调试
references/nodejs-debugging.md
针对各环境的调试模式:
- - kubectl debug、ephemeral containers、节点调试
references/container-debugging.md - - 结构化日志、correlation IDs、OpenTelemetry、错误追踪
references/production-debugging.md
针对决策支持:
- - 扩展版调试决策框架
references/decision-trees.md
针对实操示例:
- - 各语言的分步调试会话示例
examples/
Related Skills
相关技能
For authentication patterns, see the skill.
For performance profiling (complementary to debugging), see the skill.
For Kubernetes operations (kubectl debug is part of), see the skill.
For test debugging strategies, see the skill.
For observability setup (logging, tracing), see the skill.
auth-securityperformance-engineeringkubernetes-operationstesting-strategiesobservability如需认证模式,请参阅技能。
如需性能分析(调试的补充技能),请参阅技能。
如需Kubernetes操作(kubectl debug是其中一部分),请参阅技能。
如需测试调试策略,请参阅技能。
如需可观测性配置(日志、追踪),请参阅技能。
auth-securityperformance-engineeringkubernetes-operationstesting-strategiesobservability