PHPDBG是一个PHP的SAPI模块,可以在不用修改代码和不影响性能的情况下控制PHP的运行环境
可以在PHP5.4和之上版本中使用。在PHP5.6和之上版本将内部集成
功能
- 单步调试
- 灵活的下断点方式(类方法、函数、文件:行、内存地址、opcode)
- 可直接调用php的eval
- 可以查看当前执行的代码
- 用户空间API(userland/user space)
- 方便集成
- 支持指定php配置文件
- JIT全局变量
- readline支持(可选),终端操作更方便
- 远程debug,使用java GUI
- 操作简便(具体看help)
安装
如果是PHP56一下
cd /usr/src/php-src/sapigit clone https://github.com/krakjoe/phpdbgcd .././buildconf --force./config.nicemake -j8make 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
the setup of auto completionIn addition, .phpdbginit can contain embedded code, allowing, for example
- 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 -vPHP 5.6.16 (cli) (built: Dec 8 2015 09:10:23) (DEBUG)Copyright (c) 1997-2015 The PHP GroupZend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies <span style="color:transparent">~来1源gaodai#ma#com搞*代#码1网</span><abbr>搞代gaodaima码</abbr> with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans[root@localhost ~]# phpdbg -Vphpdbg 0.4.0 (built: Dec 8 2015 09:10:43)PHP 5.6.16, Copyright (c) 1997-2015 The PHP GroupZend 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 <http://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 listout 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 functionsType 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 listCommand: 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 aclass. 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 topopulate these tables for a given execution context.phpdbg> help breakCommand: 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 thefollowing 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 atthe 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 watchCommand: watch Alias: w set watchpoint Sets watchpoints on variables as long as they are definedPassing 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.