1. 程式人生 > 其它 >根據函式地址查詢函式名稱,或根據函式名稱查詢函式地址的實現

根據函式地址查詢函式名稱,或根據函式名稱查詢函式地址的實現

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; }