intmain(){ printf("hello world\n"); for(int i = 0; i < 10; i++){ printf("i is %d\n", i); test(i); } return0; }
设置断点,观察next结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
(gdb) break 13 Breakpoint 1 at 0x400599: file test.c, line 13. (gdb) run Starting program: /work/chejian/test/test hello world
Breakpoint 1, main () at test.c:13 13 printf("i is %d\n", i); (gdb) n i is 0 14 test(i); (gdb) n >>不会进入test函数 in function test 12 for(int i = 0; i < 10; i++){ (gdb) n
Breakpoint 1, main () at test.c:13 13 printf("i is %d\n", i);
(gdb) b 13 Breakpoint 1 at 0x400599: file test.c, line 13. (gdb) run Starting program: /work/chejian/test/test hello world
Breakpoint 1, main () at test.c:13 13 printf("i is %d\n", i); (gdb) n i is 0 14 test(i); (gdb) step >>进入test函数内部 test (j=0) at test.c:7 7 printf("in function test\n"); (gdb) n in function test 8 } (gdb) n main () at test.c:12 12 for(int i = 0; i < 10; i++){
(gdb) run Starting program: /work/chejian/test/test hello world Breakpoint 1, main () at test.c:13 13 printf("i is %d\n", i); (gdb) c Continuing. i is 0 in function test Breakpoint 1, main () at test.c:13 13 printf("i is %d\n", i); (gdb) c Continuing. i is 1 in function test Breakpoint 1, main () at test.c:13 13 printf("i is %d\n", i);
backtrace
简写为bt,打印堆栈
1 2 3 4 5 6 7 8 9 10 11 12
(gdb) b 7 Breakpoint 1 at 0x400571: file test.c, line 7. (gdb) run Starting program: /work/chejian/test/test hello world i is 0
Breakpoint 1, test (j=0) at test.c:7 7 printf("in function test\n"); (gdb) bt #0 test (j=0) at test.c:7 >>此时stop在test函数中,因此是栈顶 #1 0x00000000004005b7 in main () at test.c:14
(gdb) b 7 Breakpoint 1 at 0x400571: file test.c, line 7. (gdb) b 13 Breakpoint 2 at 0x400599: file test.c, line 13. (gdb) run Starting program: /work/chejian/test/test hello world
Breakpoint 2, main () at test.c:13 13 printf("i is %d\n", i); (gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x0000000000400571 in test at test.c:7 2 breakpoint keep y 0x0000000000400599 in main at test.c:13 breakpoint already hit 1 time (gdb) c Continuing. i is 0
Breakpoint 1, test (j=0) at test.c:7 7 printf("in function test\n"); (gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x0000000000400571 in test at test.c:7 breakpoint already hit 1 time 2 breakpoint keep y 0x0000000000400599 in main at test.c:13 breakpoint already hit 1 time
print
简写为p,打印一个变量的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
(gdb) b 13 Breakpoint 1 at 0x400599: file test.c, line 13. (gdb) run Starting program: /work/chejian/test/test hello world
Breakpoint 1, main () at test.c:13 13 printf("i is %d\n", i); (gdb) p i $1 = 0 (gdb) c Continuing. i is 0 in function test
Breakpoint 1, main () at test.c:13 13 printf("i is %d\n", i); (gdb) p i $2 = 1
x
按照选项要求来显示对应地址的内容,使用方式为x/FMT ADDRESS
FMT有如下几种设置:
o(octal), x(hex), d(decimal), u(unsigned decimal), t(binary), f(float), a(address), i(instruction), c(char), s(string) z(hex, zero padded on the left).
测试代码修改如下:
1 2 3 4 5 6
6voidtest(int j){ 7int addr = &j; 8char buf='C'; 9char *buf1="hello world"; 10printf("in function the addr of j is: %p, test j == %d\n", addr, j); 11 }
(gdb) b 10 Breakpoint 1 at 0x400588: file test.c, line 10. (gdb) run Starting program: /work/chejian/test/test hello world i is 0 Breakpoint 1, test (j=0) at test.c:10 10 printf("in function the addr of j is: %p, test j == %d\n", addr, j); (gdb) c Continuing. in function the addr of j is: 0xffffe51c, test j == 0 i is 1 Breakpoint 1, test (j=1) at test.c:10 10 printf("in function the addr of j is: %p, test j == %d\n", addr, j); (gdb) c Continuing. in function the addr of j is: 0xffffe51c, test j == 1 i is 2 Breakpoint 1, test (j=2) at test.c:10 10 printf("in function the addr of j is: %p, test j == %d\n", addr, j); (gdb) c Continuing. in function the addr of j is: 0xffffe51c, test j == 2 i is 3 Breakpoint 1, test (j=3) at test.c:10 >>第三次断点捕捉 10 printf("in function the addr of j is: %p, test j == %d\n", addr, j); (gdb) display j 1: j = 3 >>此时j为3 (gdb) x/i &j >>i 对应指令 ,打印j的地址和对应的汇编指令 0x7fffffffe51c: add (%rax),%eax (gdb) x/o &j >>八进制打印 0x7fffffffe51c: 03 (gdb) x/x &j >>十六进制打印 0x7fffffffe51c: 0x00000003 (gdb) x/d &j >>十进制打印 0x7fffffffe51c: 3 (gdb) x/u &j >>无符号十进制打印 0x7fffffffe51c: 3 (gdb) x/t &j >>二进制打印 0x7fffffffe51c: 00000000000000000000000000000011 (gdb) x/a &j >>地址打印 0x7fffffffe51c: 0x7800000000000003 (gdb) x/c &buf >>字符打印 0x7fffffffe523: 67 'C' (gdb) print buf $1 = 67 'C' (gdb) display buf 2: buf = 67 'C' (gdb) x/c &buf1 0x7fffffffe528: 122 'z' >>为什么z?因为 0x7fffffffe528存放的是0x40067a, 7a就是z的ASCII码 (gdb) x/s buf1 0x40067a: "hello world" (gdb) x/c buf1 0x40067a: 104 'h'
x/i $pc也可以打印当前pc指令的地址和汇编信息,见display测试部分。
display
在==每一次==程序运行停止前打印变量,功能和print类似,但是有更多扩展,一般用法为display var
(gdb) b 7 Breakpoint 1 at 0x400571: file test.c, line 7. (gdb) run Starting program: /work/chejian/test/test hello world i is 0
Breakpoint 1, test (j=0) at test.c:7 7 printf("in function test j == %d\n", j); (gdb) display j 1: j = 0 >>打印变量 (gdb) c Continuing. in function test j == 0 i is 1 >>停止后打印一次
Breakpoint 1, test (j=1) at test.c:7 7 printf("in function test j == %d\n", j); 1: j = 1 (gdb) c Continuing. in function test j == 1 i is 2
Breakpoint 1, test (j=2) at test.c:7 7 printf("in function test j == %d\n", j); 1: j = 2 >>停止后打印一次 (gdb) display &j >>打印j变量的地址 3: &j = (int *) 0x7fffffffe51c >>3是指第几次执行display命令
(gdb) b 13 Breakpoint 1 at 0x400599: file test.c, line 13. (gdb) run Starting program: /work/chejian/test/test hello world
Breakpoint 1, main () at test.c:13 13 printf("i is %d\n", i); (gdb) p i $1 = 0 (gdb) set variable i=3 (gdb) p i $2 = 3 (gdb) c Continuing. i is 3 in function test
Breakpoint 1, main () at test.c:13 13 printf("i is %d\n", i); (gdb) p i $3 = 4
(gdb)disassemble/smain Dump of assembler code for function main: test.c: 10intmain() { 0x000000000040057e<+0>:push%rbp 0x000000000040057f<+1>:mov%rsp,%rbp 0x0000000000400582<+4>:sub$0x10,%rsp