本文共 3119 字,大约阅读时间需要 10 分钟。
Gdb 的命令可以通过查看help 进行查找,由于Gdb 的命令很多,因此Gdb 的help 将其分成了很多种类(class),用户可以通过进一步查看相关class找到相应的命令。如下所示: List of classes of commands: aliases -- Aliases of other commands breakpoints -- Making program stop at certain points 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 各个分类的命令。接下来可以具体查找各分类种的命令。如下所示: 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”。 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 中的命令主要可以分为以下几类:工作环境相关命令、设置断点与恢复命令、源代码查看命令、查看运行数据相关命令及修改运行参数命令。以下就分别对这几类的命令进行讲解。 Gdb中不仅可以调试所运行的程序,而且还可以对程序相关的工作环境进行相应的设定,甚至还可以使用shell 中的命令进行相关的操作,其功能极其强大。Gdb的常见工作环境相关命令如表 3‑8所示。 Gdb中设置断点与恢复的常见命令如表 3‑9所示。 由于设置断点在Gdb的调试中非常重要,所以在此再着重讲解一下Gdb中设置断点的方法。Gdb 中设置断点有多种方式:其一是按行设置断点,设置方法在前面已经指出,在此就不重复了。另外还可以设置函数断点和条件断点,在此结合上一小节的代码,具体介绍后两种设置断点的方法。 Gdb 中按函数设置断点只需要把函数名列在命令“b”之后,如下所示: Breakpoint 1 at 0x80484ba: file test.c, line 16. Num Type Disp Enb Address What 1 breakpoint keep y 0x080484ba in sum at test.c:16 要注意的是,此时的断点实际是在函数的定义处,也就是在16 行处(注意第16 行还未执行)。 Gdb 中设置条件断点的格式为:b 行数或函数名if 表达式。具体实例如下面所示: Breakpoint 1 at 0x804848c: file test.c, line 8. Num Type Disp Enb Address What 1 breakpoint keep y 0x0804848c in main at test.c:8 Starting program: /home/yul/test Breakpoint 1, main () at test.c:9 可以看到,该例中在第8行(也就是运行完第7行的for循环)设置了一个“i==0”的条件断点,在程序运行之后可以看出,程序确实在i为10的时候暂停运行。 在Gdb中可以查看源码以方便其他操作,它的常见相关命令如表 3‑10所示。 Gdb 中查看运行数据是指当程序处于“运行”或“暂停”状态时,可以查看的变量及表达式的信息,其常见命令如表 3‑11所示: Gdb 还可以修改运行时的参数,并使该变量按照用户当前输入值继续运行。它的设置方式为:在单步执行的过程中,键入命令“set 变量=设定值”。这样,在此之后,程序就会按照该设定的值运行了。下面,笔者结合上一节的代码将n的初始值设为4,其代码如下所示: Breakpoint 5 at 0x804847a: file test.c, line 7. Starting program: /home/yul/test Breakpoint 5, main () at test.c:7 Program exited with code 031. 只有在代码处于“运行”或“暂停”状态时才能查看变量值。 本文转载于,【Linux基础】Gdb基本命令 (出处: )