使用GDB調(diào)試PHP代碼,解決PHP代碼死循環(huán)問(wèn)題
來(lái)源:易賢網(wǎng) 閱讀:941 次 日期:2015-03-04 10:36:56
溫馨提示:易賢網(wǎng)小編為您整理了“使用GDB調(diào)試PHP代碼,解決PHP代碼死循環(huán)問(wèn)題”,方便廣大網(wǎng)友查閱!

最近在幫同事解決Swoole Server問(wèn)題時(shí),發(fā)現(xiàn)有1個(gè)worker進(jìn)程一直處于R的狀態(tài),而且CPU耗時(shí)非常高。初步斷定是PHP代碼中發(fā)生死循環(huán)。

下面通過(guò)一段代碼展示如何解決PHP死循環(huán)問(wèn)題。

代碼如下:

#dead_loop.php

$array = array();

for($i = 0; $i < 10000; $i++)

{

$array[] = $i;

}

include __DIR__."/include.php";

#include.php

while(1)

{

usleep(10);

$keys = array_flip($array);

$index = array_search(rand(1500, 9999), $array);

$str = str_repeat('A', $index);

$strb = test($index, $str);

}

function test($index, $str)

{

return str_replace('A', 'B', $str);

}

通過(guò)ps aux得到進(jìn)程ID和狀態(tài)如下,使用gdb -p 進(jìn)程ptrace跟蹤,通過(guò)bt命令得到調(diào)用棧

代碼如下:

htf 3834 2.6 0.2 166676 22060 pts/12 R+ 10:50 0:12 php dead_loop.php

gdb -p 3834

(gdb) bt

#0 0x00000000008cc03f in zend_mm_check_ptr (heap=0x1eaa2c0, ptr=0x2584910, silent=1, __zend_filename=0xee3d40 "/home/htf/workspace/php-5.4.27/Zend/zend_variables.c",

__zend_lineno=182, __zend_orig_filename=0xee1888 "/home/htf/workspace/php-5.4.27/Zend/zend_execute_API.c", __zend_orig_lineno=437)

at /home/htf/workspace/php-5.4.27/Zend/zend_alloc.c:1485

#1 0x00000000008cd643 in _zend_mm_free_int (heap=0x1eaa2c0, p=0x2584910, __zend_filename=0xee3d40 "/home/htf/workspace/php-5.4.27/Zend/zend_variables.c", __zend_lineno=182,

__zend_orig_filename=0xee1888 "/home/htf/workspace/php-5.4.27/Zend/zend_execute_API.c", __zend_orig_lineno=437) at /home/htf/workspace/php-5.4.27/Zend/zend_alloc.c:2064

#2 0x00000000008cebf7 in _efree (ptr=0x2584910, __zend_filename=0xee3d40 "/home/htf/workspace/php-5.4.27/Zend/zend_variables.c", __zend_lineno=182,

__zend_orig_filename=0xee1888 "/home/htf/workspace/php-5.4.27/Zend/zend_execute_API.c", __zend_orig_lineno=437) at /home/htf/workspace/php-5.4.27/Zend/zend_alloc.c:2436

#3 0x00000000008eda0a in _zval_ptr_dtor (zval_ptr=0x25849a0, __zend_filename=0xee3d40 "/home/htf/workspace/php-5.4.27/Zend/zend_variables.c", __zend_lineno=182)

at /home/htf/workspace/php-5.4.27/Zend/zend_execute_API.c:437

#4 0x00000000008fe687 in _zval_ptr_dtor_wrapper (zval_ptr=0x25849a0) at /home/htf/workspace/php-5.4.27/Zend/zend_variables.c:182

#5 0x000000000091259f in zend_hash_destroy (ht=0x7f7263f6e380) at /home/htf/workspace/php-5.4.27/Zend/zend_hash.c:560

#6 0x00000000008fe2c5 in _zval_dtor_func (zvalue=0x7f726426fe50, __zend_filename=0xeea290 "/home/htf/workspace/php-5.4.27/Zend/zend_execute.c", __zend_lineno=901)

