Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB Python support, ImportError no module named gdb

I am trying to compile gdb with python support so I can use the PrettyPrinters provided at : http://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python

I downloaded the latest gdb source from (http://ftp.gnu.org/gnu/gdb/gdb-7.6.1.tar.gz) and compiled it on my Centos 6.4 as follows: 1. ./configure --with-python 2. make

Do I need to provide a path or another argument to --with-python with the path to python libs or executable?

After compilation when I run gdb, I see this warning:

Python Exception <type 'exceptions.ImportError'> No module named gdb:
warning:
Could not load the Python gdb module from `/usr/local/share/gdb/python'.
Limited Python support is available from the _gdb module.
Suggest passing --data-directory=/path/to/gdb/data-directory.

The exception is obvious here and whatever I am going to do next is going to fail because it needs the gdb module, but I gave it a try anyways. So I added the following lines to ~/.gdbinit:

import sys 

sys.path.insert(0, '/tmp/pretty/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)

Now when I start gdb, I get this error:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "/tmp/pretty/python/libstdcxx/v6/printers.py", line 18, in <module>
    import gdb
ImportError: No module named gdb
Error while executing Python code.

Can someone help me resolve this issue?

like image 698
alphabit Avatar asked Dec 04 '25 10:12

alphabit


1 Answers

If anyone is stumbling upon this post trying to compile Python and GDB in Solus 4.1, please read the following setup guide created by me (Obviously swap out any directory names/trees to your own ones)

Set up .profile like this:

source /usr/share/defaults/etc/profile

# Adding variables to PATH
PATH=$HOME/.local/bin:$PATH;

# Emscripten variables
PATH=$HOME/Applications/emsdk:$PATH;
PATH=$HOME/Applications/emsdk/node/12.18.1_64bit/bin:$PATH;
PATH=$HOME/Applications/emsdk/upstream/emscripten:$PATH; 

# GDB variables


# Python variables

PATH=$HOME/Applications/Python-3.9.1/out/bin:$PATH;
LD_LIBRARY_PATH=$HOME/Applications/Python-3.9.1/out/lib:$LD_LIBRARY_PATH;
alias python=/home/jeremi-solus/Applications/Python-3.9.1/out/bin/python3.9

# GDBM variables


# GEF variables


# Various apps variables
PATH=$HOME/Applications:$PATH;

export PATH
export LD_LIBRARY_PATH


# ... remaining .profile

Set up .bashrc like this

#import profile data if exists  
if [ -f "$HOME/.profile" ]; then
  source "$HOME/.profile"
#else load default profile environment variables
else
  source /usr/share/defaults/etc/profile
fi

# ... remaining .bashrc

Compile Python (debug build + dev (--enable-shared is equivalent to python3-dev)) (create the "out" directory first - then run both commands from it)

../configure --with-pydebug --with-lto --enable-optimizations --prefix=/home/jeremi-solus/Applications/Python-3.9.1/out --exec-prefix=/home/jeremi-solus/Applications/Python-3.9.1/out --enable-shared

make altinstall

After compiling Python, you can start compiling GDB. set these flags before running the "configure" command (before compiling)

export CFLAGS="-I/home/jeremi-solus/Applications/Python-3.9.1/out/include/python3.9d -I/home/jeremi-solus/Applications/Python-3.9.1/out/include/python3.9d"
export LDFLAGS="-L/home/jeremi-solus/Applications/Python-3.9.1/out/lib  -lcrypt -lpthread -ldl  -lutil -lm" 
export LIBS="-lcrypt -lpthread -ldl  -lutil -lm"

Note down the path to compiled Python binary. You need to pass this path to GDB configure script

/home/jeremi-solus/Applications/Python-3.9.1/out/bin/python3.9

Run configure script like this - make sure you don't close the bash shell after exporting the earlier set values!! Also, make sure earlier set .profile vars are set by running source ~/.bashrc

./configure --prefix=/home/jeremi-solus/Applications/gdb-10.1/out --exec-prefix=/home/jeremi-solus/Applications/gdb-10.1/out --enable-lto --with-python=/home/jeremi-solus/Applications/Python-3.9.1/out/bin/python3.9

Make GDB (run this from the out directory)

make

Start GDB with this command

./gdb -data-directory ./data-directory
like image 152
JerryWebOS Avatar answered Dec 07 '25 01:12

JerryWebOS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!