39

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.

3

3 Answers 3

31

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.

5
  • can I know how my gdb is bullt? (I mean with what options?)
    – Chan Kim
    Aug 16, 2016 at 4:53
  • 3
    @ChanKim: To see which version of the Python library is linked to your installed GDB, try ldd $(which gdb) | grep python
    – kevinarpe
    Feb 4, 2017 at 16:31
  • 1
    @kevinarpe The method you propose works only if gdb is linked dynamically against libpython. The method OP used (print(sys.version)) works always. Feb 4, 2017 at 16:48
  • 2
    Crazy... I have a copy of GDB distributed with JetBrains CLion, and I cannot figure out which Python it is linked against using ldd -- does not show libpython. However, if using GDB from JetBrains CLion and I run commands python import sys then python print(sys.version), I see the Python interpreter is v2 not v3.
    – kevinarpe
    Feb 4, 2017 at 18:29
  • 1
    This answer is the best as far I know. However there are couple things to mention; first give this link linuxfromscratch.org/blfs/view/svn/general/gdb.html, where you can find an step by step, how to build a custom GDB and the sources. Second is point out that when you build GDB, by default, it uses references to python2 and you must specify python version only if you want use python3. Hope this helps Apr 28, 2020 at 22:24
-2

No need to rebuild gdb.

Just invoke it differently:

gdb -ex run --args /usr/bin/python2 test.py
-4
$ 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
1
  • 2
    This is just running the two python interpreters in gdb. It has nothing to do with the version of python that gdb has bundled. Sep 18, 2018 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.