build-id.c 12.8 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * build-id.c
 *
 * build-id support
 *
 * Copyright (C) 2009, 2010 Red Hat Inc.
 * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
 */
9 10
#include "util.h"
#include <stdio.h>
11 12 13 14
#include "build-id.h"
#include "event.h"
#include "symbol.h"
#include <linux/kernel.h>
15
#include "debug.h"
16
#include "session.h"
17
#include "tool.h"
18 19
#include "header.h"
#include "vdso.h"
20

21 22 23

static bool no_buildid_cache;

24 25
int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused,
			   union perf_event *event,
26
			   struct perf_sample *sample,
27 28
			   struct perf_evsel *evsel __maybe_unused,
			   struct machine *machine)
29 30 31
{
	struct addr_location al;
	u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
32
	struct thread *thread = machine__findnew_thread(machine, sample->pid,
33
							sample->tid);
34 35 36 37 38 39 40

	if (thread == NULL) {
		pr_err("problem processing %d event, skipping it.\n",
			event->header.type);
		return -1;
	}

41
	thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, &al);
42 43 44 45

	if (al.map != NULL)
		al.map->dso->hit = 1;

46
	thread__put(thread);
47 48 49
	return 0;
}

50
static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused,
51
				       union perf_event *event,
52 53
				       struct perf_sample *sample
				       __maybe_unused,
54
				       struct machine *machine)
55
{
56 57 58
	struct thread *thread = machine__findnew_thread(machine,
							event->fork.pid,
							event->fork.tid);
59

60 61
	dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
		    event->fork.ppid, event->fork.ptid);
62

63
	if (thread) {
64
		machine__remove_thread(machine, thread);
65 66
		thread__put(thread);
	}
67 68 69 70

	return 0;
}

71
struct perf_tool build_id__mark_dso_hit_ops = {
72
	.sample	= build_id__mark_dso_hit,
73
	.mmap	= perf_event__process_mmap,
74
	.mmap2	= perf_event__process_mmap2,
75
	.fork	= perf_event__process_fork,
76
	.exit	= perf_event__exit_del_thread,
77 78
	.attr		 = perf_event__process_attr,
	.build_id	 = perf_event__process_build_id,
79
	.ordered_events	 = true,
80
};
81

82 83 84 85 86 87 88 89 90 91 92 93
int build_id__sprintf(const u8 *build_id, int len, char *bf)
{
	char *bid = bf;
	const u8 *raw = build_id;
	int i;

	for (i = 0; i < len; ++i) {
		sprintf(bid, "%02x", *raw);
		++raw;
		bid += 2;
	}

94
	return (bid - bf) + 1;
95 96
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id)
{
	char notes[PATH_MAX];
	u8 build_id[BUILD_ID_SIZE];
	int ret;

	if (!root_dir)
		root_dir = "";

	scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir);

	ret = sysfs__read_build_id(notes, build_id, sizeof(build_id));
	if (ret < 0)
		return ret;

	return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
}

int filename__sprintf_build_id(const char *pathname, char *sbuild_id)
{
	u8 build_id[BUILD_ID_SIZE];
	int ret;

	ret = filename__read_build_id(pathname, build_id, sizeof(build_id));
	if (ret < 0)
		return ret;
	else if (ret != sizeof(build_id))
		return -EINVAL;

	return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
/* asnprintf consolidates asprintf and snprintf */
static int asnprintf(char **strp, size_t size, const char *fmt, ...)
{
	va_list ap;
	int ret;

	if (!strp)
		return -EINVAL;

	va_start(ap, fmt);
	if (*strp)
		ret = vsnprintf(*strp, size, fmt, ap);
	else
		ret = vasprintf(strp, fmt, ap);
	va_end(ap);

	return ret;
}

static char *build_id__filename(const char *sbuild_id, char *bf, size_t size)
{
	char *tmp = bf;
	int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
			    sbuild_id, sbuild_id + 2);
	if (ret < 0 || (tmp && size < (unsigned int)ret))
		return NULL;
	return bf;
}

158
char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
159
{
160
	char build_id_hex[SBUILD_ID_SIZE];
161

162
	if (!dso->has_build_id)
163 164
		return NULL;

165
	build_id__sprintf(dso->build_id, sizeof(dso->build_id), build_id_hex);
166
	return build_id__filename(build_id_hex, bf, size);
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
bool dso__build_id_is_kmod(const struct dso *dso, char *bf, size_t size)
{
	char *id_name, *ch;
	struct stat sb;

	id_name = dso__build_id_filename(dso, bf, size);
	if (!id_name)
		goto err;
	if (access(id_name, F_OK))
		goto err;
	if (lstat(id_name, &sb) == -1)
		goto err;
	if ((size_t)sb.st_size > size - 1)
		goto err;
	if (readlink(id_name, bf, size - 1) < 0)
		goto err;

	bf[sb.st_size] = '\0';

	/*
	 * link should be:
	 * ../../lib/modules/4.4.0-rc4/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko/a09fe3eb3147dafa4e3b31dbd6257e4d696bdc92
	 */
	ch = strrchr(bf, '/');
	if (!ch)
		goto err;
	if (ch - 3 < bf)
		goto err;

	return strncmp(".ko", ch - 3, 3) == 0;
err:
	/*
	 * If dso__build_id_filename work, get id_name again,
	 * because id_name points to bf and is broken.
	 */
	if (id_name)
		id_name = dso__build_id_filename(dso, bf, size);
	pr_err("Invalid build id: %s\n", id_name ? :
					 dso->long_name ? :
					 dso->short_name ? :
					 "[unknown]");
	return false;
}

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
#define dsos__for_each_with_build_id(pos, head)	\
	list_for_each_entry(pos, head, node)	\
		if (!pos->has_build_id)		\
			continue;		\
		else

static int write_buildid(const char *name, size_t name_len, u8 *build_id,
			 pid_t pid, u16 misc, int fd)
{
	int err;
	struct build_id_event b;
	size_t len;

	len = name_len + 1;
	len = PERF_ALIGN(len, NAME_ALIGN);

	memset(&b, 0, sizeof(b));
	memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
	b.pid = pid;
	b.header.misc = misc;
	b.header.size = sizeof(b) + len;

	err = writen(fd, &b, sizeof(b));
	if (err < 0)
		return err;

	return write_padded(fd, name, name_len + 1, len);
}

242
static int machine__write_buildid_table(struct machine *machine, int fd)
243
{
244
	int err = 0;
245 246
	char nm[PATH_MAX];
	struct dso *pos;
247 248 249 250 251 252 253
	u16 kmisc = PERF_RECORD_MISC_KERNEL,
	    umisc = PERF_RECORD_MISC_USER;

	if (!machine__is_host(machine)) {
		kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
		umisc = PERF_RECORD_MISC_GUEST_USER;
	}
254

255
	dsos__for_each_with_build_id(pos, &machine->dsos.head) {
256 257
		const char *name;
		size_t name_len;
258
		bool in_kernel = false;
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

		if (!pos->hit)
			continue;

		if (dso__is_vdso(pos)) {
			name = pos->short_name;
			name_len = pos->short_name_len + 1;
		} else if (dso__is_kcore(pos)) {
			machine__mmap_name(machine, nm, sizeof(nm));
			name = nm;
			name_len = strlen(nm) + 1;
		} else {
			name = pos->long_name;
			name_len = pos->long_name_len + 1;
		}

275 276 277
		in_kernel = pos->kernel ||
				is_kernel_module(name,
					PERF_RECORD_MISC_CPUMODE_UNKNOWN);
278
		err = write_buildid(name, name_len, pos->build_id, machine->pid,
279
				    in_kernel ? kmisc : umisc, fd);
280
		if (err)
281
			break;
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	}

	return err;
}

int perf_session__write_buildid_table(struct perf_session *session, int fd)
{
	struct rb_node *nd;
	int err = machine__write_buildid_table(&session->machines.host, fd);

	if (err)
		return err;

	for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
		struct machine *pos = rb_entry(nd, struct machine, rb_node);
		err = machine__write_buildid_table(pos, fd);
		if (err)
			break;
	}
	return err;
}

