如何使用GDB调试PHP程序

一般来说,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; }

编译

1

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 <> 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 /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; } }

开始调试,加上断点

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

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