1

I was wondering if there 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

3
  • Please specify if you need the real name or just want to be able to distinglish calls from different functions. Also, if it can be hardware specific. Commented Aug 8, 2010 at 12:10
  • 4
    If your problem with __FUNCTION__ is that it's not portable, note that many compilers have a similar feature, just sometimes with a different name. It's been standardized to __func__ in C99. Commented Aug 8, 2010 at 13:04
  • 3
    __FUNCTION__ and __func__ don't actually use the preprocessor (because the preprocessor does not understand C syntax and can't identify function declarations). In effect they're compiler intrinsics. Proof: run gcc -E on the code, look at the output. Commented Aug 8, 2010 at 16:00

6 Answers 6

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Another problem with debug symbols is that they will show a different result if the function was inlined by the compiler.
@Gilles - Good point. I completely blanked on the fact that an given 'function' at a C level might not be a function at all once the optimizer is done with it.
as time as gdb can print the back trace, then it is possible. of course, using internal structures of the system.
6

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);
}

Comments

2

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.

Comments

1

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

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.