Author dmalcolm
Recipients dmalcolm
Date 2010-02-28.23:40:28
SpamBayes Score 2.77556e-16
Marked as misclassified No
Message-id <>
gdb 7 can be extended with Python code, allowing the writing of domain-specific pretty-printers and commands.  

I've been working on gdb 7 hooks to make it easier to debug python itself, as mentioned here:

I'm attaching a patch for merger with "trunk".  My hope is to be the maintainer of this work, although I do not yet have SVN commit rights (see )

The code is fully compatible with the existing "Misc/gdbinit" macros - you can freely use both as needed.

= Two versions of Python =
This code is intended to run inside gdb.  There are potentially two Python versions involved here: that of the "inferior process" (the one being debugged), and that of the debugger.

As I understand things, gdb only supports embedding Python 2 at the moment.  This code is thus targeting that version of Python.

So far, I've attempted to keep this code so that it will run when the "inferior process" is either Python 2 or Python 3.  I could vary this code in the py3k branch if desired.  The code would then track the "inferior process" version of Python, so that code to debug Python 3 would live in the py3k branch.  That code would still be written for Python 2, though.

= Makefile changes =

The Makefile installs the gdb hooks to relative to the built python (even if you're not using them), which some may find irritating.  It needs to do this for the selftest to work (and for the gdb hooks to be usable if you're actually using them to debug your build of Python).

Should I write a gdb configuration test to check for the availability of gdb built with python?

I've added the file to .hgignore and .bzrignore.  IIRC, a similar thing can be done to the SVN metadata (I don't think this is expressable as a patch, though).  Alternatively, I could wire up the gdb tests to load the file from its location in the source tree.

However, I intend for this code to be installed to a location alongside the build Python, so that it can be automatically detected and used by gdb.  Typically this means copying it to the path of the ELF file with a file.  In my RPM builds I add an extra copy, locating it relative to the location of the stripped DWARF data (e.g. /usr/lib/debug/usr/lib64/

= Selftests =
The selftest runs whatever version of "gdb" is in the path, which then invokes the built version of python, running simple "print" commands and verifying that gdb is corrrectly representing the results in backtraces (even in the face of corrupt data).  I haven't fully tested the error cases yet (e.g. for when gdb is not installed).

The tests take about 14 seconds to run on my box:
[david@brick trunk-gdb]$ time ./python Lib/test/ -v -s test_gdb
The CWD is now /tmp/test_python_19369
test_NULL_ob_type (test.test_gdb.DebuggerTests) ... ok
test_NULL_ptr (test.test_gdb.DebuggerTests)
Ensure that a NULL PyObject* is handled gracefully ... ok
test_classic_class (test.test_gdb.DebuggerTests) ... ok
test_corrupt_ob_type (test.test_gdb.DebuggerTests) ... ok
test_corrupt_tp_flags (test.test_gdb.DebuggerTests) ... ok
test_corrupt_tp_name (test.test_gdb.DebuggerTests) ... ok
test_dicts (test.test_gdb.DebuggerTests) ... ok
test_getting_backtrace (test.test_gdb.DebuggerTests) ... ok
test_int (test.test_gdb.DebuggerTests) ... ok
test_lists (test.test_gdb.DebuggerTests) ... ok
test_long (test.test_gdb.DebuggerTests) ... ok
test_modern_class (test.test_gdb.DebuggerTests) ... ok
test_singletons (test.test_gdb.DebuggerTests) ... ok
test_strings (test.test_gdb.DebuggerTests) ... ok
test_subclassing_list (test.test_gdb.DebuggerTests) ... ok
test_subclassing_tuple (test.test_gdb.DebuggerTests)
This should exercise the negative tp_dictoffset code in the ... ok
test_tuples (test.test_gdb.DebuggerTests) ... ok
test_unicode (test.test_gdb.DebuggerTests) ... ok

Ran 18 tests in 13.233s

1 test OK.
[36833 refs]

real	0m13.599s
user	0m11.771s
sys	0m1.384s

= Platform support =
I don't have access to anything other than Linux, so I've no idea how well this stuff works on other platforms.  My testing so far has been on Fedora, though I've heard of successful usage of this on Debian.

= Legal stuff =

Earlier versions of this code were licensed under the LGPL 2.1
I'm relicensing the code to be "under the same license as Python itself", assuming that's legally OK.  Do I need to state that in the file header, or is that redundant?

I'm in the process of doing the PSF Contributor Agreement paperwork (as an individual); waiting to get my hands on a fax machine.  My employer, Red Hat, has agreed for me to retain copyright on all contributions I make to Python.
Date User Action Args
2010-02-28 23:40:32dmalcolmsetrecipients: + dmalcolm
2010-02-28 23:40:32dmalcolmsetmessageid: <>
2010-02-28 23:40:31dmalcolmlinkissue8032 messages
2010-02-28 23:40:29dmalcolmcreate