Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

gcc - Why doesn't `-finstrument-functions` work for me?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
", this_fn );
}

void __cyg_profile_func_exit (void *this_fn, void *call_site) {
    printf( "leaving %p
", 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...