If you find Xdebug useful, please consider supporting the project.

Development Aids

Xdebug's development aids help you get better error messages and obtain better information from PHP's built-in functions. The aids include an upgraded var_dump() function; location, stack, and argument information upon Notices, Warnings and Exceptions; and numerous functions and settings to tweak PHP's behaviour.

Contents

Improved var_dump() function

Xdebug changes PHP's built-in var_dump() function to be HTML-formatted, and includes information about location. The overloaded var_dump() is active in HTML contexts (when PHP's html_errors setting is 1), as well as on the command line.

HTML Context

Without Xdebug loaded, and a <pre> tag artificially added:

array(4) {
  ["one"]=>
  string(23) "a somewhat long string!"
  ["two"]=>
  array(1) {
    ["two.one"]=>
    array(2) {
      ["two.one.zero"]=>
      int(210)
      ["two.one.one"]=>
      array(2) {
        ["two.one.one.zero"]=>
        float(3.141592564)
        ["two.one.one.one"]=>
        float(2.7)
      }
    }
  }
  ["three"]=>
  object(test)#1 (3) {
    ["pub"]=>
    *RECURSION*
    ["priv":"test":private]=>
    bool(true)
    ["prot":protected]=>
    int(42)
  }
  ["four"]=>
  array(6) {
    [0]=>
    int(0)
    [1]=>
    int(1)
    [2]=>
    int(2)
    [3]=>
    int(3)
    [4]=>
    int(4)
    [5]=>
    int(5)
  }
}

With Xdebug loaded, it will output this instead:

/tmp/var_dump.php:23:
array (size=4)
  'one' => string 'a somewhat long string!' (length=23)
  'two' =>
    array (size=1)
      'two.one' =>
        array (size=2)
          'two.one.zero' => int 210
          'two.one.one' =>
            array (size=2)
              ...
  'three' =>
    object(test)[1]
      public 'pub' =>
        &object(test)[1]
      private 'priv' => boolean true
      protected 'prot' => int 42
  'four' =>
    array (size=6)
      0 => int 0
      1 => int 1
      2 => int 2
      3 => int 3
      4 => int 4
      5 => int 5

The changes are:

There are a number of settings that change control how much output is shown and/or hidden. These settings are:

xdebug.var_display_max_data
controls how large strings can be before they're truncated, and shown with elipsis (…).
xdebug.var_display_max_depth
controls how many levels deep into a data structure information is shown.
xdebug.var_display_max_children
controls how many array elements or object properties are shown.

Command Line

The overloaded var_dump() is also active on the command line, and reacts to the same configuration settings with regards to what is shown in the HTML context. By default it does not show colours, but this can be turned on by setting xdebug.cli_color to 1.

That turns:

/tmp/var_dump2.php:12:
array(2) {
  'one' =>
  string(23) "a somewhat long string!"
  'three' =>
  class test#1 (3) {
    public $pub =>
    bool(false)
    private $priv =>
    bool(true)
    protected $prot =>
    int(42)
  }
}

Into:

/tmp/var_dump2.php:12:
array(2) {
  'one' =>
  string(23) "a somewhat long string!"
  'three' =>
  class test#1 (3) {
    public $pub =>
    bool(false)
    private $priv =>
    bool(true)
    protected $prot =>
    int(42)
  }
}

Stack Traces

When Xdebug is loaded, it overrides PHP's standard error handler callbacks, and provides one that also shows a stack trace. These stack traces contain a list of all the functions and method that were called between the start of the script and where the error occurred. An example of such a stack trace is:

( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 70
Call Stack
#TimeMemoryFunctionLocation
10.0007374160{main}( ).../stack.php:0
20.0010376752foo( $a = array (42 => FALSE, 'foo' => 9121240, 43 => class stdClass { public $bar = 100 }, 44 => class stdClass { }, 45 => resource(3) of type (stream)) ).../stack.php:84
Variables in local scope (#2)
$a =
/home/httpd/html/test/xdebug/docs/stack.php:70:
array (size=5)
  42 => boolean false
  'foo' => int 9121240
  43 => 
    object(stdClass)[1]
      public 'bar' => int 100
  44 => 
    object(stdClass)[2]
  45 => resource(3, stream)
$i =
/home/httpd/html/test/xdebug/docs/stack.php:70:int 3047104

This is an example of a stack trace with only some of the optional features turned on. The "Variables in local scope" section will only show when you set xdebug.show_local_vars to 1.

Here follows a list with settings that change the information that is shown in a stack trace:

xdebug.dump_globals
xdebug.dump_once
xdebug.dump_undefined
xdebug.dump.*
xdebug.dump_globals controls whether information from super globals is added to the stack trace. You can configure which super globals and keys are shown with the specific xdebug.dump.* settings, whether they shown only once with xdebug.dump_once, and whether to include undefined super global keys with xdebug.dump_undefined.
xdebug.file_link_format
The format of the generated link in the Location column: .../stack.php:84
xdebug.filename_format
Which part(s) of the path to show in the Location column.
xdebug.max_stack_frames
How many stack frames to show
xdebug.show_local_vars
Whether to include the available variables from the scope of the last called user defined function.
xdebug.var_display_max_data
xdebug.var_display_max_depth
xdebug.var_display_max_children
How much information to include for arguments to function calls and local variables. These are the same settings as used for improved var_dump() function.

Two more settings control whether Xdebug should show stack traces even when an Error or Exception has been caught:

xdebug.show_error_trace
xdebug.show_exception_trace

You can find more detailed information for each setting in their own documentation section.

Filtering

Xdebug includes filtering capabilities for stack traces. A filter either includes, or excludes, paths or class name prefixes (namespaces). You can use a filter to prevent anything from a vendor directory to appear in a stack trace, or to only include classes from specific namespaces.

To set-up a filter that shows only functions and methods that either have no class name, or are prefixed with "Xdebug", you would call xdebug_set_filter() with:

Filter Example

xdebug_set_filter(
	XDEBUG_FILTER_TRACING,
	XDEBUG_NAMESPACE_INCLUDE,
	[ '', 'Xdebug' ]
);

With this filter set-up, you will only see functions (without class) and all method calls of classes that start with "Xdebug". This includes built-in PHP functions (such as strlen()) and calls to XdebugTest::bar(). The filter does not enforce that "Xdebug" is the name of a namespace, and only does a strict character comparison from the start of the fully qualified class name. Add a \ to the prefix to make sure only classes in the Xdebug\ namespace are included.

The full documentation for the arguments to xdebug_set_filter() are described on its own documentation page.

Related Settings and Functions

Settings


integer xdebug.cli_color = 0 #

If this setting is 1, Xdebug will color var_dumps and stack traces output when in CLI mode and when the output is a tty. On Windows, the ANSICON tool needs to be installed.

If the setting is 2, then Xdebug will always color var_dumps and stack trace, no matter whether it's connected to a tty or whether ANSICON is installed. In this case, you might end up seeing escape codes.

See this article for some more information.


integer xdebug.collect_params = 0 #

This setting, defaulting to 0, controls whether Xdebug should collect the parameters passed to functions when a function call is recorded in either the function trace or the stack trace.

The setting defaults to 0 because for very large scripts it may use huge amounts of memory and therefore make it impossible for the huge script to run. You can most safely turn this setting on, but you can expect some problems in scripts with a lot of function calls and/or huge data structures as parameters. Xdebug 2 will not have this problem with increased memory usage, as it will never store this information in memory. Instead it will only be written to disk. This means that you need to have a look at the disk usage though.

This setting can have four different values. For each of the values a different amount of information is shown. Below you will see what information each of the values provides. See also the introduction of the feature Development Aids for a few screenshots.

ValueArgument Information Shown
0None.
1Type and number of elements (f.e. string(6), array(8)).
2

Type and number of elements, with a tool tip for the full information 1.

3Full variable contents (with the limits respected as set by xdebug.var_display_max_children, xdebug.var_display_max_data and xdebug.var_display_max_depth.
4Full variable contents and variable name.
5PHP serialized variable contents, without the name.

1 in the CLI version of PHP it will not have the tool tip, nor in output files.


string xdebug.dump.* = Empty #

* can be any of COOKIE, FILES, GET, POST, REQUEST, SERVER, SESSION. These seven settings control which data from the superglobals is shown when an error situation occurs.

Each of those php.ini setting can consist of a comma separated list of variables from this superglobal to dump, or * for all of them. Make sure you do not add spaces in this setting.

In order to dump the REMOTE_ADDR and the REQUEST_METHOD when an error occurs, and all GET parameters, add these settings:

xdebug.dump.SERVER = REMOTE_ADDR,REQUEST_METHOD
xdebug.dump.GET = *

boolean xdebug.dump_globals = true #

When this setting is set to true, Xdebug adds the values of the super globals as configured through the xdebug.dump.* to on-screen stack traces and the error log (if enabled).


boolean xdebug.dump_once = true #

Controls whether the values of the superglobals should be dumped on all error situations (set to 0) or only on the first (set to 1).


boolean xdebug.dump_undefined = false #

If you want to dump undefined values from the superglobals you should set this setting to 1, otherwise leave it set to 0.


string xdebug.file_link_format = #

This setting determines the format of the links that are made in the display of stack traces where file names are used. This allows IDEs to set up a link-protocol that makes it possible to go directly to a line and file by clicking on the filenames that Xdebug shows in stack traces. An example format might look like:

myide://%f@%l

The possible format specifiers are:

SpecifierMeaning
%fthe filename
%lthe line number

For various IDEs/OSses there are some instructions listed on how to make this work:

Firefox on Linux

  • Open about:config
  • Add a new boolean setting "network.protocol-handler.expose.xdebug" and set it to "false"
  • Add the following into a shell script ~/bin/ff-xdebug.sh:
    #! /bin/sh
    
    f=`echo $1 | cut -d @ -f 1 | sed 's/xdebug:\/\///'`
    l=`echo $1 | cut -d @ -f 2`
    
    Add to that one of (depending whether you have komodo, gvim or netbeans):
    • komodo $f -l $l
    • gvim --remote-tab +$l $f
    • netbeans "$f:$l"
  • Make the script executable with chmod +x ~/bin/ff-xdebug.sh
  • Set the xdebug.file_link_format setting to xdebug://%f@%l

Windows and netbeans

  • Create the file netbeans.bat and save it in your path (C:\Windows will work):
    @echo off
    setlocal enableextensions enabledelayedexpansion
    set NETBEANS=%1
    set FILE=%~2
    %NETBEANS% --nosplash --console suppress --open "%FILE:~19%"
    nircmd win activate process netbeans.exe
    

    Note: Remove the last line if you don't have nircmd.

  • Save the following code as netbeans_protocol.reg:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\netbeans]
    "URL Protocol"=""
    @="URL:Netbeans Protocol"
    
    [HKEY_CLASSES_ROOT\netbeans\DefaultIcon]
    @="\"C:\\Program Files\\NetBeans 7.1.1\\bin\\netbeans.exe,1\""
    
    [HKEY_CLASSES_ROOT\netbeans\shell]
    
    [HKEY_CLASSES_ROOT\netbeans\shell\open]
    
    [HKEY_CLASSES_ROOT\netbeans\shell\open\command]
    @="\"C:\\Windows\\netbeans.bat\" \"C:\\Program Files\\NetBeans 7.1.1\\bin\\netbeans.exe\" \"%1\""
    

    Note: Make sure to change the path to Netbeans (twice), as well as the netbeans.bat batch file if you saved it somewhere else than C:\Windows\.

  • Double click on the netbeans_protocol.reg file to import it into the registry.
  • Set the xdebug.file_link_format setting to xdebug.file_link_format = "netbeans://open/?f=%f:%l"

string xdebug.filename_format = ...%s%n #

This setting determines the format with which Xdebug renders filenames in HTML stack traces (default: ...%s%n) and location information through the overloaded xdebug_var_dump() (default: %f).

The possible format specifiers are listed in this table. The example output is rendered according to the full path /var/www/vendor/mail/transport/mta.php.

SpecifierMeaningExample Output
%aAncester: Two directory elements and filenamemail/transport/mta.php
%fFull path/var/www/vendor/mail/transport/mta.php
%nName: Only the file namemta.php
%pParent: One directory element and the filenametransport/mta.php
%sDirectory separator/ on Linux, OSX and other Unix-like systems, \ on Windows

integer xdebug.force_display_errors = 0 #

If this setting is set to 1 then errors will always be displayed, no matter what the setting of PHP's display_errors is.


integer xdebug.force_error_reporting = 0 #

This setting is a bitmask, like error_reporting. This bitmask will be logically ORed with the bitmask represented by error_reporting to dermine which errors should be displayed. This setting can only be made in php.ini and allows you to force certain errors from being shown no matter what an application does with ini_set().


integer xdebug.halt_level = 0 #

This setting allows you to configure a mask that determines whether, and which, notices and/or warnings get converted to errors. You can configure notices and warnings that are generated by PHP, and notices and warnings that you generate yourself (by means of trigger_error()). For example, to convert the warning of strlen() (without arguments) to an error, you would do:

ini_set('xdebug.halt_level', E_WARNING);
strlen();
echo "Hi!\n";

Which will then result in the showing of the error message, and the abortion of the script. echo "Hi!\n"; will not be executed.

The setting is a bit mask, so to convert all notices and warnings into errors for all applications, you can set this in php.ini:

xdebug.halt_level=E_WARNING|E_NOTICE|E_USER_WARNING|E_USER_NOTICE

The bitmask only supports the four level that are mentioned above.


string xdebug.log = #

Configures Xdebug's log file.

Xdebug will log to this file all file creations issues, Step Debugging connection attempts, failures, and debug communication.

Enable this functionality by setting the value to a absolute path. Make sure that the system user that PHP runs at (such as www-data if you are running with Apache) can create and write to the file.

The file is opened in append-mode, and will therefore not be overwritten by default. There is no concurrency protection available.

The log file will include any attempt that Xdebug makes to connect to an IDE:

[2693358] Log opened at 2020-09-02 07:19:09.616195
[2693358] [Step Debug] INFO: Connecting to configured address/port: localhost:9003.
[2693358] [Step Debug] ERR: Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(
[2693358] [Profiler] ERR: File '/foo/cachegrind.out.2693358' could not be opened.
[2693358] [Profiler] WARN: /foo: No such file or directory
[2693358] [Tracing] ERR: File '/foo/trace.1485761369' could not be opened.
[2693358] [Tracing] WARN: /foo: No such file or directory
[2693358] Log closed at 2020-09-02 07:19:09.617510

It includes the opening time (2020-09-02 07:19:09.616195), the IP/Hostname and port Xdebug is trying to connect to (localhost:9003), and whether it succeeded (Connected to client :-)). The number in brackets ([2693358]) is the Process ID.

It includes:

[2693358]
process ID in brackets
2020-09-02 07:19:09.616195
opening time

For Step Debugging:

INFO: Connecting to configured address/port: localhost:9003.
ERR: Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(

For Profiling:

ERR: File '/foo/cachegrind.out.2693358' could not be opened.
WARN: /foo: No such file or directory

For Function Trace:

ERR: File '/foo/trace.1485761369' could not be opened.
WARN: /foo: No such file or directory

All warnings and errors are described on the Description of errors page, with detailed instructions on how to resolve the problem, if possible. All errors are always logged through PHP's internal logging mechanism (configured with error_log in php.ini). All warnings and errors also show up in the diagnostics log that you can view by calling xdebug_info().

Step Debugger Communication

The debugging log can also log the communication between Xdebug and an IDE. This communication is in XML, and starts with the <init XML element:

<init
    xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug"
    fileuri="file:///home/httpd/www.xdebug.org/html/router.php"
    language="PHP" xdebug:language_version="7.4.11-dev"
    protocol_version="1.0" appid="2693358" idekey="XDEBUG_ECLIPSE">
        <engine version="3.0.0-dev"><![CDATA[Xdebug]]></engine>
        <author><![CDATA[Derick Rethans]]></author>
        <url><![CDATA[https://xdebug.org]]></url>
        <copyright><![CDATA[Copyright (c) 2002-2020 by Derick Rethans]]></copyright>
</init>

The fileuri attribute lists the entry point of your application, which can be useful to compare to breakpoint_set commands to see if path mappings are set-up correctly.

Beyond the <init element, you will find the configuration of features:

<- feature_set -i 4 -n extended_properties -v 1
-> <response
       xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug"
       command="feature_set" transaction_id="4" feature="extended_properties" success="1">
   </response>

And continuation commands:

<- step_into -i 9
-> <response
       xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug"
       command="step_into" transaction_id="9"
       status="break" reason="ok">
           <xdebug:message filename="file:///home/httpd/www.xdebug.org/html/router.php" lineno="3">
           </xdebug:message>
   </response>

You can read about DBGP - A common debugger protocol specification at its dedicated documation page.

The xdebug.log_level setting controls how much information is logged.

Note: Many Linux distributions now use systemd, which implements private tmp directories. This means that when PHP is run through a web server or as PHP-FPM, the /tmp directory is prefixed with something akin to:

/tmp/systemd-private-ea3cfa882b4e478993e1994033fc5feb-apache.service-FfWZRg

integer xdebug.log_level = 7 #

Configures which logging messages should be emitted.

The following levels are supported:

LevelNameExample
1ErrorsConnection errors
3WarningsConnection warnings
5CommunicationProtocol messages
7InformationInformation while connecting
10DebugBreakpoint resolving information

Errors are also logged through PHP's internal logging mechanism (configured with error_log in php.ini).

Warnings and errors show up in the diagnostics log that you can view by calling xdebug_info().


integer xdebug.max_nesting_level = 256 #

Controls the protection mechanism for infinite recursion protection. The value of this setting is the maximum level of nested functions that are allowed before the script will be aborted.

When the maximum nesting level is reached, an "Error" exception is thrown.


integer xdebug.max_stack_frames = -1 #

Controls how many stack frames are shown in stack traces, both on the command line during PHP error stack traces, as well as in the browser for HTML traces.


string xdebug.mode = display #

This setting controls which Xdebug features are enabled.

The following values are accepted:

off
Nothing is enabled. Xdebug does no work besides checking whether functionality is enabled. Use this setting if you want close to 0 overhead.
develop
Enables Development Aids including the overloaded var_dump().
coverage
Enables Code Coverage Analysis to generate code coverage reports, mainly in combination with PHPUnit.
debug
Enables Step Debugging. This can be used to step through your code while it is running, and analyse values of variables.
gcstats
Enables Garbage Collection Statistics to collect statistics about PHP's Garbage Collection Mechanism.
profile
Enables Profiling, with which you can analyse performance bottlenecks with tools like KCacheGrind.
trace
Enables the Function Trace feature, which allows you record every function call, including arguments, variable assignment, and return value that is made during a request to a file.

boolean xdebug.scream = false #

If this setting is 1, then Xdebug will disable the @ (shut-up) operator so that notices, warnings and errors are no longer hidden.


integer xdebug.show_error_trace = 0 #

When this setting is set to 1, Xdebug will show a stack trace whenever an Error is raised - even if this Error is actually caught.


integer xdebug.show_exception_trace = 0 #

When this setting is set to 1, Xdebug will show a stack trace whenever an Exception or Error is raised - even if this Exception or Error is actually caught.

Error 'exceptions' were introduced in PHP 7.


integer xdebug.show_local_vars = 0 #

When this setting is set to something != 0 Xdebug's generated stack dumps in error situations will also show all variables in the top-most scope. Beware that this might generate a lot of information, and is therefore turned off by default.


string xdebug.trigger_value = "" #

This setting can be used when xdebug.start_with_request is set to trigger, which is the default for Step Debugging and Function Trace.

In trigger mode, Xdebug will only start its functionality when the XDEBUG_TRIGGER is set in the environment, or when the XDEBUG_TRIGGER GET, POST, or COOKIE variable is set.

Normally, Xdebug does not look at which value is actually used. If this setting is set to a non-empty string, then Xdebug will only trigger if the value matches the value of this setting.

With the following settings:

xdebug.mode=profile
xdebug.start_with_request=trigger
xdebug.trigger_value=StartProfileForMe

Xdebug's profiler will only start when either the environment variable XDEBUG_TRIGGER is set to StartProfileForMe, the GET or POST variable XDEBUG_TRIGGER is set to StartProfileForMe, or when the cookie XDEBUG_TRIGGER has the value StartProfileForMe.

See also:

xdebug.start_with_request#trigger
For how the triggering mechanism works, and which environment and server variables Xdebug acts on.

integer xdebug.var_display_max_children = 128 #

Controls the amount of array children and object's properties are shown when variables are displayed with either xdebug_var_dump(), xdebug.show_local_vars or when making a Function Trace.

To disable any limitation, use -1 as value.

This setting does not have any influence on the number of children that is send to the client through the Step Debugging feature.


integer xdebug.var_display_max_data = 512 #

Controls the maximum string length that is shown when variables are displayed with either xdebug_var_dump(), xdebug.show_local_vars or when making a Function Trace.

To disable any limitation, use -1 as value.

This setting does not have any influence on the number of children that is send to the client through the Step Debugging feature.


integer xdebug.var_display_max_depth = 3 #

Controls how many nested levels of array elements and object properties are when variables are displayed with either xdebug_var_dump(), xdebug.show_local_vars or when making a Function Trace.

The maximum value you can select is 1023. You can also use -1 as value to select this maximum number.

This setting does not have any influence on the number of children that is send to the client through the Step Debugging feature.

Functions


var_dump( mixed ...$var ) : void #

Displays detailed information about a variable

This function is overloaded by Xdebug, see the description for xdebug_var_dump().


xdebug_call_class( int $depth = 2 ) : mixed #

Returns the calling class

This function returns the name of the class that defined the current method, NULL if the stack frame does not exist, or FALSE if no class is associated with this call.

Example:

<?php
class Strings
{
    static function 
fix_string($a)
    {
        echo
            
xdebug_call_class().
            
"::".
            
xdebug_call_function().
            
" is called at ".
            
xdebug_call_file().
            
":".
            
xdebug_call_line();
    }
}

$ret Strings::fix_string'Derick' );
?>

Returns:

Called @ /home/httpd/html/test/xdebug_caller.php:17 from ::{main}

To retrieve information from earlier stack frames, use the optional $depth argument. A value of 1 returns the call information of the method that executed xdebug_call_class():

Example:

<?php
class Strings
{
    static function 
fix_string$a )
    {
        echo
            
xdebug_call_class).
            
"::".
            
xdebug_call_function).
            
" is called at ".
            
xdebug_call_file).
            
":".
            
xdebug_call_line);
    }
}

$ret Strings::fix_string'Derick' );
?>

Returns:

Strings::fix_string is called at /home/httpd/html/test/xdebug_caller:17

A value of 2 (the default) returns the call information of the "grand parent" of the current method:

Example:

<?php
class Strings
{
    static function 
fix_string$a )
    {
        echo
            
xdebug_call_class).
            
"::".
            
xdebug_call_function).
            
" is called at ".
            
xdebug_call_file).
            
":".
            
xdebug_call_line);
    }

    static function 
fix_strings( array $a )
    {
        foreach ( 
$a as $element )
        {
            
self::fix_string$a );
        }
    }
}

$ret Strings::fix_strings( [ 'Derick' ] );
?>

Returns:

Strings::fix_strings is called at /home/httpd/html/test/xdebug_caller:25

A value of 0 returns the call information of the call to corresponding xdebug_call_* method:

Example:

<?php
class Strings
{
    static function 
fix_string$a )
    {
        echo
            
xdebug_call_class