linux调试gdb手册,一文入门Linux下gdb调试(一)

本文转载自【微信公众号:羽林君,ID:Conscience_Remains】

总述

在window下我们习惯了IDE的各种调试按钮,说实话确实挺方便的,但到了Linux下,没有那么多的IDE支持我们调试,但是Linux有也有强大的命令行C/C++的调试工具——GDB,GNU提供的开源调试工具。

刚开始不习惯,使用多了我们就会喜欢上他,程序调试的单步执行,跳入函数,跳出函数,设置断点,设置观察点,查看变量。GDB都有,此外gdb还可以生成程序非法执行后core dump文件,这个文件有快照功能,在程序崩溃的时候保存了程序的堆栈等信息,我们执行core文件就可以方便的找程序崩溃的原因了。

一、编译可以调试的代码

在linux下对于单个c/c++文件编译通常就是加-o 进行编译成可执行文件,但是我们如果需要要调试,则需要加一个 -g 用来向编译器进行表明该程序需要编译成可以gdb调试的代码,加上编译信息,生成的执行文件就会变大,如图所示。所以我们只在调试的时候进行 -g 编译。Java调试的时候也是类似后续也说一哈java的Linux。

f46312d8a24fa81d016b65c7d740f26b.png

Makefile的文件中我们也是如上,只不过是在Makefile文件中 -o 编译的时候添加 -g

85cf3401b3c7d276b1aa25484d99ae17.png

二、调试过程

2a26106ca9a017a2743cdf56a3e8a57a.png

调用gdb调试,先查看电脑环境里面是否有gdb调试器,一般我们安装了gcc编译器,就默认同时安装了gdb调试器

fc0b568b7542d297abb2f9f09afc2942.png

没有的话要安装gdb调试器,使用apt-get 就可以快速安装

apt-get update

apt-get install gdb

gdb 调用执行文件

gdb ./big_endian /*执行文件*/

最基本的GDB命令

85e0f7997266a848fd3cbc80b3ddc07f.png

6ab660ed8af9afacf4dcd880cf8364e3.png

b76051e46d47ea015bd788311efa8d37.png

d3a38018a5863bad62a1247588e3b8d4.png

2b6ec947ea11ce5b88cac3251170cf77.png

4ca825f80ccf169c8595e8677512746f.png

示例执行:

/*刚开始有很多打印的信息*/

book@lyn:~/Documents/linux/test/wds/wds_c++/c++_test1/c11th$ gdb person1

GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1

Copyright (C) 2016 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later

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:

.

Find the GDB manual and other documentation resources online at:

.

For help, type "help".

Type "apropos word" to search for commands related to "word"...

Reading symbols from person1...done.

(gdb) l 10 /*list 10 显示10行代码 方便下一步打断点调试*/

5 using namespace std;

6

7 class Person {

8 private:

9 static int cnt;

10 char *name;

11 int age;

12

13 public:

14

(gdb) /*不输入 直接 Enter 键 重复上一步骤*/

15 static int getCount(void);

16

17 Person() {//cout <

18 name = NULL;

19 cnt++;

20 }

21 Person(char *name)

22 {

23 //cout <

24 this->name = new char[strlen(name) + 1];

(gdb)

25 strcpy(this->name, name);

26 cnt++;

27 }

28

29 Person(char *name, int age)

30 {

31 cout <

32 this->age = age;

33

34 this->name = new char[strlen(name) + 1];

(gdb) /*......*/

......

95

96 int main(int argc, char **argv)

97 {

98 Student p;

99 p.setName("zhangsan");

100 p.setAge(16);

101 p.printInfo();

102

103 return 0;

104 }

(gdb) b 99 /*在显示的 第 99行代码处 打断点*/

Breakpoint 1 at 0x400b5d: file person.cpp, line 99.

(gdb) i b /*显示 设置的断点*/

Num Type Disp Enb Address What

1 breakpoint keep y 0x0000000000400b5d in main(int, char**) at person.cpp:99

(gdb) r /*开始全速执行代码 直到第一个断点处*/

Starting program: /home/book/Documents/linux/test/wds/wds_c++/c++

Breakpoint 1, main (argc=1, argv=0x7fffffffe3d8) at person.cpp:99

99 p.setName("zhangsan");

(gdb) p p /*打印 p 变量信息*/

$1 = { = {static cnt = 1, name = 0x0, age = 0}, }

(gdb) n /*执行下一步*/

100 p.setAge(16);

(gdb) q /*退出 gdb 调试*/

A debugging session is active.

Inferior 1 [process 3410] will be killed.

Quit anyway? (y or n) y

(gdb) i disp /*显示所有的disp变量*/

Auto-display expressions now in effect:

Num Enb Expression

1: y ap

2: y * ap

3: y *ap

4: y val

(gdb) undisplay 1 /*取消显示第一个变量*/

(gdb) undisplay 2

(gdb) undisp 3

(gdb) i disp /*查看确认*/

Auto-display expressions now in effect:

Num Enb Expression

4: y val

(gdb) s

hanoi (n=3, x=120 'x', y=121 'y', z=122 'z') at Hanoi.c:21

21 if(n==1) //欲移动n个圆盘,需先移动其上的n-1个圆盘

(gdb) n

25 hanoi(n-1, x, z, y); //将x上编号为1至n-1的圆盘移到y,z作辅助塔

这就是我分享的gdb调试一些方法,里面代码是实践过的,如果大家有什么更好的思路,欢迎分享交流哈。

本文转载自【微信公众号:羽林君,ID:Conscience_Remains】

举报/反馈

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值