英文原文:http://beej.us/guide/bggdb/
概述
这是一篇关于 GNU 调试器—— gdb 在命令终端使用的快速指南,如果想要了解更多,请参阅 Official GDB Documentation 或者使用man gdb
。
gdb 经常是通过 IDE 运行,但是很多人由于各种各样的原因不使用 IDE,那么可以在终端中通过命令行来使用 gdb。
另外,有一个比较好的 gdb 前端 DDD(the Data Display Debugger) 。
准备
要使用 gdb 调试的程序必须包含符号调试信息,这样才能在调试器中看到程序列表。如果使用 gcc 编译器,那么编译时需要加上-g
选项,例如:
$ gcc -g hello.c -o hello
$ g++ -g hello.cpp -o hello
开始
命令行中输入 gdb hello
进入调试器:
$ gdb hello
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 <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 hello...done.
(gdb)
在调试器的命令提示行输入 help
,来获取命令列表,也可参看文章末尾的命名列表:
(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
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands
Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
(gdb)
使用quite
或者q
退出调试器:
(gdb) q
$
两种打开调试器方式:控制台模式,GUI 窗口模式
让我们先来简单操作一下控制台模式:
$ gdb hello
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 <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 documentat