使用GDB調(diào)試PHP代碼,解決PHP代碼死循環(huán)問題_PHP教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:Laravel5中contracts詳解
      在Laravel5中出現(xiàn)了一個(gè)新的東西,叫做contracts,那么它到底是什么?有什么用?怎么用?我們就來探討下吧。 我們先來看看官方文檔中對contracts的定義: Laravel's Contracts are a set of interfaces that define the core services provided by the framework. 意思

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

      下面通過一段代碼展示如何解決PHP死循環(huá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);
      }

       

      通過ps aux得到進(jìn)程ID和狀態(tài)如下,使用gdb -p 進(jìn)程ptrace跟蹤,通過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工具對此進(jìn)程進(jìn)行調(diào)試。另外此進(jìn)程會(huì)中斷執(zhí)行。gdb輸入c后,程序繼續(xù)向下運(yùn)行。然后再次按下ctrl + c中斷程序。 通過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)槌绦蛟诓煌奈恢弥袛。看到execute (oparray=0x21d58b0) 這一行,這里就是PHP執(zhí)行oparray的入口了。gdb下輸入f 6,(通過調(diào)用棧編號(hào)可得)。

      分享:php打印一個(gè)邊長為N的實(shí)心和空心菱型的方法
      這篇文章主要介紹了php打印一個(gè)邊長為N的實(shí)心和空心菱型的方法,實(shí)例分析了php循環(huán)語句繪制圖形的技巧,需要的朋友可以參考下 本文實(shí)例講述了php打印一個(gè)邊長為N的實(shí)心和空心菱型的方法。分享給大家供大家參考。具體分析如下: 實(shí)心菱型計(jì)算方法: $n:邊長 $i:當(dāng)前行,0

      共2頁上一頁12下一頁
      來源:模板無憂//所屬分類:PHP教程/更新時(shí)間:2015-03-03
      相關(guān)PHP教程