《PHP教程:PHP調試的強悍利器之PHPDBG》要點:
本文介紹了PHP教程:PHP調試的強悍利器之PHPDBG,希望對您有用。如果有疑問,可以聯系我們。
PHPDBG是一個PHP的SAPI模塊,可以在不用修改代碼和不影響性能的情況下控制PHP的運行環境.PHP實戰
PHPDBG的目標是成為一個輕量級、強大、易用的PHP調試平臺.可以在PHP5.4和之上版本中使用.在php5.6和之上版本將內部集成.PHP實戰
主要功能:
PHP實戰
C 單步調試PHP實戰
C 靈活的下斷點方式(類辦法、函數、文件:行、內存地址、opcode)PHP實戰
C 可直接調用php的evalPHP實戰
C 可以查看當前執行的代碼PHP實戰
C 用戶空間API(userland/user space)PHP實戰
C 便利集成PHP實戰
C 支持指定php配置文件PHP實戰
C JIT全局變量PHP實戰
C readline支持(可選),終端操作更便利PHP實戰
C 遠程debug,使用java GUIPHP實戰
C 操作簡便(具體看help)PHP實戰
安裝
為了使用phpdgb,你首先必要下載一個php的源碼包.然后下載phpdgb的源碼包,并放在php源碼包的sapi目錄下.最后,你就可以執行命令安裝了.編譯安裝示例如下:PHP實戰
假設我們已經下載php的源碼包,并放在了/home/php目錄下.PHP實戰
#cd /home/php/sapi #git clone https://github.com/krakjoe/phpdbg #cd ../ #./buildconf --force #./config.nice #make -j8 #make install-phpdbg
注意:PHP實戰
1、如果你的php版本是php5.6或者更高的版本,phpdbg已經集成在php的代碼包中,無需單獨下載了.PHP實戰
2、編譯參數中記得要加 Cenable-phpdbg.PHP實戰
3、編譯時參數,Cwith-readline 可以選擇性添加.如果不添加,phpdbg的history等功能無法使用.PHP實戰
基本使用
1、參數介紹
phpdbg是php的一個sapi,它可以以命令行的方式調試php.常用參數如下:PHP實戰
The following switches are implemented (just like cli SAPI):PHP實戰
-n ignore php iniPHP實戰
-c search for php ini in pathPHP實戰
-z load zend extensionPHP實戰
-d define php ini entryPHP實戰
The following switches change the default behaviour of phpdbg:PHP實戰
-v disables quietnessPHP實戰
-s enabled steppingPHP實戰
-e sets execution contextPHP實戰
-b boring C disables use of colour on the consolePHP實戰
-I ignore .phpdbginit (default init file)PHP實戰
-i override .phpgdbinit location (implies -I)PHP實戰
-O set oplog output filePHP實戰
-q do not print banner on startupPHP實戰
-r jump straight to runPHP實戰
-E enable step through eval()PHP實戰
Note: passing -rr will cause phpdbg to quit after execution, rather than returning to the consolePHP實戰
2、常用功能
之前我們介紹過gdb工具.其實phpdbg和gdb功能有些地方非常相似.如,可以設置斷點,可以單步執行,等.只是他們調試的語言不一樣,gdb側重于調試c或者c++語言,而phpdbg側重于調試php語言.下面我們將對phpdbg的一些常用調試功能做下介紹.要調試的代碼如下:PHP實戰
文件test_phpdbg_inc.php源代碼如下:PHP實戰
<?php function phpdbg_inc_func() { echo "phpdbg_inc_func \n"; } ?>
文件test_phpdgb.php的源代碼如下:PHP實戰
<?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、啟動phpdbgPHP實戰
phpdbg安裝成功后,會在安裝目錄的bin目錄下.進入bin目錄,直接輸入phpdbg即可.如下:PHP實戰
#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>
要想加載要調試的php腳本,只必要執行exec命令即可.如下:PHP實戰
#phpdbg ...... prompt> exec ./test_phpdbg.php
當然我們也可以在啟動phpdbg的時候,指定e參數.如下:PHP實戰
#phpdbg -e ./test_phpdbg.php
4、查看贊助信息PHP實戰
如果你之前使用過其他的調試工具,你會發現phpdbg和他們比較相似.但是,你使用初期,還是會經常需要獲取贊助信息.通過help命令我們可以獲取贊助信息.PHP實戰
...... 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、設置斷點PHP實戰
設置斷點的命令和gdb一樣.都是break,簡寫形式為b.不過具體的命令參數還是有所差異的.和gdb的斷點命令相同之處,它們都可以“按文件名:行號” 或者 行號的方式設置斷點.除此之外,phpdbg還提供了一些針對php特有的設置斷點的方式.如,根據opline設置斷點,根據opcode設置斷點等.PHP實戰
眾所周知,php代碼最終是解析成opcode,然后由php內核一條條執行.一條php語句,可能會解析成多條opcode.如果可以按opcode設置斷點,我們就可以更精確的跟蹤程序執行過程.下面我們來看看phapdbg設置斷點的具體示例.PHP實戰
按opline設置斷點:PHP實戰
這里所說的opline,就是以辦法入口作為起點,當前代碼的行號.如test_phpdgb.php文件中,第18行的代碼“$param = $param + “baba”;”的opline就是 2.PHP實戰
...... 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、查看斷點PHP實戰
和gdb一樣,phpdbg也是使用info break命令查看斷點.示例如下:PHP實戰
.... 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 ....
通過上面的顯示,我們可以知道.info break的顯示結果中會把斷點的類型也給顯示出來.#后面的數字是斷點號.我們可以根據斷點號刪除斷點.PHP實戰
7、刪除斷點PHP實戰
和gdb命令不一樣.phpdbg的刪除斷點不是delete命令,而是break del 命令.示例如下:PHP實戰
...... prompt> break del 1 [Deleted breakpoint #1] prompt> ......
break del 后面的數字1便是斷點號.PHP實戰
8、查看代碼PHP實戰
phpdbg查看代碼的命令也是list.但是和gdb相比,使用的方式更多樣一些.PHP實戰
顯示指定函數的代碼:PHP實戰
...... prompt> l f func 00017: $param = "ali"; 00018: $param = $param + "baba"; 00019: echo "function func $param\n";; 00020: } 00021: prompt> ......
單步執行PHP實戰
phpdbg的單步執行只有一個命令 step.和gdb的step命令差不多.都是一行一行的執行代碼.注意,phpdbg是沒有next命令的.PHP實戰
.... 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: ....
繼續執行PHP實戰
和gdb一樣,phpdbg的繼續執行命令也是continue,簡寫形式為c.PHP實戰
執行php代碼PHP實戰
這個是phpdbg的一個特色.可以在調試的過程中使用ev命令執行任意的php代碼.如:PHP實戰
...... prompt> ev $var = "val"; val prompt> ev var_dump($var); string(3) "val" ......
可以通過這種方式,在調試過程中動態的修改變量值,查看執行效果.PHP實戰
以上便是本文的全部內容,輕松玩轉調試利器PHPDBG,希望大家喜歡.PHP實戰
歡迎參與《PHP教程:PHP調試的強悍利器之PHPDBG》討論,分享您的想法,維易PHP學院為您提供專業教程。