它只在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}
我们可以看到漂亮的打印程序已经安装了:
(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
待办事项: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
libstdcxx
漂亮打印机有一些不喜欢的地方,所以我进行了一些修改。 - user202729