Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gdb python debugging extension inside virtualenv

I'm running ubuntu, and installed the python-dbg package. When trying to use the installed version directly everything works great:

$ gdb python2.7-dbg
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
---x snipped x---
Reading symbols from /usr/bin/python2.7-dbg...done.
(gdb) r
Starting program: /usr/bin/python2.7-dbg
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Python 2.7.3 (default, Feb 27 2014, 19:39:25)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Program received signal SIGINT, Interrupt.
0x00007ffff6997743 in __select_nocancel () at ../sysdeps/unix/syscall-template.S:82
82      ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) py-bt (<--- works, just has nothing to do)
(gdb)

So, I've been building a virtualenv using the package's binary python2.7-dbg (since some libraries need recompiling), using this command line:

~$ virtualenv ved -p /usr/bin/python2.7-dbg

Its all working fine, but when I'm using gdb inside the virtualenv, atleast the python pretty printers stop working:

~$ . ved/bin/activate
(ved)~$ gdb python
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
---x snipped x---
Reading symbols from /home/itai/ved/bin/python...done.
(gdb) r
Starting program: /home/itai/ved/bin/python
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Python 2.7.3 (default, Feb 27 2014, 19:39:25)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Program received signal SIGINT, Interrupt.
0x00007ffff6997743 in __select_nocancel () at ../sysdeps/unix/syscall-template.S:82
82      ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) py-bt
Undefined command: "py-bt".  Try "help". (<---- PROBLEM)
(gdb)

Am I missing something within my virtualenv?

like image 407
itai Avatar asked Apr 08 '14 08:04

itai


People also ask

How do I debug an extension in Python?

In order to debug the C/C++ code of an extension, you need to use the native debugger, GDB or LLDB, and debug the interpreter process that loads your script and runs the application. For that, you can either attach to a running Python process or debug a properly configured Custom Build Application.

How do you debug Python?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.


2 Answers

I've solved the problem by using strace on gdb, grepping the "open" syscalls.

It seems that gdb makes a search for python-gdb.py in several paths it guesses (according to the python binary), and whenever the file is not found it just fails silently.

Eventually the way to solve the problem is to link /usr/lib/debug/usr/bin/python2.7-gdb.py into the env's bin directory. The name of the link should be <python binary name>-gdb.py, being in my case python2.7-dbg-gdb.py (...).

After that, everything seems to work.

like image 160
itai Avatar answered Oct 11 '22 09:10

itai


@itai's answer only partially worked for me on Ubuntu Trusty (14.04). I found a couple of other things worked better:

sudo apt-get install python2.7-dbg

then, in the virtualenv:

. bin/activate
mkdir bin/.debug
ln -s /usr/lib/debug/usr/bin/python2.7-gdb.py bin/.debug/python-gdb.py
ln -s /usr/lib/debug/usr/bin/python2.7 bin/.debug/

gdb --args bin/python2.7 ...

This helped gdb find the python debugging symbols as well as the py-bt etc commands.

like image 35
craigds Avatar answered Oct 11 '22 10:10

craigds