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__"
-
2Muahahahaaa. Haha... not sanely. – Nov 10 '10 at 05:08
4 Answers
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.

- 2,640
- 1
- 19
- 17
-
9
-
5btw: when using __builtin_return_address(0), you cannot replace 0 with a variable such as int i; otherwise, you will get some compiling error. Also, be careful when tracing back too much, such as __builtin_return_address(10). If the call stack is not as deep as 10, the kernel will happily crash. – Jiang Sep 17 '12 at 23:54
You can also print the entire call stack contents by calling dump_stack().

- 3,811
- 7
- 35
- 39
Whether or not frame pointers are needed depends on arch, IIRC. For x86, they are certainly desired to fully exploit these features. Also note that inlining can skew the accuracy of builtin_return_address for this very reason.
If you just want a stack dump to see how some place was reached, better use the dump_stack()
function than trying to fiddle around with builtin_return_address.

- 4,346
- 24
- 20
To get the caller function name, one can use the below printk command.
printk("Caller is %pF\n", __builtin_return_address(0));

- 1,589
- 2
- 20
- 32