Get function name by address in Linux

时间:2023-03-08 20:50:46
Get function name by address in Linux

Source file:

   1: #define __USE_GNU //import!

   2: #include <dlfcn.h>

   3: #include <stdio.h>

   4: void getFuncNameByAddr(void *this_fn, void *call_site) {

   5:     Dl_info  DlInfo;

   6:     int  nRet;

   7:     if ((nRet = dladdr(this_fn, &DlInfo)) != 0)

   8:     {

   9:         printf("ENTER:%s\t", DlInfo.dli_sname);

  10:     }

  11: }

 

When you compile this code with GCC, please don’t forget the flag ‘-rdynamic’ or ‘-Wl, –-export-dynamic’, which is used to tell the Linker to add the symbols to the dynamic symbol table.

The below function interface is programming interface to dynamic linking loader, so when using them, please link with –ldl when compile.

dladdr, dlclose, dlerror, dlopen, dlsym, dlvsym