示例代碼:
//============================================================================
// Name : cpp.cpp
// Author : weijl
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
#define PI 3.1415926
const int WEEK= 7;
const string NAME= "weijl";
static double SCORE= 99.0;
class CMyData
{
public:
CMyData()
{
a = 0;
str = string("weijl");
c = 65;
}
~CMyData()
{
}
int a;
string str;
float c;
int GetA()
{
return a;
}
string GetStr()
{
return str;
}
float GetC()
{
return c;
}
void setmv()
{
m_v.push_back("weijl");
m_v.push_back("shenzhen");
m_v.push_back("nanning");
m_v.push_back("C++ Program");
}
public:
typedef vector<string> vs;
typedef vector<string>::iterator It;
vs m_v;
};
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
CMyData data;
cout << data.a << endl << data.str << endl << data.c << endl;
data.setmv();
CMyData::vs v = data.m_v;
CMyData::It it = v.begin();
while( it != v.end())
{
string s = *it;
cout << s << endl;
it++;
}
return 0;
}
在gdb中,如果要打印C++ STL容器的內容,缺省的顯示結果可讀性很差:
gdb
7.0之後,可以使用gcc提供的python腳本,來改善顯示結果:
某些發行版(Fedora 11+),不需要額外的設置工作。可在gdb命令行下驗證(若沒有顯示,可按下文的方法進行設置)。
[code] (gdb) info pretty-printer
[code]
方法:1.用svn下載出一個Python工具。svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
完成後會在當前目錄下得到一個名為python的目錄 。然後你可以將其移動到合適的地方,,比如移動到~/lib/gdb_printers/目錄下。2.打開文件~/.gitinit文件。 若沒有,則創建。輸入
[plain] view plain copypython
import sys
sys.path.insert(0, '/home/yourname/lib/gdb_printers/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
[/code]
文件~/.gdbinit的路徑需要匹配python模塊安裝的路徑,即~/lib/gdb_printers/python,我的是/home/weijl/lib/gdb_printers/python
以上就完成了,現在來看gdb的調試結果:
weijl@ubuntu:/home/share/work/src$ gdb ./hello
GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2)
7.4-2012.02
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 "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/share/work/src/hello...done.
(gdb) l
58 public:
59 typedef vector<string> vs;
60 typedef vector<string>::iterator It;
61
62 vs m_v;
63 };
64
65 int main() {
66
67 cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
(gdb)
68
69 CMyData data;
70 cout << data.a << endl << data.str << endl << data.c << endl;
71
72 data.setmv();
73 CMyData::vs v = data.m_v;
74 CMyData::It it = v.begin();
75 while( it != v.end())
76 {
77 string s = *it;
(gdb)
78 cout << s << endl;
79
80 it++;
81 }
82
83 return 0;
84 }(gdb)
Line number 85 out of range; ./hello.cpp has 84 lines.
(gdb) b 75
Breakpoint 1 at 0x8048bde: file ./hello.cpp, line 75.
(gdb) b 80
Breakpoint 2 at 0x8048c1d: file ./hello.cpp, line 80.
(gdb) b 83
Breakpoint 3 at 0x8048c74: file ./hello.cpp, line 83.
(gdb) r
Starting program: /home/share/work/src/hello
!!!Hello World!!!
0
weijl
65
Breakpoint 1, main () at ./hello.cpp:75
75 while( it != v.end())
(gdb) p data
$1 = {a = 0, str = "weijl", c = 65, m_v = std::vector of length 4, capacity 4 = {"weijl", "shenzhen", "nanning", "C++ Program"}}
(gdb) p v
$2 = std::vector of length 4, capacity 4 = {"weijl", "shenzhen", "nanning", "C++ Program"}
(gdb) c
Continuing.
weijl
Breakpoint 2, main () at ./hello.cpp:80
80 it++;
(gdb) p s
$3 = "weijl"
(gdb) delete 2
(gdb) c
Continuing.
shenzhen
nanning
C++ Program
Breakpoint 3, main () at ./hello.cpp:83
83 return 0;
(gdb) p /r v
$4 = {<std::_Vector_base<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >> = {
_M_impl = {<std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >> = {<__gnu_cxx::new_allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >> = {<No data fields>}, <No data fields>}, _M_start
= 0x804d0e0, _M_finish = 0x804d0f0, _M_end_of_storage = 0x804d0f0}}, <No data fields>}
(gdb)
參考:
點擊打開鏈接
點擊打開鏈接