如何从C函数的指针获取函数的名字? [英] How to get function's name from function's pointer in C?

查看:675
本文介绍了如何从C函数的指针获取函数的名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用C获得函数的指针函数的名称?

How to get function's name from function's pointer in C?

编辑:真正的情况:我在写一个Linux内核模块和我打电话的内核函数。其中的一些功能指针和我要检查内核源码该函数的code。但我不知道哪个函数它指向。我想这可能是因为做的,当系统出现故障(内核恐慌),它在屏幕打印出与函数的名称当前调用堆栈。但是,我想我错了......我是谁?

The real case is: I'm writing a linux kernel module and I'm calling kernel functions. Some of these functions are pointers and I want to inspect the code of that function in the kernel source. But I don't know which function it is pointing to. I thought it could be done because, when the system fails (kernel panic) it prints out in the screen the current callstack with function's names. But, I guess I was wrong... am I?

推荐答案

这是不能直接,无需额外的援助。

That's not directly possible without additional assistance.

您可以:


  1. 保持你的程序映射函数指针名称的表

  1. maintain a table in your program mapping function pointers to names

检查可执行文件的符号表,如果它有一个。

examine the executable's symbol table, if it has one.

后者,然而,是硬的,并且是不可移植的。该方法将取决于操作系统的二进制格式(ELF,a.out的,.EXE,等),并且还对由连接器完成的任何重新定位

The latter, however, is hard, and is not portable. The method will depend on the operating system's binary format (ELF, a.out, .exe, etc), and also on any relocation done by the linker.

编辑:既然你现在解释你的真实使用情况是什么,答案其实并不难。内核符号表提供的/ proc / kallsyms ,并有一个API来访问它:

Since you've now explained what your real use case is, the answer is actually not that hard. The kernel symbol table is available in /proc/kallsyms, and there's an API for accessing it:

#include <linux/kallsyms.h>

const char *kallsyms_lookup(unsigned long addr, unsigned long *symbolsize,
                            unsigned long *ofset, char **modname, char *namebuf)

void print_symbol(const char *fmt, unsigned long addr)

对于简单的调试目的,后者可能会做正是你需要 - 它需要的地址,格式它,并将它发送到的printk

这篇关于如何从C函数的指针获取函数的名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