vdso.c 6.0 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 "thread.h"
16
#include "linux/string.h"
17
#include "debug.h"
18

19 20 21 22 23 24
/*
 * Include definition of find_vdso_map() also used in perf-read-vdso.c for
 * building perf-read-vdso32 and perf-read-vdsox32.
 */
#include "find-vdso-map.c"

25 26 27 28 29 30 31
#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;
32
	const char *read_prog;
33 34 35 36
};

struct vdso_info {
	struct vdso_file vdso;
37 38 39 40
#if BITS_PER_LONG == 64
	struct vdso_file vdso32;
	struct vdso_file vdsox32;
#endif
41 42
};

43 44 45 46 47
static struct vdso_info *vdso_info__new(void)
{
	static const struct vdso_info vdso_info_init = {
		.vdso    = {
			.temp_file_name = VDSO__TEMP_FILE_NAME,
48
			.dso_name = DSO__NAME_VDSO,
49
		},
50 51 52 53 54 55 56 57 58 59 60 61
#if BITS_PER_LONG == 64
		.vdso32  = {
			.temp_file_name = VDSO__TEMP_FILE_NAME,
			.dso_name = DSO__NAME_VDSO32,
			.read_prog = "perf-read-vdso32",
		},
		.vdsox32  = {
			.temp_file_name = VDSO__TEMP_FILE_NAME,
			.dso_name = DSO__NAME_VDSOX32,
			.read_prog = "perf-read-vdsox32",
		},
#endif
62 63 64 65
	};

	return memdup(&vdso_info_init, sizeof(vdso_info_init));
}
66

67
static char *get_file(struct vdso_file *vdso_file)
68 69 70 71 72 73 74
{
	char *vdso = NULL;
	char *buf = NULL;
	void *start, *end;
	size_t size;
	int fd;

75 76
	if (vdso_file->found)
		return vdso_file->temp_file_name;
77

78
	if (vdso_file->error || find_vdso_map(&start, &end))
79 80 81 82 83 84 85 86
		return NULL;

	size = end - start;

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

87
	fd = mkstemp(vdso_file->temp_file_name);
88 89 90 91
	if (fd < 0)
		goto out;

	if (size == (size_t) write(fd, buf, size))
92
		vdso = vdso_file->temp_file_name;
93 94 95 96 97 98

	close(fd);

 out:
	free(buf);

99 100
	vdso_file->found = (vdso != NULL);
	vdso_file->error = !vdso_file->found;
101 102 103
	return vdso;
}

