#include <stdio.h> int main(){ int num,result=0,i=0; scanf("%d", &num); while(i<=num){ result += i; i++; } printf("result=%d\n", result); return 0; }此程序存储在
~/demo/main.c
文件中(~
代指当前系统登录用户的主目录),并且已经其编译为可供 GDB 调试的 main.exe 可执行文件:
[root@bogon demo]# gcc main.c -o main.exe -g
[root@bogon demo]# ls
main.c main.exe
(gdb) print num
(gdb) p num
[root@bogon demo]# gdb main.exe -q
Reading symbols from ~/demo/main.exe...done.
(gdb) l
1 #include <stdio.h>
2 int main(){
3 int num,result=0,i=0;
4 scanf("%d", &num);
5 while(i<=num){
6 result += i;
7 i++;
8 }
9 printf("result=%d\n", result);
10 return 0;
(gdb)
11 }
(gdb) b 3
Breakpoint 1 at 0x40053c: file main.c, line 3.
(gdb) r
Starting program: /root/demo/main.exe
Breakpoint 1, main () at main.c:4
4 scanf("%d", &num);
(gdb) n
3
5 while(i<=num){
(gdb) p num <--输出 num 的值
$1 = 3
(gdb) p num=4 <-- 修改 num 的值为 4
$2 = 4
(gdb) b 9
Breakpoint 2 at 0x400569: file main.c, line 9.
(gdb) c
Continuing.
Breakpoint 2, main () at main.c:9
9 printf("result=%d\n", result);
(gdb) p result <-- 输出 result 的值
$2 = 10
(gdb) p result=20 <-- 修改 result 的值
$2 = 20
(gdb) c
Continuing.
result=20
Program exited normally.
(gdb)
(gdb) display expr
(gdb) display/fmt expr
/fmt | 功 能 |
---|---|
/x | 以十六进制的形式打印出整数。 |
/d | 以有符号、十进制的形式打印出整数。 |
/u | 以无符号、十进制的形式打印出整数。 |
/o | 以八进制的形式打印出整数。 |
/t | 以二进制的形式打印出整数。 |
/f | 以浮点数的形式打印变量或表达式的值。 |
/c | 以字符形式打印变量或表达式的值。 |
(gdb) b 4
Breakpoint 1 at 0x40053c: file main.c, line 4.
(gdb) b 9
Breakpoint 2 at 0x400569: file main.c, line 9.
(gdb) r
Starting program: /root/demo/main.exe
Breakpoint 1, main () at main.c:4
4 scanf("%d", &num);
(gdb) display num
1: num = 32767
(gdb) display/t result
2: /t result = 0
(gdb) n
3
5 while(i<=num){
2: /t result = 0
1: num = 3
(gdb) c
Continuing.
Breakpoint 2, main () at main.c:9
9 printf("result=%d\n", result);
2: /t result = 110
1: num = 3
(gdb) c
Continuing.
result=6
Program exited normally.
(gdb)
info dispaly
命令,可以打印出这张表:
(gdb) info display
Auto-display expressions now in effect:
Num Enb Expression
2: y /t result
1: y num
(gdb) undisplay num...
(gdb) delete display num...
(gdb) undisplay 1
(gdb) info display
Auto-display expressions now in effect:
Num Enb Expression
2: y /t result
(gdb)
(gdb) disable display num...
num... 表示要禁用的变量或表达式的编号,编号的个数可以是多个,表示一次性禁用多个变量或表达式
(gdb) disable display 2
(gdb) info display
Auto-display expressions now in effect:
Num Enb Expression
2: n /t result
(gdb)
(gdb) enable display num...
参数 num... 表示要激活的变量或表达式的编号,编号的个数可以是多个,表示一次性激活多个变量或表达式。
(gdb) enable display 2
(gdb) info display
Auto-display expressions now in effect:
Num Enb Expression
2: y /t result
(gdb)
本文链接:http://task.lmcjl.com/news/5696.html