`
ppjava
  • 浏览: 137629 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Documentation for: Xdebug 2

    博客分类:
  • php
阅读更多

Xdebug doc 因为那个网站老是不能访问。晕倒。

 

Documentation for: Xdebug 2
? Installation

This section describes on how to install Xdebug.
? Basic Features

Xdebug's basic functions include the display of stack traces on error conditions, maximum nesting level protection and time tracking.
? Variable Display Features

Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes different colors for different types and places limits on the amount of array elements/object properties, maximum depth and string lengths. There are a few other functions dealing with variable display as well.
? Stack Traces

When Xdebug is activated it will show a stack trace whenever PHP decides to show a notice, warning, error etc. The information that stack traces display, and the way how they are presented, can be configured to suit your needs.
? Function Traces

Xdebug allows you to log all function calls, including parameters and return values to a file in different formats.
? Code Coverage Analysis

Code coverage tells you which lines of script (or set of scripts) have been executed during a request. With this information you can for example find out how good your unit tests are.
? Profiling PHP Scripts

Xdebug's built-in profiler allows you to find bottlenecks in your script and visualize those with an external tool such as KCacheGrind or WinCacheGrind.
? Remote Debugging

Xdebug provides an interface for debugger clients that interact with running PHP scripts. This section explains how to set-up PHP and Xdebug to allow this, and introduces a few clients.
? FAQ

Frequently Asked Questions

Documentation for: Xdebug 2
» Feature: Installation

This section describes on how to install Xdebug.


Precompiled Modules

There are a few precompiled modules for Windows, they are all for the non-debug version of PHP. You can get those at the download page.

Installing the precompiled modules is easy. Just place them in a directory, and add the following line to php.ini: (don't forget to change the path and filename to the correct one — but make sure you use the full path ). Also, sometimes the quotes (") should be removed.

zend_extension_ts="c:/php/modules/php_xdebug-4.4.1-2.0.5.dll"
From PHP 5.3 onwards, you always need to use zend_extension and not zend_extension_ts.

PECL Installation

As of Xdebug 0.9.0 you can install Xdebug through PEAR/PECL. This only works with with PEAR version 0.9.1-dev or higher and some UNIX.

Installing with PEAR/PECL is as easy as:

# pecl install xdebug

but you still need to add the correct line to your php.ini: (don't forget to change the path and filename to the correct one — but make sure you use the full path )

zend_extension="/usr/local/php/modules/xdebug.so"

Note: You should ignore any prompts to add "extension=xdebug.so" to php.ini — this will cause problems.

Installation From Source

You can download the source of the latest stable release 2.0.5. Alternatively you can obtain Xdebug from SVN:

svn co svn://svn.xdebug.org/svn/xdebug/xdebug/trunk xdebug

This will checkout the latest development version which is currently 2.1.0beta1. You can also browse the source at http://svn.xdebug.org/cgi-bin/viewvc.cgi/xdebug/?root=xdebug .

Compiling

You compile Xdebug separately from the rest of PHP. Note, however, that you need access to the scripts 'phpize' and 'php-config'. If your system does not have 'phpize' and 'php-config', you will need to compile and install PHP from a source tarball first, as these script are by-products of the PHP compilation and installation processes. (Debian users can install the required tools with apt-get install php4-dev , or apt-get install php5-dev ). It is important that the source version matches the installed version as there are slight, but important, differences between PHP versions. Once you have access to 'phpize' and 'php-config', do the following:

 

  1. Unpack the tarball: tar -xzf xdebug-2.0.5.tgz. Note that you do not need to unpack the tarball inside the PHP source code tree. Xdebug is compiled separately, all by itself, as stated above.
  2. cd xdebug-2.0.5
  3. Run phpize: phpize (or /path/to/phpize if phpize is not in your path). See in the table below which version numbers it should show for different PHP versions. Make sure you use the phpize that belongs to the PHP version that you want to use Xdebug with.
  4. ./configure --enable-xdebug (or: ./configure --enable-xdebug --with-php-config=/path/to/php-config if php-config is not in your path).

    If this fails with something like:
    ../configure: line 1960: syntax error near unexpected token
    `PHP_NEW_EXTENSION(xdebug,'
    ../configure: line 1960: `  PHP_NEW_EXTENSION(xdebug, xdebug.c
    xdebug_code_coverage.c xdebug_com.c xdebug_handler_gdb.c
    xdebug_handler_php3.c xdebug_handlers.c xdebug_llist.c xdebug_hash.c
    xdebug_profiler.c xdebug_superglobals.c xdebug_var.c usefulstuff.c,
    $ext_shared)'
    then it means that you do not meet the PHP 4.3.x version requirement for Xdebug.

    Another problem that might occur is:
    configure: line 1145: PHP_INIT_BUILD_SYSTEM: command not found
    configure: line 1151: syntax error near unexpected token `config.nice'
    configure: line 1151: `PHP_CONFIG_NICE(config.nice)'
    You will need to upgrade your autotools (autoconf, automake and libtool) or install the known working versions: autoconf-2.13, automake-1.5 and libtool-1.4.3.
  5. make
  6. cp modules/xdebug.so /to/wherever/you/want/it

Configure PHP to Use Xdebug

  1. add the following line to php.ini: zend_extension="/wherever/you/put/it/xdebug.so" (for non-threaded use of PHP, for example the CLI, CGI or Apache 1.3 module) or: zend_extension_ts="/wherever/you/put/it/xdebug.so" (for threaded usage of PHP, for example the Apache 2 work MPM or the the ISAPI module). Note: In case you compiled PHP yourself and used --enable-debug you would have to use zend_extension_debug=. From PHP 5.3 onwards, you always need to use zend_extension and not zend_extension_ts.
  2. Restart your webserver.
  3. Write a PHP page that calls 'phpinfo() ' Load it in a browser and look for the info on the Xdebug module. If you see it next to the Zend logo, you have been successful! You can also use 'php -m' if you have a command line version of PHP, it lists all loaded modules. Xdebug should appear twice there (once under 'PHP Modules' and once under 'Zend Modules').

Compatibility

Xdebug does not work together with the Zend Optimizer or any other extension that deals with PHP's internals (DBG, APD, ioncube etc). This is due to compatibility problems with those modules.

Debugclient Installation

Unpack the Xdebug source tarball and issue the following commands:

$ cd debugclient
$ ./configure --with-libedit
$ make
# make install

This will install the debugclient binary in /usr/local/bin unless you don't have libedit installed on your system. You can either install it, or leave out the '--with-libedit' option to configure. Debian 'unstable' users can install the library with apt-get install libedit-dev libedit2 .

If the configure script can not find libedit and you are sure you have (and it's headers) installed correctly and you get link errors like the following in your configure.log:

/usr/lib64/libedit.so: undefined reference to `tgetnum'
/usr/lib64/libedit.so: undefined reference to `tgoto'
/usr/lib64/libedit.so: undefined reference to `tgetflag'
/usr/lib64/libedit.so: undefined reference to `tputs'
/usr/lib64/libedit.so: undefined reference to `tgetent'
/usr/lib64/libedit.so: undefined reference to `tgetstr'
collect2: ld returned 1 exit status

you need to change your configure command to:

$ LDFLAGS=-lncurses ./configure --with-libedit

<!-- MAIN FEATURE END -->
 

 

Documentation for: Xdebug 2
» Feature: Basic Features

Xdebug's basic functions include the display of stack traces on error conditions, maximum nesting level protection and time tracking.



Related Settings


xdebug.default_enable
Type: boolean , Default value: 1
If this setting is 1, then stacktraces will be shown by default on an error event. You can disable showing stacktraces from your code with xdebug_disable() . As this is one of the basic functions of Xdebug, it is advisable to leave this setting set to 1.

xdebug.max_nesting_level
Type: integer , Default value: 100
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.

xdebug.scream
Type: boolean , Default value: 0 , Introduced in Xdebug 2.1
If this setting is 1, then Xdebug will disable the @ (shut-up) operator so that notices, warnings and errors are no longer hidden.

Related Functions


string xdebug_call_class( )
Returns the calling class

This function returns the name of the class from which the current function/method was called from.

Example:

<?php
    
function  fix_string ( $a )
    {
        echo 
"Called @ " .
            
xdebug_call_file ().
            
":" .
            
xdebug_call_line ().
            
" from " .
            
xdebug_call_function ();
    }

    
$ret  fix_string (array( 'Derick' ));
?>

Returns:

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

string xdebug_call_file( )
Returns the calling file

This function returns the filename that contains the function/method that called the current function/method.

For an example see xdebug_call_class() .


string xdebug_call_function( )
Returns the calling function/method

This function returns the name of the function/method from which the current function/method was called from.

For an example see xdebug_call_class() .


int xdebug_call_line( )
Returns the calling line

This function returns the line number that contains the function/method that called the current function/method.

For an example see xdebug_call_class() .


void xdebug_disable( )
Disables stack traces

Disable showing stack traces on error conditions.


void xdebug_enable( )
Enables stack traces

Enable showing stack traces on error conditions.


array xdebug_get_headers( )
Returns all the headers as set by calls to PHP's header() function

Returns all the headers that are set with PHP's header() function, or any other header set internally within PHP (such as through setcookie()), as an array.

Example:

<?php
header
"X-Test" "Testing"  );
setcookie "TestCookie" "test-value"  );
var_dump xdebug_get_headers () );
?>

Returns:

array(2) {
  [0]=>
  string(6) "X-Test"
  [1]=>
  string(33) "Set-Cookie: TestCookie=test-value"
}

This function is introduced with Xdebug 2.1.


bool xdebug_is_enabled( )
Returns whether stack traces are enabled

Return whether stack traces would be shown in case of an error or not.


int xdebug_memory_usage( )
Returns the current memory usage

Returns the current amount of memory the script uses. Before PHP 5.2.1, this only works if PHP is compiled with --enable-memory-limit. From PHP 5.2.1 and later this function is always available.


int xdebug_peak_memory_usage( )
Returns the peak memory usage

Returns the maximum amount of memory the script used until now. Before PHP 5.2.1, this only works if PHP is compiled with --enable-memory-limit. From PHP 5.2.1 and later this function is always available.


float xdebug_time_index( )
Returns the current time index

Returns the current time index since the starting of the script in seconds.

Example:

<?php
echo  xdebug_time_index (),  "\n" ;
for (
$i  0 $i  250000 $i ++)
{
    
// do nothing
}
echo 
xdebug_time_index (),  "\n" ;
?>

Returns:

0.00038003921508789
0.76580691337585

<!-- MAIN FEATURE END -->
 

 

Documentation for: Xdebug 2
» Feature: Variable Display Features

Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes different colors for different types and places limits on the amount of array elements/object properties, maximum depth and string lengths. There are a few other functions dealing with variable display as well.


Effect of settings on var_dump()

There is a number of settings that control the output of Xdebug's modified var_dump() function: xdebug.var_display_max_children , xdebug.var_display_max_data and xdebug.var_display_max_depth . The effect of these three settings is best shown with an example. The script below is run four time, each time with different settings. You can use the tabs to see the difference.

The script

<?php
class  test  {
    public 
$pub  false ;
    private 
$priv  true ;
    protected 
$prot  42 ;
}
$t  = new  test ;
$t -> pub  $t ;
$data  = array(
    
'one'  =>  'a somewhat long string!' ,
    
'two'  => array(
        
'two.one'  => array(
            
'two.one.zero'  =>  210 ,
            
'two.one.one'  => array(
                
'two.one.one.zero'  =>  3.141592564 ,
                
'two.one.one.one'   =>  2.7 ,
            ),
        ),
    ),
    
'three'  =>  $t ,
    
'four'  =>  range ( 0 5 ),
);
var_dump $data  );
?>

The results

array

  'one' =>
 string
 'a somewhat long string!'
 (length=23)

  'two' =>
 
    array

      'two.one' =>
 
        array

          'two.one.zero' =>
 int
 210

          'two.one.one' =>
 
            array

              ...
  'three' =>
 
    object
(test
)[1
]
      public
 'pub' =>
 
        &
object
(test
)[1
]
      private
 'priv' =>
 boolean
 true

      protected
 'prot' =>
 int
 42

  'four' =>
 
    array

      0 =>
 int
 0

      1 =>
 int
 1

      2 =>
 int
 2

      3 =>
 int
 3

      4 =>
 int
 4

      5 =>
 int
 5

array

  'one' =>
 string
 'a somewhat long string!'
 (length=23)

  'two' =>
 
    array

      'two.one' =>
 
        array

          'two.one.zero' =>
 int
 210

          'two.one.one' =>
 
            array

              ...
  more elements...

array

  'one' =>
 string
 'a somewhat long '...
 (length=23)

  'two' =>
 
    array

      'two.one' =>
 
        array

          'two.one.zero' =>
 int
 210

          'two.one.one' =>
 
            array

              ...
  'three' =>
 
    object
(test
)[1
]
      public
 'pub' =>
 
        &
object
(test
)[1
]
      private
 'priv' =>
 boolean
 true

      protected
 'prot' =>
 int
 42

  'four' =>
 
    array

      0 =>
 int
 0

      1 =>
 int
 1

      2 =>
 int
 2

      3 =>
 int
 3

      4 =>
 int
 4

      5 =>
 int
 5

array

  'one' =>
 string
 'a somewhat long string!'
 (length=23)

  'two' =>
 
    array

      'two.one' =>
 
        array

          ...
  'three' =>
 
    object
(test
)[1
]
      public
 'pub' =>
 
        &
object
(test
)[1
]
      private
 'priv' =>
 boolean
 true

      protected
 'prot' =>
 int
 42

  'four' =>
 
    array

      0 =>
 int
 0

      1 =>
 int
 1

      2 =>
 int
 2

      3 =>
 int
 3

      4 =>
 int
 4

      5 =>
 int
 5

array

  'one' =>
 string
 'a somewh'...
 (length=23)

  'two' =>
 
    array

      ...
  'three' =>
 
    object
(test
)[1
]
      ...
  more elements...


Related Settings


xdebug.overload_var_dump
Type: boolean , Default value: 1 , Introduced in Xdebug 2.1
By default Xdebug overloads var_dump() with its own improved version for displaying variables when the html_errors php.ini setting is set to 1. In case you do not want that, you can set this setting to 0, but check first if it's not smarter to turn off html_errors.

xdebug.var_display_max_children
Type: integer , Default value: 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 through Function Traces . This setting does not have any influence on the number of children that is send to the client through the Remote Debugging feature.

xdebug.var_display_max_data
Type: integer , Default value: 512
Controls the maximum string length that is shown when variables are displayed with either xdebug_var_dump() , xdebug.show_local_vars or through Function Traces . This setting does not have any influence on the amount of data that is send to the client through the Remote Debugging feature.

xdebug.var_display_max_depth
Type: integer , Default value: 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 through Function Traces . This setting does not have any influence on the depth of children that is send to the client through the Remote Debugging feature.

Related Functions


void var_dump( [mixed var [, ...]] )
Displays detailed information about a variable

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


void xdebug_debug_zval( [string varname [, ...]] )
Displays information about a variable

This function displays structured information about one or more variables that includes its type, value and refcount information. Arrays are explored recursively with values. This function is implemented differently from PHP's debug_zval_dump() function in order to work around the problems that that function has because the variable itself is actually passed to the function. Xdebug's version is better as it uses the variable name to lookup the variable in the internal symbol table and accesses all the properties directly without having to deal with actually passing a variable to a function. The result is that the information that this function returns is much more accurate than PHP's own function for showing zval information.

Example:

<?php
    $a 
= array( 1 2 3 );
    
$b  =&  $a ;
    
$c  =&  $a [ 2 ];

    
xdebug_debug_zval ( 'a' );
?>

Returns:

a: (refcount=2, is_ref=1)=array (
	0 => (refcount=1, is_ref=0)=1, 
	1 => (refcount=1, is_ref=0)=2, 
	2 => (refcount=2, is_ref=1)=3)


void xdebug_debug_zval_stdout( [string varname [, ...]] )
Returns information about variables to stdout.

This function displays structured information about one or more variables that includes its type, value and refcount information. Arrays are explored recursively with values. The difference with xdebug_debug_zval() is that the information is not displayed through a web server API layer, but directly shown on stdout (so that when you run it with apache in single process mode it ends up on the console).

Example:

<?php
    $a 
= array( 1 2 3 );
    
$b  =&  $a ;
    
$c  =&  $a [ 2 ];

    
xdebug_debug_zval_stdout ( 'a' );

Returns:

a: (refcount=2, is_ref=1)=array (
	0 => (refcount=1, is_ref=0)=1, 
	1 => (refcount=1, is_ref=0)=2, 
	2 => (refcount=2, is_ref=1)=3)

void xdebug_dump_superglobals( )
Displays information about super globals

This function dumps the values of the elements of the super globals as specified with the xdebug.dump.* php.ini settings. For the example below the settings in php.ini are:

Example:

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

Query string:
?var=fourty%20two&array[a]=a&array[9]=b

Returns:



Dump $_SERVER
Dump $_GET
$_SERVER['REMOTE_ADDR'] =
string
 '127.0.0.1'
 (length=9)

$_GET['var'] =
string
 'fourty two'
 (length=10)

$_GET['array'] =
array

  'a' =>
 string
 'a'
 (length=1)

  9 =>
 string
 'b'
 (length=1)



void xdebug_var_dump( [mixed var [, ...]] )
Displays detailed information about a variable

This function displays structured information about one or more expressions that includes its type and value. Arrays are explored recursively with values. See the introduction of Variable Display Features on which php.ini settings affect this function.

Example:

<?php
ini_set
( 'xdebug.var_display_max_children' );
$c  = new  stdClass ;
$c -> foo  'bar' ;
$c -> file  fopen '/etc/passwd' 'r'  );
var_dump (
    array(
        array(
TRUE 2 3.14 'foo' ),
        
'object'  =>  $c
    
)
);
?>   

Returns:

array

  0 =>
 
    array

      0 =>
 boolean
 true

      1 =>
 int
 2

      2 =>
 float
 3.14

      more elements...

  'object' =>
 
    object
(stdClass
)[1
]
      public
 'foo' =>
 string
 'bar'
 (length=3)

      public
 'file' =>
 resource
(3
,
 stream
)




hen Xdebug is activated it will show a stack trace whenever PHP decides to show a notice, warning, error etc. The information that stack traces display, and the way how they are presented, can be configured to suit your needs.


The stack traces that Xdebug shows on error situations (if display_errors is set to On in php.ini) are quite conservative in the amount of information that they show. This is because large amounts of information can slow down both the execution of the scripts and the rendering of the stack traces themselves in the browser. However, it is possible to make the stack traces show more detailed information with different settings.

Variables in Stack Traces

By default Xdebug will now show variable information in the stack traces that it produces. Variable information can take quite a bit of resources, both while collecting or displaying. However, in many cases it is useful that variable information is displayed, and that is why Xdebug has the setting xdebug.collect_params . The script below, in combination with what the output will look like with different values of this setting is shown in the example below.

The script

<?php
function  foo $a  ) {
    for (
$i  1 $i  $a [ 'foo' ];  $i ++) {
        if (
$i  ==  500000 xdebug_break ();
    }
}

set_time_limit ( 1 );
$c  = new  stdClass ;
$c -> bar  100 ;
$a  = array(
    
42  =>  false 'foo'  =>  912124 ,
    
$c , new  stdClass fopen '/etc/passwd' 'r'  )
);
foo $a  );
?>

The results

Different values for the xdebug.collect_params setting give different output, which you can see below:


( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 34 Call Stack # Time Memory Function Location
1 0.0001 58564 {main}( ) ../stack.php: 0
2 0.0004 62764 foo( ) ../stack.php: 47
ini_set('xdebug.collect_params', '1');

( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 31 Call Stack # Time Memory Function Location
1 0.0001 58132 {main}( ) ../stack.php: 0
2 0.0004 62380 foo( array(5) ) ../stack.php: 47
ini_set('xdebug.collect_params', '2');

( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 31 Call Stack # Time Memory Function Location
1 0.0001 58564 {main}( ) ../stack.php: 0
2 0.0004 62812 foo( array(5) ) ../stack.php: 47
ini_set('xdebug.collect_params', '3');

( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 31 Call Stack # Time Memory Function Location
1 0.0001 58564 {main}( ) ../stack.php: 0
2 0.0004 62812 foo( array (42 => FALSE, 'foo' => 912124, 43 => class stdClass { public $bar = 100 }, 44 => class stdClass { }, 45 => resource(2) of type (stream)) ) ../stack.php: 47
ini_set('xdebug.collect_params', '4');

( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 31 Call Stack # Time Memory Function Location
1 0.0001 58132 {main}( ) ../stack.php: 0
2 0.0004 62380 foo( $a = array (42 => FALSE, 'foo' => 912124, 43 => class stdClass { public $bar = 100 }, 44 => class stdClass { }, 45 => resource(2) of type (stream)) ) ../stack.php: 47

Additional Information

On top of showing the values of variables that were passed to each function Xdebug can also optionally show information about selected superglobals by using the xdebug.dump_globals and xdebug.dump.* settings. The settings xdebug.dump_once and xdebug.dump_undefined slightly modify when and which information is shown from the available superglobals. With the xdebug.show_local_vars setting you can instruct Xdebug to show all variables available in the top-most stack level for a user defined function as well. The examples below show this (the script is used from the example above).


( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 34 Call Stack # Time Memory Function Location
1 0.0001 58564 {main}( ) ../stack.php: 0
2 0.0004 62764 foo( ) ../stack.php: 47
ini_set('xdebug.collect_vars', 'on');
ini_set('xdebug.collect_params', '4');
ini_set('xdebug.dump_globals', 'on');
ini_set('xdebug.dump.SERVER', 'REQUEST_URI');

( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 33 Call Stack # Time Memory Function Location Dump $_SERVER
1 0.0001 58132 {main}( ) ../stack.php: 0
2 0.0004 62436 foo( ) ../stack.php: 47
$_SERVER['REQUEST_URI'] =
string
 '/test/xdebug/docs/stack.php?level=5'
 (length=35)

ini_set('xdebug.collect_vars', 'on');
ini_set('xdebug.collect_params', '4');
ini_set('xdebug.dump_globals', 'on');
ini_set('xdebug.dump.SERVER', 'REQUEST_URI');
ini_set('xdebug.show_local_vars', 'on');

( ! ) Fatal error: Maximum execution time of 1 second exceeded in /home/httpd/html/test/xdebug/docs/stack.php on line 31 Call Stack # Time Memory Function Location Dump $_SERVER Variables in local scope (#2)
1 0.0001 58132 {main}( ) ../stack.php: 0
2 0.0005 62588 foo( ) ../stack.php: 47
$_SERVER['REQUEST_URI'] =
string
 '/test/xdebug/docs/stack.php?level=6'
 (length=35)


$a =
array

  42 =>
 boolean
 false

  'foo' =>
 int
 912124

  43 =>
 
    object
(stdClass
)[1
]
      public
 'bar' =>
 int
 100

  44 =>
 
    object
(stdClass
)[2
]
  45 =>
 resource
(2
,
 stream
)
$i =
int
 275447


Related Settings

相关推荐

Global site tag (gtag.js) - Google Analytics