FAIRYFAR-INTERNAL
 
  FAIRYFAR-INTERNAL  |  SITEMAP  |  ABOUT-ME  |  HOME  
Linux根据函数名查找函数地址

转自:https://www.cnblogs.com/yangyi54920/p/15179035.html

linux平台上查问题时,定位到出错的函数地址,但无法知道是哪个模块的函数,记录如下函数可实现:

snippet.c
#include <dlfcn.h>
 
int getFuncAddrByName(char *funcName, unsigned int nameLen)
{
    void *handle = NULL;
    void *iptr = NULL;
 
    /* open the needed object */
    handle = dlopen(NULL, RTLD_LOCAL | RTLD_LAZY);
    if(handle == NULL)
    {
        return NULL;
    }
 
    /* find the address of function and data objects */
    iptr = dlsym(handle, funcName);
    dlclose(handle);
 
    if(iptr == NULL)
    {
        return NULL;
    }
 
    return (int)iptr;
}
 
 
int getFuncNameByAddr (FUNCPTR funcAddr, char *funcName, unsigned int funcNameLen)
{
    void *handle = NULL;
    Dl_info dl;
    int ret = 0;
 
    /* open the needed object */
    handle = dlopen(NULL, RTLD_LOCAL | RTLD_LAZY);
    if(handle == NULL)
    {
        return -1;
    }
 
    /* find the address of function and data objects */
    ret = dladdr (funcAddr, &dl);
    dlclose(handle);
 
    if(ret == 0)
    {
        return -1;
    }
 
    strncpy(funcName, dl.dli_sname, funcNameLen - 1);
 
    return 0;
}


打赏作者以资鼓励:
移动端扫码阅读: