【问题标题】:Adding breakpoint command lists in GDB controlled from Python script在 Python 脚本控制的 GDB 中添加断点命令列表
【发布时间】:2014-08-21 16:28:23
【问题描述】:

我正在使用 Python 通过批处理命令控制 GDB。我是这样称呼 GDB 的:

$ gdb --batch --command=cmd.gdb myprogram

cmd.gdb 列表只包含调用 Python 脚本的行

source cmd.py

cmd.py 脚本尝试创建断点和附加命令列表

bp = gdb.Breakpoint("myFunc()") # break at function in myprogram
gdb.execute("commands " + str(bp.number))
# then what? I'd like to at least execute a "continue" on reaching breakpoint...  
gdb.execute("run")

问题是我不知道如何将任何 GDB 命令附加到 Python 脚本的断点。有没有办法做到这一点,还是我错过了一些更容易和更明显的工具来自动执行特定于断点的命令?

【问题讨论】:

    标签: python gdb


    【解决方案1】:

    def stop 可以使用 GDB 7.7.1 中的:

    gdb.execute('file a.out', to_string=True)
    class MyBreakpoint(gdb.Breakpoint):
        def stop (self):
            gdb.write('MyBreakpoint\n')
            # Continue automatically.
            return False
            # Actually stop.
            return True
    MyBreakpoint('main')
    gdb.execute('run')
    

    记录在:https://sourceware.org/gdb/onlinedocs/gdb/Breakpoints-In-Python.html#Breakpoints-In-Python

    另见:How to script gdb (with python)? Example add breakpoints, run, what breakpoint did we hit?

    【讨论】:

      【解决方案2】:

      我认为这可能是比使用 GDB 的“命令列表”工具更好的方法。

      bp1 = gdb.Breakpoint("myFunc()")
      
      # Define handler routines
      def stopHandler(stopEvent):
          for b in stopEvent.breakpoints:
              if b == bp1:
                  print "myFunc() breakpoint"
              else:
                  print "Unknown breakpoint"
          gdb.execute("continue")
      
      # Register event handlers
      gdb.events.stop.connect (stopHandler)
      
      gdb.execute("run")
      

      您也可以将 gdb.Breakpoint 子类化以添加“句柄”例程,而不是在循环内进行相等检查。

      【讨论】:

      • 只是一个提示。上述方法有个缺点:如果直接在stopHandler()中调用gdb.execute("continue"),在某些情况下会导致python.recursive错误。
      • @cq674350529 你能解释一下是什么问题吗?
      • @MicrosoctCprog 最初的问题是,在以原始 shell 方式运行“commands xxx”后,我无法执行多个 gdb cmds。但是通过继承gdb.Breakpoint,这样做很简单,而且还解决了另一个问题。另外,我总结了与此相关的三种常见方式:cq674350529.github.io/2020/05/09/%E6%8A%80%E5%B7%A7misc(只有中文版,谷歌翻译可能有帮助。)
      猜你喜欢
      • 2018-06-27
      • 1970-01-01
      • 2011-07-17
      • 2021-02-15
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      相关资源
      最近更新 更多