調試查看PHP Core的調用棧

PHP gdb.php 代碼如下:

<?php
 
class Test {
}
 
function a($i) {
    b(new Test, 2.3432, "reader");
}
function b($i) {
    c(array(1,2,3));
}
function c($i) {
    d(TRUE);
}
function d($i) {
    $fp = fopen("/tmp/1.php", "r");
    e($fp);
}
 
function e($i) {
    sleep(1000);
}
 
a(2);

使用php cli模式運行腳本

/usr/local/php-7.3.4/bin/php gdb.php

查看php命令對應的進程pid

ps -ef | grep gdb
root       148    41  0 14:51 pts/2    00:00:00 /usr/local/php-7.3.4/bin/php gdb.php
root       150    19  0 14:51 pts/1    00:00:00 grep --color=auto gdb

然後使用gdb調試php進程

gdb -p 148

然後進去調試調用棧,還可以查看array和object數據

(gdb) source /tmp/php-7.3.4/.gdbinit
(gdb) zbacktrace
[0x7f5f83c1d340] sleep(1000) [internal function]
[0x7f5f83c1d2d0] e(false) /data/www/test/gdb.php:20
[0x7f5f83c1d230] d(true) /data/www/test/gdb.php:16
[0x7f5f83c1d1c0] c(array(3)[0x7f5f83c1d210]) /data/www/test/gdb.php:12
[0x7f5f83c1d130] b(object[0x7f5f83c1d180], , 2.343200) /data/www/test/gdb.php:9
[0x7f5f83c1d0a0] a(2) /data/www/test/gdb.php:6
[0x7f5f83c1d030] (main) /data/www/test/gdb.php:23
(gdb)
(gdb) print ((zval *)0x7f5f83c1d210)
$1 = (zval *) 0x7f5f83c1d210
(gdb) printzv $1
[0x7f5f83c1d210] (refcount=2) array:     Packed(3)[0x7f5f83c5f1f8]: {
      [0] 0 => [0x7f5f83c66788] long: 1
      [1] 1 => [0x7f5f83c667a8] long: 2
      [2] 2 => [0x7f5f83c667c8] long: 3
}
(gdb)
(gdb) print ((zval *)0x7f5f83c1d180)
$2 = (zval *) 0x7f5f83c1d180
(gdb) printzv $2
[0x7f5f83c1d180] (refcount=1) object(Test) #1
Properties     Hash(0)[0x7f5f83c0d090]: {
}

使用PHP7.0.31版本調試,會遇到如下問題:

php-src-php-7.0.31/ php-two-psr/
(gdb) source /data/www/php-src-php-7.0.31/.gdbinit
(gdb) zbacktrace
No symbol "basic_functions_module" in current context.
(gdb)

更換爲php7.3才能正常查看;

轉載地址:http://www.laruence.com/2011/12/06/3206.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章