使用GDB 分析C++ 类的内存分布


首先,代码如下:

  1 
  2 #include <string.h>
  3 #include <iostream>
  4 
  5 class CFox
  6 {
  7 public:
  8     CFox();
  9     void Print(void);
 10 private:
 11     int a;
 12 };
 13 
 14 CFox::CFox(void)
 15 {
 16     a = 10;
 17 }
 18 
 19 void CFox::Print(void)
 20 {
 21     std::cout<< "this is print for cfox class" << std::endl;
 22 }
 23 
 24 class IMyClass
 25 {
 26 public:
 27     IMyClass(void);
 28     virtual void DoAction(int id) = 0;
 29     virtual void Test(void) = 0;
 30     void Foo(void){}
 31 protected:
 32     int id;
 33     char name[16];
 34 };
 35 
 36 IMyClass::IMyClass(void)
 37 {
 38     memset(name, 0, sizeof(name));
 39     id = 0;
 40 }
 41 class CMyClass : public IMyClass
 42 {
 43 public:
 44     CMyClass(void);
 45     void DoAction(int id);
 46     void Test(void){}
 47 private:
 48     CFox *pFox;
 49 };
 50 
 51 CMyClass::CMyClass(void)
 52 {
 53     pFox = new CFox();
 54 }
 55 void CMyClass::DoAction(int id)
 56 {
 57     if(id == 0)
 58     {
 59         memcpy(name, "Apple", 6);
 60     }
 61     else
 62     {
 63         memcpy(name, "meat", 5);
 64     }
 65 
 66     this->id = 0xABCDEF;
 67 
 68 }
 69 
 70
 71 int main(void)
 72 {
 73     IMyClass * pClass = new CMyClass;
 74     int classSize = sizeof(CMyClass);
 75     pClass->DoAction(0);
 76     delete pClass;
 77     pClass = 0;
 78 
 79     CMyClass myclass;
 80     pClass = &myclass;
 81     pClass->DoAction(0);
 82     return 0;
 83 }

在函数 CMyClass::DoAction()里设置断点,在执行完这个函数最后一个赋值语句的时候,停下来。
此时,执行gdb命令:

(gdb) where
#0  CMyClass::DoAction (this=0x11008, id=0) at test_class.cpp:68
#1  0x00008b38 in main () at test_class.cpp:75
(gdb) p *this
$1 = {<IMyClass> = {_vptr.IMyClass = 0x8ce0 <vtable for CMyClass+8>, 
    id = 11259375, name = "Apple\000\000\000\000\000\000\000\000\000\000"}, 
  pFox = 0x11028}

看一下这时的mapping:

(gdb) info proc  mappings 
process 544
Mapped address spaces:

	Start Addr   End Addr       Size     Offset objfile
	    0x8000     0x9000     0x1000        0x0 /mnt/test_class
	   0x10000    0x11000     0x1000        0x0 /mnt/test_class
	   0x11000    0x32000    0x21000        0x0 [heap]
	0xb6d39000 0xb6e64000   0x12b000        0x0 /lib/libc-2.19.so
	0xb6e64000 0xb6e6c000     0x8000   0x12b000 /lib/libc-2.19.so
	0xb6e6c000 0xb6e6e000     0x2000   0x12b000 /lib/libc-2.19.so
	0xb6e6e000 0xb6e6f000     0x1000   0x12d000 /lib/libc-2.19.so
	0xb6e6f000 0xb6e72000     0x3000        0x0 
	0xb6e72000 0xb6e8f000    0x1d000        0x0 /lib/libgcc_s.so.1
	0xb6e8f000 0xb6e97000     0x8000    0x1d000 /lib/libgcc_s.so.1
	0xb6e97000 0xb6e98000     0x1000    0x1d000 /lib/libgcc_s.so.1
	0xb6e98000 0xb6f02000    0x6a000        0x0 /lib/libm-2.19.so
	0xb6f02000 0xb6f09000     0x7000    0x6a000 /lib/libm-2.19.so
	0xb6f09000 0xb6f0a000     0x1000    0x69000 /lib/libm-2.19.so
	0xb6f0a000 0xb6f0b000     0x1000    0x6a000 /lib/libm-2.19.so
	0xb6f0b000 0xb6fc4000    0xb9000        0x0 /lib/libstdc++.so.6.0.18
	0xb6fc4000 0xb6fcb000     0x7000    0xb9000 /lib/libstdc++.so.6.0.18
	0xb6fcb000 0xb6fcf000     0x4000    0xb8000 /lib/libstdc++.so.6.0.18
	0xb6fcf000 0xb6fd1000     0x2000    0xbc000 /lib/libstdc++.so.6.0.18
---Type <return> to continue, or q <return> to quit---
	0xb6fd1000 0xb6fd7000     0x6000        0x0 
	0xb6fd7000 0xb6ff6000    0x1f000        0x0 /lib/ld-2.19.so
	0xb6ffa000 0xb6ffd000     0x3000        0x0 
	0xb6ffd000 0xb6ffe000     0x1000        0x0 [sigpage]
	0xb6ffe000 0xb6fff000     0x1000    0x1f000 /lib/ld-2.19.so
	0xb6fff000 0xb7000000     0x1000    0x20000 /lib/ld-2.19.so
	0xbefdf000 0xbf000000    0x21000        0x0 [stack]
	0xffff0000 0xffff1000     0x1000        0x0 [vectors]
