假设有这样的东西:
#include <map>
int main(){
std::map<int,int> m;
m[1] = 2;
m[2] = 4;
return 0;
}
我希望能够检查从 gdb 运行程序的 map 内容。
如果我尝试使用下标运算符,我会得到:
(gdb) p m[1]
Attempt to take address of value not located in memory.
使用 find 方法不会产生更好的结果:
(gdb) p m.find(1)
Cannot evaluate function -- may be inlined
有没有办法做到这一点?
最佳答案
此问题的现有答案非常已过时。由于 GDB 7.x 中内置的 Python 支持以及 GCC 附带的 libstdc++ pretty-print ,它使用最近的 GCC 和 GDB 即可工作TM。
对于 OP 的示例,我得到:
(gdb) print m
$1 = std::map with 2 elements = {[1] = 2, [2] = 4}
如果它不能自动为您工作,请参阅 STL Support 上的第一个要点GDB wiki 页面。
您也可以为自己的类型编写 Python pretty-print ,请参阅 Pretty Printing在 GDB 手册中。
关于c++ - 使用 gdb 检查标准容器 (std::map) 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/427589/