使用phpdbg来调试php程序的方法介绍
发布时间:2022-06-30 09:43:59 所属栏目:PHP教程 来源:互联网
导读:PHPDBG是一个PHP的SAPI模块,可以在不用修改代码和不影响性能的情况下控制PHP的运行环境。 可以在PHP5.4和之上版本中使用。在PHP5.6和之上版本将内部集成 1.jpg 功能 单步调试 灵活的下断点方式(类方法、函数、文件:行、内存地址、opcode) 可直接调用php的
|
PHPDBG是一个PHP的SAPI模块,可以在不用修改代码和不影响性能的情况下控制PHP的运行环境。 可以在PHP5.4和之上版本中使用。在PHP5.6和之上版本将内部集成 1.jpg 功能 单步调试 灵活的下断点方式(类方法、函数、文件:行、内存地址、opcode) 可直接调用php的eval 可以查看当前执行的代码 用户空间API(userland/user space) 方便集成 支持指定php配置文件 JIT全局变量 readline支持(可选),终端操作更方便 远程debug,使用java GUI 操作简便(具体看help) 安装 如果是PHP56一下 cd /usr/src/php-src/sapi git clone https://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 <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 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 mynamespacemy_function phpdbg> b mynamespacemy_function Break execution on entry to mynamespacemy_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 myclass::method#2 phpdbg> b myclass::method#2 Break at the opline #2 of the method myclass::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. 这些帮助文档足够了 示例代码 testb 与 testa的输出为什么是这样 <?php ini_set("memory_limit","-1"); class test{ public function testa(){ $a=1; $b = &$a; return 0 + (++$a) + (++$a); } public function testb(){ $a=1; $b = &$a; return ++$a + (++$a); } public function convert($size) { $unit=array('b','kb','mb','gb','tb','pb'); return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; } public function allsort($arr){ sort($arr); $len = count($arr)-1; $rs = array(); $rs[] = $arr; $x = $len; while($x > 0){ $y = $x --; if($arr[$x] < $arr[$y]){ $z = $len; while($arr[$x] > $arr[$z]){ $z--; } list($arr[$x],$arr[$z]) = array($arr[$z],$arr[$x]); for($i=$len;$i>$y;$i--,$y++){ list($arr[$i],$arr[$y]) = array($arr[$y],$arr[$i]); } $x = $len; $rs[] = $arr; } } return $rs; } } $s = new test(); $resa = $s->testa(); $resb = $s->testb(); $resc = $s->allsort([1,2,3]); echo $resa,"n",$resb,"n"; #print_r($resb); 开始调试 [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 <http://github.com/krakjoe/phpdbg/issues>] [Attempting compilation of /root/kk.php] [Success] 查看一些当前类的opcode phpdbg> p c test [User Class: test] Methods (4): L7-13 test::testa() /root/kk.php L7 0xb77b553c ZEND_EXT_NOP <unused> <unused> <unused> L9 0xb77b5558 ZEND_EXT_STMT <unused> <unused> <unused> L9 0xb77b5574 ZEND_ASSIGN $a C0 @0 L10 0xb77b5590 ZEND_EXT_STMT <unused> <unused> <unused> L10 0xb77b55ac ZEND_ASSIGN_REF $b $a @1 L11 0xb77b55c8 ZEND_EXT_STMT <unused> <unused> <unused> L11 0xb77b55e4 ZEND_PRE_INC $a <unused> @2 L11 0xb77b5600 ZEND_ADD C1 @2 @3 L11 0xb77b561c ZEND_PRE_INC $a <unused> @4 L11 0xb77b5638 ZEND_ADD @3 @4 @5 L11 0xb77b5654 ZEND_RETURN @5 <unused> <unused> L13 0xb77b5670 ZEND_EXT_STMT <unused> <unused> <unused> L13 0xb77b568c ZEND_RETURN C2 <unused> <unused> L15-21 test::testb() /root/kk.php L15 0xb77b656c ZEND_EXT_NOP <unused> <unused> <unused> L17 0xb77b6588 ZEND_EXT_STMT <unused> <unused> <unused> L17 0xb77b65a4 ZEND_ASSIGN $a C0 @0 L18 0xb77b65c0 ZEND_EXT_STMT <unused> <unused> <unused> L18 0xb77b65dc ZEND_ASSIGN_REF $b $a @1 L19 0xb77b65f8 ZEND_EXT_STMT <unused> <unused> <unused> L19 0xb77b6614 ZEND_PRE_INC $a <unused> @2 (编辑:阳江站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