static int __dsos__hit_all(struct list_head *head)
{
	struct dso *pos;

	list_for_each_entry(pos, head, node)
		pos->hit = true;

	return 0;
}

static int machine__hit_all_dsos(struct machine *machine)
{
316
	return __dsos__hit_all(&machine->dsos.head);
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
}

int dsos__hit_all(struct perf_session *session)
{
	struct rb_node *nd;
	int err;

	err = machine__hit_all_dsos(&session->machines.host);
	if (err)
		return err;

	for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
		struct machine *pos = rb_entry(nd, struct machine, rb_node);

		err = machine__hit_all_dsos(pos);
		if (err)
			return err;
	}

	return 0;
}

339 340 341 342 343
void disable_buildid_cache(void)
{
	no_buildid_cache = true;
}

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
static char *build_id_cache__dirname_from_path(const char *name,
					       bool is_kallsyms, bool is_vdso)
{
	char *realname = (char *)name, *filename;
	bool slash = is_kallsyms || is_vdso;

	if (!slash) {
		realname = realpath(name, NULL);
		if (!realname)
			return NULL;
	}

	if (asprintf(&filename, "%s%s%s", buildid_dir, slash ? "/" : "",
		     is_vdso ? DSO__NAME_VDSO : realname) < 0)
		filename = NULL;

	if (!slash)
		free(realname);

	return filename;
}

int build_id_cache__list_build_ids(const char *pathname,
				   struct strlist **result)
{
	struct strlist *list;
	char *dir_name;
	DIR *dir;
	struct dirent *d;
	int ret = 0;

375
	list = strlist__new(NULL, NULL);
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
	dir_name = build_id_cache__dirname_from_path(pathname, false, false);
	if (!list || !dir_name) {
		ret = -ENOMEM;
		goto out;
	}

	/* List up all dirents */
	dir = opendir(dir_name);
	if (!dir) {
		ret = -errno;
		goto out;
	}

	while ((d = readdir(dir)) != NULL) {
		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
			continue;
		strlist__add(list, d->d_name);
	}
	closedir(dir);

out:
	free(dir_name);
	if (ret)
		strlist__delete(list);
	else
		*result = list;

	return ret;
}

406 407
int build_id_cache__add_s(const char *sbuild_id, const char *name,
			  bool is_kallsyms, bool is_vdso)
408 409
{
	const size_t size = PATH_MAX;
410
	char *realname = NULL, *filename = NULL, *dir_name = NULL,
411
	     *linkname = zalloc(size), *targetname, *tmp;
412
	int err = -1;
413

414
	if (!is_kallsyms) {
415
		realname = realpath(name, NULL);
416 417 418
		if (!realname)
			goto out_free;
	}
419

420 421
	dir_name = build_id_cache__dirname_from_path(name, is_kallsyms, is_vdso);
	if (!dir_name)
422 423
		goto out_free;

424
	if (mkdir_p(dir_name, 0755))
425 426
		goto out_free;

427 428 429 430
	if (asprintf(&filename, "%s/%s", dir_name, sbuild_id) < 0) {
		filename = NULL;
		goto out_free;
	}
431 432 433 434 435

	if (access(filename, F_OK)) {
		if (is_kallsyms) {
			 if (copyfile("/proc/kallsyms", filename))
				goto out_free;
436 437
		} else if (link(realname, filename) && errno != EEXIST &&
				copyfile(name, filename))
438 439 440
			goto out_free;
	}

441 442 443 444
	if (!build_id__filename(sbuild_id, linkname, size))
		goto out_free;
	tmp = strrchr(linkname, '/');
	*tmp = '\0';
445 446 447 448

	if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
		goto out_free;

449
	*tmp = '/';
450
	targetname = filename + strlen(buildid_dir) - 5;
451 452 453 454 455 456 457 458
	memcpy(targetname, "../..", 5);

	if (symlink(targetname, linkname) == 0)
		err = 0;
out_free:
	if (!is_kallsyms)
		free(realname);
	free(filename);
459
	free(dir_name);
460 461 462 463 464
	free(linkname);
	return err;
}

