Adding Break Command Lists in GDB Managed with a Python Script

I use Python to control GDB using command commands. This is how I call GDB:

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

The cmd.gdb list contains only a line that calls a Python script

 source cmd.py 

And the cmd.py script tries to create a breakpoint and a list of connected commands

 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 that I don’t understand how to connect any GDB commands to the breakpoint from the Python script. Is there a way to do this, or am I missing a much simpler and more obvious way to automatically execute commands related to a breakpoint?

+5
source share
2 answers

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 is gdb script (using python)? An example adds control points, starts, at what control moment did we get?

+3
source

I think this is probably the best way to do this, and not use the GDB Command List tool.

 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 may also be able to subclass gdb.Breakpoint to add a “manual” procedure instead of performing equality checks inside the loop.

0
source

Source: https://habr.com/ru/post/1200856/


All Articles