如何使用gdb捕获函数返回false的时刻? [英] How can I use gdb to catch the moment when a function returns false?
问题描述
如何检测函数bool f()
返回false的时刻?
How can I detect the moment when my function bool f()
returns false?
f
具有复杂的if
语句,该语句返回true或false,并且为所有部分设置断点会很昂贵.
f
has complex if
statements that returns true or false, and it is costly to make breakpoints for all parts.
推荐答案
您可以尝试使用一些Python代码:
you can try with a bit of Python code:
import gdb
class FunctionFinishBreakpoint (gdb.FinishBreakpoint):
def __init__ (self):
gdb.FinishBreakpoint.__init__(self, gdb.newest_frame(),
internal=True)
self.silent = True
def stop(self):
#print("after: {}".format(int(self.return_value)))
return not int(self.return_value)
class FunctionBreakpoint(gdb.Breakpoint):
def __init__ (self, spec):
gdb.Breakpoint.__init__(self, spec)
self.silent = True
def stop (self):
#print("before")
FunctionFinishBreakpoint() # set breakpoint on function return
return False # do not stop at function entry
FunctionBreakpoint("test")
将其保存在finish.py
文件中,根据需要对其进行编辑,然后从GDB中获取它,或者在python ... end
或python-interactive
(pi
)中运行它.
Save that in a finish.py
file, edit it to your needs and source it from GDB, or run it between python ... end
or in python-interactive
(pi
).
此代码创建一个FunctionBreakpoint
,每次按下函数test
时都会触发FunctionBreakpoint.stop
.回调是静默的,仅创建FunctionFinishBreakpoint
,该回调在当前帧的末尾(即,函数的末尾)停止.第二个停靠点调用FunctionFinishBreakpoint.stop
,它测试返回值的计算结果为true还是false.如果它不是"true",它将告诉GDB停止执行.
This code creates a FunctionBreakpoint
, that triggers FunctionBreakpoint.stop
eachtime function test
is hit. The callback is silent, and only creates a FunctionFinishBreakpoint
, that stops at the end of the current frame (ie, at the end of your function). That second stop calls FunctionFinishBreakpoint.stop
, which tests if the return value evaluates to true or false. If it is "not true", it tells GDB to stop the execution.
文档参考:
- Manipulating breakpoints using Python
- Finish Breakpoints
(gdb.FinishBreakpoint
为此被添加到GDB Python接口中,我自己是:-)
(gdb.FinishBreakpoint
was added to GDB Python interface for that very purpose, by myself :-)
(我上次检查时,这些FinishBreakpoint存在效率问题,如果经常调用函数,您可能会注意到它)
(last time I checked, there was an efficiency problem with these FinishBreakpoint, you may notice it if your function is called very often)
这篇关于如何使用gdb捕获函数返回false的时刻?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!