如何使用GDB调试PHP程序(2)

[root@localhost code]# gdb php GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6) Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i686-redhat-linux-gnu". For bug reporting instructions, please see: <>... Reading symbols from /usr/bin/php...done. (gdb) b zif_sleep Breakpoint 1 at 0x8435180: file /usr/local/src/php-5.5.23/ext/standard/basic_functions.c, line 4449. (gdb) b zif_in_array Breakpoint 2 at 0x8426923: file /usr/local/src/php-5.5.23/ext/standard/array.c, line 1215. (gdb) b zif_print_r Breakpoint 3 at 0x8438273: file /usr/local/src/php-5.5.23/ext/standard/basic_functions.c, line 5553. (gdb) b zif_var_dump Breakpoint 4 at 0x847d296: file /usr/local/src/php-5.5.23/ext/standard/var.c, line 178. (gdb) b zif_printf Function "zif_printf" not defined. Make breakpoint pending on future shared library load? (y or [n]) n (gdb) b zif_sprintf Function "zif_sprintf" not defined. Make breakpoint pending on future shared library load? (y or [n]) n (gdb) b printf Breakpoint 5 at 0x806a390 (gdb) b memcpy Breakpoint 6 at 0x8069390 (gdb) b zif_print Function "zif_print" not defined. Make breakpoint pending on future shared library load? (y or [n]) n (gdb) b zif_echo Function "zif_echo" not defined. Make breakpoint pending on future shared library load? (y or [n]) n (gdb) info b Num Type Disp Enb Address What 1 breakpoint keep y 0x08435180 in zif_sleep at /usr/local/src/php-5.5.23/ext/standard/basic_functions.c:4449 2 breakpoint keep y 0x08426923 in zif_in_array at /usr/local/src/php-5.5.23/ext/standard/array.c:1215 3 breakpoint keep y 0x08438273 in zif_print_r at /usr/local/src/php-5.5.23/ext/standard/basic_functions.c:5553 4 breakpoint keep y 0x0847d296 in zif_var_dump at /usr/local/src/php-5.5.23/ext/standard/var.c:178 5 breakpoint keep y 0x0806a390 <printf@plt> 6 breakpoint keep y 0x08069390 <memcpy@plt> (gdb)

加几个断点测试一下 syntax:break [file_name]:func_name,这里大致可以看一下 echo print等不是函数了

然后开始调试

(gdb) p *return_value $1 = {value = {lval = 1515870810, dval = 1.7838867517321418e+127, str = {val = 0x5a5a5a5a <Address 0x5a5a5a5a out of bounds>, len = 1515870810}, ht = 0x5a5a5a5a, obj = {handle = 1515870810, handlers = 0x5a5a5a5a}}, refcount__gc = 1, type = 0 '\000', is_ref__gc = 0 '\000'} (gdb) p return_value->value $2 = {lval = 1515870810, dval = 1.7838867517321418e+127, str = {val = 0x5a5a5a5a <Address 0x5a5a5a5a out of bounds>, len = 1515870810}, ht = 0x5a5a5a5a, obj = {handle = 1515870810, handlers = 0x5a5a5a5a}} (gdb) p return_value->value->lval $3 = 1515870810

我们还可以使用内置的gdbinit来调试

(gdb) source /usr/local/src/php-5.5.23/.gdbinit (gdb) zbacktrace [0xb7fa1144] sleep(3) /code/kk.php:4

查看当前堆栈,PHP内核的执行过程

(gdb) bt #0 zif_sleep (ht=1, return_value=0xb7fbd6f0, return_value_ptr=0x0, this_ptr=0x0, return_value_used=0) at /usr/local/src/php-5.5.23/ext/standard/basic_functions.c:4449 #1 0x085f6870 in execute_internal (execute_data_ptr=0xb7fa1144, fci=0x0, return_value_used=0) at /usr/local/src/php-5.5.23/Zend/zend_execute.c:1484 #2 0x085aea5f in dtrace_execute_internal (execute_data_ptr=0xb7fa1144, fci=0x0, return_value_used=0) at /usr/local/src/php-5.5.23/Zend/zend_dtrace.c:97 #3 0x00935c33 in pt_execute_core (internal=1, execute_data=0xb7fa1144, fci=0x0, rvu=0) at /usr/local/src/trace-0.3.0/extension/trace.c:941 #4 0x00935e49 in pt_execute_internal (execute_data=0xb7fa1144, fci=0x0, return_value_used=0) at /usr/local/src/trace-0.3.0/extension/trace.c:1005 #5 0x085f7523 in zend_do_fcall_common_helper_SPEC (execute_data=0xb7fa1144) at /usr/local/src/php-5.5.23/Zend/zend_vm_execute.h:552 #6 0x085fb2a9 in ZEND_DO_FCALL_SPEC_CONST_HANDLER (execute_data=0xb7fa1144) at /usr/local/src/php-5.5.23/Zend/zend_vm_execute.h:2332 #7 0x085f6deb in execute_ex (execute_data=0xb7fa1144) at /usr/local/src/php-5.5.23/Zend/zend_vm_execute.h:363 #8 0x085ae9dc in dtrace_execute_ex (execute_data=0xb7fa1144) at /usr/local/src/php-5.5.23/Zend/zend_dtrace.c:73 #9 0x00935c5e in pt_execute_core (internal=0, execute_data=0xb7fa1144, fci=0x0, rvu=0) at /usr/local/src/trace-0.3.0/extension/trace.c:946 #10 0x00935e10 in pt_execute_ex (execute_data=0xb7fa1144) at /usr/local/src/trace-0.3.0/extension/trace.c:1000 #11 0x085f6e4a in zend_execute (op_array=0xb7fbc7b4) at /usr/local/src/php-5.5.23/Zend/zend_vm_execute.h:388 #12 0x085c1cf2 in zend_execute_scripts (type=8, retval=0x0, file_count=3) at /usr/local/src/php-5.5.23/Zend/zend.c:1327 #13 0x085470f9 in php_execute_script (primary_file=0xbffff4a4) at /usr/local/src/php-5.5.23/main/main.c:2525 #14 0x0865af46 in do_cli (argc=2, argv=0x8b9b908) at /usr/local/src/php-5.5.23/sapi/cli/php_cli.c:994 #15 0x0865bff3 in main (argc=2, argv=0x8b9b908) at /usr/local/src/php-5.5.23/sapi/cli/php_cli.c:1378

查看代码段

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/236a00949302db508563bd4cff2b5afe.html