博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Linux基础】Gdb基本命令
阅读量:4282 次
发布时间:2019-05-27

本文共 3119 字,大约阅读时间需要 10 分钟。

Gdb 的命令可以通过查看help 进行查找,由于Gdb 的命令很多,因此Gdb 的help 将其分成了很多种类(class),用户可以通过进一步查看相关class找到相应的命令。如下所示:
(gdb) help
List of classes of commands:
aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
Type "help" followed by a class name for a list of commands in that class.
Type "help" followed by command name for full documentation.
Command name abbreViations are allowed if unambiguous.
上述列出了Gdb 各个分类的命令。接下来可以具体查找各分类种的命令。如下所示:
(gdb) help data
Examining data.
List of commands:
call -- Call a function in the program
delete display -- Cancel some expressions to be displayed when program stops
delete mem -- Delete memory region
disable display -- Disable some expressions to be displayed when program stops
Type "help" followed by command name for full documentation.
Command name abbreViations are allowed if unambiguous.
至此,若用户想要查找call命令,就可键入“help call”。
(gdb) help call
Call a function in the program.
The argument is the function name and arguments, in the notation of the
current working language. The result is printed and saved in the value
history, if it is not void.
当然,若用户已知命令名,直接键入“help [command]”也是可以的。
Gdb 中的命令主要可以分为以下几类:工作环境相关命令、设置断点与恢复命令、源代码查看命令、查看运行数据相关命令及修改运行参数命令。以下就分别对这几类的命令进行讲解。
1. 工作环境相关命令
Gdb中不仅可以调试所运行的程序,而且还可以对程序相关的工作环境进行相应的设定,甚至还可以使用shell 中的命令进行相关的操作,其功能极其强大。Gdb的常见工作环境相关命令如表 3‑8所示。

01.jpg (52.65 KB, 下载次数: 0)

前天 09:17 上传

2. 设置断点与恢复命令
Gdb中设置断点与恢复的常见命令如表 3‑9所示。

02.jpg (90.65 KB, 下载次数: 0)

前天 09:19 上传

由于设置断点在Gdb的调试中非常重要,所以在此再着重讲解一下Gdb中设置断点的方法。Gdb 中设置断点有多种方式:其一是按行设置断点,设置方法在前面已经指出,在此就不重复了。另外还可以设置函数断点和条件断点,在此结合上一小节的代码,具体介绍后两种设置断点的方法。
① 函数断点
Gdb 中按函数设置断点只需要把函数名列在命令“b”之后,如下所示:
(gdb) b sum
Breakpoint 1 at 0x80484ba: file test.c, line 16.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x080484ba in sum at test.c:16
要注意的是,此时的断点实际是在函数的定义处,也就是在16 行处(注意第16 行还未执行)。
② 条件断点
Gdb 中设置条件断点的格式为:b 行数或函数名if 表达式。具体实例如下面所示:
(gdb) b 8 if i==10
Breakpoint 1 at 0x804848c: file test.c, line 8.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0804848c in main at test.c:8
stop only if i == 10
(gdb) r
Starting program: /home/yul/test
The sum of 1-m is 1275
Breakpoint 1, main () at test.c:9
9 n += i;
(gdb) p i
$1 = 10
可以看到,该例中在第8行(也就是运行完第7行的for循环)设置了一个“i==0”的条件断点,在程序运行之后可以看出,程序确实在i为10的时候暂停运行。
3. Gdb中源码查看相关命令
在Gdb中可以查看源码以方便其他操作,它的常见相关命令如表 3‑10所示。

03.jpg (43.92 KB, 下载次数: 0)

前天 09:21 上传

4. Gdb 中查看运行数据相关命令
Gdb 中查看运行数据是指当程序处于“运行”或“暂停”状态时,可以查看的变量及表达式的信息,其常见命令如表 3‑11所示:

04.jpg (38.09 KB, 下载次数: 0)

前天 09:23 上传

5. Gdb 中修改运行参数相关命令
Gdb 还可以修改运行时的参数,并使该变量按照用户当前输入值继续运行。它的设置方式为:在单步执行的过程中,键入命令“set 变量=设定值”。这样,在此之后,程序就会按照该设定的值运行了。下面,笔者结合上一节的代码将n的初始值设为4,其代码如下所示:
(Gdb) b 7
Breakpoint 5 at 0x804847a: file test.c, line 7.
(Gdb) r
Starting program: /home/yul/test
The sum of 1-m is 1275
Breakpoint 5, main () at test.c:7
7 for(i=1; i<=50; i++)
(Gdb) set n=4
(Gdb) c
Continuing.
The sum of 1-50 is 1279
Program exited with code 031.
可以看到,最后的运行结果的确比之前的值大了4。
Gdb的使用切记点:
在Gcc编译选项中一定要加入“-g”。
只有在代码处于“运行”或“暂停”状态时才能查看变量值。
设置断点后程序在指定行之前停止。
本文转载于,【Linux基础】Gdb基本命令
(出处: )
你可能感兴趣的文章
linux下的"BusHound"——usb_mon非常好
查看>>
linux usb枚举过程分析【host】
查看>>
android之通过USB插拔流程来了解android UEvent
查看>>
[RK3288][Android6.0] USB 枚举过程小结
查看>>
Android调试方法
查看>>
android的usb作为从设备的程序流程
查看>>
android作为主usb设备,加载流程
查看>>
Linux 下使用USB 网络
查看>>
CarPlay简介
查看>>
CarPlay wired(USB)连接方案
查看>>
CarPlay介绍
查看>>
CarPlay wireless(蓝牙+WiFi)连接方案(蓝牙部分)
查看>>
CarPlay wireless(蓝牙+WiFi)连接方案(Wi-Fi部分)
查看>>
CarPlay wired连接与wireless连接相互切换
查看>>
USB linux NCM usbnet驱动详解
查看>>
USB OTG规范的SRP和HNP协议
查看>>
USB协议架构及驱动架构
查看>>
usb-OTG-ADP-HNP-SRP
查看>>
usb驱动的层次结构简述
查看>>
控制Linux内核启动中的打印
查看>>