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

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 }

 

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

上一篇:贪婪算法之兑换硬币及问题所在


下一篇:视图--请求和响应