我已按照说明 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/