您的位置:首页 > 其它

GDB调试方法汇总

2016-06-15 19:36 309 查看
一、初始化

输入gdb进入gdb调试环境。或者直接输入gdb + progfile来加载文件。

注意该文件是使用gcc(或g++)编译得到的。为了使 gdb 正常工作, 必须

使你的程序在编译时包含调试信息,编译时必须使用-g参数来。

或者进入gdb环境后,通过命令file + progfile来加载需要调试的可

执行文件文件。

查看源代码:list [函数名][行数]

设置程序运行参数:set args

二、暂停程序   

gdb可以使用几种方式来暂停程序:断点,观察点,捕捉点,信号,线

程停止。当程序被暂停后,可以使用continue、next、step来继续执行程序。

continue         执行到下一暂停点或程序结束。

next         执行一行源代码但不进入函数内部。

step        执行一行源代码而且进入函数内部。

1、设置断点:

a、break + [源代码行号][源代码函数名][内存地址]

b、break ... if condition   ...可以是上述任一参数,condition

条件。例如在循环体中可以设置break ... if i = 100 来设置循环次数。

2、设置观察点:

a、watch + [变量][表达式]  当变量或表达式值改变时即停住程序。

b、rwatch + [变量][表达式] 当变量或表达式被读时,停住程序。

c、awatch + [变量][表达式] 当变量或表达式被读或被写时,停住程序。

3、设置捕捉点:

catch + event  当event发生时,停住程序。event可以是下面的内容:

1)、throw 一个C++抛出的异常。(throw为关键字)

2)、catch 一个C++捕捉到的异常。(catch为关键字)

3)、exec 调用系统调用exec时。(exec为关键字,目前此功能只在HP-UX下有用)

4)、fork 调用系统调用fork时。(fork为关键字,目前此功能只在HP-UX下有用)

5)、vfork 调用系统调用vfork时。(vfork为关键字,目前此功能只在HP-UX下有用)

6)、load 或 load 载入共享库(动态链接库)时。(load为关键字,

目前此功能只在HP-UX下有用)

7)、unload 或 unload 卸载共享库(动态链接库)时。(unload为关

键字,目前此功能只在HP-UX下有用)

4、捕捉信号:

handle + [argu] + signals

signals:是Linux/Unix定义的信号,SIGINT表示中断字符信号,也就是

Ctrl+C的信号,SIGBUS表示硬件故障的信号;SIGCHLD表示子进程状态改

变信号; SIGKILL表示终止程序运行的信号,等等。

argu:

nostop   当被调试的程序收到信号时,GDB不会停住程序的运行,但

会打出消息告诉你收到这种信号。

stop     当被调试的程序收到信号时,GDB会停住你的程序。

print    当被调试的程序收到信号时,GDB会显示出一条信息。

noprint  当被调试的程序收到信号时,GDB不会告诉你收到信号的信息。

pass or noignore    当被调试的程序收到信号时,GDB不处理信号。

这表示,GDB会把这个信号交给被调试程序会处理。

nopass or ignore     当被调试的程序收到信号时,GDB不会让被调

试程序来处理这个信号。

5、线程中断:

break [linespec] thread [threadno] [if ...]

linespec 断点设置所在的源代码的行号。如: test.c:12表示文件为

test.c中的第12行设置一个断点。

threadno 线程的ID。是GDB分配的,通过输入info threads来查看正在

运行中程序的线程信息。

if ...   设置中断条件。

三、查看信息

1、查看数据

print  variable        查看变量

print  *array@len      查看数组(array是数组指针,len是需要数据长度)

可以通过添加参数来设置输出格式:

/x 按十六进制格式显示变量。

/d 按十进制格式显示变量。

/u 按十六进制格式显示无符号整型。

/o 按八进制格式显示变量。

/t 按二进制格式显示变量。

/a 按十六进制格式显示变量。

/c 按字符格式显示变量。

/f 按浮点数格式显示变量。

2、查看内存

examine /n f u + 内存地址(指针变量)

n 表示显示内存长度

f 表示输出格式(见上)

u 表示字节数制定(b 单字节;h 双字节;w 四字节;g 八字节;默认为四字节)

如:

x /10cw pFilePath  (pFilePath为一个字符串指针,指针占4字节)

x 为examine命令的简写。

3、查看栈信息      

backtrace [-n]

n  表示只打印栈顶上n层的栈信息。

-n 表示只打印栈底上n层的栈信息。

不加参数,表示打印所有栈信息。

4、info

info address -- Describe where symbol SYM is stored

info all-registers -- List of all registers and their contents

info args -- Argument variables of current stack frame

info auxv -- Display the inferior's auxiliary vector

info breakpoints -- Status of user-settable breakpoints

info catch -- Exceptions that can be caught in the current stack frame

info checkpoints -- IDs of currently known forks/checkpoints

info classes -- All Objective-C classes

info common -- Print out the values contained in a Fortran COMMON block

info copying -- Conditions for redistributing copies of GDB

info dcache -- Print information on the dcache performance

info display -- Expressions to display when program stops

info extensions -- All filename extensions associated with a source language

info files -- Names of targets and files being debugged

info float -- Print the status of the floating point unit

info forks -- IDs of currently known forks/checkpoints

info frame -- All about selected stack frame

info functions -- All function names

info handle -- What debugger does when program gets various signals

info line -- Core addresses of the code for a source line

info linkmap -- Display the inferior's linkmap

info locals -- Local variables of current stack frame

info macro -- Show the definition of MACRO

info mem -- Memory region attributes

info proc -- Show /proc process information about any running process

info program -- Execution status of the program

info registers -- List of integer registers and their contents

info scope -- List the variables local to a scope

info selectors -- All Objective-C selectors

info set -- Show all GDB settings

info sharedlibrary -- Status of loaded shared object libraries

info signals -- What debugger does when program gets various signals

info source -- Information about the current source file

info sources -- Source files in the program

info stack -- Backtrace of the stack

info symbol -- Describe what symbol is at location ADDR

info target -- Names of targets and files being debugged

info terminal -- Print inferior's saved terminal status

info threads -- IDs of currently known threads

info tracepoints -- Status of tracepoints

info types -- All type names

info variables -- All global and static variable names

info vector -- Print the status of the vector unit

info warranty -- Various kinds of warranty you do not have

info watchpoints -- Synonym for ``info breakpoints''

info win -- List of all displayed windows

附注:

基本gdb命令:

---------------------------------------------------------------------

命令          简写         功能

---------------------------------------------------------------------

file                             装入想要调试的可执行文件.

kill             k              终止正在调试的程序.

list             l               列出产生执行文件的源代码的一部分.

next           n              执行一行源代码但不进入函数内部.

step          s              执行一行源代码而且进入函数内部.

continue  c               继续执行程序,直至下一中断或者程序结束。

run            r               执行当前被调试的程序.

quit           q               终止 gdb.

watch                        使你能监视一个变量的值而不管它何时被改变.

catch                         设置捕捉点.

thread       t               查看当前运行程序的线程信息.

break        b              在代码里设置断点, 这将使程序执行到这里时被挂起.

make                        使你能不退出 gdb 就可以重新产生可执行文件.

shell                         使你能不离开 gdb 就执行 UNIX shell 命令. 

print          p              打印数据内容。

examine  x               打印内存内容。
backtrace bt             查看函数调用栈的所有信息。

更多内容转自:http://www.cnblogs.com/visayafan/archive/2011/09/27/2193632.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: