绝对新手Linux学习笔记(9)初试GDB

 

 

gdb是GNU开源组织发布的一个强大的UNIX下的程序调试工具。农民甲刚刚开始用它,还不是很熟悉,不过即便如此,昨天居然也用它发现了一个很复杂的程序的bug,恩~好的开始是成功的一半。

 

编译的时候使用编译器(cc/gcc/g++)的 -g 参数可以将调试信息加到可执行文件中,这样我们在使用gdb调试的时候就不会只是看到一片的地址偏移量了!

 

编译好了一个可执行文件之后,最简单的方法就是直接:

$ gdb <your program>

进行调试了。此时会进入调试状态,前面的提示符变成:

(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.

 

这里gdb把它的命令分成了12个类别,要想知道某个类别具体有哪些命令,可以输入help <类别名>,如我们来看breakpoint相关命令:

(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
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 display -- Cancel some expressions to be displayed when program stops
delete mem -- Delete memory region
delete tracepoints -- Delete specified tracepoints
disable -- Disable some breakpoints
disable breakpoints -- Disable some breakpoints
disable display -- Disable some expressions to be displayed when program stops
disable mem -- Disable memory region
disable tracepoints -- Disable specified tracepoints
enable -- Enable some breakpoints
enable delete -- Enable breakpoints and delete when hit
enable display -- Enable some expressions to be displayed when program stops
enable mem -- Enable memory region
enable once -- Enable breakpoints for one hit
enable tracepoints -- Enable specified tracepoints
hbreak -- Set a hardware assisted  breakpoint
ignore -- Set ignore-count of breakpoint number N to COUNT
rbreak -- Set a breakpoint for all functions matching REGEXP
rwatch -- Set a read watchpoint for an expression
tbreak -- Set a temporary breakpoint
tcatch -- Set temporary catchpoints to catch events
thbreak -- Set a temporary hardware assisted breakpoint
watch -- Set a watchpoint for an expression

 

想看某个具体命令的用法?没问题,还是help,如看看设置断点的命令break:

(gdb) help break
Set breakpoint at specified line or function.
break [LOCATION] [thread THREADNUM] [if CONDITION]
LOCATION may be a line number, function name, or "*" and an address.
If a line number is specified, break at start of code for that line.
If a function is specified, break at start of code for that function.
If an address is specified, break at that exact address.
With no LOCATION, uses current execution address of selected stack frame.
This is useful for breaking on return to a stack frame.

THREADNUM is the number from "info threads".
CONDITION is a boolean expression.

Multiple breakpoints at one place are permitted, and useful if conditional.

 

gdb中,输入命令时,可以不用打全命令,只用打命令的前几个字符就可以了,当然,命令的前几个字符应该要标志着一个唯一的命令,在Linux下,你可以敲击两次TAB键来补齐命令的全称,如果有重复的,那么gdb会把其例出来。如:

b    break    添加断点

r    run     开始执行

c    continue    继续执行

l    list    列出源码

n    next    单步执行

s    step     单步进入

p    print     打印变量值

[ENTER]    直接回车表示重复上一次命令

q    quit    退出gbd

。。。。。。

 

 

一般的初级应用流程是:

1。$ gdb <your program>

2。(gdb) b <xxx>

xxx是行号或者函数名等

3。(gdb) set args <yyy>

yyy是需要设置的your program运行的参数

4。(gdb) r

5。(gdb) p <zzz>

zzz是需要察看的变量名

。。。。。。

 

 

恩,初级的农民甲暂时只能到这一步了,我们会在之后继续深入学习!

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值