4

I am trying to implement backtrace like function call myself . I have been successfully able to unroll stack and i have all return address present on my stack . Now i want to get function name, variable name input to my function from this information. Also i want to do this programmatically i.e at run time i should be able to get info about all the functions that have been called till now in my program. Lets assume i am fine compiling my with -g flag while compiling.

I am trying to use dladdr() function to get function name, but this did not work . It gives me error that "error: unknown type name ‘Dl_info’" . Here is my code snippet :

const char * get_func_name(void *ip){
        Dl_info info;
        int ret;
        ret = dladdr(ip,&info);
        if(ret < 0)
                return NULL;
        return info.dli_fname;
}

I tried researching bfd library , but didnt have good example/tutorial , any help ? I am using ubunutu 14.04

app
  • 421
  • 1
  • 4
  • 7

1 Answers1

2

Per the man page, be sure to #define _GNU_SOURCE before you #include <dlfcn.h>. This structure, and dladdr(), are GNU extensions.

Davislor
  • 14,674
  • 2
  • 34
  • 49