资源描述:
《使用gdb调试php代码,解决php代码死循环问题》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、使用GDB调试PHP代码,解决PHP代码死循环问题最近在帮同事解决SwooleServer问题时,发现有1个worker进程一直处于R的状态,而且CPU耗时非常高。初步断定是PHP代码中发生死循环。下面通过一段代码展示如何解决PHP死循环问题。代码如下:#dead_loop・php$array=array();for($i=0;$ilt:10000;$i++){$array[]=$i;}include_DIR_・"/include・php";#include.phpwh订e(1){usleep(lO);$keys=arra
2、y_flip($array);$index=array_search(rand(1500,9999),$array);$str=str_repeat('A',$index);$strb=test($index,$str);functiontest($index,$str)returnstr_replaceCA','B',$str);}通过psaux得到进程ID和状态如下,使用gdb-p进程ptrace跟踪,通过bt命令得到调用栈代码如下:htf38342.60.216667622060pts/12R+10:500:12ph
3、pdeadloop・phpgdb~p3834(gdb)bt#00x00000000008cc03finzend_mm_checkptr(heap=0xleaa2c0,ptr=0x2584910,silent二1,_zend_filename=0xee3d40〃/home/htf/workspace/php-5.4.27/Zend/zendvariables・c"、_zend_lineno二182,_zendorig_filename=0xeel888〃/home/htf/workspace/php-5・4.27/Zend/
4、zend_execute_API._zend_orig_lineno=437)at/home/htf/workspace/php-5.4.27/Zend/zend_alloc・c:1485#10x00000000008cd643inzendmmfreeint(heap=0xleaa2cO,p=0x2584910,_zend_filename=0xee3d40/z/home/htf/workspace/php-5・4.27/Zend/zendvariables.zendlineno=182,zend_orig_filenam
5、e=0xeel888z//home/htf/workspace/php-5.4.27/Zend/zendexecuteAPI.c"、zendorig_lineno=437)at/home/htf/workspace/php-5・4.27/Zend/zendalloc・c:2064#20x00000000008cebf7in_efree(ptr=0x2584910,zendfilename=0xee3d40/z/home/htf/workspace/php-5・4.27/Zend/zendvariables.zendline
6、no二182,zendMMorig_filename=0xeel888z//home/htf/workspace/php-5.4.27/Zend/zend_execute_API.c"、zendorig_linenoM37)at/home/htf/workspace/php-5・4.27/Zend/zendalloc・c:2436#30x00000000008edaOain_zval_ptr_dtor(zval_ptr=0x25849a0,_zend_filename=0xee3d407home/htf/workspace
7、/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#40x00000000008fe687in_zval_ptr_dtor_wrapper(zval_ptr=0x25849a0)at/home/htf/workspace/php-5.4.27/Zend/zend_variables・c:182#50x000000000091259finzendhash
8、destroy(ht=0x7f7263f6e380)at/home/htf/workspace/php-5.4.27/Zend/zendhash.c:560#60X00000000008fe2c5in_zval_dtor_func(zvalue=0x7f726426fe50,_zend_filename