GDB Python支持,ImportError没有名为gdb的模块

3
我正在尝试编译带有Python支持的gdb,以便我可以使用提供在以下网址中的PrettyPrinters: http://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python 我从(http://ftp.gnu.org/gnu/gdb/gdb-7.6.1.tar.gz)下载了最新的gdb源代码,并在我的Centos 6.4上进行了如下编译: 1. ./configure --with-python 2. make 我需要为--with-python提供路径或其他参数来指定Python库或可执行文件的路径吗?
在编译后当我运行gdb时,我看到这个警告:
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.

这里很明显出现了异常,无论接下来我将要做什么都会失败,因为它需要gdb模块,但是我还是尝试了一下。所以我在~/.gdbinit中添加了以下几行:

import sys 

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

现在当我启动gdb时,出现以下错误:
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.

有人能帮我解决这个问题吗?
4个回答

1
如果有人正在尝试在Solus 4.1中编译Python和GDB,那么请阅读我创建的以下设置指南(显然,将任何目录名称/树替换为您自己的)。像这样设置.profile:
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

按照以下方式设置.bashrc

#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

编译 Python(调试版本 + dev (--enable-shared 相当于 python3-dev))(首先创建“out”目录 - 然后从中运行两个命令)

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

在编译Python后,您可以开始编译GDB。在运行“configure”命令(编译之前)之前设置这些标志。
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"

记下编译过的Python二进制文件的路径。您需要将此路径传递给GDB配置脚本。
/home/jeremi-solus/Applications/Python-3.9.1/out/bin/python3.9

像这样运行配置脚本 - 确保在导出之前设置的值后不要关闭bash shell!另外,通过运行source ~/.bashrc确保之前设置的.profile变量已设置

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

在 out 目录下运行 GDB(使用以下命令)

make

Start GDB with this command

./gdb -data-directory ./data-directory

0
检查 /usr/local/share/gdb/python 的权限。 即使执行了 "make install",我仍然有以下权限:

drwxrwx--- 4 root root 4096 Mar 16 08:46 /usr/local/share/gdb

将其递归地设置为 go+rx,包括所有子目录,警告消失了。


如果gdb未全局安装,而是位于本地文件夹中,例如:~/gdb-7.12.1/,我该怎么办?文件夹gdb/python存在,但在其中。我应该如何将其展示给Python? - avp
你收到了哪个错误信息?你如何调用gdb? - FrankL

0

CentOS 6 的 gdb 已经支持 Python。所以你不需要自己构建。

但是,既然你已经这样做了,你尝试过按照 gdb 错误消息中建议的方法操作吗?

此外,你执行了 "make install" 吗?你必须这样做才能使其正常工作。

最后,如果 CentOS 6 没有包含 libstdc++ 的漂亮打印器,我会感到惊讶。


Tom,gdb构建的系统与实际使用的系统不同(我们进行打包)。在这种情况下,--data-directory应该指向什么?我以前没有使用过该选项,因此不知道应该指向哪个目录? - alphabit
更重要的是正确设置--with-separate-debug-dir。在CentOS上,我想/usr/lib/debug是正确的。您还可以将一些东西从/usr/share/gdb符号链接到您的安装树中;也许只是/usr/share/gdb/auto-load。但如果您将其打包为RPM,则可能应该从官方SRPM开始并从那里进行。 - Tom Tromey

0

我认为你需要升级你的操作系统版本。 我在仅更新了 glibc 后遇到了这个问题。 据我所知, 在其他情况下,旧版的操作系统可能会导致此问题,例如 Ubuntu 14.04。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接