Linux内核中的函数调用程序 [英] Function caller in linux kernel
问题描述
有没有一种方法可以在Linux内核中获取函数调用者?我知道 __ func __ 返回正在执行的函数名称.我正在寻找名为" __ func __ "
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__"
推荐答案
您可以使用__builtin_return_address(0)
来获得呼叫者.
You can get the caller with __builtin_return_address(0)
.
呼叫者的呼叫者是__builtin_return_address(1)
,依此类推.
The caller's caller is __builtin_return_address(1)
and so on.
这是GCC扩展,在gcc手册中有记录: http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html
It's a GCC extension, documented in the gcc manual: http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html
编辑:我可能应该指出,这可以为您提供呼叫者的地址.如果需要函数名称,可以使用%pS
打印它,例如:
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));
如果不想打印,可以使用kallsyms_lookup()
等.
If you don't want to print it, you can use kallsyms_lookup()
etc.
这篇关于Linux内核中的函数调用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!