下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、用法GDB调试PHP代码,解决PHP代码死循环问题_ 最近在帮同事解决Swoole Server问题时,发觉有1个worker进程始终处于R的状态,而且CPU耗时特别高。初步断定是PHP代码中发生死循环。 下面通过一段代码展现如何解决PHP死循环问题。 代码如下: #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); $ind
2、ex = 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); 通过ps aux得到进程ID和状态如下,用法gdb -p 进程ptrace跟踪,通过bt指令得到调用栈 代码如下: htf 3834 2.6 0.2 166676 22060 pts/12 R+ 10:50 0:12 php dead_loop.php gdb -p 383
3、4 (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)
4、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_exec
5、ute_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/
6、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
7、_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_dto
8、r_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/Z
9、end/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_SPE
10、C_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_c
11、ount=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
12、:989 #14 0x00000000009a4491 in main (argc=2, argv=0x7ffffe004d18) at /home/htf/workspace/php-5.4.27/sapi/cli/php_cli.c:1365 执行gdb后,死循环的进程会变成T的状态,表示正在Trace。这个是独占的,所以不能再用法strace/gdb或者其他ptrace工具对此进程进行调试。另外此进程会中断执行。gdb输入c后,程序连续向下运行。然后再次按下ctrl + c中断程序。 通过bt指令查看进程的调用栈。 代码如下: (gdb) bt #0 _zend_mm_alloc_int
13、 (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 0x00000000008ceb
14、86 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
15、 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_
16、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_e
17、xecute.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信息不一样,这是由于程序在不同的位
18、置中断。看到execute (oparray=0x21d58b0) 这一行,这里就是PHP执行oparray的入口了。gdb下输入f 6,(通过调用栈编号可得)。 代码如下: (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
19、 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_con
20、t = 0, try_catch_array = 0x0, last_try_catch = 0, static_variables = 0x0, this_var = 4294967295, filename = 0x7f726424ba38 /home/htf/root/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是哪个PHP文件的。然后输入f 0进入当前位置。 代码如下: (gdb) p *executor_globals.opline_ptr $4 = handler = 0x93ff9c , op1 = constant = 1680133296, var = 1680133296, num = 1680133296, hash = 1401
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 结婚礼服买卖合同范本
- 绿化工人招聘合同范本
- 老式钻机转让合同范本
- 联合成了公司合同范本
- 聘请工地保安合同范本
- 腐殖酸钠购销合同范本
- 苹果手机收购合同范本
- 菌菇大棚搭建合同范本
- 装修公司广告合同范本
- 装修合同逾期补充协议
- 管理经济学:理论与案例 第2版 课件全套 毛蕴诗 第1-14章 企业性质与环境、企业目标 -政府与企业
- 2024单位消防安全评估导则
- 传感器应用技术 课程思政课件 项目一 传感器概述
- 《百年孤独》专用课件
- 将配偶追加为被执行人申请书
- 煤矿井下零星工程管理制度
- 退耕还林工程
- 省纸打印版:法国 纪录片《家园》(Home)中英字幕
- 抗菌药物的合理应用
- GB/T 18380.11-2022电缆和光缆在火焰条件下的燃烧试验第11部分:单根绝缘电线电缆火焰垂直蔓延试验试验装置
- DB4113T 021-2022 南阳艾 艾绒标准
评论
0/150
提交评论