gdb 查看vector, list, map 内容

先下载gdb_stl_utils.tar.gz, extract it, and run make. This will compile and install the necessary files in ~/.gdb (edit the Makefile if you want to use a different directory). To use the p_stl_* functions, add:

source ~/.gdb/gdb_stl_utils

to your ~/.gdbinit.

I've tested this with GCC 3.3.3 and 3.4.0, and GDB 6.0. Let me know if you have any problems with it!

如何使用请查看gdbint文件,里面描述得很详细。

注意:
比如打印vector:
1.(gdb) p vec
$1 = {
  <std::_Vector_base<int,std::allocator<int> >> = {
    _M_impl = {
      <std::allocator<int>> = {
        <__gnu_cxx::new_allocator<int>> = {<No data fields>}, <No data fields>},
      members of std::_Vector_base<int,std::allocator<int> >::_Vector_impl:
      _M_start = 0x92ec040,
      _M_finish = 0x92ec058,
      _M_end_of_storage = 0x92ec060
    }
  }, <No data fields>}

 


2.(gdb) p_stl_vector vec->_M_impl(_M_start和_M_finish位于_M_impl具体情况可能不同)
Vector Element 0:  $2 = 0
Vector Element 1:  $3 = 1
Vector Element 2:  $4 = 2
Vector Element 3:  $5 = 3
Vector Element 4:  $6 = 4
Vector Element 5:  $7 = 100

 

 

http://blog.chinaunix.net/u/9861/showart_1888226.html

Debugging STL code with GDB: http://wiki.freaks-unidos.net/weblogs/arhuaco/debugging-stl-code-with-gdb

通向 UNIX 天堂的 10 个阶梯(使用 STL 和 gdb, 通过结合使用 gdb 与 Valgrind 和 Electric-Fence 解决内存错误)

http://www.ibm.com/developerworks/cn/aix/library/au-aixnirvana/




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

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

2. #cat dbinit_stl_views-1.03.txt >> ~/.gdbinit
3. 若正处于gdb中,运行命令:
(gdb) source ~/.gdbinit
4. 例如,如下代码:
bugging.cpp

  1. #include <vector>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6. vector<int> vec;  
  7. vec.push_back(2);  
  8. vec.push_back(3);  
  9. vec.push_back(4);  
  10. return 0;  
  11. }  

编译:

  1. #g++ -o bugging -g bugging.cpp  
gdb调试:.

  1. # gdb bugging  
  2. GNU gdb 6.8  
  3. Copyright (C) 2008 Free Software Foundation, Inc.  
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>  
  5.   
  6. This is free software: you are free to change and redistribute it.  
  7. There is NO WARRANTY, to the extent permitted by law. Type "show copying"  
  8. and "show warranty" for details.  
  9. This GDB was configured as "i486-slackware-linux"...  
  10. (gdb) help pvector   
  11. Prints std::vector<T> information.  
  12. Syntax: pvector <vector> <idx1> <idx2>  
  13. Note: idx, idx1 and idx2 must be in acceptable range [0..<vector>.size()-1].  
  14. Examples:  
  15. pvector v - Prints vector content, size, capacity and T typedef  
  16. pvector v 0 - Prints element[idx] from vector  
  17. pvector v 1 2 - Prints elements in range [idx1..idx2] from vector  
  18. (gdb) break main  
  19. Breakpoint 1 at 0x80485c6: file bugging.cpp, line 6.  
  20. (gdb) run   
  21. Starting program: /root/learn/c++/bugging   
  22.   
  23. Breakpoint 1, main () at bugging.cpp:6  
  24. 6 vector<int> vec;  
  25. (gdb) n  
  26. 7 vec.push_back(2);  
  27. (gdb)   
  28. 8 vec.push_back(3);  
  29. (gdb) pvector   
  30. Prints std::vector<T> information.  
  31. Syntax: pvector <vector> <idx1> <idx2>  
  32. Note: idx, idx1 and idx2 must be in acceptable range [0..<vector>.size()-1].  
  33. Examples:  
  34. pvector v - Prints vector content, size, capacity and T typedef  
  35. pvector v 0 - Prints element[idx] from vector  
  36. pvector v 1 2 - Prints elements in range [idx1..idx2] from vector  
  37. (gdb) pvector vec  
  38. elem[0]: $1 = 2  
  39. Vector size = 1  
  40. Vector capacity = 1  
  41. Element type = int *  
  42. (gdb) n  
  43. 9 vec.push_back(4);  
  44. (gdb)   
  45. 10 return 0;  
  46. (gdb) pvector vec  
  47. elem[0]: $2 = 2  
  48. elem[1]: $3 = 3  
  49. elem[2]: $4 = 4  
  50. Vector size = 3  
  51. Vector capacity = 4  
  52. Element type = int *  
  53. (gdb)  
5. 默认情况下gdb不能用[]查看stl容器的数据元素,提示如下错误:

  1. <span style="font-size:16px;">(gdb) print vec[0]  
  2. One of the arguments you tried to pass to operator[] could not be converted to what the function wants.</span>  

一些常用内置的命令

Data type   GDB command   
std::vector<T>    pvector stl_variable   
std::list<T>  plist stl_variable T   
std::map<T,T> pmap stl_variable   
std::multimap<T,T>    pmap stl_variable   
std::set<T>   pset stl_variable T   
std::multiset<T>  pset stl_variable   
std::deque<T> pdequeue stl_variable   
std::stack<T> pstack stl_variable   
std::queue<T> pqueue stl_variable   
std::priority_queue<T>    ppqueue stl_variable   
std::bitset<n>td>  pbitset stl_variable   
std::string pstring stl_variable   
std::widestring pwstring stl_variable 

原文地址: http://blog.chinaunix.net/space.php?uid=20594049&do=blog&cuid=1953788
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值