Ubuntu – Cannot compile GDB7.8 with Python support

gdbpython

I am trying to install GDB7.8 with Python support. From the source folder, I am running ./configure --with-python
When I did tab-complete from –with- I did not see Python in the list.
But when I ran configure with that flag, it did not baulk.

When I run make, it complains that Python is not found.

checking for python2.7... no

but Python is installed:

 $ which python
python                python2.7             python2.7-dbg-config
python2               python2.7-dbg 

$ which python2.7 
/usr/bin/python2.7

I compiled GDB without –with-python
and things installed without error. I was under the impression that GDB7.8 had Python support without the need for special flags. But when I run:

$gdb python
(gdb) run test.py

I get some sort of cannot import gdb Import error

So then I tried calling "pi":

(gdb) pi printf.py
Python scripting is not supported in this copy of GDB.

So… how do I get Python support in GDB7.8? is it actually not supported? Or should I not call "pi"?

Best Answer

I spent quite a bit of time working on getting gdb (7.9) to work with Python (2.7). In the end everything worked rather well. However, there are a bunch of things that you have to get right. The key point is that the gdb configure script tries to compile a small C program that looks like this.

#include "Python.h"
int
main ()
{
Py_Initialize ();
  ;
  return 0;
}

If this program won't compile, then Python support won't be built. For this program to compile, the Python.h include file must be found in /usr/include/python2.7. This file will only exist if the python-devel package is installed. On my system (redhat), the command for installing this package is sudo yum install python-devel.

However, that's not enough to get Python installed. Before the configure script tries to compile the C program, it gets various options from python-config.py. If these options aren't correct, then the C program won't compile. On my system, python-config.py returned the options below.

-lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic

These options didn't cause any problems in my environment. Other folks have had problems with the options returned from python-config.py and have made changes to python-config.py to resolve these problems. On my system the complete compile command was

gcc -o conftest -g -O2   -I/usr/include/python2.7 -I/usr/include/python2.7 \
    conftest.c -ldl -lncurses -lz -lm -ldl    -lpthread -ldl -lutil -lm \
    -lpython2.7 -Xlinker -export-dynamic

This compile command completed without any errors as soon as I installed python-devel. Note you don't have to manually enter the gcc command. I did run the gcc command several times to make sure everything was correct. Normally, the configure script will run the compiler for you. Also note that to get the overall gdb install process to complete, makeinfo also had to be installed. The command for installing makeinfo was sudo yum install texinfo.

Overall, the correct set of steps seems to be

  1. Install python-devel

  2. Install texinfo

  3. Download the gdb source and gunzip it and untar it.

  4. cd to the gdb-7.9 directory with the configure file.

  5. ./configure --prefix=/usr --with-python
    make 
    sudo make install
    

It should be possible to get gdb to work with Python 3. The various gdb scripts and install programs mention Python 3 in many, many places. However, the correct procedure for installing gdb with Python 3 is unknown to me at this point.

Related Question