Reputation: 171
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?
Upvotes: 8
Views: 2900
Reputation: 382462
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?
Upvotes: 5
Reputation: 171
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.
Upvotes: 1