1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> void hello(void); int main(int argc, char const *argv[]) { int i; int a = 13; printf("before for %d\n", a); for (i = 1; i < 3; ++i) { printf("before hello %d\n", i); hello(); printf("after hello %d\n", i); } return 0; } void hello(void) { puts("hello"); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
//-g表示将源码信息编译到可执行文件中,便于gdb调试 gcc a.c -g //启动gdb gdb //使用file命令载入被调试的文件 file a.out //运行(run)正常输出 r //在第7行设置断点(break) b 7 //再次运行 r //输出如下: Starting program: /home/wwwroot/c/a.out Breakpoint 1, 0x08048461 in main () //按s步进一步(step) s //输出如下: Single stepping until exit from function main, which has no line number information. before for 13 before hello 1 hello after hello 1 before hello 2 hello after hello 2 0x00147d36 in __libc_start_main () from /lib/libc.so.6 //由于没有第2个断点,程序直接走完了 //在hello函数处设置断点 b hello //再次运行后按c(continue) //s(step):一行一行执行 n(next):函数内部一行一行执行 c(continue):越过当前断点至下一个断点处中断 c //输出如下: Continuing. before for 13 before hello 1 Breakpoint 2, 0x080484c5 in hello () //再按2次c(如下) Breakpoint 2, 0x080484c5 in hello () (gdb) c //第一次 Continuing. hello after hello 1 before hello 2 Breakpoint 2, 0x080484c5 in hello () (gdb) c //第二次 Continuing. hello after hello 2 Program exited normally. //info b能查看断点设置 (gdb) info b Num Type Disp Enb Address What 1 breakpoint keep y 0x08048461 a.c:7 breakpoint already hit 1 time 2 breakpoint keep y 0x080484c5 <hello+6> breakpoint already hit 2 times //使用d删除断点,删除指定断点可以输入d num(号码) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
//启动gdb(带上php执行文件),也可以使用file子命令 gdb /usr/local/php7/bin/php //设置需要调试的php文件 set args ./test.php //通过help子命令可以了解指定子命令的用法 help set help set args //设置php断点的时候,行号和函数名必须是C源码中的行号和函数名 //可以指定文件名:行号或文件名:函数名 可以通过搜索PHP源代码PHP_FUNCTION(php函数名)定位 b basic_functions.c:5475 b zend.c:zend_print_zval_r //搜索源代码PHP_FUNCTION(print_r)定位 //还有种更简单的方法,增加 zif_ 前缀,可以直接定位到PHP_FUNCTION(php函数名) b zif_print_r //在断点处暂停后可以通过l(list)来查看上下文源码 //也可以通过 l 文件:行号 或 l 函数名 来查看指定位置的源代码 l basic_functions.c:5475 l zif_print_r //在断点处p(print_r)可以打印list处源码的变量值 p do_return //如果打印出来的值不符合预期,可以用set进行动态设置调试 set do_return=13 //观察l出来的变量,如果变量被修改过,会把Old value和New value都输出 watch do_return //(bt)backtrace查看程序执行的堆栈信息 //finish执行到当前函数的结束 //q(quit)退出 //gdb调试进程gdb -p 进程ID |