可以看出,this是在heap里的。

再看下成员变量的地址:

(gdb) p &name
$4 = (char (*)[16]) 0x11010
(gdb) p pFox
$5 = (CFox *) 0x11028
(gdb) p &this->id
$6 = (int *) 0x1100c
然后,看下this 所在的 heap的内存:

(gdb) x /20w this
0x11008:	0x00008ce0	0x00abcdef	0x6c707041	0x00000065
0x11018:	0x00000000	0x00000000	0x00011028	0x00000011
0x11028:	0x0000000a	0x00000000	0x00000000	0x00020fd1
0x11038:	0x00000000	0x00000000	0x00000000	0x00000000
0x11048:	0x00000000	0x00000000	0x00000000	0x00000000


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
google-perftools 简介 google-perftools 是一款针对 C/C++ 程序的性能分析工具,它是一个遵守 BSD 协议的开源项目。使用该工具可以对 CPU 时间片、内存等系统资源的分配和使用进行分析,本文将重点介绍如何进行 CPU 时间片的剖析。 google-perftools 对一个程序的 CPU 性能剖析包括以下几个步骤。 1. 编译目标程序,加入对 google-perftools 库的依赖。 2. 运行目标程序,并用某种方式启动 / 终止剖析函数并产生剖析结果。 3. 运行剖结果转换工具,将不可读的结果数据转化成某种格式的文档(例如 pdf,txt,gv 等)。 安装 您可以在 google-perftools 的网站 (http://code.google.com/p/google-perftools/downloads/list) 上下载最新版的安装包。为完成步骤 3 的工作,您还需要一个将剖析结果转化为程序员可读文档的工具,例如 gv(http://www.gnu.org/software/gv/)。 编译与运行 您需要在原有的编译选项中加入对 libprofiler.so 的引用,这样在目标程序运行时会加载工具的动态库。例如本例中作者的系统中,libprofiler.so 安装在"/usr/lib"目录下,所以需要在 makefile 文件中的编译选项加入“-L/usr/lib -lprofiler”。 google-perftools 需要在目标代码的开始和结尾点分别调用剖析模块的启动和终止函数,这样在目标程序运行时就可以对这段时间内程序实际占用的 CPU 时间片进行统计和分析。工具的启动和终止可以采用以下两种方式。 a. 使用调试工具 gdb 在程序中手动运行性能工具的启动 / 终止函数。 gdb 是 Linux 上广泛使用的调试工具,它提供了强大的命令行功能,使我们可以在程序运行时插入断点并在断点处执行其他函数。具体的文档请参照 http://www.gnu.org/software/gdb/,本文中将只对用到的几个基本功能进行简单介绍。使用以下几个功能就可以满足我们性能调试的基本需求,具体使用请参见下文示例。 命令 功能 ctrl+c 暂停程序的运行 c 继续程序的运行 b 添加函数断点(参数可以是源代码中的行号或者一个函数名) p 打印某个量的值或者执行一个函数调用 b. 在目标代码中直接加入性能工具函数的调用,该方法就是在程序代码中直接加入调试函数的调用。 两种方式都需要对目标程序重新编译,加入对性能工具的库依赖。对于前者,他的好处是使用比较灵活,但工具的启动和终止依赖于程序员的手动操作,常常需要一些暂停函数(比如休眠 sleep)的支持才能达到控制程序的目的,因此精度可能受到影响。对于后者,它需要对目标代码的进行修改,需要处理函数声明等问题,但得到的结果精度较高,缺点是每次重新设置启动点都需要重新编译,灵活度不高,读者可以根据自己的实际需求采用有效的方式。 示例详解 该程序是一个简单的例子,文中有两处耗时的无用操作,并且二者间有一定的调用关系。 清单 1. 示例程序 void consumeSomeCPUTime1(int input){ int i = 0; input++; while(i++ < 10000){ i--; i++; i--; i++; } }; void consumeSomeCPUTime2(int input){ input++; consumeSomeCPUTime1(input); int i = 0; while(i++ < 10000){ i--; i++; i--; i++; } }; int stupidComputing(int a, int b){ int i = 0; while( i++ < 10000){ consumeSomeCPUTime1(i); } int j = 0; while(j++ < 5000){ consumeSomeCPUTime2(j); } return a+b; }; int smartComputing(int a, int b){ return a+b; }; void main(){ int i = 0; printf("reached the start point of performance bottle neck\n"); sleep(5); //ProfilerStart("CPUProfile"); while( i++ MyProfile.pdf 转换后产生的结果文档如下图。图中的数字和框体的大小代表了的某个函数的运行时间占整个剖析时间的比例。由代码的逻辑可知,stupidComputing,stupidComputing2 都是费时操作并且它们和 consumeSomeCPUTime 存在着一定的调用关系。 图 1. 剖析结果 结束语 本文介绍了一个 Linux 平台上的性能剖析工具 google-perftools,并结合实例向读者展示了如何使用该工具配置、使用分析性能瓶颈。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值