PHPµ÷ÊÔµÄÇ¿º·ÀûÆ÷Ö®PHPDBG

5年以前  |  阅读数:873 次  |  编程语言:PHP 

PHPDBGÊÇÒ»¸oPHPµÄSAPIÄ£¿e£¬¿ÉÒÔÔÚ²»ÓÃÐ޸ĴuÂeºÍ²»Ó°ÏiÐÔÄܵÄÇe¿oÏ¿ØÖÆPHPµÄÔËÐл*¾³¡£

PHPDBGµÄÄ¿±eÊdzÉΪһ¸oÇaÁ¿¼¶¡¢Ç¿´o¡¢Ò×ÓõÄPHPµ÷ÊÔƽ̨¡£¿ÉÒÔÔÚPHP5.4ºÍÖ®ÉÏ°ae±¾ÖÐʹÓá£ÔÚphp5.6ºÍÖ®ÉÏ°ae±¾½«ÄÚ²¿¼¯³É¡£

Ö÷Òª¹¦ÄÜ£º

¨C µ¥²½µ÷ÊÔ

¨C Áe»iµÄ϶ϵa½Ê½£¨Àa½¨¡¢º¯Êý¡¢Îļþ:ÐС¢ÄÚ´aeµØÖ¡¢opcode£(C)

¨C ¿ÉÖ±½Óµ÷ÓÃphpµÄeval

¨C ¿ÉÒÔ²e¿´µ±Ç°Ö´ÐеĴuÂe

¨C Óû§¿Õ¼aAPI£¨userland/user space£(C)

¨C *½±a¼¯³É

¨C Ö§³ÖÖ¸¶¨phpÅaÖÃÎļþ

¨C JITÈ«¾Ö±aÁ¿

¨C readlineÖ§³Ö£¨¿ÉÑ¡£(C)£¬Öն˲Ù×÷¸u*½±a

¨C Ô¶³Ìdebug£¬Ê¹ÓÃjava GUI

¨C ²Ù×÷¼o±a£¨¾ßÌa¿´help£(C)

°²×°
ΪÁËʹÓÃphpdgb£¬ÄaÊ×ÏÈÐeÒªÏÂÔØÒ»¸ophpµÄÔ´Âe°u¡£È»ºoÏÂÔØphpdgbµÄÔ´Âe°u£¬²¢*ÅÔÚphpÔ´Âe°uµÄsapiĿ¼Ï¡£×iºo£¬Äa¾Í¿ÉÒÔÖ´ÐÐÃuÁi°²×°ÁË¡£±aÒe°²×°Ê¾ÀýÈçÏ£º

¼ÙÉeÎÒÃÇÒѾ­ÏÂÔØphpµÄÔ´Âe°u£¬²¢*ÅÔÚÁË/home/phpĿ¼Ï¡£


    #cd /home/php/sapi
    #git clone https://github.com/krakjoe/phpdbg
    #cd ../
    #./buildconf --force
    #./config.nice
    #make -j8
    #make install-phpdbg

×¢Òa£º

1¡¢Èç¹uÄaµÄphp°ae±¾ÊÇphp5.6»oÕ߸u¸ßµÄ°ae±¾£¬phpdbgÒѾ­¼¯³ÉÔÚphpµÄ´uÂe°uÖУ¬ÎÞÐeµ¥¶ÀÏÂÔØÁË¡£

2¡¢±aÒe²ÎÊýÖмǵÃÒª¼Ó ¨Cenable-phpdbg¡£

3¡¢±aÒeʱ²ÎÊý£¬¨Cwith-readline ¿ÉÒÔÑ¡ÔñÐÔÌi¼Ó¡£Èç¹u²»Ìi¼Ó£¬phpdbgµÄhistoryµÈ¹¦ÄÜÎÞ*¨Ê¹Óá£

»u±¾Ê¹ÓÃ
1¡¢²ÎÊý½eÉÜ
phpdbgÊÇphpµÄÒ»¸osapi£¬Ëu¿ÉÒÔÒÔÃuÁiÐеÄ*½Ê½µ÷ÊÔphp¡£³£ÓòÎÊýÈçÏ£º

The following switches are implemented (just like cli SAPI):

-n ignore php ini

-c search for php ini in path

-z load zend extension

-d define php ini entry

The following switches change the default behaviour of phpdbg:

-v disables quietness

-s enabled stepping

-e sets execution context

-b boring ¨C disables use of colour on the console

-I ignore .phpdbginit (default init file)

-i override .phpgdbinit location (implies -I)

-O set oplog output file

-q do not print banner on startup

-r jump straight to run

-E enable step through eval()

Note: passing -rr will cause phpdbg to quit after execution, rather than returning to the console

