This skill should be used when the user asks to "debug this", "fix this error", "investigate this bug", "troubleshoot this issue", "find the problem", "something is broken", "this isn't working", "why is this failing", or reports errors/exceptions/bugs. Provides systematic debugging workflow and common error patterns.
系统化的DebugProcess,用于排查和解决CodeError、异常和故障。提供结构化的DebugMethod和常见ErrorPattern识别。
Debug是科学问题的解决过程,需要:
在开始Debug前,明确以下Information:
必须收集的Information:
提问模板:
1. 具体的Error消息是什么?
2. Error发生在哪个File的哪一行?
3. 如何复现这个问题?请提供详细Step
4. 期望的Result是什么?实际发生了什么?
5. 最近有什么改动可能引入这个问题?
根据ErrorClass型选择Debug策略:
| ErrorClass型 | 特征 | DebugMethod |
|---|---|---|
| 语法Error | Code无法Parse | Check语法、括号匹配、引号 |
| 导入Error | ModuleNotFoundError | CheckModule安装、PathConfiguration |
| Class型Error | TypeError | CheckDataClass型、Class型Convert |
| PropertyError | AttributeError | CheckObjectProperty是否存在 |
| KeyError | KeyError | Check字典Key是否存在 |
| 索引Error | IndexError | Check列表/数组索引范围 |
| 空指针 | NoneType/NullPointerException | CheckVariable是否为 None |
| 网络Error | ConnectionError/Timeout | Check网络连接、URL、超时Set |
| 权限Error | PermissionError | CheckFile权限、用户权限 |
| 资源Error | FileNotFoundError | CheckFilePath是否存在 |
使用以下Method定位问题:
1. 二分法定位
2. 日志追踪
3. 断点Debug
4. 堆栈跟踪分析
假设框架:
假设:[问题Description]导致[Error现象]
VerifyStep:
1. [VerifyMethod1]
2. [VerifyMethod2]
预期Result:
- 如果假设正确:[预期现象]
- 如果假设Error:[预期现象]
Fix后必须Verify:
# ❌ 赋Value不能有空格
name = "John" # Error:尝试Run name Command
# ✅ 正确的赋Value
name="John"
# ❌ 条件Testing缺少空格
if[$name -eq 1]; then # Error
# ✅ 正确
if [ $name -eq 1 ]; then
# ❌ 单引号内Variable不展开
echo 'The value is $var' # Output: The value is $var
# ✅ 使用双引号
echo "The value is $var" # Output: The value is actual_value
# ❌ Command替换使用反引号(易混淆)
result=`command`
# ✅ 使用 $()
result=$(command)
# ❌ Variable未引用,空Value会导致Error
rm -rf $dir/* # 如果 dir 为空,会Delete当前Directory所有File
# ✅ 始终引用Variable
[ -n "$dir" ] && rm -rf "$dir"/*
# 或使用 set -u 防止未定义Variable
set -u # 或 set -o nounset
# ❌ 管道Create子 shell,外部Variable不改变
cat file.txt | while read line; do
count=$((count + 1)) # 外部 count 不会改变
done
echo "Total: $count" # Output 0
# ✅ 使用进程替换或重定向
while read line; do
count=$((count + 1))
done < file.txt
echo "Total: $count" # 正确Output
# ❌ Error的数组访问
arr=(1 2 3)
echo $arr[1] # Output 1[1]
# ✅ 正确的数组访问
echo ${arr[1]} # Output 2
echo ${arr[@]} # Output所有元素
echo ${#arr[@]} # Output数组长度
# ❌ 使用 = 比较字符串
if [ $name = "John" ]; then # 在某些 shell 中不是标准
# ✅ 使用 = 或 ==
if [ "$name" = "John" ]; then
if [[ "$name" == "John" ]]; then
# ❌ 数字比较使用 -eq 不是 =
if [ $age = 18 ]; then # Error
# ✅ 数字比较使用算术运算符
if [ $age -eq 18 ]; then
if (( age == 18 )); then
# ❌ CommandFail后ContinueExecute
cd /nonexistent
rm file.txt # 会Delete当前Directory的 file.txt
# ✅ 使用 set -e 在Error时退出
set -e # 或 set -o errexit
cd /nonexistent # 脚本在此处退出
rm file.txt
# 或CheckCommand是否Success
cd /nonexistent || exit 1
# DebugPatternRun脚本
bash -x script.sh # 打印每个Command
bash -v script.sh # 打印Command原文
bash -n script.sh # 语法Check,不Execute
# 在脚本中启用Debug
set -x # 启用Command追踪
set -v # 启用 verbose Pattern
set -e # Error时退出
set -u # 未定义Variable报错
set -o pipefail # 管道中任一CommandFail则Fail
For detailed debugging techniques and patterns:
references/python-errors.md - Python Error详解references/javascript-errors.md - JavaScript/TypeScript Error详解references/shell-errors.md - Bash/Zsh 脚本Error详解references/debugging-tools.md - Debug工具使用Guidereferences/common-patterns.md - 常见ErrorPatternWorking debugging examples:
examples/debugging-workflow.py - 完整DebugProcessExampleexamples/error-handling-patterns.py - ErrorProcessPatternexamples/debugging-workflow.sh - Shell 脚本DebugExample