日期:2014-05-16  浏览次数:20922 次

配置GDB以支持查看stl容器数据

转自:http://blog.csdn.net/sunlylorn/article/details/6788768

?

GDB的自定义命令非常有用,通过自定义命令,直接操作容器中的数据,可以方便的查看STL容器中的数据。

这个链接dbinit_stl_views是Dan C Marinescu写的查看STL容器的自定义命令(如果不适合你的STL版本的话,可以自行修改)。把它添加到你的.gdbinit中,就可以方便的查看STL容器了。它提供了查看vector,list,map,multimap,set,multiset,deque,stack,queue,priority_queue,bitset,string,widestring等对象的方法,非常好用!

?

1. copy如下文本到本地文件dbinit_stl_views-1.03.txt
2. #cat?dbinit_stl_views-1.03.txt >>?~/.gdbinit
3. 若正处于gdb中,运行命令:
(gdb) source ~/.gdbinit

使用查看map类型

(gdb) pmap m_channelMap

Map type = std::map<int, codefarm::Channel*, std::less<int>, std::allocator<std::pair<int const, codefarm::Channel*> > >

Use pmap <variable_name> <left_element_type> <right_element_type> to see the elements in the map.

?

(gdb) pmap m_channelMap int codefarm::Channel*

elem[0].left: $2 = 3

elem[0].right: $3 = (codefarm::Channel *) 0x60890000000000

elem[1].left: $4 = 4

elem[1].right: $5 = (codefarm::Channel *) 0x608bb000000000

elem[2].left: $6 = 5

elem[2].right: $7 = (codefarm::Channel *) 0x608d7000007fff

Map size = 3

?

?

#                                                                                                        
#   STL GDB evaluators/views/utilities - 1.03
#
#   The new GDB commands:                                                         
# 	    are entirely non instrumental                                             
# 	    do not depend on any "inline"(s) - e.g. size(), [], etc
#       are extremely tolerant to debugger settings
#                                                                                 
#   This file should be "included" in .gdbinit as following:
#   source stl-views.gdb or just paste it into your .gdbinit file
#
#   The following STL containers are currently supported:
#
#       std::vector<T> -- via pvector command
#       std::list<T> -- via plist or plist_member command
#       std::map<T,T> -- via pmap or pmap_member command
#       std::multimap<T,T> -- via pmap or pmap_member command
#       std::set<T> -- via pset command
#       std::multiset<T> -- via pset command
#       std::deque<T> -- via pdequeue command
#       std::stack<T> -- via pstack command
#       std::queue<T> -- via pqueue command
#       std::priority_queue<T> -- via ppqueue command
#       std::bitset<n> -- via pbitset command
#       std::string -- via pstring command
#       std::widestring -- via pwstring command
#
#   The end of this file contains (optional) C++ beautifiers
#   Make sure your debugger supports $argc
#
#   Simple GDB Macros writen by Dan Marinescu (H-PhD) - License GPL
#   Inspired by intial work of Tom Malnar, 
#     Tony Novac (PhD) / Cornell / Stanford,
#     Gilad Mishne (PhD) and Many Many Others.
#   Contact: dan_c_marinescu@yahoo.com (Subject: STL)
#
#   Modified to work with g++ 4.3 by Anders Elton
#   Also added _member functions, that instead of printing the entire class in map, prints a