赞
踩
一段c语言程序:
- #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;
- }
next 是最常用来进行单步调试的命令,其最大的特点是当遇到包含调用函数的语句时,无论函数内部包含多少行代码,next 指令都会一步执行完。也就是说,对于调用的函数来说,next 命令只会将其视作一行代码。
(gdb) next count
参数 count 表示单步执行多少行代码,默认为 1 行。
- [root@localhost day5]# gcc test1.c -o test1 -g
- [root@localhost day5]# ls
- test1 test1.c
- [root@localhost day5]# gdb test1 -q
- Reading symbols from /home/gdb/day5/test1...done.
- (gdb) l 0
- 1 #include <stdio.h>
- 2 int print(int num){
- 3 int ret = num * num;
- 4 return ret;
- 5 }
- 6 int myfunc(int num){
- 7 int i = 1;
- 8 int sum = 0;
- 9 while(i <= num){
- 10 sum += print(i);
- (gdb)
- 11 i++;
- 12 }
- 13 return sum;
- 14 }
- 15 int main(){
- 16 int num =0;
- 17 scanf("%d", &num);
- 18 int result = myfunc(num);
- 19 printf("%d", result);
- 20 return 0;
- (gdb)
- 21 }
- (gdb)
- Line number 22 out of range; test1.c has 21 lines.
- (gdb) b 16
- Breakpoint 1 at 0x4005c8: file test1.c, line 16.
- (gdb) r
- Starting program: /home/gdb/day5/test1
-
- Breakpoint 1, main () at test1.c:16
- 16 int num =0;
- Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64
- (gdb) n 2
- 3
- 18 int result = myfunc(num);
- (gdb) n <---------跳过myfunc函数,直接到19行的代码
- 19 printf("%d", result);
- (gdb) n
- 20 return 0;
- (gdb) p result
- $1 = 14
- (gdb)
通常情况下,step 命令和 next 命令的功能相同,都是单步执行程序。不同之处在于,当 step 命令所执行的代码行中包含函数时,会进入该函数内部,并在函数第一行代码处停止执行。
(gdb) step count
- [root@localhost day5]# clear
- [root@localhost day5]# gcc test1.c -o test1 -g
- [root@localhost day5]# gdb test1 -q
- Reading symbols from /home/gdb/day5/test1...done.
- (gdb) l 0
- 1 #include <stdio.h>
- 2 int print(int num){
- 3 int ret = num * num;
- 4 return ret;
- 5 }
- 6 int myfunc(int num){
- 7 int i = 1;
- 8 int sum = 0;
- 9 while(i <= num){
- 10 sum += print(i);
- (gdb)
- 11 i++;
- 12 }
- 13 return sum;
- 14 }
- 15 int main(){
- 16 int num =0;
- 17 scanf("%d", &num);
- 18 int result = myfunc(num);
- 19 printf("%d", result);
- 20 return 0;
- (gdb)
- 21 }
- (gdb) b 18
- Breakpoint 1 at 0x4005e5: file test1.c, line 18.
- (gdb) r
- Starting program: /home/gdb/day5/test1
- 3
-
- Breakpoint 1, main () at test1.c:18
- 18 int result = myfunc(num);
- Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64
- (gdb) step <------------程序进入mufunc函数,并在第一行代码处停止
- myfunc (num=3) at test1.c:7
- 7 int i = 1;
- (gdb)
- 1、(gdb) until
- 2、(gdb) until location
其中,参数 location 为某一行代码的行号。until 命令可以简写为 u 命令。
不带参数的 until 命令,可以使 GDB 调试器快速运行完当前的循环体,并运行至循环体外停止。注意,until 命令并非任何情况下都会发挥这个作用,只有当执行至循环体尾部(最后一行代码)时,until 命令才会发生此作用;反之,until 命令和 next 命令的功能一样,只是单步执行程序。
- [root@localhost day5]# gdb test1 -q
- Reading symbols from /home/gdb/day5/test1...done.
- (gdb) l 0
- 1 #include <stdio.h>
- 2 int print(int num){
- 3 int ret = num * num;
- 4 return ret;
- 5 }
- 6 int myfunc(int num){
- 7 int i = 1;
- 8 int sum = 0;
- 9 while(i <= num){
- 10 sum += print(i);
- (gdb)
- 11 i++;
- 12 }
- 13 return sum;
- 14 }
- 15 int main(){
- 16 int num =0;
- 17 scanf("%d", &num);
- 18 int result = myfunc(num);
- 19 printf("%d", result);
- 20 return 0;
- (gdb)
- 21 }
- (gdb)
- Line number 22 out of range; test1.c has 21 lines.
- (gdb) b 17
- Breakpoint 1 at 0x4005cf: file test1.c, line 17.
- (gdb) r
- Starting program: /home/gdb/day5/test1
-
- Breakpoint 1, main () at test1.c:17
- 17 scanf("%d", &num);
- Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64
- (gdb) u
- 3
- 18 int result = myfunc(num);
- (gdb) step
- myfunc (num=3) at test1.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
- 9 while(i <= num){
- (gdb) u <-------------重点:快速执行完循环体
- 13 return sum;
- (gdb) p sum
- $1 = 14
- (gdb)
until 命令还可以后跟某行代码的行号,以指示 GDB 调试器直接执行至指定位置后停止。举个例子:
- [root@localhost day5]# gdb test1 -q
- Reading symbols from /home/gdb/day5/test1...done.
- (gdb) l 0
- 1 #include <stdio.h>
- 2 int print(int num){
- 3 int ret = num * num;
- 4 return ret;
- 5 }
- 6 int myfunc(int num){
- 7 int i = 1;
- 8 int sum = 0;
- 9 while(i <= num){
- 10 sum += print(i);
- (gdb)
- 11 i++;
- 12 }
- 13 return sum;
- 14 }
- 15 int main(){
- 16 int num =0;
- 17 scanf("%d", &num);
- 18 int result = myfunc(num);
- 19 printf("%d", result);
- 20 return 0;
- (gdb)
- 21 }
- (gdb)
- Line number 22 out of range; test1.c has 21 lines.
- (gdb) b 17
- Breakpoint 1 at 0x4005cf: file test1.c, line 17.
- (gdb) r
- Starting program: /home/gdb/day5/test1
-
- Breakpoint 1, main () at test1.c:17
- 17 scanf("%d", &num);
- Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64
- (gdb) until 19 <-------------------直接执行至第19行停止
- 3
- main () at test1.c:19
- 19 printf("%d", result);
- (gdb) p result <---------------打印result结果
- $1 = 14
- (gdb)
可以看到,通过执行 until 19 命令,GDB 调试器直接从第 17 行代码处执行至指定的第 19 行。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。