From var_dump() to xdebug_start_trace()
A Paradigm Shift in AI-Assisted Development
I watched Claude Code debug PHP:
var_dump($variable); // AI adds this
// Run tests, analyze output
var_dump($other); // AI adds another
// Remove first, add third...
var_dump($result); // Repeat... and cleanup...
๐ฑ
"State-of-the-art AI was debugging like it was 1995"
We were asking a detective to solve a crime using only ๐ท crime scene photos.
What we provide:
But we never show what actually happened! ๐น๐บ๐ฅ
"AI is always hungry for data."
What AI really needs:
Before (Static Analysis) | After (Runtime Intelligence) |
---|---|
var_dump() AI examines code structure โ Makes educated guesses โ Suggests print debugging |
xdebug_start_trace() AI analyzes execution traces โ Identifies exact failure point โ Provides precise fix |
// Multiple debugging attempts
// Modify code, test, repeat...
# Conditional breakpoint: Stop when problem occurs
./vendor/bin/xdebug-debug --break='checkout.php:89:$total<0' --steps=50 --exit-on-break -- php checkout.php
# AI analyzes and reports:
"Found it: $50 discount applied to $30 cart = -$20 total
Fix: Add $this->recalculateDiscount() after removeItem()"
# 1. Clone from GitHub
git clone https://github.com/koriym/xdebug-mcp.git
cd xdebug-mcp && composer install
# 2. Create a simple test
echo '<?php
$result = "success";
if (rand(0,1)) { $result = null; }
echo $result;
?>' > test.php
# 3. Let AI analyze execution
./vendor/bin/xdebug-trace --context="Find why \$result becomes null" -- php test.php
# 4. Enable full AI integration
claude mcp add xdebug php "$(pwd)/vendor/bin/xdebug-mcp"
Challenge | ๐ค Human with IDE | ๐ค AI with Runtime Data |
---|---|---|
Intermittent bugs | Set breakpoint, hope to catch it | Set conditional breakpoint, capture every occurrence |
Complex flows | Step through manually, miss details | Process entire execution trace, identify all patterns |
Race conditions | Nearly impossible to catch | Set time-based conditionals, capture exact timing |
๐ท Photo Shooting
(Fragmented Snapshots)
// Step 1: Set breakpoint
// Step 2: Run to breakpoint (๐ท snapshot)
// Step 3: Check $user variable
// Step 4: Step into function
// Step 5: Another snapshot (๐ท)
// Step 6: Check another variable...
// โ Multiple photos, missing the story
๐ฅ Video Shooting
(Complete Recording)
# Single command records entire movie
./vendor/bin/xdebug-debug --break='script.php:10:$user==null' --steps=300 --exit-on-break -- php script.php
# 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
AI processes complete context โ Provides insights to humans
AI can process complexity that overwhelms humans
"Here's a trace file. Figure out what it means."
Schema = Meaning + Data Structure + AI Guidance
2 1 0 0.000489 401872 testFunction 1 test.php 35 2 $a=null $b=10
2 1 1 0.000521 403216
2 1 R array(3) { [0]=>int(1) [1]=>int(2) [2]=>int(3) }
{
"trace_file": "/tmp/trace.1034012359.xt",
"total_lines": 847,
"unique_functions": 23,
"max_call_depth": 12,
"database_queries": 15,
"specification": "https://xdebug.org/docs/trace",
"analysis_context": "Debugging user authentication flow with database queries",
"content": ["2\t1\t0\t0.000489\t401872\ttestFunction\t1\t\ttest.php\t35\t2\t$a=null $b=10"]
}
AI doesn't just parse data - it knows its mission
"Here's data in format X. Good luck figuring out what's important."
"Here's data with complete analysis instructions built-in."
We're not just giving AI runtime data.
We're giving AI the knowledge of how to use that data effectively.
Bridging the understanding gap between humans and AI
We can continue treating AI as a sophisticated autocomplete engine...
Or we can make AI our partner through complete transparency
Not just sharing our code, but our entire development context.
I wondered what kind of development that would lead to
// Must pollute your code
if ($userId === 0) {
var_dump($userData, $permissions, $sessionData);
error_log("Debug: User ID 0 detected");
// Risk of forgetting to remove in production...
}
Problems: Code pollution, production risks, cleanup
# Wait silently until condition met, then start tracing
./vendor/bin/xdebug-debug --break='User.php:85:$userId==0' --steps=100 --exit-on-break -- php app.php
# Get complete execution history: how did we get $userId=0?
# AI analyzes complete execution trace leading to condition
Benefits: No code changes, complete context, safe
# Catch condition-specific problems with certainty
./vendor/bin/xdebug-debug --break='DataProcessor.php:42:empty($items)' --steps=200 --exit-on-break -- php batch.php
# AI analysis to identify root cause
# Analyzes the complete path to empty array condition
# Microsecond-precision performance profiling
./vendor/bin/xdebug-profile --context="API performance analysis" -- php slow_api.php
# AI analyzes bottlenecks and provides optimization suggestions
# Result: Complete performance profile with bottleneck identification
"Found: fetchUser() called 847 times (72% execution time). Add caching at line 42."
AI sees: Function call counts, execution times, memory usage patterns
# AI-optimized debugging with complete context
./vendor/bin/xdebug-debug --break='User.php:42:$user==null' --steps=150 --exit-on-break -- php app.php
# AI gets complete execution history leading to condition
# Provides comprehensive analysis of path to null user
AI receives comprehensive data:
Revolutionary: AI sees the complete story, not just fragments
# Trace only your test code, not PHPUnit internals
./vendor/bin/xdebug-phpunit --context="Test execution analysis" tests/UserTest.php::testComplexWorkflow
# AI analyzes test execution
# Shows detailed test execution path and failure analysis
Smart filtering: Traces your application code, ignores PHPUnit framework noise
AI benefits: Focus on actual business logic, not testing framework details
# Works with any PHP script, not just PHPUnit
./vendor/bin/xdebug-coverage -- php ./vendor/bin/phpunit # PHPUnit
./vendor/bin/xdebug-coverage -- php my-script.php # Any script
# Native Xdebug format - fast AI understanding
Key advantages:
# New feature: Visual branch/path coverage analysis
./bin/xdebug-coverage --branch-coverage -- php app.php
# Single execution, multiple AI-ready outputs:
# โ
Detailed branch coverage JSON
# โ
GraphViz dot file (/tmp/paths.dot)
# โ
Visualization command included
# AI can immediately visualize
dot -Tpng /tmp/paths.dot -o /tmp/coverage_paths.png
AI Benefits:
"AI can see and understand code path diagrams"
Complete execution flow with all variable states
Smart capture only when conditions met
Comprehensive metrics and timing analysis
Full context test coverage mapping
I want to see AI use these tools freely in the future
Quality Problem๐ค: Is "Passing Checks" Enough?
But is the code actually efficient? ๐ค
// Tests pass โ
assert($result === "John");
// But HOW was it obtained?
// Runtime reveals reality
DB query: 10,000 rows
Loop: 8,743 iterations
Time: 2.3 seconds
// Should be: 0.003 seconds!
"Correctness" โก๏ธ "Efficiency" - The quality evolution AI demands
"AI that uses IDE like human developers"
Stateless MCP โก Stateful Xdebug
47 commands โ Single Forward Trace
GitHub: github.com/koriym/xdebug-mcp
Install: composer require koriym/xdebug-mcp
Status Badges:
AI Native: YES Runtime Data: YES var_dump(): NO Guesswork: NOSpecial thanks to Derick Rethans for creating Xdebug, the foundation that makes all of this possible.
โ Akihito Koriyama (@koriym)