它只适用于 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));
}
Compile:
g++ -O0 -ggdb3 -o main.out -std=c++98 main.cpp
Outcome:
(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
并位于libstdc++-v3/python/libstdcxx
在GCC源代码中:https://github.com/gcc-mirror/gcc/blob/releases/gcc-6.3.0/libstdc%2B%2B-v3/python/libstdcxx/v6/printers.py#L244 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
方法并在以下位置调用它:使用 GDB 打印 C++ 类对象 https://stackoverflow.com/questions/15163261/printing-c-class-objects-with-gdb/58310207#58310207
检查优化代码中的特定元素
上次我检查时很难,你得到“无法评估函数 - 可能是内联的”C++、STL、GDB:无法评估可能内联的函数 https://stackoverflow.com/questions/40633787/c-stl-gdb-cannot-evaluate-function-maybe-inlined
在未优化的代码上它可以工作:使用 gdb 检查标准容器 (std::map) 内容 https://stackoverflow.com/questions/427589/inspecting-standard-container-stdmap-contents-with-gdb