Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 272
vlad-ardelean Avatar asked Oct 07 '14 19:10

vlad-ardelean


People also ask

Which Python is GDB using?

GDB can be built against either Python 2 or Python 3; which one you have depends on this configure-time option. Python scripts used by GDB should be installed in data-directory /python , where data-directory is the data directory as determined at GDB startup (see Data Files).

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


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

like image 176
Employed Russian Avatar answered Sep 22 '22 14:09

Employed Russian