您的位置:首页 > 运维架构 > Linux

Linux下C语言的调试器 Gdb

2014-02-26 14:36 323 查看
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
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各个分类的命令, 注意底部的加粗部分说明其为分类命令. 接下来可以具体查找各个分类的命令.
(gdb) help breakpoints
Making program stop at certain points.

List of commands:

awatch -- Set a watchpoint for an expression
break -- Set breakpoint at specified line or function
catch -- Set catchpoints to catch events
catch assert -- Catch failed Ada assertions
catch catch -- Catch an exception
catch exception -- Catch Ada exceptions
catch exec -- Catch calls to exec
catch fork -- Catch calls to fork
catch syscall -- Catch system calls by their names and/or numbers
catch throw -- Catch an exception
catch vfork -- Catch calls to vfork
clear -- Clear breakpoint at specified line or function
commands -- Set commands to be executed when a breakpoint is hit
condition -- Specify breakpoint number N to break only
if COND is true
delete -- Delete some breakpoints or auto-display expressions
delete breakpoints -- Delete some breakpoints or auto-display expressions
delete checkpoint -- Delete a checkpoint (experimental)
........

Gdb使用的注意点:
在Gcc编译选项中一定要加入 "-g"
只有在代码处于"运行"或"暂停'状态时才能查看变量值
设置断点后程序在指定行之前停止

(1) 工作环境相关命令
    Gdb中不仅可以调试所运行的程序, 而且还可以对程序相关的工作环境进行相应的设置, 甚至还可以使用shell中的命令进行相关的操作, 其功能极其强大. 
                                       Gdb工作环境相关的命令
 命令格式含义 
 set args 运行时的参数指定运行时参数 如 set args 2 
 show args查看设置好的运行参数 
path dir 设置程序的运行路径 
show paths 查看程序的运行路径 
set enVironment var [=value] 设置环境变量 
show enVironment [var] 查看环境变量 
cd dir 进入到dir目录, 相当于shell中的cd命令 
pwd 显示当前工作目录 
shell command  运行shell的command命令
2. 设置断点与恢复命令
                                     Gdb中设置断点与恢复的常见命令
 命令格式含义 
info b 查看所设断点 
break 行号或函数名 <条件表达式> 设置断点 
tbreak 行号或函数名 <条件表达式> 设置临时断点, 到达后被自动删除 
delete [断点号] 删除指定断点, 其断点号为"info b"中的第一列的数字. 若缺省断点号则删除所有断点 
disable [断点号] 停止指定断点, 使用"info b"仍能查看到此断点. 同delete一样, 省略断点号则停止所有断点 
 enable [断点]激活指定断点, 即激活被disable停止的断点 
condition [断点号] <条件表达式> 修改对应断点的条件 
ignore [断点号]<num> 在程序执行中, 忽略对应断点num次 
step 单步恢复程序运行, 且进入调用\函数的内部
 next单步恢复程序运行, 但不进入函数调用 
finish 运行程序, 直到当前函数完成返回 
继续执行函数, 直到函数结束或遇到新的断点 
(3) Gdb中源代码查看相关命令
 命令格式含义 
list <行号>|<函数名> 查看指定位置的代码 
file [文件名] 加载指定文件 
forward-search 正则表达式 源代码前向搜索 
reverse-search 正则表达式 源代码后向搜索 
 dir dir体质路径名 
show directories 显示定义了的源文件搜索路径 
info line 显示加载到Gdb内存中的代码 
(4) Gdb 中查看运行数据相关命令
 命令格式含义 
print 表达式|变量 查看程序运行时对应表达式和变量的值 
x <n/f/u> 查看内存变量内容. 其中n为整数表示显示内存的长度, f表示显示的格式, u表示从当前地址往后请求显示的字节数 
display 表达式 设定在单步运行或其他情况中, 自动显示的对应表达式的内容 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: