使用 'gdb' 在函数内的特定行中设置断点
setting a breakpoint in a specific line inside a function with 'gdb'
我正在尝试在 class(我创建的 class)的成员函数内的第五行设置断点 'gdb'。
从 here 我知道如何在函数的 开始 处设置断点,但我想将其设置在 函数内的特定行,或此函数开头的特定offset。
一般来说,'gdb' 中有没有一种方法可以通过设置与我已有的另一个断点的偏移量来将断点设置到一行?
谢谢!
您可以在 gdb breakpoint +<offset>
当前停止位置的偏移处创建断点。
您还可以使用 gdb break <linenumber>
(对于当前源文件)或 gdb break <filename>:<linenumber>
(对于当前文件以外的文件)在特定行号上创建断点。
docs 中有更多详细信息。
无法设置相对于函数开头的断点,以便在修改源文件时它会保留其相对位置。这有时会有用;但它只是一个没有人添加到 gdb 的功能。
它可以从 Python 中模拟出来,尽管它不能像普通断点那样工作,因为 Python 无法访问 gdb 中的断点重置机制。
可以按照其他答案或 Python.
中所示的方式完成一次性解决方案
当我需要这种功能时——断点中间功能对源代码更改相当稳健——我使用了 "SDT" 静态探测点。这些可以让你在你的来源中命名这些点。
info fun <function name>
或完全合格 info functions <function name>
获取函数及其源文件
list <function name>
Print lines centered around the beginning of function function.
将列出所有函数的源代码,下面有一些代码。
- 选择你想要的线路
break <filename:linenum>
以下是使用 GDB 的 python 脚本实现自动化的方法:
class RelativeFunctionBreakpoint (gdb.Breakpoint):
def __init__(self, functionName, lineOffset):
super().__init__(RelativeFunctionBreakpoint.calculate(functionName, lineOffset))
def calculate(functionName, lineOffset):
"""
Calculates an absolute breakpoint location (file:linenumber)
based on functionName and lineOffset
"""
# get info about the file and line number where the function is defined
info = gdb.execute("info line "+functionName, to_string=True)
# extract file name and line number
m = re.match(r'Line[^\d]+(\d+)[^"]+"([^"]+)', info)
if not m:
raise Exception('Failed to find function %s.' % functionName)
line = int(m.group(1))+lineOffset #add the lineOffset
fileName = m.group(2)
return "%s:%d" % (fileName, line)
基本用法:
RelativeFunctionBreakpoint("yourFunctionName", lineOffset=5)
您也可以编写自定义断点。在这里查看更多:
使用 python 编写 GDB 脚本
我正在尝试在 class(我创建的 class)的成员函数内的第五行设置断点 'gdb'。
从 here 我知道如何在函数的 开始 处设置断点,但我想将其设置在 函数内的特定行,或此函数开头的特定offset。
一般来说,'gdb' 中有没有一种方法可以通过设置与我已有的另一个断点的偏移量来将断点设置到一行?
谢谢!
您可以在 gdb breakpoint +<offset>
当前停止位置的偏移处创建断点。
您还可以使用 gdb break <linenumber>
(对于当前源文件)或 gdb break <filename>:<linenumber>
(对于当前文件以外的文件)在特定行号上创建断点。
docs 中有更多详细信息。
无法设置相对于函数开头的断点,以便在修改源文件时它会保留其相对位置。这有时会有用;但它只是一个没有人添加到 gdb 的功能。
它可以从 Python 中模拟出来,尽管它不能像普通断点那样工作,因为 Python 无法访问 gdb 中的断点重置机制。
可以按照其他答案或 Python.
中所示的方式完成一次性解决方案当我需要这种功能时——断点中间功能对源代码更改相当稳健——我使用了 "SDT" 静态探测点。这些可以让你在你的来源中命名这些点。
info fun <function name>
或完全合格info functions <function name>
获取函数及其源文件list <function name>
Print lines centered around the beginning of function function.
将列出所有函数的源代码,下面有一些代码。
- 选择你想要的线路
break <filename:linenum>
以下是使用 GDB 的 python 脚本实现自动化的方法:
class RelativeFunctionBreakpoint (gdb.Breakpoint):
def __init__(self, functionName, lineOffset):
super().__init__(RelativeFunctionBreakpoint.calculate(functionName, lineOffset))
def calculate(functionName, lineOffset):
"""
Calculates an absolute breakpoint location (file:linenumber)
based on functionName and lineOffset
"""
# get info about the file and line number where the function is defined
info = gdb.execute("info line "+functionName, to_string=True)
# extract file name and line number
m = re.match(r'Line[^\d]+(\d+)[^"]+"([^"]+)', info)
if not m:
raise Exception('Failed to find function %s.' % functionName)
line = int(m.group(1))+lineOffset #add the lineOffset
fileName = m.group(2)
return "%s:%d" % (fileName, line)
基本用法:
RelativeFunctionBreakpoint("yourFunctionName", lineOffset=5)
您也可以编写自定义断点。在这里查看更多:
使用 python 编写 GDB 脚本