如何在GDB中以普通人类可以理解的方式查看容器的内容?
很多博客都说了,比如这篇:http://blog.csdn.net/meteor1113/article/details/5180293
现摘抄如下:
GDB 7.0加入了python脚本支持,可以通过python编写pretty-printers用来显示 stl容器。
方法是通过svn下载python libstdc++ printers:
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
但是,这个地址co下来是有问题的:
(gdb) print m_HandlerMap
$3 = std::map with 1 elementsTraceback (most recent call last):
File "/home/lxf/.gdb/python/libstdcxx/v6/printers.py", line 427, in children
rep_type = find_type(self.val.type, '_Rep_type')
File "/home/lxf/.gdb/python/libstdcxx/v6/printers.py", line 45, in find_type
raise ValueError, "Cannot find type %s::%s" % (str(orig), name)
ValueError: Cannot find type std::map<int, SelectEvent*, std::less<int>, std::allocator<std::pair<int const, SelectEvent*> > >::_Rep_type
查看map的时候会有上面的错误信息,所以,stackoverflow告诉我答案了:http://stackoverflow.com/questions/9102967/pretty-printers-for-maps-throwing-a-type-error
出现上面错误原因是:libstdc++变了,所以pretty-printers的代码也变了,上面给的SVN地址就变得旧了。新的SVN地址:
svn co svn://gcc.gnu.org/svn/gcc/branches/gcc-4_6-branch/libstdc++-v3/python
这样就可以了。
把下载来的python目录放在用户目录下的.gdb下。之后在~/.gdbinit中加入下面的内容:
- python
- import sys
- import os
- p = os.path.expanduser('~/.gdb/python') 这一行中引号内容要改成你刚才从SVN上checkout下来的目录
- print p
- if os.path.exists(p):
- sys.path.insert(0, p)
- from libstdcxx.v6.printers import register_libstdcxx_printers
- register_libstdcxx_printers(None)
- end
这样就为stl容器安装好了pretty-printers,在GDB中直接print容器会直接 print出容器内容,而不是以前那一堆乱七八糟的东西。