| name | debugging-strategies |
| description | Systematic debugging: stack trace analysis, log-based diagnosis, bisecting regressions, memory leak detection, and profiling. |
| metadata | {"thinkfleetbot":{"emoji":"🐛","requires":{"anyBins":["git","node","python3"]}}} |
Debugging Strategies
Systematic approaches to finding and fixing bugs.
Stack Trace Analysis
Read the trace bottom-up
The bottom of a stack trace is the most recent call — start there.
NODE_OPTIONS="--stack-trace-limit=50" node app.js
python3 -u -X tracemalloc app.py
Common patterns
- TypeError: Cannot read property of undefined → Check the object one level up in the chain
- ECONNREFUSED → Service isn't running or wrong port
- ENOMEM → Memory exhaustion, check for leaks
- Segfault → Native addon or buffer overflow
Log-Based Debugging
tail -f app.log | grep -E "ERROR|WARN"
grep -n "error" app.log | tail -20
grep "req-abc123" app.log
docker logs --since 5m --follow myapp 2>&1 | grep -i error
kubectl logs -l app=myapp --since=10m | grep -i "exception\|error\|fatal"
kubectl logs -l app=myapp --all-containers --prefix --since=5m
Git Bisect (Find the Breaking Commit)
git bisect start
git bisect bad
git bisect good v1.0.0
git bisect good
git bisect bad
git bisect run npm test
git bisect reset
git bisect log
Memory Leak Detection
Node.js
node --inspect app.js
node -e "
setInterval(() => {
const mem = process.memoryUsage();
console.log(JSON.stringify({
rss: (mem.rss / 1024 / 1024).toFixed(1) + 'MB',
heap: (mem.heapUsed / 1024 / 1024).toFixed(1) + 'MB',
external: (mem.external / 1024 / 1024).toFixed(1) + 'MB'
}));
}, 5000);
require('./app.js');
"
node --max-old-space-size=512 --heap-prof app.js
Python
python3 -c "
import tracemalloc
tracemalloc.start()
# ... run code ...
snapshot = tracemalloc.take_snapshot()
for stat in snapshot.statistics('lineno')[:10]:
print(stat)
"
python3 -c "
import objgraph
objgraph.show_most_common_types(limit=10)
"
Network Debugging
dig example.com +short
nslookup example.com
nc -zv localhost 3000
curl -s -o /dev/null -w "DNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n" https://example.com
lsof -i :3000
netstat -an | grep 3000
Reproduce and Isolate
node --version && npm --version && echo "OS: $(uname -s)"
env | grep -iE "node|npm|path|home" | sort
for i in $(seq 1 10); do
npm test 2>&1 | tail -1
done
Notes
- Reproduce first, fix second. If you can't reproduce it, you can't verify the fix.
git bisect run is underused — it automates finding the exact commit that broke something.
- Memory leaks show as gradually increasing RSS over time. Take snapshots at intervals and compare.
- When stuck, explain the bug to someone (or write it down). Rubber duck debugging works.
- Check the obvious first: is the service running? Are env vars set? Is the config correct?