at /home/htf/workspace/php-5.4.27/Zend/zend_variables.c:45

#7 0x0000000000936656 in _zval_dtor (zvalue=0x7f726426fe50, __zend_filename=0xeea290 "/home/htf/workspace/php-5.4.27/Zend/zend_execute.c", __zend_lineno=901)

at /home/htf/workspace/php-5.4.27/Zend/zend_variables.h:35

#8 0x0000000000939747 in zend_assign_to_variable (variable_ptr_ptr=0x7f7263f8e738, value=0x7f726426f6a8) at /home/htf/workspace/php-5.4.27/Zend/zend_execute.c:901

#9 0x0000000000997ee5 in ZEND_ASSIGN_SPEC_CV_VAR_HANDLER (execute_data=0x7f726d04b2a8) at /home/htf/workspace/php-5.4.27/Zend/zend_vm_execute.h:33168

#10 0x000000000093b5fd in execute (op_array=0x21d58b0) at /home/htf/workspace/php-5.4.27/Zend/zend_vm_execute.h:410

#11 0x0000000000901692 in zend_execute_scripts (type=8, retval=0x0, file_count=3) at /home/htf/workspace/php-5.4.27/Zend/zend.c:1315

#12 0x000000000087926a in php_execute_script (primary_file=0x7ffffe0038d0) at /home/htf/workspace/php-5.4.27/main/main.c:2502

#13 0x00000000009a32e3 in do_cli (argc=2, argv=0x7ffffe004d18) at /home/htf/workspace/php-5.4.27/sapi/cli/php_cli.c:989

#14 0x00000000009a4491 in main (argc=2, argv=0x7ffffe004d18) at /home/htf/workspace/php-5.4.27/sapi/cli/php_cli.c:1365

執(zhí)行g(shù)db后,死循環(huán)的進(jìn)程會(huì)變成T的狀態(tài),表示正在Trace。這個(gè)是獨(dú)占的,所以不能再使用strace/gdb或者其他ptrace工具對(duì)此進(jìn)程進(jìn)行調(diào)試。另外此進(jìn)程會(huì)中斷執(zhí)行。gdb輸入c后,程序繼續(xù)向下運(yùn)行。然后再次按下ctrl + c中斷程序。 通過(guò)bt命令查看進(jìn)程的調(diào)用棧。

代碼如下:

(gdb) bt

#0 _zend_mm_alloc_int (heap=0x1eaa2c0, size=72, __zend_filename=0xe43410 "/home/htf/workspace/php-5.4.27/ext/standard/array.c", __zend_lineno=2719,

__zend_orig_filename=0xee5a38 "/home/htf/workspace/php-5.4.27/Zend/zend_hash.c", __zend_orig_lineno=412) at /home/htf/workspace/php-5.4.27/Zend/zend_alloc.c:1895

#1 0x00000000008ceb86 in _emalloc (size=72, __zend_filename=0xe43410 "/home/htf/workspace/php-5.4.27/ext/standard/array.c", __zend_lineno=2719,

__zend_orig_filename=0xee5a38 "/home/htf/workspace/php-5.4.27/Zend/zend_hash.c", __zend_orig_lineno=412) at /home/htf/workspace/php-5.4.27/Zend/zend_alloc.c:2425

#2 0x0000000000911d85 in _zend_hash_index_update_or_next_insert (ht=0x2257a10, h=3972, pData=0x7ffffe0012b0, nDataSize=8, pDest=0x0, flag=1,

__zend_filename=0xe43410 "/home/htf/workspace/php-5.4.27/ext/standard/array.c", __zend_lineno=2719) at /home/htf/workspace/php-5.4.27/Zend/zend_hash.c:412

#3 0x00000000007767e1 in zif_array_flip (ht=1, return_value=0x7f726424ea68, return_value_ptr=0x0, this_ptr=0x0, return_value_used=1)

