【问题标题】:GDB Python support, ImportError no module named gdbGDB Python 支持,ImportError no module named gdb
【发布时间】:2013-09-27 08:04:07
【问题描述】:

我正在尝试使用 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.制作

我是否需要为 --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.

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: c++ python linux stl gdb


    【解决方案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(调试构建 + 开发(--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
    

    制作 GDB(从 out 目录运行)

    make
    

    用这个命令启动 GDB

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

    【讨论】:

      【解决方案2】:

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

      但是,既然您这样做了,您是否尝试过 gdb 在错误消息中建议的操作?

      另外,您是否“进行安装”?您必须这样做才能使其正常工作。

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

      【讨论】:

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

      检查 /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 揭示它?
      • 你得到了哪个错误信息,你如何调用gdb?
      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-10
        • 2011-04-08
        • 2016-08-09
        • 1970-01-01
        • 2017-02-10
        • 2017-04-15
        • 2018-03-08
        • 2016-12-23
        相关资源
        最近更新 更多