使gdb支持string、vector、map等STL类型数据的查看(linux)

前提条件

(1)需要安装python

[danni@vm-xxx-18 develop]$ python --version
Python 2.6.6

(2)需要有gcc

[danni@vm-xxx-18 develop]$ gcc --version
gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18)

(3)需要有gdb。并且在gdb安装的时候需要设置python选项。确保GDB版本为7.0或更高版本,这样它支持Python脚本。

[danni@vm-bestgame-18 ~]$ gdb --version
GNU gdb (GDB) 7.4.1



支持查看STL类型的数据的查看的gdb安装
(1)完成基本的gdb安装

wget http://ftp.gnu.org/gnu/gdb/gdb-7.4.1.tar.gz
tar xvzf gdb-7.4.1.tar.gz
cd gdb-7.4.1
./configuration --with-python='/usr/bin/python'

通过操作./configuration -h可以发现并没有发现--with-python的安装选项,不过可以自己加上这个安装选项,可以通过which python找到python的执行路径,通过--with-python='/usr/bin/python'来设置的python的路径
查看python所在的地址:

[danni@vm-xxx-18 gdb-7.4.1]$ which python
/usr/bin/python



(2)激活python的gdb功能
首先checkout:svn checkout svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python stlprettyprinter
并在用户目录(cd ~)下编写.gdbinit文件,其中sys.path.insert(0, '/home/danni/stlprettyprinter')中的路径即为你svncheckout的目录

[danni@vm-xxx-18 ~]$ cd ~
[danni@vm-xxx-18 ~]$ vim .gdbinit
python
import sys
sys.path.insert(0, '/home/danni/stlprettyprinter')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end



(3)如果现在你的gdb可以查看string类型,可以查看vector类型,但是不能查看map类型的数据,则需要检查你的gcc版本
如果你的gcc版本小于4.7,则需要使用另一个printer.py文件替换刚才checkout的文件

[danni@vm-xxx-18 v6]$ cd ~/stlprettyprinter/libstdcxx/v6
[danni@vm-xxx-18 v6]$ rm printers.py
[danni@vm-xxx-18 v6]$ wget https://gcc.gnu.org/svn/gcc/branches/gcc-4_6-branch/libstdc++-v3/python/libstdcxx/v6/printers.py



实例演示
首先编写一个测试文件:

#include 
#include 
#include 
using namespace std;
int main()
{
    string a("test string");

    vector<int> v;
    v.push_back(1);
    v.push_back(2);

    map<int,int> iMap;
    iMap.insert(make_pair(1,2));

    return 0;
}

通过 b xxx 进行断点,通过 r 运行程序,通过 p 查看对象的值

[danni@vm-bestgame-18 testStr]$ g++ -g Test.cpp 
[danni@vm-bestgame-18 testStr]$ gdb ./a.out 
GNU gdb (GDB) 7.4.1
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later //gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
For bug reporting instructions, please see:
//www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/danni/testStr/a.out...done.
(gdb) b main
Breakpoint 1 at 0x400ae2: file Test.cpp, line 7.
(gdb) r
Starting program: /home/danni/testStr/a.out 

Breakpoint 1, main () at Test.cpp:7
7           string a("test string");
(gdb) n
9           vector<int> v;
(gdb) p a
$1 = "test string"
(gdb) n
10          v.push_back(1);
(gdb) n
11          v.push_back(2);
(gdb) p v
$2 = std::vector of length 1, capacity 1 = {1}
(gdb) n
13          map<int,int> iMap;
(gdb) n
14          iMap.insert(make_pair(1,2));
(gdb) n
16          return 0;
(gdb) p iMap
$3 = std::map with 1 elements = {[1] = 2}
(gdb) 

你可能感兴趣的:(gdb,gdb查看map的值,gdb查看string的值,gdb查看STL的值)