5 中断和继续

info program     显示程序状态信息:是否在运行,什么进程,为何中断。

5.1 断点,监视点,捕获点

断点:以行号,函数名或程序的绝对地址中断,某些系统可以在可执行程序运行前,在共享库里设置断点。

监视点:特殊的断点,在表达式的值改变的时候中断程序。

捕获点:特殊的断点,用来在某些事件发生时中断程序。

5.1.1 设置断点

break location

break ...ifcond

带参数设置断点:在每次断点时计算cond表达式,且当表达式为真(表达式不为0)时中断

tbreak args

temporary breakpoint

设置一个只中断一次的断点

hbreak args

hardware breakpoint

设置一个硬件支持的断点

thbreak args

设置一个只中断一次的硬件支持断点

rbreak regex

匹配正则表达式

info breakpoints

info break

info watchpoints

打印断点,监视点和捕获点

5.1.2 设置监视点

表达式改变

watch expr [thread threadnum]

设置一个表达式监视点,在表达式expr被改写和值改变时GDB中断程序。

[thread threadnum]GDB只在threadnum标识的线程改变表达式expr时中断。注意,只在硬件监视点上GDB才起作用。

watch foo

最常用的用法时监视变量

"

gdb test

b change

r

watch len1

c

c

q

"

rwatch expr [thread threadnum]

读表达式的值时中断

awatch expr [thread threadnum]

读或写表达式的值时中断

info watchpoints

set can-use-hw-watchpoints     设置使用硬件监视点

show can-use-hw-watchpoints     显示硬件监视点的当前模式

5.1.3 设置捕获点

事件中断

catch event     在event发生时中断

event:

throw     C++异常的抛出

catch     C++异常的捕获

exception [exceptionname]     

exception unhandled     没有被程序处理的异常

assert     

exec

fork

vfork

load [libname]     加载共享库

unload [libname]     卸载共享库

tcatch event

5.1.4 删除断点

clear     在选定的栈帧上的下一个指令上删除所有的断点

clear location     在指定的位置删除所有的断点

clear function     删除在名为function入口点上的断点

clear file:function

clear linenum     删除在指定行上的断点

clear file:linenum

delete [breakpoints] [range...]     在指定的范围内删除断点,指定断点num号

5.1.5 禁用断点

disable [breakpoints] [range...]

enable [breakpoints] [range...]

5.1.6 中断条件

condition bnum expression     为断点bnum指定断点条件表达式

condition bnum     从断点bnum里删除条件

"

b change

info breakpoints

condition 1 s1=="hanyu"

r

Error in testing breakpoint condition:

Couldn't write extended state status: 错误的地址.       //不清楚为何报错WWWWWWW,查了下说是linux的bug,可以到window的虚拟机试试

"

一个特殊的条件断点例子是在断点到达一定次数后才中断。

可以使用断点的忽略计数。

ignore bnum count     将断点bnum的忽略计数设置为count

5.1.7 断点命令列表

可以为断点设置一系列命令让断点中断时执行。

为断点bnum指定命令列表:

commands [bnum]

... commands-list ...

end

不带bnum参数的话,commmands指定最后的断点

下面的例子可以用断点命令在foo的入口点,当x>0时打印x的值:

break foo if x>0

commands 

printf "x is %d \n", x

continue

end

用命令给需要的变量设置正确的值:

break 403

commands

set x = y +4

continue

end

5.1.8 断点菜单

对于函数名重载的函数如何打断点

5.2 继续和单步跟踪

continue | c

step [count] | s          

step in单步执行

step count                

单步执行n次

next [count] | n          

当前栈帧单步执行

set step-mode

show step-mode

显示GDB是否中断或越过不带源代码行调试信息的函数

set step-mode on

如果你对一个不带符号信息的函数指令感兴趣,不想GDB越过这个函数

set step-mode off

设置step命令越过不带调试信息的函数

until | u

在当前栈帧上继续执行直到越过当前行的源代码行。和next相比,用来避免多次单步执行一个循环。

until location | u location

继续执行到程序指定的位置。

使用临时断点,比不带参数的util要快。

只有在指定位置的当前帧上,它才会真正被执行。意味着until可以用来跳过函数嵌套调用。

until 99

94 int factorial(int value)

95 {

96      if (value > 1) {

97           value *= factorial(value-1);

98      }

99      return value;

100 }

advance location

继续执行到程序指定的位置。

目标位置不必在当前帧上,不会跳过函数嵌套调用。

stepi arg | si arg

执行一个机器指令,然后中断返回。如display/i $pc,自动显示指令??? $8.6节自动显示

nexti arg | ni arg

5.3 信号

信号是程序里发生的异步事件。

SIGINT     中断信号ctrl+c

SIGSEGV    段错误

SIGALRM    定时器超时

info signals

info handle

handle signal [keywords...]

keywords:

nostop

stop

print

noprint

pass | noignore

nopass | ignore

5.4 中断和开始多线程程序

break linespec thread threadno

break linespec thread threadno if ...

linespec:指定源代码行

break frik.c:13 thread 28 if ab > lim

GDB中断后所有线程将中断,可以查看状态

猜你喜欢

转载自blog.csdn.net/u012906122/article/details/113926686