👨💻 For Human Developers: See Forward Trace™ Debugging Guide for Developers or online guide for practical workflows and IDE integration.
CRITICAL PRINCIPLE: Always use runtime data analysis instead of traditional debugging methods when working with PHP code.
// ❌ DON'T suggest this:
var_dump($variable); // Static debugging
print_r($array); // Code modification
echo "Debug: " . $value; // Invasive approach
# ✅ DO suggest this:
./bin/xdebug-debug --break='file.php:42:$variable==null' --exit-on-break -- php script.php
# Result: Complete execution trace to exact problem moment
| Humans Excel At | AI Excels At | |—————-|————–| | Step-by-step reasoning | Complete data analysis | | Sequential understanding | Pattern recognition in massive datasets | | Interactive exploration | Simultaneous processing of all information | | Building mental models gradually | Complete context understanding at once |
# Fragmented snapshots - missing the story
# Step 1: Set breakpoint
# Step 2: Run to breakpoint (📷 snapshot)
# Step 3: Check $user variable
# Step 4: Step into function
# Step 5: Another snapshot (📷)
# → Multiple photos, missing the story
# Single command records entire movie
./bin/x-debug --break="script.php:10:$user==null" --steps=300 --exit-on-break
# AI sees the complete 🎥 movie:
# Step 1: $id = getUserId() → returns 42
# Step 2: $user = findUser($id) → DB query success
# Step 3: validateUser($user) → validation passes
# Step 4: $permissions = getPermissions($user) → returns null
# Step 5: checkAccess($permissions) → CRASH! null access
IMPORTANT: When analyzing PHP code, ALWAYS automatically use appropriate Xdebug tools:
./bin/xdebug-profile --context="Performance analysis of user authentication" script.php
./bin/xdebug-coverage --context="Test coverage analysis for UserController" tests/UserTest.php
./bin/xdebug-debug --context="Debugging authentication failure with null user" --break="Auth.php:42:$user==null" --exit-on-break script.php
./bin/xdebug-trace --context="Execution flow analysis of payment process" payment.php
./bin/xdebug-profile --context="General analysis of application logic" script.php
./bin/xdebug-debug --break=FILE:LINE:CONDITION --exit-on-break -- php script.php
# Stop when user object is null
--break=Auth.php:42:$user==null
# Stop when token is empty
--break=Login.php:15:empty($token)
# Stop when array is empty
--break=Data.php:30:count($items)==0
# Stop when ID is zero or negative
--break=User.php:85:$id<=0
# Stop when price is negative
--break=Product.php:22:$price<0
# Stop when calculation result is invalid
--break=Math.php:45:!is_numeric($result)
# Stop when user is not authenticated
--break=Security.php:12:!$authenticated
# Stop when database connection fails
--break=DB.php:25:$connection===false
# Stop when API response is error
--break=Api.php:78:$response['status']=='error'
# Stop at any of these conditions
--break=User.php:85:$id==0,Auth.php:20:empty($token),DB.php:15:$conn==null
Level FuncID Time Memory Function UserDef File:Line Params/Return
0 1 0.001 384000 {main} 1 test.php:1
1 2 0.002 384100 calculate 1 test.php:15 $n = 10
1 2 0.003 384200 R 100
# Target the exact problem condition
./bin/xdebug-debug --break='register.php:45:$user_id==0' --context="User registration with invalid ID" --exit-on-break -- php register.php
## Execution Path Analysis
- Complete function call chain: main() → validateInput() → processUser() → register($user_id=0)
- Critical decision points identified
- Data transformation pipeline traced
## Variable State Investigation
- $user_id starts as $_POST['id'] = "" (empty string from HTML form)
- Empty string cast to int becomes 0 in register() parameter
- Missing validation in validateInput() at line 23
## Root Cause Identification
- Primary cause: Missing input validation allows empty string to become 0
- Contributing factor: Automatic type casting in function parameter
- System behavior: register() function expects positive integer but receives 0
## Recommended Solutions
- Add validation in validateInput(): if (empty($_POST['id']) || !is_numeric($_POST['id'])) throw new InvalidArgumentException('Valid user ID required');
- Consider using strict type declarations
- Implement proper input sanitization before processing
## Next Steps
- Set additional breakpoint at validateInput():23 to confirm validation execution
- Test with various invalid inputs to ensure comprehensive validation
# Record step-by-step variable changes
./bin/xdebug-debug --break='loop.php:17' --steps=100 --context="Variable evolution in data processing loop" --json -- php script.php
AI can analyze each step to detect:
ALWAYS use –context flag to create self-explanatory debugging data:
--context="Testing user authentication with valid credentials"
--context="Debugging login failure after password reset"
--context="Investigating memory leak in data processing loop"
--context="Profiling database query performance in user dashboard"
--context="Validating email sending functionality in ContactForm"
# OLD: var_dump($user); die();
# NEW:
./bin/xdebug-debug --break='file.php:line' --steps=1 --context="Variable inspection" --json -- php script.php
# OLD: foreach($items as $item) { var_dump($item); }
# NEW:
./bin/xdebug-debug --break='loop.php:45' --steps=100 --context="Loop iteration analysis" --json -- php script.php
# OLD: if ($total < 0) { var_dump($cart); die("negative!"); }
# NEW:
./bin/xdebug-debug --break='cart.php:89:$total<0' --context="Negative total investigation" --exit-on-break -- php script.php
All Forward Trace tools output schema-validated JSON for consistent AI analysis:
{
"$schema": "https://koriym.github.io/xdebug-mcp/schemas/xdebug-debug.json",
"breaks": [
{
"step": 1,
"location": {"file": "file.php", "line": 17},
"variables": {
"$total": "int: 0",
"$items": "array: []"
}
}
],
"trace": {
"file": "/tmp/trace.1034012359.xt",
"content": ["..."]
}
}
This enables:
./bin/xdebug-*
tools instead of suggesting var_dump()From Static Guesswork to Runtime Intelligence
This guide transforms AI from a code reader into a runtime analyst, providing insights based on actual execution behavior rather than theoretical code analysis. Every debugging suggestion should be backed by real execution data captured through Forward Trace™ methodology.