使用 gdb 調試 PHP 擴充

來源:互聯網
上載者:User

來自: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/

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.