views:

3710

answers:

7

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

Edit: 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?

+1  A: 

You can't. The function name isn't attached to the function by the time it's compiled and linked. It's all by memory address at that point, not name.

sblundy
+14  A: 

That's not directly possible without additional assistance.

You could:

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

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

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.

EDIT: 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)

For simple debug purposes the latter will probably do exactly what you need - it takes the address, formats it, and sends it to printk.

Alnitak
I don't think I can call kallsyms_lookup from a kernel module. When I compile, I get "kallsyms_lookup undefined"
Daniel Silveira
If you're getting a compile time error then you need to make sure that you've got the kernel headers available and in your include-path.
Alnitak
I'm getting a link time error. The headers are fine. #include <linux/kallsyms.h>
Daniel Silveira
ok, that suggests that your module compile stuff is wrong somewhere. Modules need to call symbols that are in the kernel, so by definition the symbol can't be resolved completely at link time.
Alnitak
p.s. sample Makefiles for compiling Linux kernel modules is probably a good candidate for another question. My reference stuff for this is at work where I can't get it at the moment.
Alnitak
A: 

Check out Visual Leak Detector to see how they get their callstack printing working. This assumes you are using Windows, though.

Jim Buck
+1  A: 

You can't diectly but you can implement a different approach to this problem if you want. You can make a struct pointer instead pointing to a function as well as a descriptive string you can set to whatever you want. I also added a debugging posebilety since you problably do not want these vars to be printet forever.

// Define it like this
typedef struct
{
  char        *dec_text;
  #ifdef _DEBUG_FUNC
  void        (*action)(char);
  #endif
} func_Struct;

// Initialize it like this
func_Struct func[3]= {
#ifdef _DEBUG_FUNC
{"my_Set(char input)",&my_Set}};
{"my_Get(char input)",&my_Get}};
{"my_Clr(char input)",&my_Clr}};
#else
{&my_Set}};
{&my_Get}};
{&my_Clr}};
#endif 

// And finally you can use it like this
func[0].action( 0x45 );
#ifdef _DEBUG_FUNC
printf("%s",func.dec_text);
#endif
eaanon01
+1  A: 

There is no way how to do it in general.

If you compile the corresponding code into a DLL/Shared Library, you should be able to enlist all entry points and compare with the pointer you've got. Haven't tried it yet, but I've got some experience with DLLs/Shared Libs and would expect it to work. This could even be implemented to work cross-plarform.

Someone else mentioned already to compile with debug symbols, then you could try to find a way to analyse these from the running application, similiar to what a debugger would do. But this is absolutely proprietary and not portable.

mh
A: 

You wouldn't know how you look like without a reflecting mirror. You'll have to use a reflection-capable language like C#.

yogman
Yea, sure. Write a linux kernel module in C#. I'm sure that works.
JesperE
At first, I thought your humor was bitter than me. But it turns out that there had been an edit of the original question.
yogman
+5  A: 

I'm surprised why everybody says it is not possible. It is possible on Linux for non-static functions.

I know at least two ways to achieve this.

There are GNU functions for backtrace printing: backtrace() and backtrace_symbols() (See man). In your case you don't need backtrace() as you already have function pointer, you just pass it to backtrace_symbols().

Example (working code):

#include <stdio.h>
#include <execinfo.h>

void foo(void) {
    printf("foo\n");
}

int main(int argc, char *argv[]) {
    void    *funptr = &foo;

    backtrace_symbols_fd(&funptr, 1, 1);

    return 0;
}

Compile with gcc test.c -rdynamic

Output: ./a.out(foo+0x0)[0x8048634]

It gives you binary name, function name, pointer offset from function start and pointer value so you can parse it.

Another way is to use dladdr() (another extension), I guess print_backtrace() uses dladdr(). dladdr() returns Dl_info structure that has function name in dli_sname field. I don't provide code example here but it is obvious - see man dladdr for details.

NB! Both approaches require function to be non-static!

Well, there is one more way - use debug information using libdwarf but it would require unstripped binary and not very easy to do so I don't recommend it.

qrdl