Linux GDB debug C + + standard library STL, print content of STL object __ static function

Source: Internet
Author: User
Tags svn python script

Sample code:

============================================================================//Name:cpp.cpp//Author:w Eijl//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; }
In GDB, if you want to print the contents of a C + + STL container, the default display results are poor readability:


After GDB 7.0, you can use the Python script provided by GCC to improve the display results:

Some distributions (Fedora 11+) do not require additional setup work. Can be validated at the GDB command line (if not shown, set in the following ways).

    (GDB) Info pretty-printer

Method:

1. Download a Python tool with SVN.

SVN Co Svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python

When you're done, you'll get a directory named Python in the current directory.

Then you can move it to the right place, like moving to the ~/lib/gdb_printers/directory.

2. Open the file ~/.gitinit file. If not, create it.

Input

[plain] view plain copy python import sys sys.path.insert (0, '/home/yourname/lib/gdb_printers/python ') fr The path required for the OM libstdcxx.v6.printers import register_libstdcxx_printers register_libstdcxx_printers (None) End file ~/.gdbinit Match the path of the Python module installation, that is, ~/lib/gdb_printers/python, mine is/home/weijl/lib/gdb_printers/python.

The above is done, now to see the debugging results of GDB:

weijl@ubuntu:/home/share/work/src$ gdb./hello

GNU gdb (Ubuntu/linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This is the free software:you are and redistribute it.
There is NO WARRANTY and to the extent permitted by. Type "Show copying"
and "Show warranty" for details.
This is GDB was configured as "I686-linux-gnu".
For bugs reporting instructions, please:
Reading symbols From/home/share/work/src/hello...done.
(GDB) L
The public:
the typedef vector<string> vs;
typedef vector<string>::iterator IT;
61
M_v vs.
63};
64
the int main () {
66
cout << "!!! Hello World!!! "<< Endl; Prints!!! Hello World!!!
(GDB)
68
CMyData data;
cout << data.a << endl << data.str << endl << data.c << Endl;
71
DATA.SETMV ();
Cmydata::vs v = data.m_v;
Cmydata::it It = V.begin ();
while (it!= v.end ())
76 {
A string s = *it;
(GDB)
cout << s << endl;
79
it++;
81}
82
0;
Down} (GDB)
Line number is out of range;./hello.cpp has 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
while (it!= v.end ())
(GDB) P data
$ = {A = 0, str = "Weijl", c = m_v = Std::vector of length 4, capacity 4 = {"Weijl", "Shenzhen", "Nanning", "C + + Prog Ram "}}
(GDB) P V
$ = std::vector of length 4, capacity 4 = {"Weijl", "Shenzhen", "Nanning", "C + + program"}
(GDB) C
Continuing.
Weijl


Breakpoint 2, Main () at./hello.cpp:80
it++;
(GDB) P s
$ = "Weijl"
(GDB) Delete 2
(GDB) C
Continuing.
Shenzhen
Nanning
C + + Program


Breakpoint 3, Main () at./hello.cpp:83
0;
(GDB) P/R V
$ = {<std::_vector_base<std::basic_string<char, std::char_traits<char>, std::allocator<char> ", Std::allocator<std::basic_string<char, Std::char_traits<char&gt, 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&gt}, <no data fields>}, _m_start = 0x804d0e0, _m_finish = 0x804d0f0, _m_end_of_storage = 0x804d0f0}}, <no data fields>}
(GDB)





Reference:

Click to open the link

Click to open the link

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.