標籤:
以前, 我曾經介紹過如何通過PHP的Core檔案擷取資訊:如何調試PHP的Core之擷取基本資料, 對於調用參數這塊, 當時介紹的擷取方法比較複雜.
於是今天我為PHP 5.4的.gdbinit做了一個改進, 以後如果你遇到了PHP 5.4的core, 那麼就可以簡單的得到PHP 5.4發生Core時, 包括參數的函數調用棧的資訊.
假設對於如下的指令碼:
- <?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();
使用後台運行以後, PHP5.4會sleep在e函數的sleep中, 這時, 如果我們使用gdb attach上去,
- gdb --pid= xxx //使用ps獲得後台運行指令碼的pid
然後, source PHP原始碼下面的.gdbinit:
- (gdb) source php54-src/.gdbini
然後, 讓我們嘗試調用下zbacktrace, 看看什麼結果:
- (gdb) zbacktrace
- [0x2a95dac5e0] sleep(1000) /tmp/1.php:21
- [0x2a95dac4c0] e(resource(#5)) /tmp/1.php:17
- [0x2a95dac3f0] d(true) /tmp/1.php:13
- [0x2a95dac300] c(array(3)[0x2a95de7db0]) /tmp/1.php:10
- [0x2a95dac1c0] b(object[0x2a95de7840], 2.343200, "reader") /tmp/1.php:7
- [0x2a95dac0e8] a() /tmp/1.php:2
恩, 對於array和object, 因為我們為了保持不要亂屏, 所以沒有展開, 不過, 如果我們要查看這個array具體是什麼元素, 可以這樣做, 注意到上面的:array(3)[0x2a95de7db0]:
- (gdb) print ((zval *)0x2a95de7db0)
- $4 = (struct _zval_struct *) 0x2a95de7db0
- (gdb) printzv $4
- [0x2a95de7db0] (refcount=2) array(3): {
- 0 => [0x2a95de79d0] (refcount=1) long: 1
- 1 => [0x2a95de7b80] (refcount=1) long: 2
- 2 => [0x2a95de7c98] (refcount=1) long: 3
-
類似的, 對於object, 注意到上面的: object[0x2a95de7840]
- (gdb) print ((zval *)0x2a95de7840)
- $5 = (struct _zval_struct *) 0x2a95de7840
- (gdb) printzv $5
- [0x2a95de7840] (refcount=2) object
- (Test) #1"no properties found
要注意的一點是, 對於object, 如果你是在調式Core檔案, 而不是attach到一個啟動並執行進程上, 那麼上面的嘗試會得到一個錯誤:
- (gdb) printzv $5
- [0x2a95de7840] (refcount=2) objectYou can‘t do that without a process to debug
不過, 即使這樣, 我們還是有辦法, 只不過就比較麻煩了.在NTS下面:
- (gdb) p ((zval *)0x2a95de7840)->value.obj.handle
- $6 = 1
- //注意, 下面用到了這個$6的值:1
- (gdb) p (zend_object*) executor_globals->objects_store.object_buckets[1].bucket.obj.object
- $7 = (struct _zend_object *) 0x2a95de3ec0
- (gdb) p $9->ce->name
- $8 = 0x2a95e200b0 "Test
呵呵, 怎麼樣, 有了這些資訊, 分析Core的原因, 是不是就更簡單了呢? enjoy~
最後, 還是要提醒下: PHP 5.4還處於開發階段, 在最終release之前, 任何新特性都可能被調整或者更改. 如果大家有任何建議, 也歡迎反饋, 協助我們使得PHP變得更好
重現PHP Core的調用棧