at /home/htf/workspace/php-5.4.27/ext/standard/array.c:2719

#4 0x000000000093c03e in zend_do_fcall_common_helper_SPEC (execute_data=0x7f726d04b2a8) at /home/htf/workspace/php-5.4.27/Zend/zend_vm_execute.h:643

#5 0x00000000009400e6 in ZEND_DO_FCALL_SPEC_CONST_HANDLER (execute_data=0x7f726d04b2a8) at /home/htf/workspace/php-5.4.27/Zend/zend_vm_execute.h:2233

#6 0x000000000093b5fd in execute (op_array=0x21d58b0) at /home/htf/workspace/php-5.4.27/Zend/zend_vm_execute.h:410

兩次的BT信息不一樣,這是因?yàn)槌绦蛟诓煌奈恢弥袛唷?吹絜xecute (oparray=0x21d58b0) 這一行,這里就是PHP執(zhí)行oparray的入口了。gdb下輸入f 6,(通過(guò)調(diào)用棧編號(hào)可得)。

代碼如下:

(gdb) f 6

#6 0x000000000093b5fd in execute (op_array=0x21d58b0) at /home/htf/workspace/php-5.4.27/Zend/zend_vm_execute.h:410

410 if ((ret = OPLINE->handler(execute_data TSRMLS_CC)) > 0) {

(gdb) p *op_array

$2 = {type = 2 '\002', function_name = 0x7f726d086540 "test", scope = 0x0, fn_flags = 134217728, prototype = 0x0, num_args = 2, required_num_args = 2, arg_info = 0x7f726d086bd8,

refcount = 0x7f726d0870f0, opcodes = 0x7f726424d600, last = 8, vars = 0x7f726424e890, last_var = 2, T = 1, brk_cont_array = 0x0, last_brk_cont = 0, try_catch_array = 0x0,

last_try_catch = 0, static_variables = 0x0, this_var = 4294967295, filename = 0x7f726424ba38 "/home/htf/wwwroot/include.php", line_start = 12, line_end = 15, doc_comment = 0x0,

doc_comment_len = 0, early_binding = 4294967295, literals = 0x7f726424eae0, last_literal = 4, run_time_cache = 0x7f726450bfb0, last_cache_slot = 1, reserved = {0x0, 0x0, 0x0, 0x0}}

這里的filename就能看到op_array是哪個(gè)PHP文件的。然后輸入f 0進(jìn)入當(dāng)前位置。

代碼如下:

(gdb) p **executor_globals.opline_ptr

$4 = {handler = 0x93ff9c , op1 = {constant = 1680133296, var = 1680133296, num = 1680133296, hash = 140129283132592, opline_num = 1680133296,

jmp_addr = 0x7f726424ccb0, zv = 0x7f726424ccb0, literal = 0x7f726424ccb0, ptr = 0x7f726424ccb0}, op2 = {constant = 0, var = 0, num = 0, hash = 0, opline_num = 0, jmp_addr = 0x0,

zv = 0x0, literal = 0x0, ptr = 0x0}, result = {constant = 32, var = 32, num = 32, hash = 32, opline_num = 32, jmp_addr = 0x20, zv = 0x20, literal = 0x20, ptr = 0x20},

extended_value = 1, lineno = 5, opcode = 60 '

這里的lineno表示OPCODE所在的代碼行數(shù),可以到對(duì)應(yīng)文件里去看下是哪行代碼。使用GDB可以查看到更多的信息,這里就不再一一介紹了,有興趣各位可以自行嘗試。

zbacktrace的使用

zend官方提供了一個(gè)gdb的腳本,對(duì)指令進(jìn)行了封裝,可以直接看到php函數(shù)的調(diào)用關(guān)系。在php源代碼包的根目錄中有一個(gè).gdbinit。使用

代碼如下:

source your_php_src_path/.gdbinit

zbacktrace

可以直接看到PHP函數(shù)的調(diào)用堆棧。

以上就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)