Thursday, May 26, 2011

SessionManager for gdb

I had written today a simple SessionManager for GDB via GDB's Python-API
You need to place the following python lines in /usr/share/gdb/python/
import os.path

def gettargetname():
    target = gdb.execute("info target", False, True).strip()
    target = target.splitlines()

    if len(target) > 2 and target[1].count("Local") > 0:
        name = target[2].strip().split(',')[0]
        name = os.path.basename(name.replace("`", "").replace("'", ""))
        return name
    return None

class SessionManager(gdb.Command):
    """Save and Loads gdb Sessions
    session save - saves session
    session load - loads session"""

    def __init__(self):
        super(SessionManager, self).__init__("session", gdb.COMMAND_SUPPORT)
        print "[+] Registered command 'session'"
    def save(self):
        name = gettargetname()
        breakpoints = gdb.breakpoints()
        if not breakpoints:

        print "[*] write to %s.gdbsession" % name
        gdb.execute("save breakpoints %s.gdbsession" % name)
        print "done"

    def invoke(self, arg, from_tty):
        if arg == "save":
        elif arg == "load":
            name = gettargetname()
            gdb.execute("source %s.gdbsession" % name)
            print "Invalid argument. see 'help session' for details"

Now you can add the following lines to your .gdbinit
session load

define hook-quit
    session save
Have fun with automated breakpoint saving/loading :-)


No comments:

Post a Comment