GDB调试打印STL对象

其实就gdb_stl_utils是定义的一些语句,就如同shell中的.bashrc一样,可以在启动gdb后把它source进取,也可以重命名到系统目录或者当前路径 .gdbinit 这样gdb在启动的时候就加载了这些所谓的语句(在gdb里设置断点的输入命令也可以加到.gdbinit中,如 b main)

本文做记录,包含

0.备注

1.gdb_stl_utils

2.实例源码

3.调试过程的输入输出

0 >> 备注

参考:http://staff.science.uva.nl/~gilad/stl/stl_gdb.html

.gdbinit: http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt

gdb: http://www.yolinux.com/TUTORIALS/GDB-Commands.html

注意:这里的操作在32位linux机器上没有问题,但是在64位就杯具了,囧里个囧.我也不知道为啥呢
 如果在64位机器上运行,会出这样的问题:(gdb) pmap map_str char* char*elem[0]->left: $1 = 0x14000000 <Address 0x14000000 out of bounds>elem[0]->right: $2 = 0xbfc2f7 <Address 0xbfc2f7 out of bounds>Attempt to dereference a generic pointer.

1 >>gdb_stl_utils

wet http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt
mv dbinit_stl_views-1.03.txt  .gdbinit

2 >>实例源码: 

/********************************** 实例源码 start@peterguo  ***************************/
#include <string>
#include <vector>
#include <map>
#include <iostream>

int main ()
{
    cout << "map ........" << endl;
    map <string, string> map_ctr;
    map_ctr.insert(pair<string, string> (string("key1"), string("value1")));
    map_ctr.insert(pair<string, string> (string("key2"), string("value2")));
    map_ctr.insert(pair<string, string> (string("key3"), string("value3")));
    
    map <string, string>::iterator it = map_ctr.begin();
    for (;it != map_ctr.end(); it++)
    {
        cout << it->first << " -> " << it->second << endl;
    }

    map <int, string> map_int2str;
    map_int2str.insert(pair<int, string> (9527, "shit"));
    map_int2str.insert(pair<int, string> (9528, "shit2"));


    map <int, int> map_int2int;
    map_int2int.insert(pair<int, int> (9527, 9528));
    map_int2int.insert(pair<int, int> (9528, 9529));

    cout << "vectors ........" << endl;
    vector <int> vector_int;
    vector_int.push_back(9527);
    vector_int.push_back(9528);

    vector <string> vecotr_str;
    vecotr_str.push_back("value1");
    vecotr_str.push_back("value2");
 
    cout << "pvector [vector_int] [vecotr_str]" << endl;
    cout << "pmap [map_ctr] [map_int2str] [map_int2int]" << endl;
    cout << "Set break point here at [" << __FILE__ << ":" << __LINE__ << "]" << endl;
    return 0;
}
/********************************** 实例源码 end@peterguo  ***************************/

1 >>调试过程:
root@XXX:/tmp/peterguo# gdb ./test_stl_gdb.exe 
GNU gdb 6.4
Copyright 2005 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i586-suse-linux"...Using host libthread_db library "/lib/libthread_db.so.1".

(gdb) b test_stl_gdb.cpp:43
Breakpoint 1 at 0x80499d8: file test_stl_gdb.cpp, line 43.
(gdb) r
Starting program: /tmp/peterguo/test_stl_gdb.exe 
map ........
key1 -> value1
key2 -> value2
key3 -> value3
vectors ........
pvector [vector_int] [vecotr_str]
pmap [map_ctr] [map_int2str] [map_int2int]

Breakpoint 1, main () at test_stl_gdb.cpp:43
43      cout << "Set break point here at [" << __FILE__ << ":" << __LINE__ << "]" << endl;
(gdb) pvector vector_int
elem[0]: $1 = 9527
elem[1]: $2 = 9528
Vector size = 2
Vector capacity = 2
Element type = int *
(gdb) pvector vecotr_str
elem[0]: $3 = {static npos = 4294967295, 
  _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x80501f4 "value1"}}
elem[1]: $4 = {static npos = 4294967295, 
  _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x805020c "value2"}}
Vector size = 2
Vector capacity = 2
Element type = std::basic_string<char,std::char_traits<char>,std::allocator<char> > *
(gdb) pmap map_ctr char* char*
elem[0]->left: $5 = 0x805004c "key1"
elem[0]->right: $6 = 0x8050034 "value1"
elem[1]->left: $7 = 0x805009c "key2"
elem[1]->right: $8 = 0x8050084 "value2"
elem[2]->left: $9 = 0x80500ec "key3"
elem[2]->right: $10 = 0x80500d4 "value3"
Map size = 3
(gdb) pmap map_int2str int char*
elem[0]->left: $11 = 9527
elem[0]->right: $12 = 0x8050124 "shit"
elem[1]->left: $13 = 9528
elem[1]->right: $14 = 0x805015c "shit2"
Map size = 2
(gdb) pmap map_int2int int int
elem[0]->left: $15 = 9527
elem[0]->right: $16 = 9528
elem[1]->left: $17 = 9528
elem[1]->right: $18 = 9529
Map size = 2


转载于:https://my.oschina.net/sanpeterguo/blog/207280

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值