如何使用GDB调试PHP程序
发布时间:2022-07-16 10:55:00 所属栏目:PHP教程 来源:互联网
导读:GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具,如果你是在 UNIX平台下做软件,你会发现GDB这个调试工具有比VC、BCB的图形化调试器更强大的功能,同时GDB也具有例如ddd这样的图形化的调试端。 一般来说,GDB主要完成下面四个方面的功能: (1)启动你
GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具,如果你是在 UNIX平台下做软件,你会发现GDB这个调试工具有比VC、BCB的图形化调试器更强大的功能,同时GDB也具有例如ddd这样的图形化的调试端。 一般来说,GDB主要完成下面四个方面的功能: (1)启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。 (2)可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式) (3)当程序被停住时,可以检查此时你的程序中所发生的事。 (4)动态的改变你程序的执行环境。 1、简介 GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具。如果你是在 UNIX平台下做软件,你会发现GDB这个调试工具有比VC、BCB的图形化调试器更强大的功能。同时GDB也具有例如ddd这样的图形化的调试端。 2、调试C/C++程序 直接上代码了 #include<iostream> using namespace std; long factorial(int n); int main() { int n(0); cin>>n; long val=factorial(n); cout<<val<<endl; cin.get(); return 0; } long factorial(int n) { long result(1); while(n--) { result*=n; } return result; } 编译 g++ k.cpp -g -Wall -Werror -o main 开始调试 [root@localhost code]# gdb ./main 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 <http://gnu.org/licenses/gpl.html> 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: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /code/main...done. (gdb) l warning: Source file is more recent than executable. 1 #include<iostream> 2 using namespace std; 3 long factorial(int n); 4 5 int main() 6 { 7 int n(0); 8 cin>>n; 9 long val=factorial(n); 10 cout<<val<<endl; (gdb) 设置断点 break linenumber (gdb) b 9 Breakpoint 1 at 0x80486f9: file k.cpp, line 9. (gdb) r Starting program: /code/main 4 Breakpoint 1, main () at k.cpp:9 9 long val=factorial(n); 设置观察点 watch var (gdb) s factorial (n=4) at k.cpp:17 17 long result(1); (gdb) l 12 return 0; 13 } 14 15 long factorial(int n) 16 { 17 long result(1); 18 while(n--) 19 { 20 result*=n; 21 } (gdb) watch n Hardware watchpoint 2: n (gdb) watch result Hardware watchpoint 3: result (gdb) c Continuing. Hardware watchpoint 3: result Old value = 0 New value = 1 factorial (n=4) at k.cpp:18 18 while(n--) (gdb) Continuing. Hardware watchpoint 2: n Old value = 4 New value = 3 0x08048764 in factorial (n=3) at k.cpp:18 18 while(n--) (gdb) Continuing. Hardware watchpoint 3: result Old value = 1 New value = 3 factorial (n=3) at k.cpp:18 18 while(n--) (gdb) Continuing. Hardware watchpoint 2: n Old value = 3 New value = 2 0x08048764 in factorial (n=2) at k.cpp:18 18 while(n--) (gdb) Continuing. Hardware watchpoint 3: result Old value = 3 New value = 6 factorial (n=2) at k.cpp:18 18 while(n--) (gdb) Continuing. Hardware watchpoint 2: n Old value = 2 New value = 1 0x08048764 in factorial (n=1) at k.cpp:18 18 while(n--) (gdb) Continuing. Hardware watchpoint 2: n Old value = 1 New value = 0 0x08048764 in factorial (n=0) at k.cpp:18 18 while(n--) (gdb) Continuing. Watchpoint 2 deleted because the program has left the block in which its expression is valid. Watchpoint 3 deleted because the program has left the block in which its expression is valid. 0x08048705 in main () at k.cpp:9 9 long val=factorial(n); (gdb) p val $1 = 11476980 (gdb) 可以看到是while那里,导致n越界了,fix while(n>0) //doesn't let n reach 0 { result*=n; n--; //decrements only after the evaluation } 一些快捷命令 l – list p – print print {variable} c – continue s – step b - break break line_number/break [file_name]:line_number/break [file_name]:func_name r - run set <var> = <value> watch <var> ENTER: pressing enter key would execute the previously executed command again. c/n/s的区别 •c or continue: Debugger will continue executing until the next break point. •n or next: Debugger will execute the next line as single instruction. •s or step: Same as next, but does not treats function as a single instruction, instead goes into the function and executes it line by line 3、调试PHP程序 PHP代码 <?php. for($i = 0; $i < 10; $i++){ echo $i."n"; sleep(3); if(in_array($i,[1,9,20])){ print_r($i*$i); var_dump($i*$i); print $i*$i; } } 开始调试,加上断点 [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 <http://gnu.org/licenses/gpl.html> 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: <http://www.gnu.org/software/gdb/bugs/>... 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 ' |