2011-05-30 85 views
5

this answer,它应该打印所有的函数名称:为什么'-finstrument-functions`不适合我?

[[email protected] test]# cat hw.c 
#include <stdio.h> 

int func(void) 
{ 
    return 1; 
} 
int main(void) 
{ 
    func(); 
    printf("%d",6); 
    return 6; 
} 
[[email protected] test]# gcc -Wall hw.c -o hw -finstrument-functions 
[[email protected] test]# ./hw 
6 
[[email protected] test]# gcc --version 
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48) 
Copyright (C) 2006 Free Software Foundation, Inc. 

但为什么它不是为我工作?

+0

答案是错的。 – 2011-05-30 12:37:27

回答

5

这是从GCC手册:

-finstrument函数

产生 入口和出口功能的仪器调用。在功能输入之后和在 函数退出之前,只有 函数将被调用,当前函数的 地址和其调用地点的 。 (在某些平台上, __builtin_return_address不会超出当前FUNC-重刑工作,所以 调用位置信息可能不 可供分析函数 否则)

无效__cyg_profile_func_enter(void *的this_fn,无效* call_site );

空隙__cyg_profile_func_exit(无效* this_fn,无效* call_site);

除非somthing实现这些功能,否则您将收到链接器错误(这是发生在MinGW上的错误)。可以想象,你的GCC版本提供了空白的实现。

我得到它通过提供此实现与MinGW的海湾合作委员会的工作:

#include <stdio.h> 

void __cyg_profile_func_enter (void *this_fn, void *call_site) { 
    printf("entering %p\n", this_fn); 
} 

void __cyg_profile_func_exit (void *this_fn, void *call_site) { 
    printf("leaving %p\n", this_fn); 
} 

但这只是给出了函数的地址。我原以为应该有一个GCC的默认实现这个,但似乎没有。

的人也可能有兴趣在this visualisation of the call tree,它使用-fintrument-功能标志 - 警告,我还没有尝试过自己。

+0

@Neil Butterworth,我可以手动将这两个函数添加到我的'.c'文件中吗? – 2011-05-30 12:43:37

+0

你可以,但实施它们是相当复杂的。 – 2011-05-30 12:44:24

+0

@Neil Butterworth,我只是想让它打印函数名称,为什么它很复杂? – 2011-05-30 12:47:06

1

您并未实际执行任何检测。 -finstrument-functions开关只是告诉gcc在进入和退出每个函数时调用某个函数。但是你必须自己定义这些函数(通常这是通过链接一个profiler库来完成的)。

+0

需要采取哪些额外步骤才能使用'-finstrument-functions'开关?我希望尽可能最小化...... – 2011-05-30 12:41:02

+0

@ compile-fan:[documentation](http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-finstrument_002dfunctions-2168)提到了必须实施的两个功能。我不知道是否有现成的图书馆使用它们。 – 2011-05-30 14:14:10

0

编码__cyg_profile_func_enter和__cyg_profile_func_exit并不复杂。最简单的解决方案是确保上述功能将地址详细信息写入文件,并有一个独立的过程从文件中读取地址并使用可执行文件的符号表解析它们。如果您尝试在函数本身中执行地址解析,则可能需要一些时间。

我碰到下面的文章来 - http://balau82.wordpress.com/2010/10/06/trace-and-profile-function-calls-with-gcc/

其中采用addr2line从binutils的用于这一目的。检查这有助于你

+0

有时..只是为了调试正确性而打印fn名称真的很有用 – kchoi 2016-07-28 22:19:06

0

一个实现你在找什么,提供here

+0

对我而言不起作用:-(https://github.com/elcritch/etrace/issues/3 – 2017-11-05 11:07:43