#include <stdio.h> int print(int num){ int ret = num * num; return ret; } int myfunc(int num){ int i = 1; int sum = 0; while(i <= num){ sum += print(i); i++; } return sum; } int main(){ int num =0; scanf("%d", &num); int result = myfunc(num); printf("%d", result); return 0; }此程序存储在
~/demo/main.c
源文件中(~ 表示当前用户的主目录),功能是根据用户输入的 num 值,输出 12+22+...+num2 的值。(gdb) next count
参数 count 表示单步执行多少行代码,默认为 1 行。
(gdb) b 16
Breakpoint 2 at 0x40058b: file main.c, line 16.
(gdb) r
Starting program: /root/demo/main.exe
Breakpoint 2, main () at main.c:16
16 int num =0;
(gdb) n 2 <-- 单步执行 2 次
3
18 int result = myfunc(num);
(gdb) n
19 printf("%d", result);
(gdb) n
20 return 0;
(gdb)
(gdb) step count
参数 count 表示一次执行的行数,默认为 1 行。
(gdb) b 18
Breakpoint 1 at 0x4005ab: file main.c, line 18.
(gdb) r
Starting program: ~/demo/main.exe
Breakpoint 1, main () at main.c:18
18 int result = myfunc(num);
(gdb) step <-- step 命令进入 myfunc() 函数内部执行
myfunc (num=0) at main.c:7
7 int i = 1;
1、(gdb) until
2、(gdb) until location
(gdb) b 17
Breakpoint 1 at 0x1201: file main.c, line 17.
(gdb) r
Starting program: ~/demo/main.exe
Breakpoint 1, main () at main.c:17
17 scanf("%d", &num);
(gdb) u
3
18 int result = myfunc(num);
(gdb) step
myfunc (num=3) at main.c:7
7 int i = 1;
(gdb) u
8 int sum = 0;
(gdb) u
9 while(i <= num){
(gdb) u
10 sum += print(i);
(gdb) u
11 i++;
(gdb) u <-- 执行 i++ 操作
9 while(i <= num){
(gdb) u <-- 快速执行完循环体
13 return sum;
(gdb) p sum
$1 = 14
(gdb) r
Starting program:~/demo/main.exe
Breakpoint 1, main () at main.c:17
17 scanf("%d", &num);
(gdb) until 19 <-- 执行至第 19 行停止
3
main () at main.c:19
19 printf("%d", result);
(gdb) p result
$3 = 14
本文链接:http://task.lmcjl.com/news/16458.html