根据函数地址查找函数名称,或根据函数名称查找函数地址的实现

时间:2021-08-24
本文章向大家介绍根据函数地址查找函数名称,或根据函数名称查找函数地址的实现,主要包括根据函数地址查找函数名称,或根据函数名称查找函数地址的实现使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
linux平台上查问题时,定位到出错的函数地址,但无法知道是哪个模块的函数,记录如下函数可实现: 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; }

原文地址:https://www.cnblogs.com/yangyi54920/p/15179035.html