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
You can do it with a gcc builtin.
void * __builtin_return_address(int level)
The following way should print the immediate caller of a function a().
Example:
a() {
printf ("Caller name: %pS\n", __builtin_return_address(0));
}
Refer: https://www.gnu.org/software/libc/manual/html_node/Backtraces.html
A backtrace is a list of the function calls that are currently active in a thread. The usual way to inspect a backtrace of a program is to use an external debugger such as gdb. However, sometimes it is useful to obtain a backtrace programmatically from within a program, e.g., for the purposes of logging or diagnostics.
The header file execinfo.h declares three functions that obtain and manipulate backtraces of the current thread.
If your platform is Windows, you may use this: walking the callstack
If you're only after knowing where you were for logging/debug purposes you can use a macro to avoid __func__
giving the name of your logging/debug function but of the function calling it.
Being in a macro will not result in a change to __func__
but will "feel" like using a function.
e.g.
#define LOG(s, data...) log("%s: "s, __function__, ## data)