investigate
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseInvestigation Toolkit
代码排查工具包
Debug issues through systematic search and AI-powered analysis.
通过系统化搜索和AI驱动的分析来调试问题。
Prerequisites
前置依赖
bash
undefinedbash
undefinedripgrep for fast search
ripgrep for fast search
brew install ripgrep
brew install ripgrep
Gemini for analysis
Gemini for analysis
pip install google-generativeai
export GEMINI_API_KEY=your_api_key
undefinedpip install google-generativeai
export GEMINI_API_KEY=your_api_key
undefinedSearch Commands
搜索命令
ripgrep Basics
ripgrep基础用法
bash
undefinedbash
undefinedSearch for pattern
Search for pattern
rg "pattern" src/
rg "pattern" src/
Case insensitive
Case insensitive
rg -i "error" src/
rg -i "error" src/
Whole word
Whole word
rg -w "user" src/
rg -w "user" src/
File types
File types
rg -t ts "function" src/
rg -t py "def " src/
rg -t ts "function" src/
rg -t py "def " src/
Exclude patterns
Exclude patterns
rg "TODO" --glob "!node_modules"
rg "TODO" --glob "!node_modules"
Show context
Show context
rg -C 3 "error" src/ # 3 lines before and after
rg -B 5 "crash" src/ # 5 lines before
rg -A 5 "crash" src/ # 5 lines after
rg -C 3 "error" src/ # 3 lines before and after
rg -B 5 "crash" src/ # 5 lines before
rg -A 5 "crash" src/ # 5 lines after
Just filenames
Just filenames
rg -l "pattern" src/
rg -l "pattern" src/
Count matches
Count matches
rg -c "pattern" src/
undefinedrg -c "pattern" src/
undefinedFinding Definitions
查找定义
bash
undefinedbash
undefinedFunction definitions (TypeScript)
Function definitions (TypeScript)
rg "function\s+functionName" src/
rg "(const|let|var)\s+functionName\s*=" src/
rg "export\s+(async\s+)?function\s+\w+" src/
rg "function\s+functionName" src/
rg "(const|let|var)\s+functionName\s*=" src/
rg "export\s+(async\s+)?function\s+\w+" src/
Class definitions
Class definitions
rg "class\s+ClassName" src/
rg "class\s+ClassName" src/
Interface/Type definitions
Interface/Type definitions
rg "(interface|type)\s+TypeName" src/
undefinedrg "(interface|type)\s+TypeName" src/
undefinedTracing Usage
追踪调用
bash
undefinedbash
undefinedWhere is this function called?
Where is this function called?
rg "functionName(" src/
rg "functionName(" src/
Where is this imported?
Where is this imported?
rg "import.*functionName" src/
rg "import.*functionName" src/
Where is this exported?
Where is this exported?
rg "export.*functionName" src/
undefinedrg "export.*functionName" src/
undefinedInvestigation Patterns
排查模式
Bug Investigation
Bug排查
bash
undefinedbash
undefined1. Search for error message
1. Search for error message
rg "exact error message" .
rg "exact error message" .
2. Find where error is thrown
2. Find where error is thrown
rg "throw.*Error" src/ -C 3
rg "throw.*Error" src/ -C 3
3. Trace the function
3. Trace the function
rg "functionThatFails" src/ -C 5
rg "functionThatFails" src/ -C 5
4. Check recent changes
4. Check recent changes
git log --oneline -20 --all -- src/problematic-file.ts
git diff HEAD~5 -- src/problematic-file.ts
undefinedgit log --oneline -20 --all -- src/problematic-file.ts
git diff HEAD~5 -- src/problematic-file.ts
undefinedTrace Execution Flow
追踪执行流程
bash
#!/bin/bash
ENTRY_POINT=$1
echo "=== Entry Point ==="
rg -A 10 "export.*$ENTRY_POINT" src/
echo "=== Called Functions ==="
rg -o "\w+\(" src/$ENTRY_POINT*.ts | sort -u
echo "=== Dependencies ==="
rg "^import" src/$ENTRY_POINT*.tsbash
#!/bin/bash
ENTRY_POINT=$1
echo "=== Entry Point ==="
rg -A 10 "export.*$ENTRY_POINT" src/
echo "=== Called Functions ==="
rg -o "\w+\(" src/$ENTRY_POINT*.ts | sort -u
echo "=== Dependencies ==="
rg "^import" src/$ENTRY_POINT*.tsAI-Assisted Debugging
AI辅助调试
bash
undefinedbash
undefinedAnalyze error with context
Analyze error with context
ERROR="Your error message here"
CODE=$(cat problematic-file.ts)
gemini -m pro -o text -e "" "Debug this error:
ERROR: $ERROR
CODE:
$CODE
Provide:
- Most likely cause
- How to verify
- How to fix
- How to prevent in future"
undefinedERROR="Your error message here"
CODE=$(cat problematic-file.ts)
gemini -m pro -o text -e "" "Debug this error:
ERROR: $ERROR
CODE:
$CODE
Provide:
- Most likely cause
- How to verify
- How to fix
- How to prevent in future"
undefinedHypothesis Testing
假设验证
bash
undefinedbash
undefinedGenerate hypotheses
Generate hypotheses
gemini -m pro -o text -e "" "Given this bug symptom:
SYMPTOM: [describe what's happening]
CONTEXT: [relevant code/system info]
Generate 5 hypotheses ranked by likelihood, with a test for each."
gemini -m pro -o text -e "" "Given this bug symptom:
SYMPTOM: [describe what's happening]
CONTEXT: [relevant code/system info]
Generate 5 hypotheses ranked by likelihood, with a test for each."
Then test each hypothesis
Then test each hypothesis
rg "hypothesis-related-pattern" src/
undefinedrg "hypothesis-related-pattern" src/
undefinedCommon Investigations
常见排查场景
Find All Error Handling
查找所有错误处理逻辑
bash
rg "catch|\.catch|try\s*{" src/ -t ts
rg "throw\s+new" src/ -t tsbash
rg "catch|\.catch|try\s*{" src/ -t ts
rg "throw\s+new" src/ -t tsFind API Endpoints
查找API端点
bash
rg "(get|post|put|delete|patch)\s*\(" src/ -i
rg "router\.(get|post|put|delete)" src/
rg "@(Get|Post|Put|Delete)" src/bash
rg "(get|post|put|delete|patch)\s*\(" src/ -i
rg "router\.(get|post|put|delete)" src/
rg "@(Get|Post|Put|Delete)" src/Find Database Queries
查找数据库查询
bash
rg "(SELECT|INSERT|UPDATE|DELETE)" src/ -i
rg "\.query\(|\.execute\(" src/
rg "prisma\.\w+\.(find|create|update|delete)" src/bash
rg "(SELECT|INSERT|UPDATE|DELETE)" src/ -i
rg "\.query\(|\.execute\(" src/
rg "prisma\.\w+\.(find|create|update|delete)" src/Find Configuration
查找配置信息
bash
rg "process\.env\." src/
rg "(config|settings)\[" src/
rg "getenv|os\.environ" src/ -t pybash
rg "process\.env\." src/
rg "(config|settings)\[" src/
rg "getenv|os\.environ" src/ -t pyFind Security Issues
查找安全问题
bash
undefinedbash
undefinedSQL injection potential
SQL injection potential
rg "query.+."|'.*+" src/
rg "query.+."|'.*+" src/
Hardcoded secrets
Hardcoded secrets
rg "(password|secret|key|token)\s*=\s*['"]" src/ -i
rg "(password|secret|key|token)\s*=\s*['"]" src/ -i
Unsafe eval
Unsafe eval
rg "eval(" src/
undefinedrg "eval(" src/
undefinedDeep Investigation Script
深度排查脚本
bash
#!/bin/bashbash
#!/bin/bashinvestigate.sh - Comprehensive code investigation
investigate.sh - Comprehensive code investigation
TERM=$1
echo "=== Investigating: $TERM ==="
echo ""
echo "### Definitions ###"
rg "^(export\s+)?(function|const|class|interface|type)\s+$TERM" src/
echo ""
echo "### Usage ###"
rg "$TERM" src/ --stats | head -50
echo ""
echo "### Recent Changes ###"
git log --oneline -10 -S "$TERM"
echo ""
echo "### Blame ###"
for f in $(rg -l "$TERM" src/); do
echo "--- $f ---"
git blame -L "/$TERM/,+5" "$f" 2>/dev/null | head -10
done
undefinedTERM=$1
echo "=== Investigating: $TERM ==="
echo ""
echo "### Definitions ###"
rg "^(export\s+)?(function|const|class|interface|type)\s+$TERM" src/
echo ""
echo "### Usage ###"
rg "$TERM" src/ --stats | head -50
echo ""
echo "### Recent Changes ###"
git log --oneline -10 -S "$TERM"
echo ""
echo "### Blame ###"
for f in $(rg -l "$TERM" src/); do
echo "--- $f ---"
git blame -L "/$TERM/,+5" "$f" 2>/dev/null | head -10
done
undefinedBest Practices
最佳实践
- Start with the error - Search for exact message first
- Expand context - Use ,
-C,-Bfor surrounding code-A - Check history - finds when code was introduced
git log -S - Use AI for complex - When pattern matching isn't enough
- Document findings - Note what you discover
- Test hypotheses - Verify before assuming
- 从错误入手 - 首先搜索确切的错误信息
- 扩展上下文 - 使用、
-C、-B参数查看周边代码-A - 查看历史记录 - 可以查找代码的引入时间
git log -S - 复杂场景用AI - 当模式匹配无法解决时使用AI
- 记录排查结果 - 记录你的发现
- 验证假设 - 先验证再下结论