Who is Calling Whom?

Have you ever wanted to know which function is calling a specific function. This snippet will help in digging up those pesky dependencies at runtime.

#include < stdio.h >
 
// Fn Function
void Fn( char* val )
{
  printf("Ahoy World! %s\n", val);
}
 
// Debug Call Conversion
#define Fn(...) \
  printf( "%s:%d %s()\n",__FILE__, __LINE__, __FUNCTION__); \
  Fn(__VA_ARGS__)
 
//Main
int main( void )
{
  Fn("Arrr!");
  return 0;
}

This will give you a nice alert every time your function is called.

$ gcc test.c -o test.exe && ./test.exe
test.c:17 main()
Ahoy World! Arrr!

More Reading

Function like Macros
Variadic Macros
Standard Predefined Macros

Leave a Reply