简体   繁体   中英

Adding breakpoint command lists in GDB controlled from Python script

I'm using Python to control GDB via batch commands. Here's how I'm calling GDB:

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

The cmd.gdb listing just contains the line calling the Python script

source cmd.py

And the cmd.py script tries to create a breakpoint and attached command list

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. 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.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

See also: How to script gdb (with 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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