vdso.c 2.6 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
/*
 * 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"

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

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,
42
			.dso_name = DSO__NAME_VDSO,
43 44 45 46 47
		},
	};

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

49
static char *get_file(struct vdso_file *vdso_file)
50 51 52 53 54 55 56
{
	char *vdso = NULL;
	char *buf = NULL;
	void *start, *end;
	size_t size;
	int fd;

57 58
	if (vdso_file->found)
		return vdso_file->temp_file_name;
59

60
	if (vdso_file->error || find_vdso_map(&start, &end))
61 62 63 64 65 66 67 68
		return NULL;

	size = end - start;

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

69
	fd = mkstemp(vdso_file->temp_file_name);
70 71 72 73
	if (fd < 0)
		goto out;

	if (size == (size_t) write(fd, buf, size))
74
		vdso = vdso_file->temp_file_name;
75 76 77 78 79 80

	close(fd);

 out:
	free(buf);

81 82
	vdso_file->found = (vdso != NULL);
	vdso_file->error = !vdso_file->found;
83 84 85
	return vdso;
}

86
void vdso__exit(struct machine *machine)
87
{
88 89 90 91 92
	struct vdso_info *vdso_info = machine->vdso_info;

	if (!vdso_info)
		return;

93 94
	if (vdso_info->vdso.found)
		unlink(vdso_info->vdso.temp_file_name);
95 96

	zfree(&machine->vdso_info);
97 98
}

A
Adrian Hunter 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112
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;
}

113 114
struct dso *vdso__dso_findnew(struct machine *machine,
			      struct thread *thread __maybe_unused)
115
{
116 117 118 119 120 121 122 123 124
	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;
125

126
	dso = dsos__find(&machine->user_dsos, DSO__NAME_VDSO, true);
127 128 129
	if (!dso) {
		char *file;

130
		file = get_file(&vdso_info->vdso);
131 132 133
		if (!file)
			return NULL;

134
		dso = vdso__new(machine, DSO__NAME_VDSO, file);
135 136 137 138
	}

	return dso;
}
139 140 141 142 143

bool dso__is_vdso(struct dso *dso)
{
	return !strcmp(dso->short_name, DSO__NAME_VDSO);
}