static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
465 466
				 const char *name, bool is_kallsyms,
				 bool is_vdso)
467
{
468
	char sbuild_id[SBUILD_ID_SIZE];
469 470 471

	build_id__sprintf(build_id, build_id_size, sbuild_id);

472
	return build_id_cache__add_s(sbuild_id, name, is_kallsyms, is_vdso);
473 474
}

475 476 477 478 479 480 481 482 483 484 485 486
bool build_id_cache__cached(const char *sbuild_id)
{
	bool ret = false;
	char *filename = build_id__filename(sbuild_id, NULL, 0);

	if (filename && !access(filename, F_OK))
		ret = true;
	free(filename);

	return ret;
}

487
int build_id_cache__remove_s(const char *sbuild_id)
488 489 490
{
	const size_t size = PATH_MAX;
	char *filename = zalloc(size),
491
	     *linkname = zalloc(size), *tmp;
492 493 494 495 496
	int err = -1;

	if (filename == NULL || linkname == NULL)
		goto out_free;

497 498
	if (!build_id__filename(sbuild_id, linkname, size))
		goto out_free;
499 500 501 502 503 504 505 506 507 508 509 510 511

	if (access(linkname, F_OK))
		goto out_free;

	if (readlink(linkname, filename, size - 1) < 0)
		goto out_free;

	if (unlink(linkname))
		goto out_free;

	/*
	 * Since the link is relative, we must make it absolute:
	 */
512 513
	tmp = strrchr(linkname, '/') + 1;
	snprintf(tmp, size - (tmp - linkname), "%s", filename);
514 515 516 517 518 519 520 521 522 523 524

	if (unlink(linkname))
		goto out_free;

	err = 0;
out_free:
	free(filename);
	free(linkname);
	return err;
}

525
static int dso__cache_build_id(struct dso *dso, struct machine *machine)
526 527 528 529 530 531 532 533 534 535 536 537
{
	bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
	bool is_vdso = dso__is_vdso(dso);
	const char *name = dso->long_name;
	char nm[PATH_MAX];

	if (dso__is_kcore(dso)) {
		is_kallsyms = true;
		machine__mmap_name(machine, nm, sizeof(nm));
		name = nm;
	}
	return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
538
				     is_kallsyms, is_vdso);
539 540 541
}

static int __dsos__cache_build_ids(struct list_head *head,
542
				   struct machine *machine)
543 544 545 546 547
{
	struct dso *pos;
	int err = 0;

	dsos__for_each_with_build_id(pos, head)
548
		if (dso__cache_build_id(pos, machine))
549 550 551 552 553
			err = -1;

	return err;
}

554
static int machine__cache_build_ids(struct machine *machine)
555
{
556
	return __dsos__cache_build_ids(&machine->dsos.head, machine);
557 558 559 560 561 562 563
}

int perf_session__cache_build_ids(struct perf_session *session)
{
	struct rb_node *nd;
	int ret;

564 565 566
	if (no_buildid_cache)
		return 0;

567
	if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
568 569
		return -1;

570
	ret = machine__cache_build_ids(&session->machines.host);
571 572 573

	for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
		struct machine *pos = rb_entry(nd, struct machine, rb_node);
574
		ret |= machine__cache_build_ids(pos);
575 576 577 578 579 580
	}
	return ret ? -1 : 0;
}

static bool machine__read_build_ids(struct machine *machine, bool with_hits)
{
581
	return __dsos__read_build_ids(&machine->dsos.head, with_hits);
582 583 584 585 586 587 588 589 590 591 592 593 594 595
}

bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
{
	struct rb_node *nd;
	bool ret = machine__read_build_ids(&session->machines.host, with_hits);

	for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
		struct machine *pos = rb_entry(nd, struct machine, rb_node);
		ret |= machine__read_build_ids(pos, with_hits);
	}

	return ret;
}