c++ - 如何在 GDB 中漂亮地打印 STL 容器?

我已按照说明 on the GDB wiki安装用于查看 STL 容器的 python pretty-print 。我的 ~/.gdbinit 现在看起来像这样:

python 
import sys 
sys.path.insert(0, '/opt/gdb_prettyprint/python') 
from libstdcxx.v6.printers import register_libstdcxx_printers 
register_libstdcxx_printers (None) 
end 

但是,当我运行 GDB 并尝试打印 STL 类型时,我得到以下信息:

print myString
Python Exception <class 'gdb.error'> No type named std::basic_string<char>::_Rep.: 
$3 = 

任何人都可以对此有所了解吗?我正在运行 GDB 7.4 附带的 Ubuntu 12.04。

最佳答案

它只适用于 Ubuntu 17.04

Debian 似乎现在终于可以正确地集成东西了:

main.cpp

#include <map>
#include <utility>
#include <vector>

int main() {
    std::vector<int> v;
    v.push_back(0);
    v.push_back(1);
    v.push_back(2);
    std::map<int,int> m;
    m.insert(std::make_pair(0, 0));
    m.insert(std::make_pair(1, -1));
    m.insert(std::make_pair(2, -2));
}

编译:

g++ -O0 -ggdb3 -o main.out -std=c++98 main.cpp

结果:

(gdb) p v
$1 = std::vector of length 3, capacity 4 = {0, 1, 2}
(gdb) p m
$2 = std::map with 3 elements = {[0] = 0, [1] = -1, [2] = -2}

我们可以看到 pretty-print 安装了:

(gdb) info pretty-printer

其中包含以下行:

global pretty-printers:
  objfile /usr/lib/x86_64-linux-gnu/libstdc++.so.6 pretty-printers:
  libstdc++-v6
    std::map
    std::vector

打印机由文件提供:

/usr/share/gcc-7/python/libstdcxx/v6/printers.py

它与主 C++ 库包 libstdc++6 一起提供,位于 GCC 源代码中的 libstdc++-v3/python/libstdcxx 下: https://github.com/gcc-mirror/gcc/blob/releases/gcc-6.3.0/libstdc%2B%2B-v3/python/libstdcxx/v6/printers.py#L244

TODO:GDB 如何找到该文件是最后的谜团,它不在我的 Python 路径中:python -c "import sys; print('\n'.join(sys.path))" 所以它必须在某个地方硬编码?

自定义类

查看如何定义自定义 toString 方法并在以下位置调用它:Printing C++ class objects with GDB

检查优化代码中的特定元素

上次我检查时很难,你得到“无法评估函数——可能是内联的”C++, STL, GDB: Cannot evaluate function maybe inlined

在未优化的代码上有效:Inspecting standard container (std::map) contents with gdb

https://stackoverflow.com/questions/11606048/

相关文章:

linux - 如何使用 sed 提取子字符串

linux - 如何将一个文本文件拆分为多个 *.txt 文件?

python - standard_init_linux.go :178: exec user pr

linux - 在 mac 上通过 ssh 连接到 amazon aws linux 服务器

linux - 在 Unix 上计算每行/字段的字符出现次数

linux - 如何运行 nohup 并将其 pid 文件写入单个 bash 语句

linux - 如何自动化 HTML 到 PDF 的转换?

linux - 远程 Linux 服务器到远程 linux 服务器目录复制。如何?

linux - 如何获得linux中两个文件之间的差异(仅添加)

linux - 如何从 linux 命令行获取视频文件的分辨率(宽度和高度)?