linux下GDB调试C++标准库STL,打印STL对象的内容
来源:互联网 发布:易通网络vpn加速器 编辑:程序博客网 时间:2024/06/14 23:38
示例代码:
//============================================================================// 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.1415926const 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
文件~/.gdbinit的路径需要匹配python模块安装的路径,即~/lib/gdb_printers/python,我的是/home/weijl/lib/gdb_printers/python方法:
1.用svn下载出一个Python工具。
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
完成后会在当前目录下得到一个名为python的目录 。
然后你可以将其移动到合适的地方,,比如移动到~/lib/gdb_printers/目录下。
2.打开文件~/.gitinit文件。 若没有,则创建。
输入
- python
- 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
以上就完成了,现在来看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)
参考:
点击打开链接
点击打开链接
- linux下GDB调试C++标准库STL,打印STL对象的内容
- win7环境下eclipse gdb调试输出stl容器内容
- windows下eclipse中gdb调试输出stl容器的内容(终极解决方案)
- STL标准容器中内容的几种打印技巧
- 使用linux的GDB打印STL(vector,map,set..................)
- 20170725使用linux的GDB打印STL(vector,map,set..................)
- GDB STL vector打印
- GDB打印STL
- GDB STL vector打印
- GDB STL vector打印
- GDB pretty printer: linux下用gdb调试c++时如何更好的查看STL容器值
- 让GDB支持STL的标准库的方法
- 让GDB支持STL的标准库的方法
- GDB调试STL容器
- gdb 调试stl
- gdb调试C++ STL
- gdb调试stl相关
- gdb调试stl
- 做一个有冒险精神的人!开启漫漫的agera之旅
- JavaWeb开发之Servlet的请求流程剖析-图片版 (跟龙哥学javaweb)笔记
- Visual studio 2008环境配置:C#显示代码行号、全屏显示
- 3017
- Frobenius norm(弗罗贝尼乌斯范数)
- linux下GDB调试C++标准库STL,打印STL对象的内容
- SICP ex2-3 2-4 2-5(lambda calculus)
- URL编码与解码原理
- RequestDispatcher提供两个方法:forward,include有什么区别
- 跨境电商在海关报关中 FTP 文件的上传和下载
- fn标签
- jQuery Ajax前后端使用JSON进行交互
- App数据格式之解析Json
- RunTime改变控件字体