How to know what function called another

后端 未结 7 1664
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-11 17:53

I wanna know if there is any way to know where the function currently in execution was called, this is, in what file and line. I\'m using C language, and I\'m looking for so

7条回答
  •  [愿得一人]
    2021-01-11 18:30

    You can use logs .

    #define BEGIN_FUNC(X,Y,Z) printf("Function %s Entered at line %d from file %s",X,Z,Y)
    #define END_FUNC(X)  printf("Function %s Exited at line %d from file %s",X,Z,Y)
    
    foo()
    {
    BEGIN_FUNC(__func__,__FILE__,__LINE__);
    
    //Your code here
    
    
    END_FUNC(__func___FILE__,__LINE__);
    

    }

    OR

    Use bt in gdb. I call it backtrace.

提交回复
热议问题