Skip to main content
 首页 » 编程设计

python之通过python接口(interface)在gdb中的断点处执行命令

2024年10月24日12shanyou

不是 this question 的副本,因为我正在通过 gdb 的 python 接口(interface)工作。
This one类似但没有答案。

我在 python 中扩展了一个 gdb.breakpoint,这样它将某些寄存器写入文件,然后跳转到一个地址:在 0x4021ee , 我想写东西到文件,然后跳转到 0x4021f3

但是,command 中没有任何内容永远被处决。

import gdb 
class DebugPrintingBreakpoint(gdb.Breakpoint): 
    def __init__(self, spec, command): 
        super(DebugPrintingBreakpoint, self).__init__(spec, gdb.BP_BREAKPOINT, internal = False) 
        self.command = command 
 
    def stop(self): 
        with open('tracer', 'a') as f: 
            f.write(chr(gdb.parse_and_eval("$rbx") ^ 0x71)) 
            f.close() 
        return False 
 
 
 
gdb.execute("start") 
DebugPrintingBreakpoint("*0x4021ee", "jump *0x4021f3") 
gdb.execute("continue") 

如果我明确添加 gdb.execute(self.command)到 stop() 结束, 我得到 Python Exception <class 'gdb.error'> Cannot execute this command while the selected thread is running.:

有人在 python gdb 中有带断点的命令列表的工作示例吗?

请您参考如下方法:

尝试几个选项:

  1. 使用gdb.post_event从 stop() 稍后运行所需的命令。我相信您需要从您的函数返回 True,然后从您的事件中继续调用。
  2. 创建一个普通断点并监听events.stop检查您的断点是否命中。