vdso.c 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13

#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"
14
#include "machine.h"
15
#include "linux/string.h"
16
#include "debug.h"
17

18 19 20 21 22 23 24 25 26 27 28 29 30
#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;
};

31 32 33 34 35 36 37 38 39 40 41
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));
}
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

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

74
static char *get_file(struct vdso_file *vdso_file)
75 76 77 78 79 80 81
{
	char *vdso = NULL;
	char *buf = NULL;
	void *start, *end;
	size_t size;
	int fd;

82 83
	if (vdso_file->found)
		return vdso_file->temp_file_name;
84

85
	if (vdso_file->error || find_vdso_map(&start, &end))
86 87 88 89 90 91 92 93
		return NULL;

	size = end - start;

	buf = memdup(start, size);
	if (!buf)
		return NULL;

94
	fd = mkstemp(vdso_file->temp_file_name);
95 96 97 98
	if (fd < 0)
		goto out;

	if (size == (size_t) write(fd, buf, size))
99
		vdso = vdso_file->temp_file_name;
100 101 102 103 104 105

	close(fd);

 out:
	free(buf);

106 107
	vdso_file->found = (vdso != NULL);
	vdso_file->error = !vdso_file->found;
108 109 110
	return vdso;
}

111
void vdso__exit(struct machine *machine)
112
{
113 114 115 116 117
	struct vdso_info *vdso_info = machine->vdso_info;

	if (!vdso_info)
		return;

118 119
	if (vdso_info->vdso.found)
		unlink(vdso_info->vdso.temp_file_name);
120 121

	zfree(&machine->vdso_info);
122 123
}

124
struct dso *vdso__dso_findnew(struct machine *machine)
125
{
126 127 128 129 130 131 132 133 134
	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;
135

136
	dso = dsos__find(&machine->user_dsos, VDSO__MAP_NAME, true);
137 138 139
	if (!dso) {
		char *file;

140
		file = get_file(&vdso_info->vdso);
141 142 143 144 145
		if (!file)
			return NULL;

		dso = dso__new(VDSO__MAP_NAME);
		if (dso != NULL) {
146
			dsos__add(&machine->user_dsos, dso);
147
			dso__set_long_name(dso, file, false);
148 149 150 151 152
		}
	}

	return dso;
}