vlad-ardelean
vlad-ardelean

Reputation: 7622

How to change the Python Interpreter that gdb uses?

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.

Upvotes: 39

Views: 28470

Answers (4)

Claar727
Claar727

Reputation: 1

Not sure if this is generally applicable, but in Linux with GDB version 9.2, instead of launching GDB with:

gdb python

I launch it with:

gdb /path/to/python/i/want/to/use

and it works.

Upvotes: -2

Nikos Balkanas
Nikos Balkanas

Reputation: 1

No need to rebuild gdb.

Just invoke it differently:

gdb -ex run --args /usr/bin/python2 test.py

Upvotes: -3

cclauss
cclauss

Reputation: 751

$ 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

Upvotes: -5

Employed Russian
Employed Russian

Reputation: 213754

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.

Upvotes: 31

Related Questions