首页 > 其他 > 详细

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

时间:2021-08-24 11:19:08      阅读:12      评论:0      收藏:0      [点我收藏+]

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

 1 int getFuncAddrByName(char *funcName, unsigned int nameLen)
 2 {
 3     void *handle = NULL;
 4     void *iptr = NULL;
 5 
 6     /* open the needed object */
 7     handle = dlopen(NULL, RTLD_LOCAL | RTLD_LAZY);
 8     if(handle == NULL)
 9     {
10         return NULL;
11     }
12 
13     /* find the address of function and data objects */
14     iptr = dlsym(handle, funcName);
15     dlclose(handle);
16     
17     if(iptr == NULL)
18     {
19         return NULL;
20     }
21 
22     return (int)iptr;
23 }
24 
25 
26 int getFuncNameByAddr (FUNCPTR funcAddr, char *funcName, unsigned int funcNameLen)
27 {
28     void *handle = NULL;
29     Dl_info dl;
30     int ret = 0;
31 
32     /* open the needed object */
33     handle = dlopen(NULL, RTLD_LOCAL | RTLD_LAZY);
34     if(handle == NULL)
35     {
36         return -1;
37     }
38 
39     /* find the address of function and data objects */
40     ret = dladdr (funcAddr, &dl);
41     dlclose(handle);
42     
43     if(ret == 0)
44     {
45         return -1;
46     }
47     
48     strncpy(funcName, dl.dli_sname, funcNameLen - 1);
49     
50     return 0;
51 }

 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!