理解 gdb

来源:互联网 发布:哪些网络兼职可信 编辑:程序博客网 时间:2024/06/05 20:19

http://baike.baidu.com/view/639266.htm

文件清单

List
(gdb) list line1,line2
查看源代码
list lineNum 在lineNum的前后源代码显示出来
list + 列出当前行的后面代码行
list - 列出当前行的前面代码行
list function
set listsize count
设置显示代码的行数
show listsize
显示打印代码的行数
list first,last
显示从first到last的源代码行

执行程序

要想运行准备调试的程序,可使用run命令,在它后面可以跟随发给该程序的任何参数,包括标准输入和标准输出说明符(<;和>;)和shell通配符(*、?、[、])在内。如果你使用不带参数的run命令,gdb就再次使用你给予前一条run命令的参数,这是很有用的。利用set args 命令就可以修改发送给程序的参数,而使用show args 命令就可以查看其缺省参数的列表。
(gdb)set args –b –x
(gdb) show args
backtrace命令为堆栈提供向后跟踪功能。
Backtrace 命令产生一张列表,包含着从最近的过程开始的所有有效过程和调用这些过程的参数。

显示数据

利用print 命令可以检查各个变量的值。
(gdb) print p (p为变量名)
print 是gdb的一个功能很强的命令,利用它可以显示被调试的语言中任何有效的表达式。表达式除了包含你程序中的变量外,还可以包含以下内容:
对程序中函数的调用
(gdb) print find_entry(1,0)
数据结构和其他复杂对象
(gdb) print *table_start
={e=reference=’\000’,location=0x0,next=0x0}
值的历史成分
(gdb)print (为历史记录变量,在以后可以直接引用的值)
人为数组
人为数组提供了一种去显示存储器块(数组节或动态分配的存储区)内容的方法。早期的调试程序没有很好的方法将任意的指针换成一个数组。就像对待参数一样,让我们查看内存中在变量h后面的10个整数,一个动态数组的语法如下所示:
base@length
因此,要想显示在h后面的10个元素,可以使用h@10:
(gdb)print h@10
=(-1,345,23,-234,0,0,0,98,345,10)
whatis命令可以显示某个变量的类型
(gdb) whatis p
type = int *

断点

break命令(可以简写为b)可以用来在调试的程序中设置断点,该命令有如下四种形式:
break line-number 使程序恰好在执行给定行之前停止。
break function-name 使程序恰好在进入指定的函数之前停止。
break line-or-function if condition 如果condition(条件)是真,程序到达指定行或函数时停止。
break routine-name 在指定例程的入口处设置断点
如果该程序是由很多原文件构成的,你可以在各个原文件中设置断点,而不是在当前的原文件中设置断点,其方法如下:
(gdb) break filename:line-number
(gdb) break filename:function-name
要想设置一个条件断点,可以利用break if命令,如下所示:
(gdb) break line-or-function if expr
例:
(gdb) break 46 if testsize==100
从断点继续运行:continue 命令

断点管理

1.显示当前gdb的断点信息:
(gdb) info break
他会以如下的形式显示所有的断点信息:
Num Type Disp Enb Address What
1 breakpoint keep y 0x000028bc in init_random at qsort2.c:155
2 breakpoint keep y 0x0000291c in init_organ at qsort2.c:168
删除指定的某个断点:
(gdb) delete breakpoint 1
该命令将会删除编号为1的断点,如果不带编号参数,将删除所有的断点
(gdb) delete breakpoint
禁止使用某个断点
(gdb) disable breakpoint 1
该命令将禁止断点1,同时断点信息的 (Enb)域将变为 n
允许使用某个断点
(gdb) enable breakpoint 1
该命令将允许断点1,同时断点信息的 (Enb)域将变为 y
清除原文件中某一代码行上的所有断点
(gdb)clear number
注:number 为原文件的某个代码行的行号

变量检查赋值

whatis:识别数组或变量的类型
ptype:比whatis的功能更强,他可以提供一个结构的定义
set variable = value:将值赋予变量
print variable = value or p variable = value : 除了显示一个变量的值外,还可以用来赋值

单步执行

next 不进入的单步执行
step 进入的单步执行如果已经进入了某函数,而想退出该函数返回到它的调用函数中,可使用命令finish

函数调用

call name 调用和执行一个函数
(gdb) call gen_and_sork(1234,1,0)
(gdb) call printf(“abcd”)
=4
finish 结束执行当前函数,显示其返回值(如果有的话)

机器语言工具

有一组专用的gdb变量可以用来检查和修改计算机的通用寄存器,gdb提供了目前每一台计算机中实际使用的4个寄存器的标准名字:
$pc :程序计数器
$fp :帧指针(当前堆栈帧)
$sp :栈指针
$ps :处理器状态


http://www.bobbog.com/archives/24

GDB 内存断点的使用例子:

http://blog.csdn.net/livelylittlefish/article/details/5110234#quote

 

在使用的过程中有什么忘掉了或是不懂的就直接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.

 

比如,我的可执行文件包含多个C/C++ 源文件,想查看其中一个文件的指定行,使用list 命令,help 如下

(gdb) help list
List specified function or line.
With no argument, lists ten more lines after or around previous listing.
"list -" lists the ten lines before a previous ten-line listing.
One argument specifies a line, and ten lines are listed around that line.
Two arguments with comma between specify starting and ending lines to list.
Lines can be specified in these ways:
  LINENUM, to list around that line in current file,
  FILE:LINENUM, to list around that line in that file,
  FUNCTION, to list around beginning of that function,
  FILE:FUNCTION, to distinguish among like-named static functions.
  *ADDRESS, to list around the line containing that address.
With two args if one is empty it stands for ten lines away from the other arg.

 数据读断点/写断点 :

(gdb) help awatchSet a watchpoint for an expression.Usage: awatch [-l|-location] EXPRESSIONA watchpoint stops execution of your program whenever the value ofan expression is either read or written.If -l or -location is given, this evaluates EXPRESSION and watchesthe memory to which it refers.


数据写断点:

(gdb) help watchSet a watchpoint for an expression.Usage: watch [-l|-location] EXPRESSIONA watchpoint stops execution of your program whenever the value ofan expression changes.If -l or -location is given, this evaluates EXPRESSION and watchesthe memory to which it refers.