2011-04-16 46 views
2

我正在实施一个分析器。我想使用Constructor/Destructor成语来跟踪我何时进入/退出函数。如何让GCC实例化一个具有非平凡const/dest的类实例?

我的代码粗线条如下:

class Profile 
{ 
    Profile(void); //Start timing 
    ~Profile(void); //Stop timer and log 
}; 
//... 
Game::Game(void) : m_Quit(false) 
{ 
    Profile p(); 
    InitalizeModules(); 
    //... 
} 

然而,当我运行它的构造函数和析构函数没有被调用。即使在我反汇编时,也没有对Profile :: Profile()的引用。我明白该标准指出具有不平凡构造函数的实例不能被编译器优化。

编译器或链接器的命令行上没有优化标志。

我也试过指定属性((使用)),但无济于事。

这里是拆解:

(gdb) disassemble Ztk::Game::Game 
Dump of assembler code for function Ztk::Game::Game(): 
    0x00000000004cd798 <+0>:  push %rbp 
    0x00000000004cd799 <+1>:  mov %rsp,%rbp 
    0x00000000004cd79c <+4>:  push %r12 
    0x00000000004cd79e <+6>:  push %rbx 
    0x00000000004cd79f <+7>:  sub $0x30,%rsp 
    0x00000000004cd7a3 <+11>: mov %rdi,-0x38(%rbp) 
    0x00000000004cd7a7 <+15>: mov -0x38(%rbp),%rax 
    0x00000000004cd7ab <+19>: mov %rax,%rdi 
    0x00000000004cd7ae <+22>: callq 0x4cdc6a <Ztk::Highlander<Ztk::Game, int>::Highlander()> 
    /** CALL SHOULD BE HERE **/ 
    0x00000000004cd7b3 <+27>: mov -0x38(%rbp),%rax 
    0x00000000004cd7b7 <+31>: movb $0x0,(%rax) 
    0x00000000004cd7ba <+34>: callq 0x4e59f0 <Ztk::InitializeModules()> 

确实有生成的代码,并链接到可执行文件

(gdb) disassemble Ztk::Profile::Profile(void) 
Dump of assembler code for function Ztk::Profile::Profile(): 
    0x0000000000536668 <+0>:  push %rbp 
    0x0000000000536669 <+1>:  mov %rsp,%rbp 
    0x000000000053666c <+4>:  sub $0x20,%rsp 
    0x0000000000536670 <+8>:  mov %rdi,-0x18(%rbp) 
    0x0000000000536674 <+12>: mov 0x8(%rbp),%rax 
    0x0000000000536678 <+16>: mov %rax,-0x8(%rbp) 
    0x000000000053667c <+20>: mov -0x8(%rbp),%rax 
    0x0000000000536680 <+24>: mov %rax,%rsi 
    0x0000000000536683 <+27>: mov $0x802440,%edi 
    0x0000000000536688 <+32>: callq 0x5363ca <Ztk::Profiler::FindNode(void*)> 
+0

如果目的是要找到瓶颈,很多人想这样做,你要(登录程序的入口/出口)的方式,但有一个更好的方法 - 叠采样。 [这是一个解释。](http://stackoverflow.com/questions/4387895/if-profiler-is-not-the-answer-what-other-choices-do-we-have/4390868#4390868)[放大] (http://www.rotateright.com/)是一个非常受尊敬的商业分析工具。这个概念可能是一个弯路,但其实很简单。 – 2011-04-16 17:28:04

回答

8
Profile p(); 

你在这里做什么是声明的函数,称为P,返回一个Profile类型的对象。你想要的是这样的:

Profile p; 
+0

得爱C++最烦人的解析:) http://arstechnica.com/civis/viewtopic.php?f=20&t=767929 – Drahakar 2011-04-16 15:02:25

+0

哦!那一定是吧!我习惯于使用这个习惯用于构造参数的参数,当我回到我的GCC环境时,我会尝试它。 – 2011-04-16 15:58:42