用于调试 stdlib 变量的 Python pretty-print 将无法工作

标签 python debugging printing gdb

我按照这篇文章以一种漂亮的方式调试变量 The value of strings doesn't appear in eclipse mars CDT

但是我最终得到如下错误消息:

 File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in <module>
    from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named 'libstdcxx'

我该如何解决这个问题?

最佳答案

看起来你的 .gdbinit 没有正确的内容,或者没有运行。

确保添加的路径对您的机器是正确的,并且 gdbinit 文件正在运行。

由于您收到该错误,因此只需要将正确的路径添加到 python。

这是一个示例跟踪,它首先不起作用,然后在更正路径后起作用:

$ cat hello.cc 
#include <string>
using namespace std;

int main() {
    string mystring = "my string here";
    return 0;
}

$ g++ hello.cc -g -o hello.elf

$ gdb hello.elf --quiet

Reading symbols from hello.elf...done.
(gdb) b hello.cc:6
Breakpoint 1 at 0x400863: file hello.cc, line 6.
(gdb) r
Starting program: /tmp/x/hello.elf 
Traceback (most recent call last):
  File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in <module>
    from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named 'libstdcxx'

Breakpoint 1, main () at hello.cc:6
6       return 0;
(gdb) p mystring
$1 = {static npos = <optimised out>, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, 
    _M_p = 0x602028 "my string here"}}
(gdb) python
>import sys
>sys.path.insert(0, '/usr/share/gcc-4.8/python/')
>end
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /tmp/x/hello.elf 

Breakpoint 1, main () at hello.cc:6
6       return 0;
(gdb) p mystring
$2 = "my string here"
(gdb) 

上面例子的版本信息:

$ g++ --version
g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gdb --version
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".

使用 Eclipse CDT 时

使用 Eclipse CDT 时,您需要手动指定 gdbinit 文件。 Eclipse CDT 使用 --nx 标志启动 GDB,这会阻止 GDB 自动获取任何 .gdbinit 文件。相反,您应该在启动配置中指定一个 CDT 适当的初始化文件:

gdbinit in cdt

此外,您可以在“首选项”中为新的启动配置更改默认启动 gdbinit,如此对话框所示:

default gdb init

关于用于调试 stdlib 变量的 Python pretty-print 将无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33482030/

相关文章:

html - 输入打印时内部没有文本,但在屏幕上可见

.net - Outlook 互操作 PrintOut 文档名称

python - Tensorflow:如何替换计算图中的节点?

.net - 为什么 Debug模式文件大小比 Release模式大?

debugging - 您能否在 BIDS 中监视 SSIS 包在服务器上运行时的执行情况?

java - 如何在 FXML 中打印选定的 Pane

python - 遵循照片应用程序教程时出现 Django models.py 错误

python - 列表列表的索引复杂度是多少(例如 list[x][y])

python - 使用 Python 3 发送 WM_COPYDATA

python - 给定一个 pyqtBoundSignal 如何确定插槽?