Function caller in linux kernel

时光毁灭记忆、已成空白 提交于 2019-12-20 08:08:43

问题


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__"


回答1:


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:


You can also print the entire call stack contents by calling dump_stack().




回答3:


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:


To get the caller function name, one can use the below printk command.

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



来源:https://stackoverflow.com/questions/4141324/function-caller-in-linux-kernel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!