C++ 使用 gdb 检查标准容器 (std::map) 内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/427589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inspecting standard container (std::map) contents with gdb
提问by Paolo Tedesco
Supposing to have something like this:
假设有这样的事情:
#include <map>
int main(){
std::map<int,int> m;
m[1] = 2;
m[2] = 4;
return 0;
}
I would like to be able to inspect the contents of the map running the program from gdb.
If I try using the subscript operator I get:
我希望能够检查从 gdb 运行程序的地图的内容。
如果我尝试使用下标运算符,我会得到:
(gdb) p m[1]
Attempt to take address of value not located in memory.
Using the find method does not yield better results:
使用 find 方法不会产生更好的结果:
(gdb) p m.find(1)
Cannot evaluate function -- may be inlined
Is there a way to accomplish this?
有没有办法做到这一点?
采纳答案by jpalecek
I think there isn't, at least not if your source is optimized etc. However, there are some macros for gdb that can inspect STL containers for you:
我认为没有,至少如果您的源代码经过优化等,则没有。但是,gdb 有一些宏可以为您检查 STL 容器:
http://sourceware.org/ml/gdb/2008-02/msg00064.html
http://sourceware.org/ml/gdb/2008-02/msg00064.html
However, I don't use this, so YMMV
但是,我不使用这个,所以 YMMV
回答by Jonathan Wakely
The existing answers to this question are veryout of date. With a recent GCC and GDB it Just WorksTMthanks to the built-in Python support in GDB 7.x and the libstdc++ pretty printers that come with GCC.
现有的这个问题的答案是非常不合时宜的。随着最近的GCC和GDB它只是工作TM得益于内置的GDB 7.x和的的libstdc ++附带GCC漂亮打印机Python支持。
For the OP's example I get:
对于 OP 的示例,我得到:
(gdb) print m
= std::map with 2 elements = {[1] = 2, [2] = 4}
If it doesn't work automatically for you see the first bullet point on the STL Supportpage of the GDB wiki.
如果它不能自动为您工作,请查看GDB wiki的STL 支持页面上的第一个要点。
You can write Python pretty printers for your own types too, see Pretty Printingin the GDB manual.
您也可以为自己的类型编写 Python 漂亮的打印机,请参阅GDB 手册中的漂亮打印。
回答by Mr.Ree
There's always the obvious: Define your own test-function... Call it from gdb. E.g.:
总是有显而易见的:定义你自己的测试函数...从 gdb 调用它。例如:
#define SHOW(X) cout << # X " = " << (X) << endl
void testPrint( map<int,int> & m, int i )
{
SHOW( m[i] );
SHOW( m.find(i)->first );
}
int
main()
{
std::map<int,int> m;
m[1] = 2;
m[2] = 4;
return 0; // Line 15.
}
And:
和:
....
Breakpoint 1 at 0x400e08: file foo.C, line 15.
(gdb) run
Starting program: /tmp/z/qD
Breakpoint 1, main () at qD.C:15
(gdb) call testPrint( m, 2)
m[i] = 4
(*m.find(i)).first = 2
(gdb)
回答by Employed Russian
The stl-views.gdb
used to be the best answer there was, but not anymore.
在stl-views.gdb
曾经是最好的答案出现了,但现在不是了。
This isn't integrated into the mainline GDB
yet, but here is what you get using the 'archer-tromey-python' branch:
这还没有集成到主线中GDB
,但这是您使用“archer-tromey-python”分支获得的内容:
(gdb) list
1 #include <map>
2 int main(){
3 std::map<int,int> m;
4 m[1] = 2;
5 m[2] = 4;
6 return 0;
7 }
(gdb) break 6
Breakpoint 1 at 0x8048274: file map.cc, line 6.
(gdb) run
Breakpoint 1, main () at map.cc:6
6 return 0;
(gdb) print m
= std::map with 2 elements = {
[1] = 2,
[2] = 4
}
(gdb) quit
回答by anand
Try De-Referencing STL Containers: on this page: http://www.yolinux.com/TUTORIALS/GDB-Commands.html
尝试取消引用 STL 容器:在此页面上:http: //www.yolinux.com/TUTORIALS/GDB-Commands.html
回答by Mazhar MIK
The answers above are working and fine. In case you are using stl-views.gdb, here is the proper way of viewing the maps and elements inside it.
Let your map is as follows :
std::map<char, int> myMap;
上面的答案工作正常。如果您使用 stl-views.gdb,这里是查看其中的地图和元素的正确方法。让你的地图如下:
std::map<char, int> myMap;
(gdb) pmap myMap char int
i.e. pmap <variable_name> <left_element_type> <right_element_type>
to see the elements in the map.
即pmap <variable_name> <left_element_type> <right_element_type>
查看地图中的元素。
Hope that helps.
希望有帮助。
回答by Dan
You can get around the second problem (Cannot evaluate function -- may be inlined
) by making sure that your compiler uses DWARF-2 (or 3 or 4) debugging information when you compile your program. DWARF-2 includes inlining information, so you should be able to use either of the methods you described to access elements of your std::map
container.
您可以Cannot evaluate function -- may be inlined
通过确保编译器在编译程序时使用 DWARF-2(或 3 或 4)调试信息来解决第二个问题 ( )。DWARF-2 包含内联信息,因此您应该能够使用您描述的任一方法来访问std::map
容器的元素。
To compile with DWARF-2 debug info, add the -gdwarf-2
flag to your compile command.
要使用 DWARF-2 调试信息-gdwarf-2
进行编译,请将该标志添加到您的编译命令中。