How can we know the caller function's name?

后端 未结 10 1552
北海茫月
北海茫月 2020-12-02 10:26

In the C language, __FUNCTION__ can be used to get the current function\'s name. But if I define a function named a() and it is called

相关标签:
10条回答
  • 2020-12-02 11:04
    #include <stdio.h>
    #include <stdlib.h>
    

    #define FUNCTION_NAME(FUNCTION) printf("FUNCTION=%s \r\n", #FUNCTION);

    int a() {
      printf("A function call");
    }
    
    int b() {
      printf("B function call");
    }
    
    int main(){
    FUNCTION_NAME(a);
    FUNCTION_NAME(b);
    return 0;
    

    }

    0 讨论(0)
  • 2020-12-02 11:07

    If the function in question is in a different c file, you can do

    #define name_of_function(...) \
        printf("Function %s is parent\n", __FUNCTION__); \
        name_of_function(__VA_ARGS__);
    

    And at the top of the c file it lives in

    #ifdef name_of_function
    #undef name_of_function
    #endif
    

    If they're in the same file, you can wrap the function definition in the second macro, then redefine the first macro at the end. It's not terribly extensible because you can't generate new defines from other defines, but if you're trying to track down parents for a particular function it works without any nonsense.

    https://godbolt.org/z/f2jKOm

    0 讨论(0)
  • 2020-12-02 11:14

    You can tag each function that calls a() with an integer identifier which is passed to a() as a parameter and then use a switch-case construct in a() to tell which function has invoked a().A printf() would tell which function invoked a() depending on the integer identifier value if you use that as an argument to a switch-case construct in a()

    #include<stdio.h>
    
    void a(int);
    void b();
    void c();
    void d();
    
    int main(void)
    {
    
    b();
    c();
    d();
    
    }
    
    void b()
    {
    
    int x=1;
    a(x);
    
    }
    
    void c()
    {
    
    int x=2;
    a(x);
    
    }
    
    void d()
    {
    
    int x=3;
    a(x);
    
    }
    
    void a(int x)
    {
    
    switch(x)
    {
    case 1:
    printf("b called me\n");
    break;
     case 2:
    printf("c called me\n");
    break;
    case 3:
    printf("d called me\n");
    }
    
    }
    
    0 讨论(0)
  • 2020-12-02 11:16

    There's nothing you can do only in a.

    However, with a simple standard macro trick, you can achieve what you want, IIUC showing the name of the caller.

    void a()
    {
        /* Your code */
    }
    
    void a_special( char const * caller_name )
    {
        printf( "a was called from %s", caller_name );
        a();
    }
    
    #define a() a_special(__func__)
    
    void b()
    {
        a();
    }
    
    0 讨论(0)
  • 2020-12-02 11:17

    If you are using Linux system, you can use the backtrace() function.

    See the man page for more details and a code example.

    0 讨论(0)
  • 2020-12-02 11:18

    Try this:

    void a(<all param declarations to a()>);
    
    #ifdef DEBUG
    #  define a(<all params to a()>) a_debug(<all params a()>, __FUNCTION__)
    void a_debug(<all params to a()>, const char * calledby);
    #endif
    
    void b(void)
    {
      a(<all values to a()>);
    }
    
    #ifdef DEBUG
    #  undef a
    #endif
    
    void a(<all param declarations to a()>)
    {
      printf("'%s' called\n", __FUNCTION__);
    }
    
    #ifdef DEBUG
    void a_debug(<all param declarations to a()>, const char * calledby)
    {
      printf("'%s' calledby '%s'", __FUNCTION__, calledby);
      a(<all params to a()>);
    }
    #endif
    

    If for example <all param declarations to a()> is int i, double d, void * p then <all params to a()> is i, d, p.


    Or (less evil ;->> - but more code modding, as each call to a() needs to be touched):

    void a((<all params of normal a()>    
    #ifdef DEBUG
      , const char * calledby
    #endif
      );
    
    void a((<all params of normal a()>    
    #ifdef DEBUG
      , const char * calledby
    #endif
      )
    {
    #ifdef DEBUG
      printf("'%s' calledby '%s', __FUNCTION__, calledby);
    #endif
      ...
    }
    
    ...
    
    void b(void)
    {
        a(<all params of normal a()>
    #ifdef DEBUG
          , __FUNC__
    #endif
        );
    }
    

    __FUNCTION__ is available on GCC (at least?), if using a different C99 compiler replace it with __func__.

    0 讨论(0)
提交回复
热议问题