How to change the Python Interpreter that gdb uses?

Question:

I’m using ubuntu 14.04, where python3 is a default system package.

I want to debug Python2.7 programs with gdb, but I seem to encounter this issue:

When i’m in gdb, using the py command puts me in an interpreter, so i ran these commands in the interpreter:

First I check the interpreter version:

(gdb) py
>import sys
>print(sys.version)
>end
3.4.0 (default, Apr 11 2014, 13:08:40) 
[GCC 4.8.2]

Then I check what interpreter executable is being used

(gdb) py
>import sys
>print(sys.executable)
>end
/usr/bin/python
(gdb) 

Then in bash, I check the interpreter:

12:34]hostname ~ $ls -l /usr/bin/python 
lrwxrwxrwx 1 root root 9 Dec 21  2013 /usr/bin/python -> python2.7

So although gdb says it’s using my 2.7 interpreter, it’s actually using another one. I need a 2.7 interpreter to be able to use it with the python specific extensions that the ubuntu package ‘python2.7-dbg’ provides, because as far as i know there’s no such package for python 3.4 yet, and even if there was, the programs that i want to debug run python 2.7

My question is how do i make it use the interpreter I want?

[EDIT]
Do not uninstall python3 btw. I did it on ubuntu 14.04 and it wrecked my system. Couldn’t manage to get it up again. I’m currently using it with no window-manager (it’s cool and 1337), but you get the idea.

Asked By: vlad-ardelean

||

Answers:

So although gdb says it’s using my 2.7 interpreter

GDB doesn’t say that. It says it’s using 3.4.0, and that interpreter is linked into GDB, in the form of libpython3.4.a or libpython3.4.so.

Since there is no actual Python binary involved, the (minor) bug here is that sys.executable returns /usr/bin/python. It would possibly be better for it to return /usr/bin/gdb instead.

I need a 2.7 interpreter

In that case, you’ll have to rebuild gdb from source, after configuring it with appropriate --with-python value.

Answered By: Employed Russian
$ apt-get -qq update
$ apt-get install gdb python2.7-dbg python3-all-dbg
$ gdb -ex r -ex quit --args python2 -c "import sys ; print(sys.version)" # Py2.7
$ gdb -ex r -ex quit --args python3 -c "import sys ; print(sys.version)" # Py3.6
Answered By: cclauss

No need to rebuild gdb.

Just invoke it differently:

gdb -ex run --args /usr/bin/python2 test.py
Answered By: Nikos Balkanas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.