Alex Rivera | Logout

GDB automatically load Python Script

Asked 2012-08-07T17:58:03.200
9

I'm trying to setup a system where I can add python scripts to a directory, and the next time I load gdb they will be accessible (the point being to place this in version control and allow other engineers simple access to my helper scripts). For starters I've written a hello.py file, and when I type source /path/to/hello.py in gdb, and then type hello it works as expected.

How do I make GDB do this automatically? The documentation suggests using the data-directory, and placing scripts in data-directory/python/gdb/command. Placing my hello.py file in this directory does nothing though (however it does end up creating a hello.pyc file).

I've additionally tried adding this directory to my directory listing with dir /path/to/hello/ and then hoping to be able to type source hello.py but this also fails.

Edit
Report

1 Answer

6

I was looking for an answer to the same question today. Eventually I came up with the following:

.gdbinit

python
import glob

python_dir = "/path/to/python"

# Search the python dir for all .py files, and source each
py_files = glob.glob("%s/*.py" % python_dir)
for py_file in py_files:
    gdb.execute('source %s' % py_file)
end

Note that .gdbinit can be ~/.gdbinit or ./.gdbinit; see gdbinit(5).

answered 2013-02-22T20:13:25.030

Your Answer