当前位置:首页 > PHP教程 > php高级应用 > 列表

使用phpdbg来调试php程序的方法介绍

发布:smiling 来源: PHP粉丝网  添加日期:2022-06-04 09:22:45 浏览: 评论:0 

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一下

  1. cd /usr/src/php-src/sapi 
  2.  
  3. git clone https://github.com/krakjoe/phpdbg 
  4.  
  5. cd ../ 
  6.  
  7. ./buildconf --force 
  8.  
  9. ./config.nice 
  10.  
  11. make -j8 
  12.  
  13. make install-phpdbg 

PHP56的话直接启用phpdbg就可以了

注意: php 配置中可以启用 --with-readline for phpdbg to support history, autocompletion, tab-listing etc

  1. phpdbginit 
  2.  
  3. Setting up your debugging session automatically 
  4.   
  5. phpdbg -imy.phpdbginit 
  6.  
  7. In addition, .phpdbginit can contain embedded code, allowing, for example 
  8.  
  9. the setup of auto completion 
  10.  
  11. the registration of functions 
  12.  
  13. the acquisition and pre-compilation of code 
  14.  
  15. bootstrapping a web application 
  16.  
  17. The default .phpdbginit 
  18.  
  19.  
  20. ########################################################## 
  21.  
  22. # .phpdbginit 
  23.  
  24.  
  25. # Lines starting with # are ignored 
  26.  
  27. # Code must start and end with <: and :> respectively 
  28.  
  29. ########################################################## 
  30.  
  31. # Place initialization commands one per line 
  32.  
  33. ########################################################## 
  34.  
  35. exec sapi/phpdbg/test.php 
  36.  
  37.    
  38.  
  39. ########################################################## 
  40.  
  41. # Embedding code in .phpdbginit 
  42.  
  43. ########################################################## 
  44.  
  45. <: 
  46.  
  47. /* 
  48.  
  49.  If readline is loaded, you might want to setup completion: 
  50.  
  51. */ 
  52.  
  53. if (function_exists('readline_completion_function')) { 
  54.  
  55.     readline_completion_function(function(){ 
  56.  
  57.         return array_merge
  58.  
  59.             get_defined_functions()['user'], 
  60.  
  61.             array_keys(get_defined_constants()) 
  62.  
  63.         ); 
  64.  
  65.     }); 
  66.  
  67.  
  68. :> 
  69.  
  70. ########################################################## 
  71.  
  72. # Now carry on initializing phpdbg ... 
  73.  
  74. ########################################################## 
  75.  
  76. # R my_debugging_function 
  77.  
  78.    
  79.  
  80. ########################################################## 
  81.  
  82. # PHP has many functions that might be useful 
  83.  
  84. # ... you choose ... 
  85.  
  86. ########################################################## 
  87.  
  88. # R touch 
  89.  
  90. # R unlink 
  91.  
  92. # R scandir 
  93.  
  94. # R glob 
  95.  
  96.    
  97.  
  98. ########################################################## 
  99.  
  100. # Remember: *you have access to the shell* 
  101.  
  102. ########################################################## 
  103.  
  104. # The output of registered function calls is not, 
  105.  
  106. # by default, very pretty (unless you implement 
  107.  
  108. and register a new implementation for phpdbg) 
  109.  
  110. # The output of shell commands will usually be more 
  111.  
  112. # readable on the console 
  113.  
  114. ########################################################## 
  115.  
  116. # TLDR; if you have a good shell, use it ... 
  117.  
  118. ########################################################## 

查看版本号

  1. [root@localhost ~]# php -v 
  2.  
  3. PHP 5.6.16 (cli) (built: Dec  8 2015 09:10:23) (DEBUG) 
  4.  
  5. Copyright (c) 1997-2015 The PHP Group 
  6.  
  7. Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies 
  8.  
  9.     with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans 
  10.  
  11. [root@localhost ~]# phpdbg -V 
  12.  
  13. phpdbg 0.4.0 (built: Dec  8 2015 09:10:43) 
  14.  
  15. PHP 5.6.16, Copyright (c) 1997-2015 The PHP Group 
  16.  
  17. Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies 
  18.  
  19.     with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans 

