简体   繁体   English

在Python脚本中控制的GDB中添加断点命令列表

[英]Adding breakpoint command lists in GDB controlled from Python script

I'm using Python to control GDB via batch commands. 我正在使用Python通过批处理命令控制GDB。 Here's how I'm calling GDB: 这是我如何调用GDB:

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

The cmd.gdb listing just contains the line calling the Python script cmd.gdb列表只包含调用Python脚本的行

source cmd.py

And the cmd.py script tries to create a breakpoint and attached command list 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")

The problem is I'm at a loss as to how to attach any GDB commands to the breakpoint from the Python script. 问题是我不知道如何将任何GDB命令附加到Python脚本的断点。 Is there a way to do this, or am I missing some much easier and more obvious facility for automatically executing breakpoint-specific commands? 有没有办法做到这一点,或者我错过了一些更容易和更明显的自动执行断点特定命令的工具?

def stop from GDB 7.7.1 can be used: 可以使用GDB 7.7.1的def stop

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')

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

See also: How to script gdb (with python)? 另请参见: 如何编写gdb脚本(使用python)? Example add breakpoints, run, what breakpoint did we hit? 示例添加断点,运行,我们遇到了什么断点?

I think this is probably a better way to do it rather than using GDB's "command list" facility. 我认为这可能是一种更好的方法,而不是使用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")

You could probably also subclass gdb.Breakpoint to add a "handle" routine instead of doing the equality check inside the loop. 您也可以将gdb.Breakpoint子类化为添加“句柄”例程,而不是在循环内进行相等性检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM