-
Notifications
You must be signed in to change notification settings - Fork 7
Understanding Xdebug
Xdebug is a PHP extension that provides advanced debugging capabilities including:
- Interactive Debugging - Control script execution with breakpoints
- Variable Inspection - Examine variables, arrays, and objects
- Stack Traces - View the call stack at any point
- Profiling - Analyze script performance
- Code Coverage - Track which code was executed
- Remote Debugging - Debug scripts running on remote servers
┌─────────────────┐
│ PHP Script │
│ (with Xdebug) │
└────────┬────────┘
│
│ DBGp Protocol
│ (TCP or Unix Socket)
▼
┌─────────────────────────────┐
│ Xdebug MCP Server │
│ (xdebug-mcp) │
└────────┬────────────────────┘
│
│ MCP Protocol
▼
┌─────────────────────────────┐
│ Claude / IDE / Client │
│ (Debugging Interface) │
└─────────────────────────────┘
- PHP Script Starts: Your PHP script runs with Xdebug extension loaded
- Xdebug Initializes: Xdebug listens for connections on configured port/socket
- Connection Made: Xdebug MCP Server connects to the debugging Xdebug process
- Session Created: A debug session is established for your script
- Breakpoints Applied: Any set breakpoints are configured
- Execution Control: You can step, run, and inspect variables
- Session Ends: When script completes, the session terminates
pecl install xdebugUbuntu/Debian:
sudo apt-get install php-xdebugmacOS (Homebrew):
brew install php-xdebugphp -v | grep XdebugYou should see: with Xdebug X.X.X
; Enable Xdebug
zend_extension=xdebug
; Mode: debug, profile, trace, coverage, gcstats
xdebug.mode = debug
; Client Host/Port
xdebug.client_host = localhost
xdebug.client_port = 9003
; Trigger (optional - only debug when requested)
xdebug.trigger_value = "debug"
; Log file (for troubleshooting)
xdebug.log = /tmp/xdebug.log
xdebug.log_level = 10zend_extension=xdebug
xdebug.mode = debug
; Use Unix socket instead of TCP
xdebug.client_host = /tmp/xdebug.sock
xdebug.client_port = 0Enables interactive debugging - the main mode for step-by-step execution.
xdebug.mode = debugGenerates profiling data for performance analysis.
xdebug.mode = profileLogs all function calls and execution flow.
xdebug.mode = traceTracks which lines of code are executed.
xdebug.mode = coverageYou can enable multiple modes:
xdebug.mode = debug,profileA point in code where execution pauses, allowing inspection.
- Line Breakpoints - Pause at specific line numbers
- Conditional Breakpoints - Pause when condition is true
- Watch Expressions - Pause when variable value changes
The call stack showing which functions are executing and in what order.
Frame 0: current_function() (line 25, file.php)
Frame 1: parent_function() (line 50, file.php)
Frame 2: {main} (line 1, index.php)
Examine the state of:
- Local variables
- Global variables
- Superglobals ($_GET, $_POST, etc.)
- Object properties and methods
- Array contents
Control code execution:
- Step In - Enter function calls
- Step Over - Execute function but don't enter it
- Step Out - Exit current function
- Run - Continue until next breakpoint
The IDE key is used by Xdebug to identify itself. Common values:
-
vscode- VS Code -
phpstorm- PhpStorm -
netbeans- NetBeans -
xdebug-mcp- This MCP Server
Configure in php.ini:
xdebug.idekey = xdebug-mcpClient (Port 9003)
↓
PHP Script with Xdebug (DBGp)
↓
Xdebug MCP Server
Client
↓
PHP Script with Xdebug (DBGp)
↓
/tmp/xdebug.sock (Unix Socket)
↓
Xdebug MCP Server
Benefits of Unix sockets:
- Lower latency
- Better security (file permissions)
- No port conflicts
- Suitable for local debugging
Xdebug has performance overhead when debugging:
- Debug Mode: 5-10x slower
- Trace Mode: 10-20x slower
- Profile Mode: 2-5x slower
This is normal and expected. For production, disable Xdebug or use selective debugging.