为什么'-finstrument-functions`不适合我? [英] Why doesn't `-finstrument-functions` work for me?

查看:153
本文介绍了为什么'-finstrument-functions`不适合我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此它应该打印所有函数名称:

  [root @ test]#cat hw.c 
#include< stdio.h>

int func(void)
{
return 1;

int main(void)
{
func();
printf(%d,6);
返回6;
}
[root @ test]#gcc -Wall hw.c -o hw -finstrument-functions
[root @ test]#./hw
6
[root @ test]#gcc --version
gcc(GCC)4.1.2 20080704(Red Hat 4.1.2-48)
版权所有(C)2006 Free Software Foundation,Inc.

但为什么它不适合我?

这是从gcc手册:


-finstrument-functions



生成用于
条目的检测调用并退出函数。只有
在函数入口之后,并且在
函数退出之前,将使用当前函数的
地址和
的调用位置调用以下分析函数
函数。 (在某些平台上,
__builtin_return_address不能超出当前功能,因此
调用站点信息可能不是
,否则对于分析函数
可用。)
p>

void __cyg_profile_func_enter(void * this_fn,void * call_site);


$ b void __cyg_profile_func_exit(void * this_fn,void * call_site);

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



我通过提供这个实现与MinGW GCC合作:

  #include< stdio.h> 

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

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

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



人们可能也对这个调用树的可视化,它使用了-fintrument-functions标志 - 警告,我没有尝试过。

According to this answer,it should print all function names:

[root@ test]# cat hw.c
#include <stdio.h>

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

But why it's not working for me?

解决方案

This is from the gcc manual:

-finstrument-functions

Generate instrumentation calls for entry and exit to functions. Just after func- tion entry and just before function exit, the following profiling functions will be called with the address of the current function and its call site. (On some platforms, __builtin_return_address does not work beyond the current func- tion, so the call site information may not be available to the profiling functions otherwise.)

void __cyg_profile_func_enter (void *this_fn, void *call_site);

void __cyg_profile_func_exit (void *this_fn, void *call_site);

Unless somthing implements those functions, you will get linker errors (which is what happens with MinGW). Conceivably, your GCC version is providing empty implementations.

I got it to work with MinGW GCC by providing this implementation:

#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 );
}

but this only gives the function addresses. I'd have thought there should be a GCC default implementation of this, but there doesn't seem to be.

People may also be interested in this visualisation of the call tree, which uses the -fintrument-functions flag - caveat, I haven't tried it myself.

这篇关于为什么'-finstrument-functions`不适合我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