Linux下gdb调试快速入门
创始人
2025-05-29 08:31:27

Linux下gdb调试

首先,我们使用Linux发布的可执行文件都是release版本,要进行调试的话,需要将其改为debug版本。
Linux下发布debug版本的格式:使用-g

[root@node02 network]# g++ -g baba.cpp -o baba

1、启动gdb

[root@node02 network]# gdb baba
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/admin/network/baba...done.

上述方法会有gdb的一些版本介绍,界面不太简介
可以使用下面的方法启动gdb, 加一个 -q

[root@node02 network]# gdb -q baba
Reading symbols from /home/admin/network/baba...done.

2、查看源码

gdb启动成功后,会出现(gdb), 使用小写l 即可查看源码。默认显示10行,回车可查看剩余的

(gdb) l

3、执行可执行程序

使用run,简写r即可,运行程序直到遇到 结束或者遇到断点等待下一个命令

(gdb) r
Starting program: /home/admin/network/baba
...

4、打断点

break(简写 b)
格式:b 行号; 在某行设置断点
infor breakpoints:显示断点信息
Num:断点编号
Disp:断点执行一次之后是否有效 kep:有效 dis:无效
Enb:当前断点是否有效 y:有效 n:无效
Address:内存地址
What:位置
break func(break缩写为b):在函数func()的入口处设置断点

(gdb) b 22
Breakpoint 1 at 0x400c19: file baba.cpp, line 22.
(gdb) b 26
Breakpoint 2 at 0x400c6a: file baba.cpp, line 26.
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400c19 in test01() at baba.cpp:22
2       breakpoint     keep y   0x0000000000400c6a in test01() at baba.cpp:26

5、单步执行

使用continue(简写c)、next(简写n)、step(简写s)命令。
continue(简写 c):继续执行程序,直到下一个断点或者结束;
next(简写 n ):单步执行程序,但是遇到函数时会直接跳过函数,不进入函数;
step(简写 s) :单步执行程序,但是遇到函数会进入函数;

(gdb) r
Starting program: /home/admin/network/baba Breakpoint 1, test01 () at baba.cpp:22
22	    while(itBegin != itEnd)
(gdb) c
Continuing.
10
20
30Breakpoint 2, test01 () at baba.cpp:29
29	    for(vector::iterator it = v.begin(); it != v.end(); it++)
(gdb) n
31	        cout<< *it<< endl;
(gdb) n
10
29	    for(vector::iterator it = v.begin(); it != v.end(); it++)
(gdb) n
31	        cout<< *it<< endl;
(gdb) n
20
29	    for(vector::iterator it = v.begin(); it != v.end(); it++)
(gdb) n
31	        cout<< *it<< endl;

6、其他操作

delete 断点号n:删除第n个断点
disable 断点号n:暂停第n个断点
enable 断点号n:开启第n个断点
clear 行号n:清除第n行的断点
info b (info breakpoints) :显示当前程序的断点设置情况
delete breakpoints:清除所有断点:

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400c19 in test01() at baba.cpp:22breakpoint already hit 1 time
2       breakpoint     keep y   0x0000000000400c6a in test01() at baba.cpp:26breakpoint already hit 1 time
(gdb) delete 1
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
2       breakpoint     keep y   0x0000000000400c6a in test01() at baba.cpp:26breakpoint already hit 1 time
(gdb) disable 2
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
2       breakpoint     keep n   0x0000000000400c6a in test01() at baba.cpp:26breakpoint already hit 1 time
(gdb) enable 2
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
2       breakpoint     keep y   0x0000000000400c6a in test01() at baba.cpp:26breakpoint already hit 1 time
(gdb) delete breakpoints
Delete all breakpoints? (y or n) y
(gdb) info b
No breakpoints or watchpoints.

使用print、whatis查看变量(前提:先打断点让程序执行到这块,然后才能查看刚刚执行的*it的值,以及它的类型)

(gdb) print *it
$2 = (int &) @0x604014: 20
(gdb) whatis *it
type = __gnu_cxx::__normal_iterator > >::reference

until:退出循环体。
until+行号:运行至某行,不仅仅用来跳出循环

7、退出gdb

使用q退出

(gdb) q

相关内容

热门资讯

记者手记|时代大潮里的笃行与担... 来源:新华社记者手记|时代大潮里的笃行与担当新华社记者韩梁 朱瑞卿 伍岳9月的新德里草木葱郁,阳光热...
被骂 “君子之贼” 高若讷有点...     ◎宋慧敏    北宋景祐三年(1036年)一个春日,谏官高若讷接到一封信,写信人是刚刚入朝任...
烂缦胡同:烟火气里涌动青春创业...     近日,在“全国文化中心建设巡礼”主题采访活动中,北京青年报记者走进北京市西城区牛街街道烂缦胡...
科技赋能实体 金融护航出海     9月9日至13日,2026年中国国际服务贸易交易会(以下简称服贸会)在北京首钢园举办,今年的...
湖北大学最新或2023(历届)...   1、考生自最新或2023(历届)4月1日起可在湖北大学招生信息网(http://zsxx.hub...