| name | debugging |
| description | Systematically diagnose and fix bugs using structured debugging techniques. Use when investigating errors, unexpected behavior, failing tests, or production issues. |
| allowed-tools | ["Read","Grep","Glob","Bash"] |
| tags | ["debugging","errors","troubleshooting","logs","stack-trace","bugs","diagnostics"] |
| platforms | ["Claude","ChatGPT","Gemini"] |
| author | locusai |
Debugging
When to use this skill
- Investigating error messages or stack traces
- Diagnosing unexpected behavior
- Fixing failing tests
- Debugging production issues
- Understanding why code doesn't work as expected
Step 1: Reproduce the problem
Before fixing anything, confirm you can reproduce it:
npm test -- --grep "failing test name"
pytest -xvs test_file.py::test_name
tail -100 logs/error.log
docker compose logs app --tail 100
Key questions:
- What exactly is the error? (exact message, stack trace)
- When did it start? (recent commit, dependency update?)
- Where does it happen? (specific input, environment, timing?)
- How often? (every time, intermittent, under load?)
Step 2: Read the error carefully
TypeError: Cannot read properties of undefined (reading 'map')
at UserList (src/components/UserList.tsx:15:23)
at renderWithHooks (node_modules/react-dom/...)
Parse the stack trace:
- Error type:
TypeError — wrong type, likely null/undefined
- Message:
Cannot read properties of undefined — something is undefined
- Property:
reading 'map' — trying to call .map() on undefined
- Location:
UserList.tsx:15 — exact file and line
- Context: React component render — data likely not loaded yet
Step 3: Narrow the scope
Binary search approach
If you don't know where the bug is, bisect:
git bisect start
git bisect bad
git bisect good abc123
git bisect run npm test
Isolate variables
- Does it fail with minimal input?
- Does it fail in a fresh environment?
- Does it fail without feature flags / config?
- Does it fail with the previous version of dependencies?
Step 4: Inspect state
Check the data
grep -rn "userData\s*=" --include="*.ts" src/
psql -c "SELECT * FROM users WHERE id = 123"
curl -s http://localhost:3000/api/users/123 | jq .
Add targeted logging
console.log('[DEBUG] userData:', JSON.stringify(userData, null, 2));
console.log('[DEBUG] typeof userData:', typeof userData);
console.log('[DEBUG] userData keys:', Object.keys(userData ?? {}));
import logging
logger = logging.getLogger(__name__)
logger.debug(f"user_data: {user_data!r}")
logger.debug(f"type: {type(user_data)}, len: {len(user_data) if user_data else 'N/A'}")
Step 5: Common bug patterns
Null/undefined access
const names = users.map(u => u.name);
const names = (users ?? []).map(u => u.name);
Async timing
const data = fetchData();
console.log(data.length);
const data = await fetchData();
Stale closure
useEffect(() => {
const interval = setInterval(() => {
setCount(count + 1);
}, 1000);
return () => clearInterval(interval);
}, []);
setCount(prev => prev + 1);
Off-by-one
for (let i = 0; i < arr.length - 1; i++) { ... }
for (let i = 0; i < arr.length; i++) { ... }
Race condition
fetchUser(1).then(setUser);
fetchUser(2).then(setUser);
const controller = new AbortController();
fetchUser(id, { signal: controller.signal }).then(setUser);
return () => controller.abort();
Step 6: Verify the fix
- Reproduce the original bug one more time
- Apply the fix
- Verify the bug is gone
- Check that no other tests broke
- Test edge cases related to the fix
- Remove all debug logging
npm test -- --grep "the test that was failing"
npm test
curl -X POST http://localhost:3000/api/users -d '{"name": "test"}'
Debugging checklist