开发者

Getting the name of the calling function in C (without using the preprocessor)

开发者 https://www.devze.com 2023-01-10 12:46 出处:网络
I was wondering if t开发者_开发技巧here is a way of finding which function called the current function (at runtime) in C.

I was wondering if t开发者_开发技巧here is a way of finding which function called the current function (at runtime) in C.

I know you could use __FUNCTION__ in gcc, but is there a way without using the C preprocessor?

Probably not.

Cheers


No, there isn't. C isn't a particularly introspective language - things like the name of a function (or pieces of your call stack) simply aren't available at runtime in any sane fashion.

If, for some reason, you are looking for a lot of work for very little benefit, then you can build your programs with debug symbols, and you can write stack-walking and debug symbol lookup code. Then you might be able to find this out on the fly. But be careful, because the symbols you'll see in the debug info will be decorated with type info if you've got any C++ involved.

You've tagged this post gcc, so the relevant details ARE available, however this falls into the 'not recommended' and 'not guaranteed to be the same between compiler versions' territory.


Assuming you have a function f() from which you want to know the caller.

Rename that function to f_func() and define a macro f() that prints __func__ and then calls f_func(). Example:

void
f_func()
{
        do something;
}

#define f() \
        do { \
                printf("f called from %s\n", __func__); \
                f_func(); \
        } while (0)

void
a()
{
        f();
}

void
b()
{
        f();
}

int
main(int argc, char **argv)
{
        a();
        b();

        return(0);
}


There's no way to get a function name in the runtime. The only way is the preprocessor but it's very limited in its capabilities. In case you have debug information available, you could walk the stack and get the function names from the debugging information. This is, however, neither a robust nor a portable solution.


There are couple of GNU functions that allow you to get function addresses and names from backtrace - backtrace() and backtrace_symbols(), but you need to compile your binary with -rdynamic flag


NO

The short answer is NO

but with preprocessor it can be done like this

Getting the name of the calling function in C (using the preprocessor)

Assuming you have a function f() from which you want to know the caller only for debugging purpose.

Rename that function to f_func() and define a macro f() that calls a version of f that prints func and then calls f_func() when DEBUG is defined.

In the final release the information is removed by calling the real function f_func()

Example

#ifdef DEBUG
  #define f(a,b,c) f_debug(a,b,c, __func__)
#else
  #define f(a,b,c) f_func(a,b,c)
#endif

bool f_func(int par1, int par2, int par3)
{
  do_somthing();
}

bool f_debug((int par1, int par2, int par3, const char calling_func_name[])
{
  printf("f called from %s\n", calling_func_name); 
  f_func(); 
}

void a()
{
  f();
}

void b()
{
  f();
}

int main(int argc, char **argv)
{
        a();
        b();

        return(0);
}

Result is:

when DEBUG is defined

f called from a
f called from b


Use the __func__ identifier. The standard (section 6.4.2.2) requires that it be present for precisely this purpose:

The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration

static const char __func__[] = "function-name";

appeared, where function-name is the name of the lexically-enclosing function.

As Steve Jessop notes in a comment, this isn't part of the preprocessor as such, but an intrinsic part of the compiler.

There may well be ways of finding out this name by walking the stack and looking at debugging symbols. Cute, but insane.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号