GDB 从7.x版本开始就支持pretty printer
可是一直以来都没有见到这个东西起作用,最近就研究了一下。
我的环境是Mac Mountain Lion + macports,GCC 4.7,GDB 7.5
首先,按照
http://sourceware.org/gdb/wiki/STLSupport
下载checkout STL的pretty printer。
设置好.gdbinit以后,运行告知已经注册,不能重复注册。
$locate printers.py
发现在/opt/local/share/gcc-4.7/gcc-4.7.1/python/libstdcxx/v6已经有一份现成的了。
好吧,我们就使用现有的好了。
写个程序,调试,print,怎么回事?还是不成?
头晕了好久,好在python的源码是可以随便看随便改的,于是在printers.py里面改来改去,调试了半天,发现:
这个printers.py注册了好多stl类型的printer,打印出来有以下这些:
std::__cxx1998::__7::unordered_multiset
std::tr1::unordered_multiset
std::unordered_set
std::tr1::unordered_multimap
std::tr1::__7::weak_ptr
std::__7::bitset
std::__cxx1998::multiset
std::bitset
std::forward_list
std::__7::forward_list
std::__cxx1998::forward_list
std::__7::_Rb_tree_iterator
std::__7::list
std::__debug::unordered_multimap
__gnu_cxx::slist
std::__7::priority_queue
std::__debug::map
std::_List_const_iterator
std::__debug::unordered_map
__gnu_cxx::_Slist_iterator
std::set
std::__cxx1998::__7::_Deque_const_iterator
std::__7::_List_const_iterator
std::__7::multiset
std::shared_ptr
std::__7::queue
std::__cxx1998::_List_iterator
std::tr1::unordered_map
std::__7::vector
std::__debug::queue
std::__norm::_Deque_const_iterator
std::__7::unordered_set
std::__norm::_Deque_iterator
std::__cxx1998::_Deque_iterator
__gnu_cxx::__7::_Slist_iterator
std::__cxx1998::list
std::__cxx1998::__7::list
std::unordered_multimap
std::__cxx1998::unordered_multiset
std::__cxx1998::__7::_List_iterator
std::__7::map
std::__debug::vector
std::tr1::__7::unordered_set
std::__7::basic_string
std::weak_ptr
std::__7::set
std::__7::unordered_multiset
__gnu_cxx::__7::__normal_iterator
std::__cxx1998::__7::vector
std::unordered_map
std::list
std::tr1::__7::unordered_map
std::__7::unordered_multimap
std::vector
std::tr1::unordered_set
std::_List_iterator
std::__7::_Deque_iterator
std::__cxx1998::map
std::__cxx1998::bitset
std::__7::weak_ptr
std::__cxx1998::vector
std::__cxx1998::unordered_set
std::priority_queue
__gnu_cxx::__7::slist
std::__7::_Rb_tree_const_iterator
std::_Deque_const_iterator
std::__7::deque
std::__cxx1998::set
std::__cxx1998::__7::bitset
__gnu_debug::_Safe_iterator
std::tr1::__7::unordered_multimap
__gnu_cxx::__normal_iterator
std::__cxx1998::__7::multiset
std::__cxx1998::__7::unordered_map
std::__debug::unique_ptr
std::__cxx1998::_List_const_iterator
std::_Deque_iterator
std::tr1::__7::unordered_multiset
std::unordered_multiset
std::__cxx1998::__7::unordered_multimap
std::__debug::multiset
std::tr1::weak_ptr
std::__cxx1998::_Deque_const_iterator
std::__cxx1998::unordered_map
std::__7::unique_ptr
std::__debug::list
std::__debug::unordered_multiset
std::__cxx1998::deque
std::_Rb_tree_const_iterator
std::__debug::bitset
std::queue
std::tr1::__7::shared_ptr
std::__debug::unordered_set
std::tr1::shared_ptr
std::__cxx1998::__7::_List_const_iterator
std::__cxx1998::__7::_Deque_iterator
std::__cxx1998::__7::map
std::__7::stack
std::unique_ptr
std::__cxx1998::__7::deque
std::map
std::__7::multimap
std::stack
std::__debug::stack
std::_Rb_tree_iterator
std::multimap
std::__norm::_List_const_iterator
std::__debug::multimap
std::__norm::_List_iterator
std::deque
std::tuple
std::__cxx1998::unordered_multimap
std::basic_string
std::__7::tuple
std::__debug::deque
std::__debug::priority_queue
std::__7::_Deque_const_iterator
std::__7::shared_ptr
std::__cxx1998::__7::multimap
std::__debug::forward_list
std::__7::unordered_map
std::__debug::set
std::__cxx1998::__7::unordered_set
std::__cxx1998::__7::set
std::__7::_List_iterator
std::__cxx1998::multimap
std::__cxx1998::__7::forward_list
std::multiset:
然后,我们的一个简单的string类型,传入到python的lookup里面查找,找不到?
最后发现,string类型传入python以后的basename是basic_string,没有前面的namespace!
于是,在Printer类的add_version函数里面,增加一行
self.add(name, function)
保存,重新打开GDB,调试程序,OK了:
(gdb) p s
$1 = "great"
(gdb) p v
$2 = vector of length 5, capacity 8 = {"a1", "a2", "a2", "a3", "a4"}