cd /usr/src/php-src/sapi
git clone github.com/krakjoe/phpdbg
cd ../
./buildconf --force
./config.nice
make -j8
make install-phpdbg
PHP56的话直接启用phpdbg就可以了
注意: php 配置中可以启用 --with-readline for phpdbg to support history, autocompletion, tab-listing etc
phpdbginit
Setting up your debugging session automatically
phpdbg -imy.phpdbginit
In addition, .phpdbginit can contain embedded code, allowing, for example
the setup of auto completion
the registration of functions
the acquisition and pre-compilation of code
bootstrapping a web application
The default .phpdbginit
##########################################################
# .phpdbginit
#
# Lines starting with # are ignored
# Code must start and end with <: and :> respectively
##########################################################
# Place initialization commands one per line
##########################################################
# exec sapi/phpdbg/test.php
##########################################################
# Embedding code in .phpdbginit
##########################################################
<:
/*
If readline is loaded, you might want to setup completion:
*/
if (function_exists('readline_completion_function')) {
readline_completion_function(function(){
return array_merge(
get_defined_functions()['user'],
array_keys(get_defined_constants())
);
});
}
:>
##########################################################
# Now carry on initializing phpdbg ...
##########################################################
# R my_debugging_function
##########################################################
# PHP has many functions that might be useful
# ... you choose ...
##########################################################
# R touch
# R unlink
# R scandir
# R glob
##########################################################
# Remember: *you have access to the shell*
##########################################################
# The output of registered function calls is not,
# by default, very pretty (unless you implement
# and register a new implementation for phpdbg)
# The output of shell commands will usually be more
# readable on the console
##########################################################
# TLDR; if you have a good shell, use it ...
##########################################################
查看版本号
[root@localhost ~]# php -v
PHP 5.6.16 (cli) (built: Dec 8 2015 09:10:23) (DEBUG)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
[root@localhost ~]# phpdbg -V
phpdbg 0.4.0 (built: Dec 8 2015 09:10:43)
PHP 5.6.16, Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
查看help
[root@localhost ~]# phpdbg
[Welcome to phpdbg, the interactive PHP debugger, v0.4.0]
To get help using phpdbg type "help" and press enter
[Please report bugs to <github.com/krakjoe/phpdbg/issues>]
phpdbg> help
phpdbg is a lightweight, powerful and easy to use debugging platform for PHP5.4+
It supports the following commands:
Information
list list PHP source
info displays information on the debug session
print show opcodes
frame select a stack frame and print a stack frame summary
back shows the current backtrace
help provide help on a topic
Starting and Stopping Execution
exec set execution context
run attempt execution
step continue execution until other line is reached
continue continue execution
until continue execution up to the given location
finish continue up to end of the current execution frame
leave continue up to end of the current execution frame and halt after the calling instruction
break set a breakpoint at the specified target
watch set a watchpoint on $variable
clear clear one or all breakpoints
clean clean the execution environment
Miscellaneous
set set the phpdbg configuration
source execute a phpdbginit script
register register a phpdbginit function as a command alias
sh shell a command
ev evaluate some code
quit exit phpdbg
Type help <command> or (help alias) to get detailed help on any of the above commands, for example help list or h l. Note that help will also match partial commands if unique (and list
out options if not unique), so help clea will give help on the clean command, but help cl will list the summary for clean and clear.
Type help aliases to show a full alias list, including any registered phpdginit functions
Type help syntax for a general introduction to the command syntax.
Type help options for a list of phpdbg command line options.
Type help phpdbginit to show how to customise the debugger environment.
phpdbg> help list
Command: list Alias: l lists some code
The list command displays source code for the given argument. The target type is specficied by a second subcommand keyword:
Type Alias Purpose
lines l List N lines from the current execution point
func f List the complete source for a specified function
method m List the complete source for a specified class::method
class c List the complete source for a specified class
Note that the context of lines, func and method can be determined by parsing the argument, so these subcommands are optional. However, you must specify the class keyword to list off a
class.
Examples
phpdbg> list 2
phpdbg> l l 2
List the next 2 lines from the current file
phpdbg> list my_function
phpdbg> l f my_function
List the source of the function my_function
phpdbg> list func .mine
phpdbg> l f .mine
List the source of the method mine from the active class in scope
phpdbg> list m my::method
phpdbg> l my::method
List the source of my::method
phpdbg> list c myClass
phpdbg> l c myClass
List the source of myClass
Note that functions and classes can only be listed if the corresponding classes and functions table in the Zend executor has a corresponding entry. You can use the compile command to
populate these tables for a given execution context.
phpdbg> help break
Command: break Alias: b set breakpoint
Breakpoints can be set at a range of targets within the execution environment. Execution will be paused if the program flow hits a breakpoint. The break target can be one of the
following types:
Target Alias Purpose
at A specify breakpoint by location and condition
del d delete breakpoint by breakpoint identifier number
Break at takes two arguments. The first is any valid target. The second is a valid PHP expression which will trigger the break in execution, if evaluated as true in a boolean context at
the specified target.
Note that breakpoints can also be disabled and re-enabled by the set break command.
Examples
phpdbg> break test.php:100
phpdbg> b test.php:100
Break execution at line 100 of test.php
phpdbg> break 200
phpdbg> b 200
Break execution at line 200 of the currently PHP script file
phpdbg> break \mynamespace\my_function
phpdbg> b \mynamespace\my_function
Break execution on entry to \mynamespace\my_function
phpdbg> break classX::method
phpdbg> b classX::method
Break execution on entry to classX::method
phpdbg> break 0x7ff68f570e08
phpdbg> b 0x7ff68f570e08
Break at the opline at the address 0x7ff68f570e08
phpdbg> break my_function#14
phpdbg> b my_function#14
Break at the opline #14 of the function my_function
phpdbg> break \my\class::method#2
phpdbg> b \my\class::method#2
Break at the opline #2 of the method \my\class::method
phpdbg> break test.php:#3
phpdbg> b test.php:#3
Break at opline #3 in test.php
phpdbg> break if $cnt > 10
phpdbg> b if $cnt > 10
Break when the condition ($cnt > 10) evaluates to true
phpdbg> break at phpdbg::isGreat if $opt == 'S'
phpdbg> break @ phpdbg::isGreat if $opt == 'S'
Break at any opcode in phpdbg::isGreat when the condition ($opt == 'S') is true
phpdbg> break at test.php:20 if !isset($x)
Break at every opcode on line 20 of test.php when the condition evaluates to true
phpdbg> break ZEND_ADD
phpdbg> b ZEND_ADD
Break on any occurence of the opcode ZEND_ADD
phpdbg> break del 2
phpdbg> b ~ 2
Remove breakpoint 2
Note: Conditional breaks are costly in terms of runtime overhead. Use them only when required as they significantly slow execution.
Note: An address is only valid for the current compilation.
phpdbg> help watch
Command: watch Alias: w set watchpoint
Sets watchpoints on variables as long as they are defined
Passing no parameter to watch, lists all actually active watchpoints
Format for $variable
$var Variable $var
$var[] All array elements of $var
$var-> All properties of $var
$var->a Property $var->a
$var[b] Array element with key b in array $var
Subcommands of watch:
Type Alias Purpose
array a Sets watchpoint on array/object to observe if an entry is added or removed
recursive r Watches variable recursively and automatically adds watchpoints if some entry is added to an array/object
delete d Removes watchpoint
Note when recursive watchpoints are removed, watchpoints on all the children are removed too
Examples
phpdbg> watch
List currently active watchpoints
phpdbg> watch $array
phpdbg> w $array
Set watchpoint on $array
phpdbg> watch recursive $obj->
phpdbg> w r $obj->
Set recursive watchpoint on $obj->
phpdbg> watch delete $obj->a
phpdbg> w d $obj->a
Remove watchpoint $obj->a
Technical note: If using this feature with a debugger, you will get many segmentation faults, each time when a memory page containing a watched address is hit.
You then you can continue, phpdbg will remove the write protection, so that the program can continue.
If phpdbg could not handle that segfault, the same segfault is triggered again and this time phpdbg will abort.
[root@localhost ~]# phpdbg -e kk.php
[Welcome to phpdbg, the interactive PHP debugger, v0.4.0]
To get help using phpdbg type "help" and press enter
[Please report bugs to <github.com/krakjoe/phpdbg/issues>]
[Attempting compilation of /root/kk.php]
[Success]
phpdbg> b test::testa
[Breakpoint #0 added at test::testa]
phpdbg> b test::testb
[Breakpoint #1 added at test::testb]
phpdbg> b 0xb77b6614
[Breakpoint #2 added at 0xb77b6614]
phpdbg> b 0xb77b6630
[Breakpoint #3 added at 0xb77b6630]
cd /usr/src/php-src/sapi
git clone github.com/krakjoe/phpdbg
cd ../
./buildconf --force
./config.nice
make -j8
make install-phpdbg
PHP56的话直接启用phpdbg就可以了
注意: php 配置中可以启用 --with-readline for phpdbg to support history, autocompletion, tab-listing etc
phpdbginit
Setting up your debugging session automatically
phpdbg -imy.phpdbginit
In addition, .phpdbginit can contain embedded code, allowing, for example
the setup of auto completion
the registration of functions
the acquisition and pre-compilation of code
bootstrapping a web application
The default .phpdbginit
##########################################################
# .phpdbginit
#
# Lines starting with # are ignored
# Code must start and end with <: and :> respectively
##########################################################
# Place initialization commands one per line
##########################################################
# exec sapi/phpdbg/test.php
##########################################################
# Embedding code in .phpdbginit
##########################################################
<:
/*
If readline is loaded, you might want to setup completion:
*/
if (function_exists('readline_completion_function')) {
readline_completion_function(function(){
return array_merge(
get_defined_functions()['user'],
array_keys(get_defined_constants())
);
});
}
:>
##########################################################
# Now carry on initializing phpdbg ...
##########################################################
# R my_debugging_function
##########################################################
# PHP has many functions that might be useful
# ... you choose ...
##########################################################
# R touch
# R unlink
# R scandir
# R glob
##########################################################
# Remember: *you have access to the shell*
##########################################################
# The output of registered function calls is not,
# by default, very pretty (unless you implement
# and register a new implementation for phpdbg)
# The output of shell commands will usually be more
# readable on the console
##########################################################
# TLDR; if you have a good shell, use it ...
##########################################################
查看版本号
[root@localhost ~]# php -v
PHP 5.6.16 (cli) (built: Dec 8 2015 09:10:23) (DEBUG)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
[root@localhost ~]# phpdbg -V
phpdbg 0.4.0 (built: Dec 8 2015 09:10:43)
PHP 5.6.16, Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
查看help
[root@localhost ~]# phpdbg
[Welcome to phpdbg, the interactive PHP debugger, v0.4.0]
To get help using phpdbg type "help" and press enter
[Please report bugs to <github.com/krakjoe/phpdbg/issues>]
phpdbg> help
phpdbg is a lightweight, powerful and easy to use debugging platform for PHP5.4+
It supports the following commands:
Information
list list PHP source
info displays information on the debug session
print show opcodes
frame select a stack frame and print a stack frame summary
back shows the current backtrace
help provide help on a topic
Starting and Stopping Execution
exec set execution context
run attempt execution
step continue execution until other line is reached
continue continue execution
until continue execution up to the given location
finish continue up to end of the current execution frame
leave continue up to end of the current execution frame and halt after the calling instruction
break set a breakpoint at the specified target
watch set a watchpoint on $variable
clear clear one or all breakpoints
clean clean the execution environment
Miscellaneous
set set the phpdbg configuration
source execute a phpdbginit script
register register a phpdbginit function as a command alias
sh shell a command
ev evaluate some code
quit exit phpdbg
Type help <command> or (help alias) to get detailed help on any of the above commands, for example help list or h l. Note that help will also match partial commands if unique (and list
out options if not unique), so help clea will give help on the clean command, but help cl will list the summary for clean and clear.
Type help aliases to show a full alias list, including any registered phpdginit functions
Type help syntax for a general introduction to the command syntax.
Type help options for a list of phpdbg command line options.
Type help phpdbginit to show how to customise the debugger environment.
phpdbg> help list
Command: list Alias: l lists some code
The list command displays source code for the given argument. The target type is specficied by a second subcommand keyword:
Type Alias Purpose
lines l List N lines from the current execution point
func f List the complete source for a specified function
method m List the complete source for a specified class::method
class c List the complete source for a specified class
Note that the context of lines, func and method can be determined by parsing the argument, so these subcommands are optional. However, you must specify the class keyword to list off a
class.
Examples
phpdbg> list 2
phpdbg> l l 2
List the next 2 lines from the current file
phpdbg> list my_function
phpdbg> l f my_function
List the source of the function my_function
phpdbg> list func .mine
phpdbg> l f .mine
List the source of the method mine from the active class in scope
phpdbg> list m my::method
phpdbg> l my::method
List the source of my::method
phpdbg> list c myClass
phpdbg> l c myClass
List the source of myClass
Note that functions and classes can only be listed if the corresponding classes and functions table in the Zend executor has a corresponding entry. You can use the compile command to
populate these tables for a given execution context.
phpdbg> help break
Command: break Alias: b set breakpoint
Breakpoints can be set at a range of targets within the execution environment. Execution will be paused if the program flow hits a breakpoint. The break target can be one of the
following types:
Target Alias Purpose
at A specify breakpoint by location and condition
del d delete breakpoint by breakpoint identifier number
Break at takes two arguments. The first is any valid target. The second is a valid PHP expression which will trigger the break in execution, if evaluated as true in a boolean context at
the specified target.
Note that breakpoints can also be disabled and re-enabled by the set break command.
Examples
phpdbg> break test.php:100
phpdbg> b test.php:100
Break execution at line 100 of test.php
phpdbg> break 200
phpdbg> b 200
Break execution at line 200 of the currently PHP script file
phpdbg> break \mynamespace\my_function
phpdbg> b \mynamespace\my_function
Break execution on entry to \mynamespace\my_function
phpdbg> break classX::method
phpdbg> b classX::method
Break execution on entry to classX::method
phpdbg> break 0x7ff68f570e08
phpdbg> b 0x7ff68f570e08
Break at the opline at the address 0x7ff68f570e08
phpdbg> break my_function#14
phpdbg> b my_function#14
Break at the opline #14 of the function my_function
phpdbg> break \my\class::method#2
phpdbg> b \my\class::method#2
Break at the opline #2 of the method \my\class::method
phpdbg> break test.php:#3
phpdbg> b test.php:#3
Break at opline #3 in test.php
phpdbg> break if $cnt > 10
phpdbg> b if $cnt > 10
Break when the condition ($cnt > 10) evaluates to true
phpdbg> break at phpdbg::isGreat if $opt == 'S'
phpdbg> break @ phpdbg::isGreat if $opt == 'S'
Break at any opcode in phpdbg::isGreat when the condition ($opt == 'S') is true
phpdbg> break at test.php:20 if !isset($x)
Break at every opcode on line 20 of test.php when the condition evaluates to true
phpdbg> break ZEND_ADD
phpdbg> b ZEND_ADD
Break on any occurence of the opcode ZEND_ADD
phpdbg> break del 2
phpdbg> b ~ 2
Remove breakpoint 2
Note: Conditional breaks are costly in terms of runtime overhead. Use them only when required as they significantly slow execution.
Note: An address is only valid for the current compilation.
phpdbg> help watch
Command: watch Alias: w set watchpoint
Sets watchpoints on variables as long as they are defined
Passing no parameter to watch, lists all actually active watchpoints
Format for $variable
$var Variable $var
$var[] All array elements of $var
$var-> All properties of $var
$var->a Property $var->a
$var[b] Array element with key b in array $var
Subcommands of watch:
Type Alias Purpose
array a Sets watchpoint on array/object to observe if an entry is added or removed
recursive r Watches variable recursively and automatically adds watchpoints if some entry is added to an array/object
delete d Removes watchpoint
Note when recursive watchpoints are removed, watchpoints on all the children are removed too
Examples
phpdbg> watch
List currently active watchpoints
phpdbg> watch $array
phpdbg> w $array
Set watchpoint on $array
phpdbg> watch recursive $obj->
phpdbg> w r $obj->
Set recursive watchpoint on $obj->
phpdbg> watch delete $obj->a
phpdbg> w d $obj->a
Remove watchpoint $obj->a
Technical note: If using this feature with a debugger, you will get many segmentation faults, each time when a memory page containing a watched address is hit.
You then you can continue, phpdbg will remove the write protection, so that the program can continue.
If phpdbg could not handle that segfault, the same segfault is triggered again and this time phpdbg will abort.
[root@localhost ~]# phpdbg -e kk.php
[Welcome to phpdbg, the interactive PHP debugger, v0.4.0]
To get help using phpdbg type "help" and press enter
[Please report bugs to <github.com/krakjoe/phpdbg/issues>]
[Attempting compilation of /root/kk.php]
[Success]
phpdbg> b test::testa
[Breakpoint #0 added at test::testa]
phpdbg> b test::testb
[Breakpoint #1 added at test::testb]
phpdbg> b 0xb77b6614
[Breakpoint #2 added at 0xb77b6614]
phpdbg> b 0xb77b6630
[Breakpoint #3 added at 0xb77b6630]