2¡¢³£Óù¦ÄÜ
֮ǰÎÒÃǽeÉܹýgdb¹¤¾ß¡£ÆaʵphpdbgºÍgdb¹¦ÄÜÓÐÐ(C)µØ½Ç³£ÏaËÆ¡£È磬¿ÉÒÔÉeÖöϵa£¬¿ÉÒÔµ¥²½Ö´ÐУ¬µÈ¡£Ö»ÊÇËuÃǵ÷ÊÔµÄÓiÑÔ²»Ò»Ñu£¬gdb²aÖØÓÚµ÷ÊÔc»oÕßc++ÓiÑÔ£¬¶øphpdbg²aÖØÓÚµ÷ÊÔphpÓiÑÔ¡£ÏÂÃaeÎÒÃǽ«¶ÔphpdbgµÄÒ»Ð(C)³£Óõ÷ÊÔ¹¦ÄÜ×oϽeÉÜ¡£Òªµ÷ÊԵĴuÂeÈçÏ£º

Îļþtest_phpdbg_inc.phpÔ´´uÂeÈçÏ£º


    <?php 
    function phpdbg_inc_func()
    {   
      echo "phpdbg_inc_func \n"; 
    } 
    ?>

Îļþtest_phpdgb.phpµÄÔ´´uÂeÈçÏ£º


    <?php 
      include(dirname(__FILE__)."/test_phpdbg_inc.php"); 
      class demo{   
        public function __construct(){
           echo __METHOD__.":".__LINE__."\n";   
        }
        public function func($param){
           $param++;
           echo "method func $param\n";
        }
        public function __destruct(){
           echo __METHOD__.":".__LINE__."\n";
        }
      } 

     function func(){   
       $param = "ali";
       $param = $param + "baba";
       echo "function func $param\n";
     }

     $demo = new demo();
     $demo->func(1);
     func();
     phpdbg_inc_func();
    ?>

3¡¢Æo¶¯phpdbg

phpdbg°²×°³É¹¦ºo£¬»aÔÚ°²×°Ä¿Â¼µÄbinĿ¼Ï¡£½øÈebinĿ¼£¬Ö±½ÓÊaÈephpdbg¼´¿É¡£ÈçÏ£º


    #phpdeg
    [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>]
    prompt>

ÒªÏe¼ÓÔØÒªµ÷ÊÔµÄphp½Å±¾£¬Ö»ÐeÒªÖ´ÐÐexecÃuÁi¼´¿É¡£ÈçÏ£º


    #phpdbg
    ......
    prompt> exec ./test_phpdbg.php

µ±È»ÎÒÃÇÒ²¿ÉÒÔÔÚÆo¶¯phpdbgµÄʱºo£¬Ö¸¶¨e²ÎÊý¡£ÈçÏ£º


    #phpdbg -e ./test_phpdbg.php

4¡¢²e¿´°iÖuÐÅÏ¢

Èç¹uÄa֮ǰʹÓùýÆaËuµÄµ÷ÊÔ¹¤¾ß£¬Äa»a*¢ÏÖphpdbgºÍËuÃDZȽÏÏaËÆ¡£µ«ÊÇ£¬ÄaʹÓóoÆÚ£¬»¹ÊÇ»a¾­³£ÐeÒª»ñÈ¡°iÖuÐÅÏ¢¡£Í¨¹ýhelpÃuÁiÎÒÃÇ¿ÉÒÔ»ñÈ¡°iÖuÐÅÏ¢¡£


    ......
    prompt> 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
    ......

5¡¢ÉeÖöϵa

ÉeÖöϵaµÄÃuÁiºÍgdbÒ»Ñu¡£¶¼ÊÇbreak£¬¼oдÐÎʽΪb¡£²»¹ý¾ßÌaµÄÃuÁi²ÎÊý»¹ÊÇÓÐËu²iÒiµÄ¡£ºÍgdbµÄ¶ÏµaÃuÁiÏa֮ͬ´¦£¬ËuÃǶ¼¿ÉÒÔ¡°°´ÎļþÃu:Ðкš± »oÕß ÐкŵĽʽÉeÖöϵa¡£³ý´ËÖ®Ía£¬phpdbg»¹Ìa¹(C)ÁËÒ»Ð(C)Õe¶ÔphpÌØÓеÄÉeÖöϵaµÄ½Ê½¡£È磬¸u¾ÝoplineÉeÖöϵa£¬¸u¾ÝopcodeÉeÖöϵaµÈ¡£

ÖÚËuÖÜÖª£¬php´uÂe×iÖÕÊǽaÎo³Éopcode£¬È»ºoÓÉphpÄÚºËÒ»ÌoÌoÖ´ÐС£Ò»ÌophpÓi¾a£¬¿ÉÄÜ»a½aÎo³É¶aÌoopcode¡£Èç¹u¿ÉÒÔ°´opcodeÉeÖöϵa£¬ÎÒÃǾͿÉÒÔ¸u¾«È*µÄ¸u×Ù³ÌÐoÖ´Ðйý³Ì¡£ÏÂÃaeÎÒÃÇÀ´¿´¿´phapdbgÉeÖöϵaµÄ¾ßÌaʾÀý¡£

°´oplineÉeÖöϵa£º

ÕaÀiËu˵µÄopline£¬¾ÍÊÇÒÔ½¨Èe¿Ú×÷ΪÆðµa£¬µ±Ç°´uÂeµÄÐкš£Èçtest_phpdgb.phpÎļþÖУ¬µÚ18ÐеĴuÂe¡°$param = $param + ¡°baba¡±;¡±µÄopline¾ÍÊÇ 2¡£


    ......
    prompt> b func#2
    prompt> r
    demo::__construct:5
    method func 2
    [Breakpoint #0 resolved at func#2 (opline 0x7f5b230a2e38)]
    [Breakpoint #0 resolved at func#2 (opline 0x7f5b230a2e38)]
    [Breakpoint #0 resolved at func#2 (opline 0x7f5b230a2e38)]
    [Breakpoint #0 in func()#2 at ./test_phpdbg.php:18, hits: 1]
    >00018:   $param = $param + "baba";
     00019:   echo "function func $param\n";;
     00020: }
    ......

6¡¢²e¿´¶Ïµa

ºÍgdbÒ»Ñu£¬phpdbgÒ²ÊÇʹÓÃinfo breakÃuÁi²e¿´¶Ïµa¡£Ê¾ÀýÈçÏ£º


    ....
    prompt> info break
    ------------------------------------------------
    File Breakpoints:
    #1   /home/hailong.xhl/test_phpdbg.php:10
    ------------------------------------------------
    Opline Breakpoints:
    #0   7ff3219e1df0    (function breakpoint)
    ------------------------------------------------
    Function opline Breakpoints:
    #0   func opline 2
    ....

ͨ¹ýÉÏÃaeµÄÏÔʾ£¬ÎÒÃÇ¿ÉÒÔÖªµÀ¡£info breakµÄÏÔʾ½a¹uÖлa°Ñ¶ÏµaµÄÀaÐÍÒ²¸øÏÔʾ³oÀ´¡£#ºoÃaeµÄÊý×ÖÊǶϵaºÅ¡£ÎÒÃÇ¿ÉÒÔ¸u¾Ý¶ÏµaºÅɾ³ý¶Ïµa¡£

7¡¢É¾³ý¶Ïµa

ºÍgdbÃuÁi²»Ò»Ñu¡£phpdbgµÄɾ³ý¶Ïµa²»ÊÇdeleteÃuÁi£¬¶øÊÇbreak del ÃuÁi¡£Ê¾ÀýÈçÏ£º


    ......
    prompt> break del 1
    [Deleted breakpoint #1]
    prompt>
    ......

break del ºoÃaeµÄÊý×Ö1¾ÍÊǶϵaºÅ¡£

8¡¢²e¿´´uÂe

phpdbg²e¿´´uÂeµÄÃuÁiÒ²ÊÇlist¡£µ«ÊǺÍgdbÏa±È£¬Ê¹ÓõÄ*½Ê½¸u¶aÑuÒ»Ð(C)¡£

ÏÔʾָ¶¨º¯ÊýµÄ´uÂe£º


    ......
    prompt> l f func
     00017:   $param = "ali";
     00018:   $param = $param + "baba";
     00019:   echo "function func $param\n";;
     00020: }
     00021:
    prompt>
    ......

µ¥²½Ö´ÐÐ

phpdbgµÄµ¥²½Ö´ÐÐÖ»ÓÐÒ»¸oÃuÁi step¡£ºÍgdbµÄstepÃuÁi²i²»¶a¡£¶¼ÊÇÒ»ÐÐÒ»ÐеÄÖ´ÐдuÂe¡£×¢Òa£¬phpdbgÊÇûÓÐnextÃuÁiµÄ¡£


    ....
    prompt> s
    [Breakpoint #0 resolved at func#2 (opline 0x152ba40)]
    [L19      0x152ba70 ZEND_ADD_STRING     C2   @0  ./test_phpdbg.php]
    >00019:   echo "function func $param\n";;
     00020: }
     00021:
    ....

¼ÌÐøÖ´ÐÐ

ºÍgdbÒ»Ñu£¬phpdbgµÄ¼ÌÐøÖ´ÐÐÃuÁiÒ²ÊÇcontinue£¬¼oдÐÎʽΪc¡£

Ö´ÐÐphp´uÂe

Õa¸oÊÇphpdbgµÄÒ»¸oÌØÉ«¡£¿ÉÒÔÔÚµ÷ÊԵĹý³ÌÖÐʹÓÃevÃuÁiÖ´ÐÐÈÎÒaµÄphp´uÂe¡£È磺


    ......
    prompt> ev $var = "val";
    val
    prompt> ev var_dump($var);
    string(3) "val"
    ......

¿ÉÒÔͨ¹ýÕaÖÖ*½Ê½£¬ÔÚµ÷ÊÔ¹ý³ÌÖж¯Ì¬µÄÐ޸ıaÁ¿Öµ£¬²e¿´Ö´ÐÐЧ¹u¡£

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬ÇaËÉÍaeתµ÷ÊÔÀuÆ÷PHPDBG£¬Ï£Íu´o¼Òϲ»¶¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例