1. 程式人生 > >使gdb支援string、vector、map等STL型別資料的檢視(linux)

使gdb支援string、vector、map等STL型別資料的檢視(linux)

前提條件

(1)需要安裝python

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

(2)需要有gcc

[[email protected]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 <string>
#include <vector>
#include <map>
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 檢視物件的值

[[email protected]18 testStr]$ g++ -g Test.cpp 
[[email protected]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 <http://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:
<http://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)