GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git Copyright (C) 2018 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 "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from gdbtest...done. (gdb)
首先输出一些系统信息,最后一行开头的(gdb)为命令提示符,输入start命令开始调试:
1 2 3 4 5 6 7
(gdb) start Temporary breakpoint 1 at 0x67b: file gdbtest.c, line 14. Starting program: /home/deeplearning/dcj/linuxCTest/GDBtest/gdbtest
Temporary breakpoint 1, main () at gdbtest.c:14 14 result = add(1, 10); (gdb)
(gdb) info locals i = 0 sum = 0 (gdb) frame 1 #1 0x000055555555468a in main () at gdbtest.c:14 14 result = add(1, 10); (gdb) info locals result = 0 (gdb)
(gdb) set var sum=100 No symbol "sum" in current context. (gdb) frame 0 #0 add (start=1, end=10) at gdbtest.c:6 6 for(i=start; i<=end; i++) (gdb) set var sum=100 (gdb) print sum $1 = 100 (gdb) info locals i = 0 sum = 100 (gdb) finish Run till exit from #0 add (start=1, end=10) at gdbtest.c:6 0x000055555555468a in main () at gdbtest.c:14 14 result = add(1, 10); Value returned is $2 = 155 (gdb)
使用set var sum=100将sum的值修改为100,注意要切换到sum变量所在的帧栈环境中执行,然后可以使用print或info locals命令查看修改后的结果,接着使用finish命令使程序自动运行结果,可以看出最终的输出的结果为155,符合预期。最后可以使用quit命令退出GDB环境:
1 2 3 4 5 6 7 8
(gdb) quit A debugging session is active.
Inferior 1 [process 31210] will be killed.
Quit anyway? (y or n) y $
键入y确认退出即可。
调试示例2
计算从1到n是和,gdbbreakpoint.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<stdio.h>
intmain() { int sum=0, i, data; while(1) { printf("please input a num(<100)\n"); scanf("%d", &data);
for(i=1; i<=data; i++) sum += i;
printf("sum from 1 to %d is: %d\n", data, sum); }
return0; }
编译并运行测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ gcc -o gdbbreakpoint gdbbreakpoint.c -g $ ./gdbbreakpoint please input a num(<100) 2 sum from 1 to 2 is: 3 please input a num(<100) 3 sum from 1 to 3 is: 9 please input a num(<100) 4 sum from 1 to 4 is: 19 please input a num(<100) ^C
(gdb) break 11 Breakpoint 1 at 0x73c: file gdbbreakpoint.c, line 11. (gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x000000000000073c in main at gdbbreakpoint.c:11 (gdb)
此处还使用了info breakpoints查看当前已设置的所有断点。
然后使用start命令启动调试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
(gdb) start Temporary breakpoint 2 at 0x702: file gdbbreakpoint.c, line 4. Starting program: /home/deeplearning/dcj/linuxCTest/GDBtest/gdbbreakpoint
Temporary breakpoint 2, main () at gdbbreakpoint.c:4 4 { (gdb) continue Continuing. please input a num(<100) 2
Breakpoint 1, main () at gdbbreakpoint.c:11 11 for(i=1; i<=data; i++) (gdb) info locals sum = 0 i = 32767 data = 2 (gdb)