Function caller in linux kernel

BHS picture BHS · Nov 10, 2010 · Viewed 16.2k times · Source

Is there a way to get function caller in linux kernel? I know __func__ returns the function name which is executing. I am looking for the function which called "__func__"

Answer

mpe picture mpe · Nov 10, 2010

You can get the caller with __builtin_return_address(0).

The caller's caller is __builtin_return_address(1) and so on.

It's a GCC extension, documented in the gcc manual: http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html

Edit: I should probably point out, that gets you the address of the caller. If you want the function name you can print it with %pS, eg:

printk("Caller is %pS\n", __builtin_return_address(0));

If you don't want to print it, you can use kallsyms_lookup() etc.