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

前端 未结 3 606
甜味超标
甜味超标 2020-12-15 12:37

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

[root@ test]# cat hw.c
#include 

int func(void)
{  
  return 1;
}
int main(void)         


        
3条回答
  •  庸人自扰
    2020-12-15 13:02

    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  
    
    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.

提交回复
热议问题