原地址: http://blog.csdn.net/redsuntim/article/details/11976265
在Linux下用GDB调试STL很麻烦, 不像windows下那么方便, 偶然发现了这么一个神奇的方法, 分享给大家.
另外使用Linux下图形化调试工具: kdbg, Affinic介绍的Affinic软件可以很方便的在图形化界面下调试STL.
GDB中print方法并不能直接打印STL容器中保存的变量,其实只要 http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt 这个文件保存为~/.gdbinit 就可以使用它提供的方法方便调试容器.
调试方法很简单: 打开 gdb, 在gdb中执行:
source ~/.gdbinit
如正常我使用GDB自带的print打印一个STL list容器变量list
- (gdb) p lst
- $4 = {
- <std::_List_base<int, std::allocator<int> >> = {
- _M_impl = {
- <std::allocator<std::_List_node<int> >> = {
- <__gnu_cxx::new_allocator<std::_List_node<int> >> = {<No data fields>}, <No data fields>},
- members of std::_List_base<int, std::allocator<int> >::_List_impl:
- _M_node = {
- _M_next = 0x804d008,
- _M_prev = 0x804d048
- }
- }
- }, <No data fields>}
现在我使用这个脚本提供的plist方法打印
- (gdb) plist lst
- List size = 5
- List type = std::list<int, std::allocator<int> >
- Use plist <variable_name> <element_type> to see the elements in the list.
- (gdb)
详细查看list中的元素信息
- (gdb) plist lst int
- elem[0]: $5 = 7
- elem[1]: $6 = 1
- elem[2]: $7 = 5
- elem[3]: $8 = 9
- elem[4]: $9 = 2
- List size = 5
- (gdb)