示例代码:

//============================================================================
// 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命令行下验证(若没有显示,可按下文的方法进行设置)。

    (gdb) info pretty-printer

方法:

1.用svn下载出一个Python工具。

svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python

完成后会在当前目录下得到一个名为python的目录 。

然后你可以将其移动到合适的地方,,比如移动到~/lib/gdb_printers/目录下。

2.打开文件~/.gitinit文件。 若没有,则创建。

输入

[plain]  view plain  copy
  1. python  
  2. import sys  
  3. sys.path.insert(0, '/home/yourname/lib/gdb_printers/python')  
  4. from libstdcxx.v6.printers import register_libstdcxx_printers  
  5. register_libstdcxx_printers (None)  
  6. end  
文件~/.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) 





参考:

点击打开链接

点击打开链接

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