Linux gdb调试

导读:本篇文章讲解 Linux gdb调试,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

gdb调试

写一个C语言程序,输入一个字符串,当字符串不为“end”时,一直输入,直到输入字符串“end”退出程序
1.编写代码

wys@DESKTOP-2OU3HRV:~/mycode/day03$  vim test.c
  1 #include<stdio.h>
  2 #include<string.h>
  3 int main()
  4 {
  5    while(1)
  6    {
  7      printf("input\n");
  8      char  buff[100] = {0};
  9      fgets(buff,100,stdin);
 10      if(strcmp(buff,"end") == 0)
 11      {
 12       break;
 13      }
 14      printf("read:%s\n",buff);
 15    }
 16    return 0;
 17 }

2.编译链接生成可执行程序:

wys@DESKTOP-2OU3HRV:~/mycode/day03$ gcc -o test test.c
wys@DESKTOP-2OU3HRV:~/mycode/day03$ ls
test  test.c

3.执行程序

wys@DESKTOP-2OU3HRV:~/mycode/day03$ ./test
input
abc
read:abc

input
hello
read:hello

input
end
read:end

input
end
read:end

input


我们发现当输入字符串“end”时,并未退出程序,所以要对程序进行调试,寻找错误
调试是对可执行文件的调试,而不是对代码的调试。
Debug 版本和 Release 版本
Debug 版本

Debug 版本为可调式版本,生成的可执行文件中包含调试需要的信息。我们作为开发人
员,最常用的就是 debug 版本的可执行文件。
Debug 版本的生成:
因为调试信息是在编译过程时加入到中间文件(.o)中的,所以必须在编译时控制其生
成包含调试信息的中间文件。
gcc -c hello.c -g —> 生成包含调试信息的中间文件
gcc -o hello hello.o
或者 gcc -o hello hello.c -g
Release 版本
Release 版本为发行版本,是提供给用户使用的版本。用 gcc 默认生成的就是 Release 版本。
首先将源代码编译、链接生成 Debug 版本的可执行文件,然后通过gdb Debug 版本的可执行文件名进入调试模式
单进程、单线程基础调试命令

  1. l //显示 main 函数所在的文件的源代码
  2. list 文件名:num //显示 filename 文件 num 行上下的源代码
  3. b 行号 //给指定行添加断点
  4. b 函数名 //给指点函数的第一有效行添加一个断点
  5. info break //显示断点信息
  6. delete 断点号 //删除指定断点
  7. disable 断点号 //将断点设定为无效的,不加断点号,将所有断点设置为无效
  8. enable 断点号 //将断点设定为有效的,不加断点号,将所有断点设置为有效
  9. r(run) //运行程序
  10. n(next) //单步执行
  11. c (continue) //继续执行,直接执行到下一个断点处
  12. s //进入将要被调用的函数中执行
  13. finish //跳出函数
  14. q //退出调试
  15. p val //打印变量 val 的值
  16. p &val //打印变量 val 的地址
  17. p a+b //打印表达式的值
  18. p arr(数组名) //打印数组所有元素的值
  19. p *parr@len //用指向数组的指针打印数组所有元素的值
  20. display //自动显示,参数和 p 命令一样
  21. info display //显示自动显示信息
  22. undisplay + 编号 //删除指定的自动显示
  23. ptype val //显示变量类型
  24. bt //显示函数调用栈
    4.调试
    4.1 将源代码编译、链接生成 Debug 版本的可执行文件
wys@DESKTOP-2OU3HRV:~/mycode/day03$ ls
test  test.c
wys@DESKTOP-2OU3HRV:~/mycode/day03$ gcc -o test test.c -g
wys@DESKTOP-2OU3HRV:~/mycode/day03$ ls
test  test.c
wys@DESKTOP-2OU3HRV:~/mycode/day03$

4.2 使用gdb命令调试

wys@DESKTOP-2OU3HRV:~/mycode/day03$ ls
test  test.c
wys@DESKTOP-2OU3HRV:~/mycode/day03$ gdb test
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 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 test...
(gdb) l
1       #include<stdio.h>
2       #include<string.h>
3       int main()
4       {
5          while(1)
6          {
7            printf("input\n");
8            char  buff[100] = {0};
9            fgets(buff,100,stdin);
10           if(strcmp(buff,"end") == 0)
(gdb) l
11           {
12            break;
13           }
14           printf("read:%s\n",buff);
15         }
16         return 0;
17      }
(gdb) b 8
Breakpoint 1 at 0x11f0: file test.c, line 8.
(gdb) n
The program is not being run.
(gdb) r
Starting program: /home/wys/mycode/day03/test
input