查看help

  1. [root@localhost ~]# phpdbg 
  2.  
  3. [Welcome to phpdbg, the interactive PHP debugger, v0.4.0] 
  4.  
  5. To get help using phpdbg type "help" and press enter 
  6.  
  7. [Please report bugs to <http://github.com/krakjoe/phpdbg/issues>] 
  8.  
  9. phpdbg> help 
  10.  
  11.    
  12.  
  13. phpdbg is a lightweight, powerful and easy to use debugging platform for PHP5.4+ 
  14.  
  15. It supports the following commands: 
  16.  
  17.    
  18.  
  19. Information 
  20.  
  21.   list     list PHP source 
  22.  
  23.   info     displays information on the debug session 
  24.  
  25.   print    show opcodes 
  26.  
  27.   frame    select a stack frame and print a stack frame summary 
  28.  
  29.   back     shows the current backtrace 
  30.  
  31.   help     provide help on a topic 
  32.  
  33.    
  34.  
  35. Starting and Stopping Execution 
  36.  
  37.   exec     set execution context 
  38.  
  39.   run      attempt execution 
  40.  
  41.   step     continue execution until other line is reached 
  42.  
  43.   continue continue execution 
  44.  
  45.   until    continue execution up to the given location 
  46.  
  47.   finish   continue up to end of the current execution frame 
  48.  
  49.   leave    continue up to end of the current execution frame and halt after the calling instruction 
  50.  
  51.   break    set a breakpoint at the specified target 
  52.  
  53.   watch    set a watchpoint on $variable 
  54.  
  55.   clear    clear one or all breakpoints 
  56.  
  57.   clean    clean the execution environment 
  58.  
  59.    
  60.  
  61. Miscellaneous 
  62.  
  63.   set      set the phpdbg configuration 
  64.  
  65.   source   execute a phpdbginit script 
  66.  
  67.   register register a phpdbginit function as a command alias 
  68.  
  69.   sh       shell a command 
  70.  
  71.   ev       evaluate some code 
  72.  
  73.   quit     exit phpdbg 
  74.  
  75.    
  76.  
  77. 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 
  78.  
  79. 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. 
  80.  
  81.    
  82.  
  83. Type help aliases to show a full alias list, including any registered phpdginit functions 
  84.  
  85. Type help syntax for a general introduction to the command syntax. 
  86.  
  87. Type help options for a list of phpdbg command line options. 
  88.  
  89. Type help phpdbginit to show how to customise the debugger environment. 
  90.  
  91. phpdbg> help list 
  92.  
  93. Command: list  Alias: l  lists some code 
  94.  
  95.    
  96.  
  97. The list command displays source code for the given argument.  The target type is specficied by a second subcommand keyword: 
  98.  
  99.    
  100.  
  101.   Type     Alias  Purpose 
  102.  
  103.   lines    l      List N lines from the current execution point 
  104.  
  105.   func     f      List the complete source for a specified function 
  106.  
  107.   method   m      List the complete source for a specified class::method 
  108.  
  109.   class    c      List the complete source for a specified class 
  110.  
  111.    
  112.  
  113. 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 
  114.  
  115. class
  116.  
  117.    
  118.  
  119. Examples 
  120.  
  121.    
  122.  
  123.     phpdbg>  list 2 
  124.  
  125.     phpdbg>  l l 2 
  126.  
  127.     List the next 2 lines from the current file 
  128.  
  129.    
  130.  
  131.     phpdbg>  list my_function 
  132.  
  133.     phpdbg>  l f my_function 
  134.  
  135.     List the source of the function my_function 
  136.  
  137.    
  138.  
  139.     phpdbg>  list func .mine 
  140.  
  141.     phpdbg>  l f .mine 
  142.  
  143.     List the source of the method mine from the active class in scope 
  144.  
  145.    
  146.  
  147.     phpdbg>  list m my::method 
  148.  
  149.     phpdbg>  l my::method 
  150.  
  151.     List the source of my::method 
  152.  
  153.    
  154.  
  155.     phpdbg>  list c myClass 
  156.  
  157.     phpdbg>  l c myClass 
  158.  
  159.     List the source of myClass 
  160.  
  161.    
  162.  
  163. 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 
  164.  
  165. populate these tables for a given execution context. 
  166.  
  167. phpdbg> help break 
  168.  
  169. Command: break  Alias: b  set breakpoint 
  170.  
  171.    
  172.  
  173. 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 
  174.  
  175. following types: 
  176.  
  177.    
  178.  
  179.   Target   Alias Purpose 
  180.  
  181.   at       A     specify breakpoint by location and condition 
  182.  
  183.   del      d     delete breakpoint by breakpoint identifier number 
  184.  
  185.    
  186.  
  187. 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 
  188.  
  189. the specified target. 
  190.  
  191.    
  192.  
  193. Note that breakpoints can also be disabled and re-enabled by the set break command. 
  194.  
  195.    
  196.  
  197. Examples 
  198.  
  199.    
  200.  
  201.     phpdbg>  break test.php:100 
  202.  
  203.     phpdbg>  b test.php:100 
  204.  
  205.     Break execution at line 100 of test.php 
  206.  
  207.    
  208.  
  209.     phpdbg>  break 200 
  210.  
  211.     phpdbg>  b 200 
  212.  
  213.     Break execution at line 200 of the currently PHP script file 
  214.  
  215.    
  216.  
  217.     phpdbg>  break \mynamespace\my_function 
  218.  
  219.     phpdbg>  b \mynamespace\my_function 
  220.  
  221.     Break execution on entry to \mynamespace\my_function 
  222.  
  223.    
  224.  
  225.     phpdbg>  break classX::method 
  226.  
  227.     phpdbg>  b classX::method 
  228.  
  229.     Break execution on entry to classX::method 
  230.  
  231.    
  232.  
  233.     phpdbg>  break 0x7ff68f570e08 
  234.  
  235.     phpdbg>  b 0x7ff68f570e08 
  236.  
  237.     Break at the opline at the address 0x7ff68f570e08 
  238.  
  239.    
  240.  
  241.     phpdbg>  break my_function#14 
  242.  
  243.     phpdbg>  b my_function#14 
  244.  
  245.     Break at the opline #14 of the function my_function 
  246.  
  247.    
  248.  
  249.     phpdbg>  break \my\class::method#2 
  250.  
  251.     phpdbg>  b \my\class::method#2 
  252.  
  253.     Break at the opline #2 of the method \my\class::method 
  254.  
  255.    
  256.  
  257.     phpdbg>  break test.php:#3 
  258.  
  259.     phpdbg>  b test.php:#3 
  260.  
  261.     Break at opline #3 in test.php 
  262.  
  263.    
  264.  
  265.     phpdbg>  break if $cnt > 10 
  266.  
  267.     phpdbg>  b if $cnt > 10 
  268.  
  269.     Break when the condition ($cnt > 10) evaluates to true 
  270.  
  271.    
  272.  
  273.     phpdbg>  break at phpdbg::isGreat if $opt == 'S' 
  274.  
  275.     phpdbg>  break @ phpdbg::isGreat if $opt == 'S' 
  276.  
  277.     Break at any opcode in phpdbg::isGreat when the condition ($opt == 'S') is true 
  278.  
  279.    
  280.  
  281.     phpdbg>  break at test.php:20 if !isset($x
  282.  
  283.     Break at every opcode on line 20 of test.php when the condition evaluates to true 
  284.  
  285.    
  286.  
  287.     phpdbg>  break ZEND_ADD 
  288.  
  289.     phpdbg>  b ZEND_ADD 
  290.  
  291.     Break on any occurence of the opcode ZEND_ADD 
  292.  
  293.    
  294.  
  295.     phpdbg>  break del 2 
  296.  
  297.     phpdbg>  b ~ 2 
  298.  
  299.     Remove breakpoint 2 
  300.  
  301.    
  302.  
  303. Note: Conditional breaks are costly in terms of runtime overhead. Use them only when required as they significantly slow execution. 
  304.  
  305.    
  306.  
  307. Note: An address is only valid for the current compilation. 
  308.  
  309. phpdbg> help watch 
  310.  
  311. Command: watch  Alias: w  set watchpoint 
  312.  
  313.    
  314.  
  315. Sets watchpoints on variables as long as they are defined 
  316.  
  317. Passing no parameter to watch, lists all actually active watchpoints 
  318.  
  319.    
  320.  
  321. Format for $variable 
  322.  
  323.    
  324.  
  325.    $var      Variable $var 
  326.  
  327.    $var[]    All array elements of $var 
  328.  
  329.    $var->    All properties of $var 
  330.  
  331.    $var->a   Property $var->a 
  332.  
  333.    $var[b]   Array element with key b in array $var 
  334.  
  335.    
  336.  
  337. Subcommands of watch: 
  338.  
  339.    
  340.  
  341.    Type     Alias      Purpose 
  342.  
  343.    array       a       Sets watchpoint on array/object to observe if an entry is added or removed 
  344.  
  345.    recursive   r       Watches variable recursively and automatically adds watchpoints if some entry is added to an array/object 
  346.  
  347.    delete      d       Removes watchpoint 
  348.  
  349.    
  350.  
  351. Note when recursive watchpoints are removed, watchpoints on all the children are removed too 
  352.  
  353.    
  354.  
  355. Examples 
  356.  
  357.    
  358.  
  359.      phpdbg>  watch 
  360.  
  361.      List currently active watchpoints 
  362.  
  363.    
  364.  
  365.      phpdbg>  watch $array 
  366.  
  367.      phpdbg>  w $array 
  368.  
  369.      Set watchpoint on $array 
  370.  
  371.    
  372.  
  373.      phpdbg>  watch recursive $obj-> 
  374.  
  375.      phpdbg>  w r $obj-> 
  376.  
  377.      Set recursive watchpoint on $obj-> 
  378.  
  379.    
  380.  
  381.      phpdbg>  watch delete $obj->a 
  382.  
  383.      phpdbg>  w d $obj->a 
  384.  
  385.      Remove watchpoint $obj->a 
  386.  
  387.    
  388.  
  389. 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. 
  390.  
  391.                 You then you can continue, phpdbg will remove the write protection, so that the program can continue
  392.  
  393.                 If phpdbg could not handle that segfault, the same segfault is triggered again and this time phpdbg will abort. 

这些帮助文档足够了

示例代码 testb 与 testa的输出为什么是这样

  1. <?php 
  2.  
  3.    
  4.  
  5. ini_set("memory_limit","-1"); 
  6.  
  7.    
  8.  
  9. class test{ 
  10.  
  11.    
  12.  
  13.     public function testa(){ 
  14.  
  15.    
  16.  
  17.         $a=1; 
  18.  
  19.         $b = &$a
  20.  
  21.         return 0 + (++$a) + (++$a); 
  22.  
  23.    
  24.  
  25.     } 
  26.  
  27.    
  28.  
  29.     public function testb(){ 
  30.  
  31.    
  32.  
  33.         $a=1; 
  34.  
  35.         $b = &$a
  36.  
  37.         return ++$a  + (++$a); 
  38.  
  39.    
  40.  
  41.     } 
  42.  
  43.    
  44.  
  45.     public function convert($size
  46.  
  47.     { 
  48.  
  49.         $unit=array('b','kb','mb','gb','tb','pb'); 
  50.  
  51.         return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; 
  52.  
  53.     } 
  54.  
  55.    
  56.  
  57.     public function allsort($arr){ 
  58.  
  59.         sort($arr); 
  60.  
  61.         $len = count($arr)-1; 
  62.  
  63.         $rs = array(); 
  64.  
  65.         $rs[] = $arr
  66.  
  67.         $x = $len
  68.  
  69.    
  70.  
  71.         while($x > 0){ 
  72.  
  73.             $y = $x --; 
  74.  
  75.             if($arr[$x] < $arr[$y]){ 
  76.  
  77.                 $z = $len
  78.  
  79.                 while($arr[$x] > $arr[$z]){ 
  80.  
  81.                     $z--; 
  82.  
  83.                 } 
  84.  
  85.                 list($arr[$x],$arr[$z]) = array($arr[$z],$arr[$x]); 
  86.  
  87.    
  88.  
  89.                 for($i=$len;$i>$y;$i--,$y++){ 
  90.  
  91.                     list($arr[$i],$arr[$y]) = array($arr[$y],$arr[$i]); 
  92.  
  93.                 } 
  94.  
  95.                 $x = $len
  96.  
  97.                 $rs[] = $arr
  98.  
  99.             } 
  100.  
  101.    
  102.  
  103.         } 
  104.  
  105.         return $rs
  106.  
  107.     } 
  108.  
  109.  
  110.    
  111.  
  112. $s = new test(); 
  113.  
  114. $resa = $s->testa(); 
  115.  
  116. $resb = $s->testb(); 
  117.  
  118. $resc = $s->allsort([1,2,3]); 
  119.  
  120. echo $resa,"\n",$resb,"\n"
  121.  
  122. #print_r($resb); 

开始调试

  1. [root@localhost ~]# phpdbg -e kk.php 
  2.  
  3. [Welcome to phpdbg, the interactive PHP debugger, v0.4.0] 
  4.  
  5. To get help using phpdbg type "help" and press enter 
  6.  
  7. [Please report bugs to <http://github.com/krakjoe/phpdbg/issues>
  8.  
  9. [Attempting compilation of /root/kk.php] 
  10.  
  11. [Success] 

查看一些当前类的opcode

  1. phpdbg> p c test 
  2.  
  3. [User Class: test] 
  4.  
  5. Methods (4): 
  6.  
  7.         L7-13 test::testa() /root/kk.php 
  8.  
  9.                 L7      0xb77b553c ZEND_EXT_NOP                   <unused>             <unused>             <unused>            
  10.  
  11.                 L9      0xb77b5558 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  12.  
  13.                 L9      0xb77b5574 ZEND_ASSIGN                    $a                   C0                   @0                  
  14.  
  15.                 L10     0xb77b5590 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  16.  
  17.                 L10     0xb77b55ac ZEND_ASSIGN_REF                $b                   $a                   @1                  
  18.  
  19.                 L11     0xb77b55c8 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  20.  
  21.                 L11     0xb77b55e4 ZEND_PRE_INC                   $a                   <unused>             @2                  
  22.  
  23.                 L11     0xb77b5600 ZEND_ADD                       C1                   @2                   @3                  
  24.  
  25.                 L11     0xb77b561c ZEND_PRE_INC                   $a                   <unused>             @4                  
  26.  
  27.                 L11     0xb77b5638 ZEND_ADD                       @3                   @4                   @5                  
  28.  
  29.                 L11     0xb77b5654 ZEND_RETURN                    @5                   <unused>             <unused>            
  30.  
  31.                 L13     0xb77b5670 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  32.  
  33.                 L13     0xb77b568c ZEND_RETURN                    C2                   <unused>             <unused>            
  34.  
  35.         L15-21 test::testb() /root/kk.php 
  36.  
  37.                 L15     0xb77b656c ZEND_EXT_NOP                   <unused>             <unused>             <unused>            
  38.  
  39.                 L17     0xb77b6588 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  40.  
  41.                 L17     0xb77b65a4 ZEND_ASSIGN                    $a                   C0                   @0                  
  42.  
  43.                 L18     0xb77b65c0 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  44.  
  45.                 L18     0xb77b65dc ZEND_ASSIGN_REF                $b                   $a                   @1                  
  46.  
  47.                 L19     0xb77b65f8 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  48.  
  49.                 L19     0xb77b6614 ZEND_PRE_INC                   $a                   <unused>             @2                  
  50.  
  51.                 L19     0xb77b6630 ZEND_PRE_INC                   $a                   <unused>             @3                  
  52.  
  53.                 L19     0xb77b664c ZEND_ADD                       @2                   @3                   @4                  
  54.  
  55.                 L19     0xb77b6668 ZEND_RETURN                    @4                   <unused>             <unused>            
  56.  
  57.                 L21     0xb77b6684 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  58.  
  59.                 L21     0xb77b66a0 ZEND_RETURN                    C1                   <unused>             <unused>            
  60.  
  61.         L23-27 test::convert() /root/kk.php 
  62.  
  63.                 L23     0xb77b66ec ZEND_EXT_NOP                   <unused>             <unused>             <unused>            
  64.  
  65.                 L23     0xb77b6708 ZEND_RECV                      <unused>             <unused>             $size              
  66.  
  67.                 L25     0xb77b6724 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  68.  
  69.                 L25     0xb77b6740 ZEND_INIT_ARRAY                C0                   <unused>             @0                  
  70.  
  71.                 L25     0xb77b675c ZEND_ADD_ARRAY_ELEMENT         C1                   <unused>             @0                  
  72.  
  73.                 L25     0xb77b6778 ZEND_ADD_ARRAY_ELEMENT         C2                   <unused>             @0                  
  74.  
  75.                 L25     0xb77b6794 ZEND_ADD_ARRAY_ELEMENT         C3                   <unused>             @0                  
  76.  
  77.                 L25     0xb77b67b0 ZEND_ADD_ARRAY_ELEMENT         C4                   <unused>             @0                  
  78.  
  79.                 L25     0xb77b67cc ZEND_ADD_ARRAY_ELEMENT         C5                   <unused>             @0                  
  80.  
  81.                 L25     0xb77b67e8 ZEND_ASSIGN                    $unit                @0                   @1                  
  82.  
  83.                 L26     0xb77b6804 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  84.  
  85.                 L26     0xb77b6820 ZEND_BEGIN_SILENCE             <unused>             <unused>             @2                  
  86.  
  87.                 L26     0xb77b683c ZEND_EXT_FCALL_BEGIN           <unused>             <unused>             <unused>            
  88.  
  89.                 L26     0xb77b6858 ZEND_EXT_FCALL_BEGIN           <unused>             <unused>             <unused>            
  90.  
  91.                 L26     0xb77b6874 ZEND_SEND_VAL                  C6                   <unused>             <unused>            
  92.  
  93.                 L26     0xb77b6890 ZEND_EXT_FCALL_BEGIN           <unused>             <unused>             <unused>            
  94.  
  95.                 L26     0xb77b68ac ZEND_EXT_FCALL_BEGIN           <unused>             <unused>             <unused>            
  96.  
  97.                 L26     0xb77b68c8 ZEND_SEND_VAR                  $size                <unused>             <unused>            
  98.  
  99.                 L26     0xb77b68e4 ZEND_SEND_VAL                  C7                   <unused>             <unused>            
  100.  
  101.                 L26     0xb77b6900 ZEND_DO_FCALL                  C8                   <unused>             @3                  
  102.  
  103.                 L26     0xb77b691c ZEND_EXT_FCALL_END             <unused>             <unused>             <unused>            
  104.  
  105.                 L26     0xb77b6938 ZEND_SEND_VAR_NO_REF           @3                   <unused>             <unused>            
  106.  
  107.                 L26     0xb77b6954 ZEND_DO_FCALL                  C9                   <unused>             @4                  
  108.  
  109.                 L26     0xb77b6970 ZEND_EXT_FCALL_END             <unused>             <unused>             <unused>            
  110.  
  111.                 L26     0xb77b698c ZEND_ASSIGN                    $i                   @4                   @5                  
  112.  
  113.                 L26     0xb77b69a8 ZEND_SEND_VAR_NO_REF           @5                   <unused>             <unused>            
  114.  
  115.                 L26     0xb77b69c4 ZEND_DO_FCALL                  C10                  <unused>             @6                  
  116.  
  117.                 L26     0xb77b69e0 ZEND_EXT_FCALL_END             <unused>             <unused>             <unused>            
  118.  
  119.                 L26     0xb77b69fc ZEND_DIV                       $size                @6                   @7                  
  120.  
  121.                 L26     0xb77b6a18 ZEND_SEND_VAL                  @7                   <unused>             <unused>            
  122.  
  123.                 L26     0xb77b6a34 ZEND_SEND_VAL                  C11                  <unused>             <unused>            
  124.  
  125.                 L26     0xb77b6a50 ZEND_DO_FCALL                  C12                  <unused>             @8                  
  126.  
  127.                 L26     0xb77b6a6c ZEND_EXT_FCALL_END             <unused>             <unused>             <unused>            
  128.  
  129.                 L26     0xb77b6a88 ZEND_END_SILENCE               @2                   <unused>             <unused>            
  130.  
  131.                 L26     0xb77b6aa4 ZEND_CONCAT                    @8                   C13                  @9                  
  132.  
  133.                 L26     0xb77b6ac0 ZEND_FETCH_DIM_R               $unit                $i                   @10                 
  134.  
  135.                 L26     0xb77b6adc ZEND_CONCAT                    @9                   @10                  @11                 
  136.  
  137.                 L26     0xb77b6af8 ZEND_RETURN                    @11                  <unused>             <unused>            
  138.  
  139.                 L27     0xb77b6b14 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  140.  
  141.                 L27     0xb77b6b30 ZEND_RETURN                    C14                  <unused>             <unused>            
  142.  
  143.         L29-54 test::allsort() /root/kk.php 
  144.  
  145.                 L29     0xb77b818c ZEND_EXT_NOP                   <unused>             <unused>             <unused>            
  146.  
  147.                 L29     0xb77b81a8 ZEND_RECV                      <unused>             <unused>             $arr               
  148.  
  149.                 L30     0xb77b81c4 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  150.  
  151.                 L30     0xb77b81e0 ZEND_EXT_FCALL_BEGIN           <unused>             <unused>             <unused>            
  152.  
  153.                 L30     0xb77b81fc ZEND_SEND_REF                  $arr                 <unused>             <unused>            
  154.  
  155.                 L30     0xb77b8218 ZEND_DO_FCALL                  C0                   <unused>             @0                  
  156.  
  157.                 L30     0xb77b8234 ZEND_EXT_FCALL_END             <unused>             <unused>             <unused>            
  158.  
  159.                 L31     0xb77b8250 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  160.  
  161.                 L31     0xb77b826c ZEND_EXT_FCALL_BEGIN           <unused>             <unused>             <unused>            
  162.  
  163.                 L31     0xb77b8288 ZEND_SEND_VAR                  $arr                 <unused>             <unused>            
  164.  
  165.                 L31     0xb77b82a4 ZEND_DO_FCALL                  C1                   <unused>             @1                  
  166.  
  167.                 L31     0xb77b82c0 ZEND_EXT_FCALL_END             <unused>             <unused>             <unused>            
  168.  
  169.                 L31     0xb77b82dc ZEND_SUB                       @1                   C2                   @2                  
  170.  
  171.                 L31     0xb77b82f8 ZEND_ASSIGN                    $len                 @2                   @3                  
  172.  
  173.                 L32     0xb77b8314 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  174.  
  175.                 L32     0xb77b8330 ZEND_INIT_ARRAY                <unused>             <unused>             @4                  
  176.  
  177.                 L32     0xb77b834c ZEND_ASSIGN                    $rs                  @4                   @5                  
  178.  
  179.                 L33     0xb77b8368 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  180.  
  181.                 L33     0xb77b8384 ZEND_ASSIGN_DIM                $rs                  <unused>             @6                  
  182.  
  183.                 L33     0xb77b83a0 UNKNOWN                        $arr                 @7                   <unused>            
  184.  
  185.                 L34     0xb77b83bc ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  186.  
  187.                 L34     0xb77b83d8 ZEND_ASSIGN                    $x                   $len                 @8                  
  188.  
  189.                 L36     0xb77b83f4 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  190.  
  191.                 L36     0xb77b8410 ZEND_IS_SMALLER                C3                   $x                   @9                  
  192.  
  193.                 L36     0xb77b842c ZEND_JMPZ                      @9                   J86                  <unused>            
  194.  
  195.                 L37     0xb77b8448 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  196.  
  197.                 L37     0xb77b8464 ZEND_POST_DEC                  $x                   <unused>             @10                 
  198.  
  199.                 L37     0xb77b8480 ZEND_ASSIGN                    $y                   @10                  @11                 
  200.  
  201.                 L38     0xb77b849c ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  202.  
  203.                 L38     0xb77b84b8 ZEND_FETCH_DIM_R               $arr                 $x                   @12                 
  204.  
  205.                 L38     0xb77b84d4 ZEND_FETCH_DIM_R               $arr                 $y                   @13                 
  206.  
  207.                 L38     0xb77b84f0 ZEND_IS_SMALLER                @12                  @13                  @14                 
  208.  
  209.                 L38     0xb77b850c ZEND_JMPZ                      @14                  J85                  <unused>            
  210.  
  211.                 L39     0xb77b8528 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  212.  
  213.                 L39     0xb77b8544 ZEND_ASSIGN                    $z                   $len                 @15                 
  214.  
  215.                 L40     0xb77b8560 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  216.  
  217.                 L40     0xb77b857c ZEND_FETCH_DIM_R               $arr                 $x                   @16                 
  218.  
  219.                 L40     0xb77b8598 ZEND_FETCH_DIM_R               $arr                 $z                   @17                 
  220.  
  221.                 L40     0xb77b85b4 ZEND_IS_SMALLER                @17                  @16                  @18                 
  222.  
  223.                 L40     0xb77b85d0 ZEND_JMPZ                      @18                  J44                  <unused>            
  224.  
  225.                 L41     0xb77b85ec ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  226.  
  227.                 L41     0xb77b8608 ZEND_POST_DEC                  $z                   <unused>             @19                 
  228.  
  229.                 L41     0xb77b8624 ZEND_FREE                      @19                  <unused>             <unused>            
  230.  
  231.                 L42     0xb77b8640 ZEND_JMP                       J36                                                           
  232.  
  233.                 L43     0xb77b865c ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  234.  
  235.                 L43     0xb77b8678 ZEND_FETCH_DIM_R               $arr                 $z                   @20                 
  236.  
  237.                 L43     0xb77b8694 ZEND_INIT_ARRAY                @20                  <unused>             @21                 
  238.  
  239.                 L43     0xb77b86b0 ZEND_FETCH_DIM_R               $arr                 $x                   @22                 
  240.  
  241.                 L43     0xb77b86cc ZEND_ADD_ARRAY_ELEMENT         @22                  <unused>             @21                 
  242.  
  243.                 L43     0xb77b86e8 ZEND_FETCH_DIM_TMP_VAR         @21                  C4                   @23                 
  244.  
  245.                 L43     0xb77b8704 ZEND_ASSIGN_DIM                $arr                 $z                   @24                 
  246.  
  247.                 L43     0xb77b8720 UNKNOWN                        @23                  @25                  <unused>            
  248.  
  249.                 L43     0xb77b873c ZEND_FETCH_DIM_TMP_VAR         @21                  C5                   @26                 
  250.  
  251.                 L43     0xb77b8758 ZEND_ASSIGN_DIM                $arr                 $x                   @27                 
  252.  
  253.                 L43     0xb77b8774 UNKNOWN                        @26                  @28                  <unused>            
  254.  
  255.                 L43     0xb77b8790 ZEND_FREE                      @21                  <unused>             <unused>            
  256.  
  257.                 L45     0xb77b87ac ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  258.  
  259.                 L45     0xb77b87c8 ZEND_ASSIGN                    $i                   $len                 @29                 
  260.  
  261.                 L45     0xb77b87e4 ZEND_IS_SMALLER                $y                   $i                   @30                 
  262.  
  263.                 L45     0xb77b8800 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  264.  
  265.                 L45     0xb77b881c ZEND_JMPZNZ                    @30                  J79 or J66           <unused>            
  266.  
  267.                 L45     0xb77b8838 ZEND_POST_DEC                  $i                   <unused>             @31                 
  268.  
  269.                 L45     0xb77b8854 ZEND_FREE                      @31                  <unused>             <unused>            
  270.  
  271.                 L45     0xb77b8870 ZEND_POST_INC                  $y                   <unused>             @32                 
  272.  
  273.                 L45     0xb77b888c ZEND_FREE                      @32                  <unused>             <unused>            
  274.  
  275.                 L45     0xb77b88a8 ZEND_JMP                       J58                                                           
  276.  
  277.                 L46     0xb77b88c4 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  278.  
  279.                 L46     0xb77b88e0 ZEND_FETCH_DIM_R               $arr                 $y                   @33                 
  280.  
  281.                 L46     0xb77b88fc ZEND_INIT_ARRAY                @33                  <unused>             @34                 
  282.  
  283.                 L46     0xb77b8918 ZEND_FETCH_DIM_R               $arr                 $i                   @35                 
  284.  
  285.                 L46     0xb77b8934 ZEND_ADD_ARRAY_ELEMENT         @35                  <unused>             @34                 
  286.  
  287.                 L46     0xb77b8950 ZEND_FETCH_DIM_TMP_VAR         @34                  C6                   @36                 
  288.  
  289.                 L46     0xb77b896c ZEND_ASSIGN_DIM                $arr                 $y                   @37                 
  290.  
  291.                 L46     0xb77b8988 UNKNOWN                        @36                  @38                  <unused>            
  292.  
  293.                 L46     0xb77b89a4 ZEND_FETCH_DIM_TMP_VAR         @34                  C7                   @39                 
  294.  
  295.                 L46     0xb77b89c0 ZEND_ASSIGN_DIM                $arr                 $i                   @40                 
  296.  
  297.                 L46     0xb77b89dc UNKNOWN                        @39                  @41                  <unused>            
  298.  
  299.                 L46     0xb77b89f8 ZEND_FREE                      @34                  <unused>             <unused>            
  300.  
  301.                 L47     0xb77b8a14 ZEND_JMP                       J61                                                           
  302.  
  303.                 L48     0xb77b8a30 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  304.  
  305.                 L48     0xb77b8a4c ZEND_ASSIGN                    $x                   $len                 @42                 
  306.  
  307.                 L49     0xb77b8a68 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  308.  
  309.                 L49     0xb77b8a84 ZEND_ASSIGN_DIM                $rs                  <unused>             @43                 
  310.  
  311.                 L49     0xb77b8aa0 UNKNOWN                        $arr                 @44                  <unused>            
  312.  
  313.                 L50     0xb77b8abc ZEND_JMP                       J85                                                           
  314.  
  315.                 L52     0xb77b8ad8 ZEND_JMP                       J23                                                           
  316.  
  317.                 L53     0xb77b8af4 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  318.  
  319.                 L53     0xb77b8b10 ZEND_RETURN                    $rs                  <unused>             <unused>            
  320.  
  321.                 L54     0xb77b8b2c ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  322.  
  323.                 L54     0xb77b8b48 ZEND_RETURN                    C8                   <unused>             <unused>            
  324.  
  325. phpdbg> 

加断点,开始调试

  1. phpdbg> b test::testa 
  2.  
  3. [Breakpoint #0 added at test::testa] 
  4.  
  5. phpdbg> b test::testb 
  6.  
  7. [Breakpoint #1 added at test::testb] 
  8.  
  9. phpdbg> b 0xb77b6614 
  10.  
  11. [Breakpoint #2 added at 0xb77b6614] 
  12.  
  13. phpdbg> b 0xb77b6630 
  14.  
  15. [Breakpoint #3 added at 0xb77b6630] 

运行、查看代码

  1. phpdbg> r 
  2.  
  3. [Breakpoint #0 in test::testa() at /root/kk.php:7, hits: 1] 
  4.  
  5.  00006: 
  6.  
  7. >00007:         public function testa(){ 
  8.  
  9.  00008: 
  10.  
  11.  00009:                 $a=1; 
  12.  
  13. phpdbg> l test::testa 
  14.  
  15. 00007:  public function testa(){ 
  16.  
  17. 00008: 
  18.  
  19. 00009:          $a=1; 
  20.  
  21. 00010:          $b = &$a
  22.  
  23. 00011:          return 0 + (++$a) + (++$a); 
  24.  
  25. 00012: 
  26.  
  27. 00013:  } 
  28.  
  29. 00014: 
  30.  
  31. phpdbg> p s 
  32.  
  33. [Stack in test::testa()] 
  34.  
  35.         L7-13 test::testa() /root/kk.php 
  36.  
  37.                 L7      0xb77b553c ZEND_EXT_NOP                   <unused>             <unused>             <unused>            
  38.  
  39.                 L9      0xb77b5558 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  40.  
  41.                 L9      0xb77b5574 ZEND_ASSIGN                    $a                   C0                   @0                  
  42.  
  43.                 L10     0xb77b5590 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  44.  
  45.                 L10     0xb77b55ac ZEND_ASSIGN_REF                $b                   $a                   @1                  
  46.  
  47.                 L11     0xb77b55c8 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  48.  
  49.                 L11     0xb77b55e4 ZEND_PRE_INC                   $a                   <unused>             @2                  
  50.  
  51.                 L11     0xb77b5600 ZEND_ADD                       C1                   @2                   @3                  
  52.  
  53.                 L11     0xb77b561c ZEND_PRE_INC                   $a                   <unused>             @4                  
  54.  
  55.                 L11     0xb77b5638 ZEND_ADD                       @3                   @4                   @5                  
  56.  
  57.                 L11     0xb77b5654 ZEND_RETURN                    @5                   <unused>             <unused>            
  58.  
  59.                 L13     0xb77b5670 ZEND_EXT_STMT                  <unused>             <unused>             <unused>            
  60.  
  61.                 L13     0xb77b568c ZEND_RETURN                    C2                   <unused>             <unused>   <br><br> 

继续执行

和gdb一样,phpdbg的继续执行命令也是continue,简写形式为c

查看断点

  1. phpdbg> info b 
  2.  
  3. ------------------------------------------------ 
  4.  
  5. Method Breakpoints: 
  6.  
  7. #0              test::testa 
  8.  
  9. #1              test::testb 
  10.  
  11. ------------------------------------------------ 
  12.  
  13. Opline Breakpoints: 
  14.  
  15. #2              0xb77b6614 
  16.  
  17. #3              0xb77b6630 

eval操作

可以执行任意的PHP代码

我们可以看到上面例子 testa 与 testb的区别

  1. L7-13 test::testa() /root/kk.php 
  2.  
  3.         L11     0xb77b55e4 ZEND_PRE_INC                   $a                   <unused>             @2 
  4.  
  5.         L11     0xb77b5600 ZEND_ADD                       C1                   @2                   @3 
  6.  
  7.         L11     0xb77b561c ZEND_PRE_INC                   $a                   <unused>             @4 
  8.  
  9.         L11     0xb77b5638 ZEND_ADD                       @3                   @4                   @5 
  10.  
  11. L15-21 test::testb() /root/kk.php 
  12.  
  13.         L19     0xb77b6614 ZEND_PRE_INC                   $a                   <unused>             @2 
  14.  
  15.         L19     0xb77b6630 ZEND_PRE_INC                   $a                   <unused>             @3 
  16.  
  17.         L19     0xb77b664c ZEND_ADD                       @2                   @3                   @4 

区别在于这里 ZEND_PRE_INC 这一个opcode不同,这个opcode就是 ++$a

testb中当引用存在的时候, $a 先自增了两次,然后再加,所以结果为6,这里应该是PHP本身的问题。

这个工具比vld丰富多了,PHP会越来越强大的。

Tags: phpdbg php程序

分享到: