转自: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等对象的方法,非常好用!
使用查看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 member. # # std::vector<> # define pvector if $argc == 0 help pvector else set $size = $arg0._M_impl._M_finish - $arg0._M_impl._M_start set $capacity = $arg0._M_impl._M_end_of_storage - $arg0._M_impl._M_start set $size_max = $size - 1 end if $argc == 1 set $i = 0 while $i < $size printf "elem[%u]: ", $i p *($arg0._M_impl._M_start + $i) set $i++ end end if $argc == 2 set $idx = $arg1 if $idx < 0 || $idx > $size_max printf "idx1, idx2 are not in acceptable range: [0..%u].\n", $size_max else printf "elem[%u]: ", $idx p *($arg0._M_impl._M_start + $idx) end end if $argc == 3 set $start_idx = $arg1 set $stop_idx = $arg2 if $start_idx > $stop_idx set $tmp_idx = $start_idx set $start_idx = $stop_idx set $stop_idx = $tmp_idx end if $start_idx < 0 || $stop_idx < 0 || $start_idx > $size_max || $stop_idx > $size_max printf "idx1, idx2 are not in acceptable range: [0..%u].\n", $size_max else set $i = $start_idx while $i <= $stop_idx printf "elem[%u]: ", $i p *($arg0._M_impl._M_start + $i) set $i++ end end end if $argc > 0 printf "Vector size = %u\n", $size printf "Vector capacity = %u\n", $capacity printf "Element " whatis $arg0._M_impl._M_start end end document pvector Prints std::vector<T> information. Syntax: pvector <vector> <idx1> <idx2> Note: idx, idx1 and idx2 must be in acceptable range [0..<vector>.size()-1]. Examples: pvector v - Prints vector content, size, capacity and T typedef pvector v 0 - Prints element[idx] from vector pvector v 1 2 - Prints elements in range [idx1..idx2] from vector end # # std::list<> # define plist if $argc == 0 help plist else set $head = &$arg0._M_impl._M_node set $current = $arg0._M_impl._M_node._M_next set $size = 0 while $current != $head if $argc == 2 printf "elem[%u]: ", $size p *($arg1*)($current + 1) end if $argc == 3 if $size == $arg2 printf "elem[%u]: ", $size p *($arg1*)($current + 1) end end set $current = $current._M_next set $size++ end printf "List size = %u \n", $size if $argc == 1 printf "List " whatis $arg0 printf "Use plist <variable_name> <element_type> to see the elements in the list.\n" end end end document plist Prints std::list<T> information. Syntax: plist <list> <T> <idx>: Prints list size, if T defined all elements or just element at idx Examples: plist l - prints list size and definition plist l int - prints all elements and list size plist l int 2 - prints the third element in the list (if exists) and list size end define plist_member if $argc == 0 help plist_member else set $head = &$arg0._M_impl._M_node set $current = $arg0._M_impl._M_node._M_next set $size = 0 while $current != $head if $argc == 3 printf "elem[%u]: ", $size p (*($arg1*)($current + 1)).$arg2 end if $argc == 4 if $size == $arg3 printf "elem[%u]: ", $size p (*($arg1*)($current + 1)).$arg2 end end set $current = $current._M_next set $size++ end printf "List size = %u \n", $size if $argc == 1 printf "List " whatis $arg0 printf "Use plist_member <variable_name> <element_type> <member> to see the elements in the list.\n" end end end document plist_member Prints std::list<T> information. Syntax: plist <list> <T> <idx>: Prints list size, if T defined all elements or just element at idx Examples: plist_member l int member - prints all elements and list size plist_member l int member 2 - prints the third element in the list (if exists) and list size end # # std::map and std::multimap # define pmap if $argc == 0 help pmap else set $tree = $arg0 set $i = 0 set $node = $tree._M_t._M_impl._M_header._M_left set $end = $tree._M_t._M_impl._M_header set $tree_size = $tree._M_t._M_impl._M_node_count if $argc == 1 printf "Map " whatis $tree printf "Use pmap <variable_name> <left_element_type> <right_element_type> to see the elements in the map.\n" end if $argc == 3 while $i < $tree_size set $value = (void *)($node + 1) printf "elem[%u].left: ", $i p *($arg1*)$value set $value = $value + sizeof($arg1) printf "elem[%u].right: ", $i p *($arg2*)$value if $node._M_right != 0 set $node = $node._M_right while $node._M_left != 0 set $node = $node._M_left end else set $tmp_node = $node._M_parent while $node == $tmp_node._M_right set $node = $tmp_node set $tmp_node = $tmp_node._M_parent end if $node._M_right != $tmp_node set $node = $tmp_node end end set $i++ end end if $argc == 4 set $idx = $arg3 set $ElementsFound = 0 while $i < $tree_size set $value = (void *)($node + 1) if *($arg1*)$value == $idx printf "elem[%u].left: ", $i p *($arg1*)$value set $value = $value + sizeof($arg1) printf "elem[%u].right: ", $i p *($arg2*)$value set $ElementsFound++ end if $node._M_right != 0 set $node = $node._M_right while $node._M_left != 0 set $node = $node._M_left end else set $tmp_node = $node._M_parent while $node == $tmp_node._M_right set $node = $tmp_node set $tmp_node = $tmp_node._M_parent end if $node._M_right != $tmp_node set $node = $tmp_node end end set $i++ end printf "Number of elements found = %u\n", $ElementsFound end if $argc == 5 set $idx1 = $arg3 set $idx2 = $arg4 set $ElementsFound = 0 while $i < $tree_size set $value = (void *)($node + 1) set $valueLeft = *($arg1*)$value set $valueRight = *($arg2*)($value + sizeof($arg1)) if $valueLeft == $idx1 && $valueRight == $idx2 printf "elem[%u].left: ", $i p $valueLeft printf "elem[%u].right: ", $i p $valueRight set $ElementsFound++ end if $node._M_right != 0 set $node = $node._M_right while $node._M_left != 0 set $node = $node._M_left end else set $tmp_node = $node._M_parent while $node == $tmp_node._M_right set $node = $tmp_node set $tmp_node = $tmp_node._M_parent end if $node._M_right != $tmp_node set $node = $tmp_node end end set $i++ end printf "Number of elements found = %u\n", $ElementsFound end printf "Map size = %u\n", $tree_size end end document pmap Prints std::map<TLeft and TRight> or std::multimap<TLeft and TRight> information. Works for std::multimap as well. Syntax: pmap <map> <TtypeLeft> <TypeRight> <valLeft> <valRight>: Prints map size, if T defined all elements or just element(s) with val(s) Examples: pmap m - prints map size and definition pmap m int int - prints all elements and map size pmap m int int 20 - prints the element(s) with left-value = 20 (if any) and map size pmap m int int 20 200 - prints the element(s) with left-value = 20 and right-value = 200 (if any) and map size end define pmap_member if $argc == 0 help pmap_member else set $tree = $arg0 set $i = 0 set $node = $tree._M_t._M_impl._M_header._M_left set $end = $tree._M_t._M_impl._M_header set $tree_size = $tree._M_t._M_impl._M_node_count if $argc == 1 printf "Map " whatis $tree printf "Use pmap <variable_name> <left_element_type> <right_element_type> to see the elements in the map.\n" end if $argc == 5 while $i < $tree_size set $value = (void *)($node + 1) printf "elem[%u].left: ", $i p (*($arg1*)$value).$arg2 set $value = $value + sizeof($arg1) printf "elem[%u].right: ", $i p (*($arg3*)$value).$arg4 if $node._M_right != 0 set $node = $node._M_right while $node._M_left != 0 set $node = $node._M_left end else set $tmp_node = $node._M_parent while $node == $tmp_node._M_right set $node = $tmp_node set $tmp_node = $tmp_node._M_parent end if $node._M_right != $tmp_node set $node = $tmp_node end end set $i++ end end if $argc == 6 set $idx = $arg5 set $ElementsFound = 0 while $i < $tree_size set $value = (void *)($node + 1) if *($arg1*)$value == $idx printf "elem[%u].left: ", $i p (*($arg1*)$value).$arg2 set $value = $value + sizeof($arg1) printf "elem[%u].right: ", $i p (*($arg3*)$value).$arg4 set $ElementsFound++ end if $node._M_right != 0 set $node = $node._M_right while $node._M_left != 0 set $node = $node._M_left end else set $tmp_node = $node._M_parent while $node == $tmp_node._M_right set $node = $tmp_node set $tmp_node = $tmp_node._M_parent end if $node._M_right != $tmp_node set $node = $tmp_node end end set $i++ end printf "Number of elements found = %u\n", $ElementsFound end printf "Map size = %u\n", $tree_size end end document pmap_member Prints std::map<TLeft and TRight> or std::multimap<TLeft and TRight> information. Works for std::multimap as well. Syntax: pmap <map> <TtypeLeft> <TypeRight> <valLeft> <valRight>: Prints map size, if T defined all elements or just element(s) with val(s) Examples: pmap_member m class1 member1 class2 member2 - prints class1.member1 : class2.member2 pmap_member m class1 member1 class2 member2 lvalue - prints class1.member1 : class2.member2 where class1 == lvalue end # # std::set and std::multiset # define pset if $argc == 0 help pset else set $tree = $arg0 set $i = 0 set $node = $tree._M_t._M_impl._M_header._M_left set $end = $tree._M_t._M_impl._M_header set $tree_size = $tree._M_t._M_impl._M_node_count if $argc == 1 printf "Set " whatis $tree printf "Use pset <variable_name> <element_type> to see the elements in the set.\n" end if $argc == 2 while $i < $tree_size set $value = (void *)($node + 1) printf "elem[%u]: ", $i p *($arg1*)$value if $node._M_right != 0 set $node = $node._M_right while $node._M_left != 0 set $node = $node._M_left end else set $tmp_node = $node._M_parent while $node == $tmp_node._M_right set $node = $tmp_node set $tmp_node = $tmp_node._M_parent end if $node._M_right != $tmp_node set $node = $tmp_node end end set $i++ end end if $argc == 3 set $idx = $arg2 set $ElementsFound = 0 while $i < $tree_size set $value = (void *)($node + 1) if *($arg1*)$value == $idx printf "elem[%u]: ", $i p *($arg1*)$value set $ElementsFound++ end if $node._M_right != 0 set $node = $node._M_right while $node._M_left != 0 set $node = $node._M_left end else set $tmp_node = $node._M_parent while $node == $tmp_node._M_right set $node = $tmp_node set $tmp_node = $tmp_node._M_parent end if $node._M_right != $tmp_node set $node = $tmp_node end end set $i++ end printf "Number of elements found = %u\n", $ElementsFound end printf "Set size = %u\n", $tree_size end end document pset Prints std::set<T> or std::multiset<T> information. Works for std::multiset as well. Syntax: pset <set> <T> <val>: Prints set size, if T defined all elements or just element(s) having val Examples: pset s - prints set size and definition pset s int - prints all elements and the size of s pset s int 20 - prints the element(s) with value = 20 (if any) and the size of s end # # std::dequeue # define pdequeue if $argc == 0 help pdequeue else set $size = 0 set $start_cur = $arg0._M_impl._M_start._M_cur set $start_last = $arg0._M_impl._M_start._M_last set $start_stop = $start_last while $start_cur != $start_stop p *$start_cur set $start_cur++ set $size++ end set $finish_first = $arg0._M_impl._M_finish._M_first set $finish_cur = $arg0._M_impl._M_finish._M_cur set $finish_last = $arg0._M_impl._M_finish._M_last if $finish_cur < $finish_last set $finish_stop = $finish_cur else set $finish_stop = $finish_last end while $finish_first != $finish_stop p *$finish_first set $finish_first++ set $size++ end printf "Dequeue size = %u\n", $size end end document pdequeue Prints std::dequeue<T> information. Syntax: pdequeue <dequeue>: Prints dequeue size, if T defined all elements Deque elements are listed "left to right" (left-most stands for front and right-most stands for back) Example: pdequeue d - prints all elements and size of d end # # std::stack # define pstack if $argc == 0 help pstack else set $start_cur = $arg0.c._M_impl._M_start._M_cur set $finish_cur = $arg0.c._M_impl._M_finish._M_cur set $size = $finish_cur - $start_cur set $i = $size - 1 while $i >= 0 p *($start_cur + $i) set $i-- end printf "Stack size = %u\n", $size end end document pstack Prints std::stack<T> information. Syntax: pstack <stack>: Prints all elements and size of the stack Stack elements are listed "top to buttom" (top-most element is the first to come on pop) Example: pstack s - prints all elements and the size of s end # # std::queue # define pqueue if $argc == 0 help pqueue else set $start_cur = $arg0.c._M_impl._M_start._M_cur set $finish_cur = $arg0.c._M_impl._M_finish._M_cur set $size = $finish_cur - $start_cur set $i = 0 while $i < $size p *($start_cur + $i) set $i++ end printf "Queue size = %u\n", $size end end document pqueue Prints std::queue<T> information. Syntax: pqueue <queue>: Prints all elements and the size of the queue Queue elements are listed "top to bottom" (top-most element is the first to come on pop) Example: pqueue q - prints all elements and the size of q end # # std::priority_queue # define ppqueue if $argc == 0 help ppqueue else set $size = $arg0.c._M_impl._M_finish - $arg0.c._M_impl._M_start set $capacity = $arg0.c._M_impl._M_end_of_storage - $arg0.c._M_impl._M_start set $i = $size - 1 while $i >= 0 p *($arg0.c._M_impl._M_start + $i) set $i-- end printf "Priority queue size = %u\n", $size printf "Priority queue capacity = %u\n", $capacity end end document ppqueue Prints std::priority_queue<T> information. Syntax: ppqueue <priority_queue>: Prints all elements, size and capacity of the priority_queue Priority_queue elements are listed "top to buttom" (top-most element is the first to come on pop) Example: ppqueue pq - prints all elements, size and capacity of pq end # # std::bitset # define pbitset if $argc == 0 help pbitset else p /t $arg0._M_w end end document pbitset Prints std::bitset<n> information. Syntax: pbitset <bitset>: Prints all bits in bitset Example: pbitset b - prints all bits in b end # # std::string # define pstring if $argc == 0 help pstring else printf "String \t\t\t= \"%s\"\n", $arg0._M_data() printf "String size/length \t= %u\n", $arg0._M_rep()._M_length printf "String capacity \t= %u\n", $arg0._M_rep()._M_capacity printf "String ref-count \t= %d\n", $arg0._M_rep()._M_refcount end end document pstring Prints std::string information. Syntax: pstring <string> Example: pstring s - Prints content, size/length, capacity and ref-count of string s end # # std::wstring # define pwstring if $argc == 0 help pwstring else call printf("WString \t\t= \"%ls\"\n", $arg0._M_data()) printf "WString size/length \t= %u\n", $arg0._M_rep()._M_length printf "WString capacity \t= %u\n", $arg0._M_rep()._M_capacity printf "WString ref-count \t= %d\n", $arg0._M_rep()._M_refcount end end document pwstring Prints std::wstring information. Syntax: pwstring <wstring> Example: pwstring s - Prints content, size/length, capacity and ref-count of wstring s end # # C++ related beautifiers (optional) # set print pretty on set print object on set print static-members on set print vtbl on set print demangle on set demangle-style gnu-v3 set print sevenbit-strings off
相关推荐
它可以让你在程序运行时查看变量状态、设置断点、单步执行代码,以诊断和修复错误。使用GDB,开发者可以检查内存泄漏、跟踪函数调用、分析崩溃堆栈等,极大地提高了调试效率。学习GDB的基本命令如`run`、`break`、`...
对于STL容器,GDB提供了增强的查看和遍历功能,使得在调试过程中更易于理解容器的内容。 八、调试库和共享对象 GDB可以调试动态链接的程序,包括调试共享库中的函数。`sharedlibrary`命令加载或卸载动态库,`info ...
- 打印各种类型的数据,如ASCII和宽字符字符串、STL容器内容、数组、变量、内存信息等。 - 打印源代码行以及结构体成员,这对于理解程序逻辑和定位错误非常有帮助。 6. 多进程/线程调试 - 调试已经运行的进程、...
##### 打印STL容器中的内容 ```bash print container_name ``` 直接打印容器内的内容,对于C++程序员来说尤其方便。 ##### 打印大数组中的内容 ```bash print array_name ``` 用于显示数组中的所有元素。 ##### ...
通过STL,开发者可以便捷地进行数据结构操作和算法实现,提高代码复用性和效率。例如,vector提供动态数组功能,而map则提供了键值对的关联容器。 在学习这些知识时,课件通常会包含详细的教程、实例代码、练习题和...
- `.gdbinit`文件可以用来配置gdb的各种设置和自定义命令,这对于提高调试效率非常重要。 2. **按何种方式解析脚本文件** - 使用`source script_file`命令可以执行gdb脚本文件,这对于自动化调试过程非常有用。 ...
2. **STL容器**:掌握vector、list、deque、set、map等STL容器的用法,它们在引擎中用于存储图形对象、顶点信息、颜色数据等。 3. **迭代器**:了解迭代器的工作原理,它是访问STL容器中元素的主要方式,可以用于...
3. **容器与迭代器**:STL(Standard Template Library,标准模板库)中的容器(如vector、list、set、map等)提供了一种方便的方式来存储和管理数据。迭代器则是遍历这些容器的工具,使得我们可以像处理数组一样...
4. **STL(标准模板库)**:C++的STL包含了一系列的容器(如vector、list、set)、迭代器、算法和函数对象,这些都是C++程序员必备的工具。源码中可能会大量使用STL来组织和操作数据。 5. **异常处理**:C++通过try...
5. **模板和STL**:C++的模板机制允许创建泛型代码,而标准模板库(STL)提供了容器(如vector、list)、算法和迭代器等工具,方便程序员进行高效的数据处理。 6. **异常处理**:C++的异常处理机制允许在程序运行时...
4. **STL(Standard Template Library)**:C++的STL提供了容器(如vector、list、map)、迭代器、算法等工具,大大简化了数据操作。在生产管理系统中,可能用到`std::vector`来存储产品库存,`std::map`来关联产品...
C++中的STL(Standard Template Library)提供了容器(如vector、list、map)和算法,能够有效地管理数据和执行操作。例如,可以使用map存储客户信息,通过键值对快速查找和更新客户数据;vector用于存储订单列表,...
- **STL库的利用**:标准模板库(STL)如vector、map等容器在数据结构和算法中广泛使用。 - **异常处理**:通过异常处理机制,确保错误发生时能及时捕获并处理。 - **多线程支持**:C++的线程库使得MuPDF能够充分...
- **STL(Standard Template Library)**:学习标准模板库,包括容器(如vector、list、set)、迭代器、算法等。 - **异常处理**:理解异常的抛出、捕获机制,以及何时应该使用异常。 - **C++11及更高版本的新...
5. 标准模板库(STL):包括容器(vector, list, set等),迭代器,算法和内存管理工具。 六、Linux系统编程 1. 文件I/O:open, read, write, close等系统调用来进行文件操作。 2. 进程管理:fork, exec, wait, ...
7. STL库:标准模板库(STL)是C++的重要组成部分,提供了容器(如vector、list、map)、算法(如排序、查找)和迭代器等工具,大大简化了数据结构和算法的实现。 8. 异常处理:C++支持异常处理机制,通过`try`、`...
6. **容器与算法**: 熟悉STL(Standard Template Library)中的容器(如vector、list、set等)和算法库,提高代码复用性和效率。 7. **异常处理**: 学习如何使用try、catch语句进行异常处理,增强程序的健壮性。 8. ...
此外,熟悉STL(Standard Template Library)中的容器(如vector和map)、算法和迭代器也会对理解和修改源代码有所帮助。 五、编译与调试 在实际开发中,开发者需要知道如何配置和使用编译器,如GCC或Clang,以及...
C++ API 包含了大量预定义的函数和类,如I/O流(iostream)用于输入输出,容器(如vector和list)用于数据存储,算法库(algorithm)提供各种通用的计算功能。API帮助文档将详细介绍这些内容,帮助开发者更有效地...