Breakpoint 1, main () at test.c:8
8            char  buff[100] = {0};
(gdb) n
9            fgets(buff,100,stdin);
(gdb) n
end
10           if(strcmp(buff,"end") == 0)
(gdb) p buff
$1 = "end\n", '\000' <repeats 95 times>
(gdb) n
14           printf("read:%s\n",buff);
(gdb) n
read:end

6          {
(gdb)

打印buff,我们发现buff是“end\n”,并不是“end”,显然与“end”不相等,找到问题所在

(gdb) p buff
$1 = "end\n", '\000' <repeats 95 times>

5.修改代码

  1 #include<stdio.h>
  2 #include<string.h>
  3 int main()
  4 {
  5    while(1)
  6    {
  7      printf("input\n");
  8      char  buff[100] = {0};
  9      fgets(buff,100,stdin);
 10      buff[strlen(buff)-1] = '\0';
 11     // buff[strlen(buff)-1] = 0;
 12      if(strcmp(buff,"end") == 0)
 13      {
 14       break;
 15      }
 16      printf("read:%s\n",buff);
 17    }
 18    return 0;
 19 }

6.重新编译运行

wys@DESKTOP-2OU3HRV:~/mycode/day03$ gcc -o test test.c
wys@DESKTOP-2OU3HRV:~/mycode/day03$ ls
test  test.c
wys@DESKTOP-2OU3HRV:~/mycode/day03$ ./test
input
hello
read:hello
input
abc
read:abc
input
end
wys@DESKTOP-2OU3HRV:~/mycode/day03$

演示gdb调试怎么进入函数

编写简单求和代码

wys@DESKTOP-2OU3HRV:~/mycode/day04$ vim test.c
  1 #include<stdio.h>
  2 int add(int x,int y)
  3 {
  4     int temp = 0;
  5     temp = x + y;
  6     return temp;
  7 }
  8
  9 int main()
 10 {
 11     int a = 2;
 12     int b = 3;
 13     printf("a + b = %d\n",add(a,b));
 14     return 0;
 15 }

将源代码编译、链接生成 Debug 版本的可执行文件

wys@DESKTOP-2OU3HRV:~/mycode/day04$ gcc test.c -o test -g
wys@DESKTOP-2OU3HRV:~/mycode/day04$ ls
test  test.c
wys@DESKTOP-2OU3HRV:~/mycode/day04$

调试
按s进入函数,finish跳出函数

wys@DESKTOP-2OU3HRV:~/mycode/day04$ gdb test
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 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 test...
(gdb) l
1       #include<stdio.h>
2       int add(int x,int y)
3       {
4           int temp = 0;
5           temp = x + y;
6           return temp;
7       }
8
9       int main()
10      {
(gdb) l
11          int a = 2;
12          int b = 3;
13          printf("a + b = %d\n",add(a,b));
14          return 0;
15      }
(gdb) b 13
Breakpoint 1 at 0x1188: file test.c, line 13.
(gdb) r
Starting program: /home/wys/mycode/day04/test

Breakpoint 1, main () at test.c:13
13          printf("a + b = %d\n",add(a,b));
(gdb) s
add (x=0, y=134222349) at test.c:3
3       {
(gdb) n
4           int temp = 0;
(gdb) n
5           temp = x + y;
(gdb) p x
$1 = 2
(gdb) p y
$2 = 3
(gdb) n
6           return temp;
(gdb) p temp
$3 = 5
(gdb) n
7       }
(gdb) n
a + b = 5
main () at test.c:14
14          return 0;
(gdb) n
15      }
(gdb) n
__libc_start_main (main=0x800116e <main>, argc=1, argv=0x7ffffffee048, init=<optimized out>, fini=<optimized out>,
    rtld_fini=<optimized out>, stack_end=0x7ffffffee038) at ../csu/libc-start.c:342
342     ../csu/libc-start.c: No such file or directory.
(gdb)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/95588.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!