104
void vdso__exit(struct machine *machine)
105
{
106 107 108 109 110
	struct vdso_info *vdso_info = machine->vdso_info;

	if (!vdso_info)
		return;

111 112
	if (vdso_info->vdso.found)
		unlink(vdso_info->vdso.temp_file_name);
113 114 115 116 117 118
#if BITS_PER_LONG == 64
	if (vdso_info->vdso32.found)
		unlink(vdso_info->vdso32.temp_file_name);
	if (vdso_info->vdsox32.found)
		unlink(vdso_info->vdsox32.temp_file_name);
#endif
119 120

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

A
Adrian Hunter 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136
static struct dso *vdso__new(struct machine *machine, const char *short_name,
			     const char *long_name)
{
	struct dso *dso;

	dso = dso__new(short_name);
	if (dso != NULL) {
		dsos__add(&machine->user_dsos, dso);
		dso__set_long_name(dso, long_name, false);
	}

	return dso;
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
#if BITS_PER_LONG == 64

static enum dso_type machine__thread_dso_type(struct machine *machine,
					      struct thread *thread)
{
	enum dso_type dso_type = DSO__TYPE_UNKNOWN;
	struct map *map;
	struct dso *dso;

	map = map_groups__first(thread->mg, MAP__FUNCTION);
	for (; map ; map = map_groups__next(map)) {
		dso = map->dso;
		if (!dso || dso->long_name[0] != '/')
			continue;
		dso_type = dso__type(dso, machine);
		if (dso_type != DSO__TYPE_UNKNOWN)
			break;
	}

	return dso_type;
}

static int vdso__do_copy_compat(FILE *f, int fd)
{
	char buf[4096];
	size_t count;

	while (1) {
		count = fread(buf, 1, sizeof(buf), f);
		if (ferror(f))
			return -errno;
		if (feof(f))
			break;
		if (count && writen(fd, buf, count) != (ssize_t)count)
			return -errno;
	}

	return 0;
}

static int vdso__copy_compat(const char *prog, int fd)
{
	FILE *f;
	int err;

	f = popen(prog, "r");
	if (!f)
		return -errno;

	err = vdso__do_copy_compat(f, fd);

	if (pclose(f) == -1)
		return -errno;

	return err;
}

static int vdso__create_compat_file(const char *prog, char *temp_name)
{
	int fd, err;

	fd = mkstemp(temp_name);
	if (fd < 0)
		return -errno;

	err = vdso__copy_compat(prog, fd);

	if (close(fd) == -1)
		return -errno;

	return err;
}

static const char *vdso__get_compat_file(struct vdso_file *vdso_file)
{
	int err;

	if (vdso_file->found)
		return vdso_file->temp_file_name;

	if (vdso_file->error)
		return NULL;

	err = vdso__create_compat_file(vdso_file->read_prog,
				       vdso_file->temp_file_name);
	if (err) {
		pr_err("%s failed, error %d\n", vdso_file->read_prog, err);
		vdso_file->error = true;
		return NULL;
	}

	vdso_file->found = true;

	return vdso_file->temp_file_name;
}

static struct dso *vdso__findnew_compat(struct machine *machine,
					struct vdso_file *vdso_file)
{
	const char *file_name;
	struct dso *dso;

	dso = dsos__find(&machine->user_dsos, vdso_file->dso_name, true);
	if (dso)
		return dso;

	file_name = vdso__get_compat_file(vdso_file);
	if (!file_name)
		return NULL;

	return vdso__new(machine, vdso_file->dso_name, file_name);
}

static int vdso__dso_findnew_compat(struct machine *machine,
				    struct thread *thread,
				    struct vdso_info *vdso_info,
				    struct dso **dso)
{
	enum dso_type dso_type;

	dso_type = machine__thread_dso_type(machine, thread);
	switch (dso_type) {
	case DSO__TYPE_32BIT:
		*dso = vdso__findnew_compat(machine, &vdso_info->vdso32);
		return 1;
	case DSO__TYPE_X32BIT:
		*dso = vdso__findnew_compat(machine, &vdso_info->vdsox32);
		return 1;
	case DSO__TYPE_UNKNOWN:
	case DSO__TYPE_64BIT:
	default:
		return 0;
	}
}

#endif

274 275
struct dso *vdso__dso_findnew(struct machine *machine,
			      struct thread *thread __maybe_unused)
276
{
277 278 279 280 281 282 283 284 285
	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;
286

287 288 289 290 291
#if BITS_PER_LONG == 64
	if (vdso__dso_findnew_compat(machine, thread, vdso_info, &dso))
		return dso;
#endif

292
	dso = dsos__find(&machine->user_dsos, DSO__NAME_VDSO, true);
293 294 295
	if (!dso) {
		char *file;

296
		file = get_file(&vdso_info->vdso);
297 298 299
		if (!file)
			return NULL;

300
		dso = vdso__new(machine, DSO__NAME_VDSO, file);
301 302 303 304
	}

	return dso;
}
305 306 307

bool dso__is_vdso(struct dso *dso)
{
308 309 310
	return !strcmp(dso->short_name, DSO__NAME_VDSO) ||
	       !strcmp(dso->short_name, DSO__NAME_VDSO32) ||
	       !strcmp(dso->short_name, DSO__NAME_VDSOX32);
311
}