來自:http://www.codefrom.com/paper/%E4%BD%BF%E7%94%A8gdb%E8%B0%83%E8%AF%95p...
php的擴充使用c/c++開發,可以很容易的使用gdb進行調試。具體步驟如下:
首先編譯php的時候需要加上** --enable-debug**參數
./configure --enable-debugmake && make install
在我的ubuntu機器上面測試,擴充的目錄預設為 /usr/local/lib/php/extensions/debug-non-zts-20131226/
這樣進行php的源碼調試也很方便。
下一步進行擴充建立,進入php源碼的ext目錄,運行
./ext_skel --extname=mydebug
目前的目錄下會自動產生mydebug目錄,然後進入該目錄,編輯config.m4檔案,去掉10~12行的dnl,如下
PHP_ARG_WITH(mydebug, for mydebug support,Make sure that the comment is aligned:[ --with-mydebug Include mydebug support])
在最後一行添加
if test -z "$PHP_DEBUG"; then AC_ARG_ENABLE(debug, [--enable-debg compile with debugging system], [PHP_DEBUG=$enableval], [PHP_DEBUG=no] )fi
這樣就表示該擴充能夠進行調試了,然後編譯該擴充,使用命令
phpize ./configure --enable-debugmake && make install
這裡的 phpize 和 php-config 需要事先配置好環境變數,然後載入該擴充。在我的機器上面地址為 /usr/local/lib/php/extensions/debug-non-zts-20131226/。進入mydebug擴充源碼目錄,預設產生的函數為 confirm_mydebug_compiled,定義在 mydebug.c,擴充自動產生的函數。
PHP_FUNCTION(confirm_mydebug_compiled){ char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "mydebug", arg); RETURN_STRINGL(strg, len, 0);}
大概意思就是擷取字串參數,然後拼成一句字串返回。通過nm命令查看產生的mydebug.so匯出的符號。
運行 nm mydebug.so返回 zif_confirm_mydebug_compiled ……
PHP_FUNCTION 實際就是在函數名前面添加 zif_,然後進行gdb調試
第一步運行: gdb php然後運行: break zif_confirm_mydebug_compiled終端提示:Function "zif_confirm_mydebug_compiled" not defined.Make breakpoint pending on future shared library load? (y or [n]) 輸入: y輸入: run /tmp/test.php此時會回顯:Breakpoint 1, zif_confirm_mydebug_compiled (ht=1, return_value=0xb7bf0d44, return_value_ptr=0xb7bd6104, this_ptr=0x0, return_value_used=1) at /...../php-5.6.6/ext/mydebug/mydebug.c:56然後輸入: l顯示:54 PHP_FUNCTION(confirm_mydebug_compiled)55 {56 char *arg = NULL;57 int arg_len, len;58 char *strg;5960 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
其中檔案/tmp/test.php的內容為:
可以看到,函數原始碼已經出來了,可以使用常用的gdb命令進行調試了。
更多精彩原創內容進入http://www.codefrom.com/