Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB automatically load Python Script

Tags:

python

gdb

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.

like image 280
Dave Avatar asked Aug 07 '12 17:08

Dave


People also ask

How do I run a python script in GDB?

Python in gdb We can access a dynamic python runtime from within gdb by simply calling python. The python code only runs when you press Control D. Another way to run your script is to import them as “new gdb commands”. This is the most useful way to use python for gdb, but it does require some boilerplate to start.

Can you use GDB with python?

Pretty much anything that can be done at the GDB command line can be done with a breakpoint from Python. You can attach commands to a breakpoint, make it conditional, ignore a certain number of hits, make the breakpoint specific to a thread or process, and all of the things you can do from the command line.

How do I know if GDB supports python?

Check if GDB was compiled with Python support The GDB Python API is a GDB compile time option that can be enabled (with the --with-python configuration argument). We can easily check if gdb has this feature enabled by checking: $ /path/to/gdb --config This GDB was configured as follows: [...] --with-python=/usr [...]

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

Make a .gdbinit file, and put all your source commands in there in the same directory where you'll be starting gdb from. I believe the file will look something like this:

.gdbinit

source /path/to/hello.py
source /path/to/foobar.py
etc, etc

reference

EDIT: Including the .gdbinit file in your version control will make sure that the files are included, independent of the global gdb settings.

like image 165
Gordon Bailey Avatar answered Sep 30 '22 07:09

Gordon Bailey


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

like image 40
Terry Santegoeds Avatar answered Sep 30 '22 09:09

Terry Santegoeds