forked from Minki/linux
d027b64001
The VDSO temporary file is unlinked when a session is deleted. That precludes the possibilities that there is no session or there is more than one session. Correctly the vdso belongs to the machine so put the information on 'struct machine' and get rid of the global variables. Reviewed-by: Jiri Olsa <jolsa@redhat.com> Signed-off-by: Adrian Hunter <adrian.hunter@intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@gmail.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Link: http://lkml.kernel.org/r/53CF9B14.7040408@intel.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
153 lines
2.7 KiB
C
153 lines
2.7 KiB
C
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <stdlib.h>
|
|
#include <linux/kernel.h>
|
|
|
|
#include "vdso.h"
|
|
#include "util.h"
|
|
#include "symbol.h"
|
|
#include "machine.h"
|
|
#include "linux/string.h"
|
|
#include "debug.h"
|
|
|
|
#define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
|
|
|
|
struct vdso_file {
|
|
bool found;
|
|
bool error;
|
|
char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
|
|
const char *dso_name;
|
|
};
|
|
|
|
struct vdso_info {
|
|
struct vdso_file vdso;
|
|
};
|
|
|
|
static struct vdso_info *vdso_info__new(void)
|
|
{
|
|
static const struct vdso_info vdso_info_init = {
|
|
.vdso = {
|
|
.temp_file_name = VDSO__TEMP_FILE_NAME,
|
|
.dso_name = VDSO__MAP_NAME,
|
|
},
|
|
};
|
|
|
|
return memdup(&vdso_info_init, sizeof(vdso_info_init));
|
|
}
|
|
|
|
static int find_vdso_map(void **start, void **end)
|
|
{
|
|
FILE *maps;
|
|
char line[128];
|
|
int found = 0;
|
|
|
|
maps = fopen("/proc/self/maps", "r");
|
|
if (!maps) {
|
|
pr_err("vdso: cannot open maps\n");
|
|
return -1;
|
|
}
|
|
|
|
while (!found && fgets(line, sizeof(line), maps)) {
|
|
int m = -1;
|
|
|
|
/* We care only about private r-x mappings. */
|
|
if (2 != sscanf(line, "%p-%p r-xp %*x %*x:%*x %*u %n",
|
|
start, end, &m))
|
|
continue;
|
|
if (m < 0)
|
|
continue;
|
|
|
|
if (!strncmp(&line[m], VDSO__MAP_NAME,
|
|
sizeof(VDSO__MAP_NAME) - 1))
|
|
found = 1;
|
|
}
|
|
|
|
fclose(maps);
|
|
return !found;
|
|
}
|
|
|
|
static char *get_file(struct vdso_file *vdso_file)
|
|
{
|
|
char *vdso = NULL;
|
|
char *buf = NULL;
|
|
void *start, *end;
|
|
size_t size;
|
|
int fd;
|
|
|
|
if (vdso_file->found)
|
|
return vdso_file->temp_file_name;
|
|
|
|
if (vdso_file->error || find_vdso_map(&start, &end))
|
|
return NULL;
|
|
|
|
size = end - start;
|
|
|
|
buf = memdup(start, size);
|
|
if (!buf)
|
|
return NULL;
|
|
|
|
fd = mkstemp(vdso_file->temp_file_name);
|
|
if (fd < 0)
|
|
goto out;
|
|
|
|
if (size == (size_t) write(fd, buf, size))
|
|
vdso = vdso_file->temp_file_name;
|
|
|
|
close(fd);
|
|
|
|
out:
|
|
free(buf);
|
|
|
|
vdso_file->found = (vdso != NULL);
|
|
vdso_file->error = !vdso_file->found;
|
|
return vdso;
|
|
}
|
|
|
|
void vdso__exit(struct machine *machine)
|
|
{
|
|
struct vdso_info *vdso_info = machine->vdso_info;
|
|
|
|
if (!vdso_info)
|
|
return;
|
|
|
|
if (vdso_info->vdso.found)
|
|
unlink(vdso_info->vdso.temp_file_name);
|
|
|
|
zfree(&machine->vdso_info);
|
|
}
|
|
|
|
struct dso *vdso__dso_findnew(struct machine *machine)
|
|
{
|
|
struct vdso_info *vdso_info;
|
|
struct dso *dso;
|
|
|
|
if (!machine->vdso_info)
|
|
machine->vdso_info = vdso_info__new();
|
|
|
|
vdso_info = machine->vdso_info;
|
|
if (!vdso_info)
|
|
return NULL;
|
|
|
|
dso = dsos__find(&machine->user_dsos, VDSO__MAP_NAME, true);
|
|
if (!dso) {
|
|
char *file;
|
|
|
|
file = get_file(&vdso_info->vdso);
|
|
if (!file)
|
|
return NULL;
|
|
|
|
dso = dso__new(VDSO__MAP_NAME);
|
|
if (dso != NULL) {
|
|
dsos__add(&machine->user_dsos, dso);
|
|
dso__set_long_name(dso, file, false);
|
|
}
|
|
}
|
|
|
|
return dso;
|
|
}
|