Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding breakpoint command lists in GDB controlled from Python script

Tags:

python

gdb

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?

like image 756
user2601195 Avatar asked Aug 21 '14 16:08

user2601195


People also ask

How do I add a breakpoint in gdb?

Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]". For example, if you wanted to set a breakpoint at line 55 of main.

Does gdb work with Python?

... but gdb supporting Python, doesn't mean Python on its own can access gdb functionality (apparently, the gdb has its own built-in separate Python interpreter).

How do I import gdb into Python?

Explanation. GDB embeds the Python interpreter so it can use Python as an extension language. You can't just import gdb from /usr/bin/python like it's an ordinary Python library because GDB isn't structured as a library. What you can do is source MY-SCRIPT.py from within gdb (equivalent to running gdb -x MY-SCRIPT.py ) ...


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

like image 35
user2601195 Avatar answered Oct 14 '22 06:10

user2601195