move sigar_dlinfo_modules to util for use with FreeBSD

This commit is contained in:
Doug MacEachern 2008-04-04 18:39:42 +00:00
parent 51a205920f
commit 3cf07fbb30
4 changed files with 76 additions and 58 deletions

View File

@ -143,6 +143,15 @@ int sigar_cpu_mhz_from_model(char *model);
char *sigar_get_self_path(sigar_t *sigar);
#if defined(__sun) || defined(__FreeBSD__)
#define SIGAR_HAS_DLINFO_MODULES
#include <dlfcn.h>
#include <link.h>
int sigar_dlinfo_modules(sigar_t *sigar, sigar_proc_modules_t *procmods);
#endif
typedef struct sigar_cache_entry_t sigar_cache_entry_t;
struct sigar_cache_entry_t {

View File

@ -1690,6 +1690,11 @@ int sigar_proc_exe_get(sigar_t *sigar, sigar_pid_t pid,
int sigar_proc_modules_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_modules_t *procmods)
{
#ifdef SIGAR_HAS_DLINFO_MODULES
if (pid == sigar_pid_get(sigar)) {
return sigar_dlinfo_modules(sigar, procmods);
}
#endif
return SIGAR_ENOTIMPL;
}

View File

@ -626,37 +626,6 @@ static int sigar_pgrab(sigar_t *sigar, sigar_pid_t pid,
return SIGAR_OK;
}
static int sigar_dlinfo_get(sigar_t *sigar, const char *func,
void **handle, Link_map **map)
{
Dl_info dli;
if (!dladdr((void *)((uintptr_t)sigar_dlinfo_get), &dli)) {
sigar_log_printf(sigar, SIGAR_LOG_ERROR,
"[%s] dladdr(%s) = %s",
func, SIGAR_FUNC, dlerror());
return ESRCH;
}
if (!(*handle = dlopen(dli.dli_fname, RTLD_LAZY))) {
sigar_log_printf(sigar, SIGAR_LOG_ERROR,
"[%s] dlopen(%s) = %s",
func, dli.dli_fname, dlerror());
return ESRCH;
}
dlinfo(*handle, RTLD_DI_LINKMAP, map);
if (!map) {
sigar_log_printf(sigar, SIGAR_LOG_ERROR,
"[%s] dlinfo = %s",
func, dlerror());
return ESRCH;
}
return SIGAR_OK;
}
int sigar_os_proc_list_get(sigar_t *sigar,
sigar_proc_list_t *proclist)
{
@ -1238,33 +1207,6 @@ static int sigar_pgrab_modules(sigar_t *sigar, sigar_pid_t pid,
return SIGAR_OK;
}
static int sigar_dlinfo_modules(sigar_t *sigar, sigar_proc_modules_t *procmods)
{
int status;
void *handle;
Link_map *map;
status = sigar_dlinfo_get(sigar, SIGAR_FUNC, &handle, &map);
if (status != SIGAR_OK) {
return status;
}
do {
int status =
procmods->module_getter(procmods->data,
map->l_name, strlen(map->l_name));
if (status != SIGAR_OK) {
/* not an error; just stop iterating */
return status;
}
} while ((map = map->l_next));
dlclose(handle);
return SIGAR_OK;
}
int sigar_proc_modules_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_modules_t *procmods)
{

View File

@ -782,6 +782,68 @@ char *sigar_get_self_path(sigar_t *sigar)
return sigar->self_path;
}
#ifdef SIGAR_HAS_DLINFO_MODULES
static int sigar_dlinfo_get(sigar_t *sigar, const char *func,
void **handle, Link_map **map)
{
Dl_info dli;
if (!dladdr((void *)((uintptr_t)sigar_dlinfo_get), &dli)) {
sigar_log_printf(sigar, SIGAR_LOG_ERROR,
"[%s] dladdr(%s) = %s",
func, SIGAR_FUNC, dlerror());
return ESRCH;
}
if (!(*handle = dlopen(dli.dli_fname, RTLD_LAZY))) {
sigar_log_printf(sigar, SIGAR_LOG_ERROR,
"[%s] dlopen(%s) = %s",
func, dli.dli_fname, dlerror());
return ESRCH;
}
dlinfo(*handle, RTLD_DI_LINKMAP, map);
if (!map) {
sigar_log_printf(sigar, SIGAR_LOG_ERROR,
"[%s] dlinfo = %s",
func, dlerror());
return ESRCH;
}
return SIGAR_OK;
}
int sigar_dlinfo_modules(sigar_t *sigar, sigar_proc_modules_t *procmods)
{
int status;
void *handle;
Link_map *map;
status = sigar_dlinfo_get(sigar, SIGAR_FUNC, &handle, &map);
if (status != SIGAR_OK) {
return status;
}
do {
int status =
procmods->module_getter(procmods->data,
(char *)map->l_name,
strlen(map->l_name));
if (status != SIGAR_OK) {
/* not an error; just stop iterating */
return status;
}
} while ((map = map->l_next));
dlclose(handle);
return SIGAR_OK;
}
#endif
SIGAR_DECLARE(void) sigar_log_printf(sigar_t *sigar, int level,
const char *format, ...)
{