Debugging with GDB学习记录(三)

来源:互联网 发布:织梦cms模板免费下载 编辑:程序博客网 时间:2024/05/15 09:10
5、Stopping and Continuing


程序在调试过程中会常常中止,虽然GDB会提供当时程序状态,也可以用info program来在任意时刻查看信息。
(gdb) info program 
Using the running image of attached Thread 0x2ad630d34900 (LWP 3757).
Program stopped at 0x3dd80d4018.


Breakpoints、Watchpoints和Catchpoints


可用break命令及其变种设置断点,对每个断点,可以增加conditions来控制细节来决定程序是否停止。
On some systems, you can set breakpoints in shared libraries before the executable is
run. There is a minor limitation on HP-UX systems: you must wait until the executable
is run in order to set breakpoints in shared library routines that are not called directly by
the program (for example, routines that are arguments in a pthread_create call).


watchpoint:特殊breakpoint,当某个表达式的值变化时,停止程序。可以设计,让程序在breakpoint停止时,显示程序一些变量等的值


catchpoint:特殊breakpoint,当特定种类事件发生时,停止程序。


每个breakpoint、watchpoint和catchpoint都有正数数字编号。断点可使能、禁止。禁止后,在重新使能之前,对程序不起作用。


一些GDB命令接受要操作的断点范围:,如‘5’,‘5-7’,当一个断点范围被交给一个命令时,所有该范围内的断点都会被操作。


断点设置
GDB调试器使用变量‘$bpnum’来记录您最近设置的断点的断点号。
1、break location
在指定location处设置断点:可以是函数名、行号或者指令地址。在执行断点处的代码之前会停止程序执行。
关于如何指定位置、语言支持函数重载时、只有特定线程/进程hit断点才停止程序等等,可查看后续博文。
2、break
当不带参数时
When called without any arguments, break sets a breakpoint at the next instruction
to be executed in the selected stack frame (see Chapter 8 [Examining
the Stack], page 79). In any selected frame but the innermost, this makes your
program stop as soon as control returns to that frame. This is similar to the
effect of a finish command in the frame inside the selected frame—except that
finish does not leave an active breakpoint. If you use break without an argument
in the innermost frame, gdb stops the next time it reaches the current
location; this may be useful inside loops.


3、break ... if cond
每当到达断点时,检查cond值,当cond值非0时,停止。
4、 tbreak args
仅设置使用一次的断点。args同break命令参数。在程序第一次停在该断点处后,就自动删除该断点。


5、hbreak args
设置硬件辅助断点。断点参数args和break命令一样,但是这里的断点需要硬件支持,也许有些硬件没这支持功能。主要用于
EPROM/ROM代码编译。但是硬件断点寄存器数量是有限制的,一般比较少。


6、thbreak args
设置一个硬件辅助断点。在程序第一次在这地停止后,自动删除该断点。需硬件支持,也可能一些硬件不支持


7、rbreak regex
与正则表达式regex匹配的所有函数上设置断点。设置的是unconditional断点,可以使能、禁止,甚至是使其成为conditional断点。
正则表达式语法类似'grep',与shell的不同。


在调试C++时,rbreak用于在不是任何特殊类成员的重载函数上设置断点。


可用rbreak在程序中所有函数上设置断点:(gdb)rbreak .


8、rbreak file:regex

仅在该file文件内查找匹配regex的函数。


9、info breakpoints [n ...]
   info break [n ...]
打印所有现存的breakpoints、watchpoints和catchpoints表,参数n指明仅打印某个的信息
Type        :      breakpoint、watchpoint或catchpoint
Disposition :       Whether the breakpoint is marked to be disabled or deleted when hit.
Enabled 或Disabled:y表示断点enabled,n表示not enabled
Address:           断点在程序中的位置,如内存地址。对于pending断点,则该域包含<PENDING>。Such breakpoint won’t fire until a
                       shared library that has the symbol or line referred by breakpoint isloaded。如果一个断点有多个位置,则该域中包含<MULTIPLE>
What    :             断点在源文件中的位置。For a pending breakpoint, the original string passed to the breakpoint command will be listed as it cannot be resolved
                       until the appropriate shared library is loaded in the future.


