GDB的使用
主要针对C/C++程序的调试过程。
1. 编译源文件
$ gcc –g hello.c –o hello
或
$ g++ -g hello.cpp –o hello
2. 打开GDB调试工具
$ gdb hello
启动GDB的几种方式
1. gdb <program>program 也就是你的执行文件,一般在当前目录下。
2. gdb <program> core
用gdb 同时调试一个运行程序和core 文件,core 是程序非法执行后core dump 后产生的文件。
3. gdb <program> <PID>
如果你的程序是一个服务程序,那么你可以指定这个服务程序运行时的进程ID。gdb会自动attach 上去,并调试它。program 应该在PATH 环境变量中搜索到。
常用开关
最常用的也就是第一种启动方式,在启动gdb是有一些开关,详细的开关可以用gdb –help 来查看
-symbols <file>-s <file>
从指定文件中读取符号表。
-se file
从指定文件中读取符号表信息,并把他用在可执行文件中。
GDB命令概貌
启动gdb 后,就进入了gdb 的调试环境,就可以使用gdb 的命令开始调试程序了,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.
常用的几种类型
aliases 命令别名
breakpoints 断点定义
data 数据查看
files 指定并查看文件
internals 维护命令
running 程序执行
stack 调用栈查看
statu 状态查看
tracepoints 跟踪程序执行
until 跳到某行(单步执行的时候常用)
finish 单步执行的时候,跳出进入的子函数
breakpoints 断点定义
data 数据查看
files 指定并查看文件
internals 维护命令
running 程序执行
stack 调用栈查看
statu 状态查看
tracepoints 跟踪程序执行
until 跳到某行(单步执行的时候常用)
finish 单步执行的时候,跳出进入的子函数
gdb 的命令很多,gdb 把之分成很多种类。help 命令只是列出了gdb 的命令种类,如果要看种类中的命令,使用help <class>命令,如:help breakpoints,查看设置断点的所有命令。也可以直接help <command>来查看命令的帮助。
常用的调试命令
1. 断点breakpoint
设置断点
可以通过break+“行号”/"函数名"添加断点,例如:
(gdb) break 13
Breakpoint 1 at 0x80483fa: file gdb_test.c, line 13.
(gdb) break func
Breakpoint 2 at 0x80483ca: file gdb_test.c, line 5.
查看断点
(gdb) info breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y 0x080483fa in main at gdb_test.c:13
2 breakpoint keep y 0x080483ca in func at gdb_test.c:5
断点管理
1. 显示当前gdb的断点信息: info break
2. 删除指定的某个断点: delete breakpoint [编号] 不带编号,删除所有断点。
2. 删除指定的某个断点: delete breakpoint [编号] 不带编号,删除所有断点。
3. 禁止使用某个断点 : disable breakpoint 编号
4. 允许使用某个断点 :enable breakpoint 编号
5. 清除原文件中某一代码行上的所有断点 : clean 行号
2. run执行
continue 程序在断点处继续执行
next 单步执行(不进入子函数)
setp 单步执行(进入子函数)
until 跳出循环体或子函数