perf tools: Handle relative paths while loading module symbols

Inform util/module.c::mod_dso__load_module_paths() that relative
paths do exist in some modules.dep, and make it fail noisily should
it encounter a path that it doesn't understand, or a module it
cannot open.

Reported-by: Avi Kivity <avi@redhat.com>
Signed-off-by: Mike Galbraith <efault@gmx.de>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: rostedt@goodmis.org
Cc: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Masami Hiramatsu <mhiramat@redhat.com>
LKML-Reference: <1253779628.10513.8.camel@marge.simson.net>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
This commit is contained in:
Mike Galbraith 2009-09-24 10:07:08 +02:00 committed by Ingo Molnar
parent 508c4d0874
commit dd906a0fe8
1 changed files with 67 additions and 31 deletions

View File

@ -4,6 +4,7 @@
#include "module.h" #include "module.h"
#include <libelf.h> #include <libelf.h>
#include <libgen.h>
#include <gelf.h> #include <gelf.h>
#include <elf.h> #include <elf.h>
#include <dirent.h> #include <dirent.h>
@ -409,35 +410,40 @@ out_failure:
static int mod_dso__load_module_paths(struct mod_dso *self) static int mod_dso__load_module_paths(struct mod_dso *self)
{ {
struct utsname uts; struct utsname uts;
int count = 0, len; int count = 0, len, err = -1;
char *line = NULL; char *line = NULL;
FILE *file; FILE *file;
char *path; char *dpath, *dir;
size_t n; size_t n;
if (uname(&uts) < 0) if (uname(&uts) < 0)
goto out_failure; return err;
len = strlen("/lib/modules/"); len = strlen("/lib/modules/");
len += strlen(uts.release); len += strlen(uts.release);
len += strlen("/modules.dep"); len += strlen("/modules.dep");
path = calloc(1, len); dpath = calloc(1, len);
if (path == NULL) if (dpath == NULL)
goto out_failure; return err;
strcat(path, "/lib/modules/"); strcat(dpath, "/lib/modules/");
strcat(path, uts.release); strcat(dpath, uts.release);
strcat(path, "/modules.dep"); strcat(dpath, "/modules.dep");
file = fopen(path, "r"); file = fopen(dpath, "r");
free(path);
if (file == NULL) if (file == NULL)
goto out_failure; goto out_failure;
dir = dirname(dpath);
if (!dir)
goto out_failure;
strcat(dir, "/");
while (!feof(file)) { while (!feof(file)) {
char *name, *tmp;
struct module *module; struct module *module;
char *name, *path, *tmp;
FILE *modfile;
int line_len; int line_len;
line_len = getline(&line, &n, file); line_len = getline(&line, &n, file);
@ -445,17 +451,41 @@ static int mod_dso__load_module_paths(struct mod_dso *self)
break; break;
if (!line) if (!line)
goto out_failure; break;
line[--line_len] = '\0'; /* \n */ line[--line_len] = '\0'; /* \n */
path = strtok(line, ":"); path = strchr(line, ':');
if (!path) if (!path)
goto out_failure; break;
*path = '\0';
path = strdup(line);
if (!path)
break;
if (!strstr(path, dir)) {
if (strncmp(path, "kernel/", 7))
break;
free(path);
path = calloc(1, strlen(dir) + strlen(line) + 1);
if (!path)
break;
strcat(path, dir);
strcat(path, line);
}
modfile = fopen(path, "r");
if (modfile == NULL)
break;
fclose(modfile);
name = strdup(path); name = strdup(path);
name = strtok(name, "/"); if (!name)
break;
name = strtok(name, "/");
tmp = name; tmp = name;
while (tmp) { while (tmp) {
@ -463,26 +493,25 @@ static int mod_dso__load_module_paths(struct mod_dso *self)
if (tmp) if (tmp)
name = tmp; name = tmp;
} }
name = strsep(&name, ".");
/* Quirk: replace '-' with '_' in sound modules */ name = strsep(&name, ".");
if (!name)
break;
/* Quirk: replace '-' with '_' in all modules */
for (len = strlen(name); len; len--) { for (len = strlen(name); len; len--) {
if (*(name+len) == '-') if (*(name+len) == '-')
*(name+len) = '_'; *(name+len) = '_';
} }
module = module__new(name, path); module = module__new(name, path);
if (!module) { if (!module)
fprintf(stderr, "load_module_paths: allocation error\n"); break;
goto out_failure;
}
mod_dso__insert_module(self, module); mod_dso__insert_module(self, module);
module->sections = sec_dso__new_dso("sections"); module->sections = sec_dso__new_dso("sections");
if (!module->sections) { if (!module->sections)
fprintf(stderr, "load_module_paths: allocation error\n"); break;
goto out_failure;
}
module->active = mod_dso__load_sections(module); module->active = mod_dso__load_sections(module);
@ -490,13 +519,20 @@ static int mod_dso__load_module_paths(struct mod_dso *self)
count++; count++;
} }
free(line); if (feof(file))
fclose(file); err = count;
else
return count; fprintf(stderr, "load_module_paths: modules.dep parsing failure!\n");
out_failure: out_failure:
return -1; if (dpath)
free(dpath);
if (file)
fclose(file);
if (line)
free(line);
return err;
} }
int mod_dso__load_modules(struct mod_dso *dso) int mod_dso__load_modules(struct mod_dso *dso)