If a breakpoint is conditional, info break shows the condition on the line following the affected breakpoint; breakpoint commands, if any, are listed after
that. A pending breakpoint is allowed to have a condition specified for it. The condition is not parsed for validity until a shared library is loaded that allows
the pending breakpoint to resolve to a valid location.


info break会显示出该断点被hit多少次,可以用ignore命令忽略不关心的断点,但忽略一次,该hit值减一
GDB允许在程序同一位置设置任意数量的断点。一个断点也可能与程序中多个位置关联。
 For a C++ constructor, the gcc compiler generates several instances of the function body, used in different cases.
 For a C++ template function, a given line in the function can correspond to any number of instantiations.
 For an inlined function, a given source line can correspond to several places where that function is inlined.
在上述例子中,GDB会在所有相关位置插入一个断点,显示这种断点时,会多行显示:
For example:
Num     Type       Disp   Enb Address What
1       breakpoint keep   y   <MULTIPLE>
        stop only if i==1
        breakpoint already hit 1 time
1.1                       y 0x080486a2 in void foo<int>() at t.cc:8
1.2                       y 0x080486ca in void foo<double>() at t.cc:8
可以在enable和disable命令中传递‘断点号.位置号’来单一使能/禁止某个断点。
注意:你不能删除上面列表的单一位置,而只能以delete num来删除parent breakpoint来删除整个列表,使能/禁止parent breakpoint会影响到
属于该断点的所有位置。


可以在共享库中设置断点,但共享库在程序执行过程中,可动态加载,导致断点位置不确定,为支持该特性,在调试session开始处可以设置一个断点,
该断点是pending的,因为共享库还没加载,共享库这你哦个的Symbols也不可用。当程序执行后,包含该pending断点相关的符号的共享库加载了,则
pending breakpoints变成普通断点,但是一旦库卸载,则所有与该库中符号相关的断点都变成pending了。


This logic works for breakpoints with multiple locations, too. For example, if you havea breakpoint in a C++ template function, and a newly loaded
 shared library has an instantiation of that template, a new location is added to the list of locations for the breakpoint.


除了不确定的地址,pending breakpoints与普通断点无关。


set breakpoint pending auto
默认,当GDB无法找到断点位置时,会询问是否创建一个pending breakpoint


set breakpoint pending on
无法识别的断点位置会自动导致创建pending breakpoint


set breakpoint pending off
暗示不会创建pending breakpoints,一个未识别的断点位置导致错误,但这个设置不会影响先前创建的pending breakpoints。


show breakpoint pending
显示创建pending breakpoints的当前设置
The settings above only affect the break command and its variants. Once breakpoint is set, it will be automatically updated as shared libraries are loaded and unloaded.


对一些平台,GDB可以根据断点地址是read-only还是read-write来自行决定是软断点还是硬断点,这应用到break、next和finish设置的断点,而hbreak则使用硬件断点。


set breakpoint auto-hw on
默认行为。当GDB设置断点时,会试图使用target memory map来决定是使用硬件还是软件断点


set breakpoint auto-hw off
通知GDB不要自行决定断点类型。如果target提供了一个memory map则GDB会在当要在一个只读地址上设置一个软件断点时给出警告。


GDB一般是这样实现断点的:在断点处使用一条特殊指定来替代程序代码,使得执行到时,把控制权交给GDB。在程序重新resumed后,就这样替换了。一旦
执行到断点停止后,GDB会恢复原始的指令,这样做是为了防止GDB在程序中遗留断点。


set breakpoint always-inserted off
所有断点在target重新resumed时插入到target中,当target停止后,会删除所有的断点。


set breakpoint always-inserted on
所有断点一直被插入到target中,除非被删除


set breakpoint always-inserted auto
默认模式。If gdb is controlling the inferior in non-stop mode (see Section 5.4.2 [Non-Stop Mode], page 67), gdb behaves as if breakpoint
always-inserted mode is on. If gdb is controlling the inferior in all-stop mode, gdb behaves as if breakpoint always-inserted mode is off.


GDB为特殊目的,也会自动设置一些内部断点,例如:处理longjmp。这些内部断点被赋给负数,从-1开始。info breakpoints不会显示它们。可以使用
maint info breakpoints来查看之。

原创粉丝点击