vdso.c 6.3 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 machine__exit_vdso(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
}

123 124
static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name,
					  const char *long_name)
A
Adrian Hunter 已提交
125 126 127 128 129
{
	struct dso *dso;

	dso = dso__new(short_name);
	if (dso != NULL) {
130
		__dsos__add(&machine->dsos, dso);
A
Adrian Hunter 已提交
131 132 133 134 135 136
		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
#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;
}

233 234
static struct dso *__machine__findnew_compat(struct machine *machine,
					     struct vdso_file *vdso_file)
235 236 237 238
{
	const char *file_name;
	struct dso *dso;

239
	dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true);
240
	if (dso)
241
		goto out;
242 243 244

	file_name = vdso__get_compat_file(vdso_file);
	if (!file_name)
245
		goto out;
246

247
	dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
248
out:
249
	return dso;
250 251
}

252 253 254 255
static int __machine__findnew_vdso_compat(struct machine *machine,
					  struct thread *thread,
					  struct vdso_info *vdso_info,
					  struct dso **dso)
256 257 258 259
{
	enum dso_type dso_type;

	dso_type = machine__thread_dso_type(machine, thread);
260 261 262 263 264 265 266 267 268 269

#ifndef HAVE_PERF_READ_VDSO32
	if (dso_type == DSO__TYPE_32BIT)
		return 0;
#endif
#ifndef HAVE_PERF_READ_VDSOX32
	if (dso_type == DSO__TYPE_X32BIT)
		return 0;
#endif

270 271
	switch (dso_type) {
	case DSO__TYPE_32BIT:
272
		*dso = __machine__findnew_compat(machine, &vdso_info->vdso32);
273 274
		return 1;
	case DSO__TYPE_X32BIT:
275
		*dso = __machine__findnew_compat(machine, &vdso_info->vdsox32);
276 277 278 279 280 281 282 283 284 285
		return 1;
	case DSO__TYPE_UNKNOWN:
	case DSO__TYPE_64BIT:
	default:
		return 0;
	}
}

#endif

286 287
struct dso *machine__findnew_vdso(struct machine *machine,
				  struct thread *thread __maybe_unused)
288
{
289
	struct vdso_info *vdso_info;
290
	struct dso *dso = NULL;
291

292
	pthread_rwlock_wrlock(&machine->dsos.lock);
293 294 295 296 297
	if (!machine->vdso_info)
		machine->vdso_info = vdso_info__new();

	vdso_info = machine->vdso_info;
	if (!vdso_info)
298
		goto out_unlock;
299

300
#if BITS_PER_LONG == 64
301 302
	if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso))
		goto out_unlock;
303 304
#endif

305
	dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
306 307 308
	if (!dso) {
		char *file;

309
		file = get_file(&vdso_info->vdso);
310 311
		if (file)
			dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
312 313
	}

314
out_unlock:
315
	dso__get(dso);
316
	pthread_rwlock_unlock(&machine->dsos.lock);
317 318
	return dso;
}
319 320 321

bool dso__is_vdso(struct dso *dso)
{
322 323 324
	return !strcmp(dso->short_name, DSO__NAME_VDSO) ||
	       !strcmp(dso->short_name, DSO__NAME_VDSO32) ||
	       !strcmp(dso->short_name, DSO__NAME_VDSOX32);
325
}