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/session.py
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:
            return

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


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

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

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

Greetz,
bluec0re

No comments:

Post a Comment