event.c 27.3 KB
Newer Older
1
#include <linux/types.h>
2
#include <sys/mman.h>
3 4
#include "event.h"
#include "debug.h"
5
#include "hist.h"
6
#include "machine.h"
7
#include "sort.h"
8
#include "string.h"
9
#include "strlist.h"
10
#include "thread.h"
11
#include "thread_map.h"
12
#include "symbol/kallsyms.h"
13

14
static const char *perf_event__names[] = {
15 16
	[0]					= "TOTAL",
	[PERF_RECORD_MMAP]			= "MMAP",
17
	[PERF_RECORD_MMAP2]			= "MMAP2",
18 19 20 21 22 23 24 25
	[PERF_RECORD_LOST]			= "LOST",
	[PERF_RECORD_COMM]			= "COMM",
	[PERF_RECORD_EXIT]			= "EXIT",
	[PERF_RECORD_THROTTLE]			= "THROTTLE",
	[PERF_RECORD_UNTHROTTLE]		= "UNTHROTTLE",
	[PERF_RECORD_FORK]			= "FORK",
	[PERF_RECORD_READ]			= "READ",
	[PERF_RECORD_SAMPLE]			= "SAMPLE",
26
	[PERF_RECORD_AUX]			= "AUX",
27
	[PERF_RECORD_ITRACE_START]		= "ITRACE_START",
28
	[PERF_RECORD_LOST_SAMPLES]		= "LOST_SAMPLES",
29 30 31 32 33
	[PERF_RECORD_HEADER_ATTR]		= "ATTR",
	[PERF_RECORD_HEADER_EVENT_TYPE]		= "EVENT_TYPE",
	[PERF_RECORD_HEADER_TRACING_DATA]	= "TRACING_DATA",
	[PERF_RECORD_HEADER_BUILD_ID]		= "BUILD_ID",
	[PERF_RECORD_FINISHED_ROUND]		= "FINISHED_ROUND",
A
Adrian Hunter 已提交
34
	[PERF_RECORD_ID_INDEX]			= "ID_INDEX",
35 36
	[PERF_RECORD_AUXTRACE_INFO]		= "AUXTRACE_INFO",
	[PERF_RECORD_AUXTRACE]			= "AUXTRACE",
37
	[PERF_RECORD_AUXTRACE_ERROR]		= "AUXTRACE_ERROR",
38 39
};

40
const char *perf_event__name(unsigned int id)
41
{
42
	if (id >= ARRAY_SIZE(perf_event__names))
43
		return "INVALID";
44
	if (!perf_event__names[id])
45
		return "UNKNOWN";
46
	return perf_event__names[id];
47 48
}

49
static struct perf_sample synth_sample = {
50 51 52 53 54 55 56 57
	.pid	   = -1,
	.tid	   = -1,
	.time	   = -1,
	.stream_id = -1,
	.cpu	   = -1,
	.period	   = 1,
};

58 59
/*
 * Assumes that the first 4095 bytes of /proc/pid/stat contains
60
 * the comm, tgid and ppid.
61
 */
62 63
static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
				    pid_t *tgid, pid_t *ppid)
64 65
{
	char filename[PATH_MAX];
66 67 68
	char bf[4096];
	int fd;
	size_t size = 0, n;
69 70 71 72
	char *nl, *name, *tgids, *ppids;

	*tgid = -1;
	*ppid = -1;
73 74 75

	snprintf(filename, sizeof(filename), "/proc/%d/status", pid);

76 77
	fd = open(filename, O_RDONLY);
	if (fd < 0) {
78
		pr_debug("couldn't open %s\n", filename);
79
		return -1;
80 81
	}

82 83 84
	n = read(fd, bf, sizeof(bf) - 1);
	close(fd);
	if (n <= 0) {
85
		pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
86 87
			   pid);
		return -1;
88
	}
89
	bf[n] = '\0';
90

91 92
	name = strstr(bf, "Name:");
	tgids = strstr(bf, "Tgid:");
93
	ppids = strstr(bf, "PPid:");
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

	if (name) {
		name += 5;  /* strlen("Name:") */

		while (*name && isspace(*name))
			++name;

		nl = strchr(name, '\n');
		if (nl)
			*nl = '\0';

		size = strlen(name);
		if (size >= len)
			size = len - 1;
		memcpy(comm, name, size);
		comm[size] = '\0';
	} else {
		pr_debug("Name: string not found for pid %d\n", pid);
	}

	if (tgids) {
		tgids += 5;  /* strlen("Tgid:") */
116
		*tgid = atoi(tgids);
117 118 119
	} else {
		pr_debug("Tgid: string not found for pid %d\n", pid);
	}
120

121 122 123 124 125 126 127 128
	if (ppids) {
		ppids += 5;  /* strlen("PPid:") */
		*ppid = atoi(ppids);
	} else {
		pr_debug("PPid: string not found for pid %d\n", pid);
	}

	return 0;
129 130
}

131 132 133
static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
				    struct machine *machine,
				    pid_t *tgid, pid_t *ppid)
134 135
{
	size_t size;
136 137

	*ppid = -1;
138 139 140

	memset(&event->comm, 0, sizeof(event->comm));

141 142 143 144 145 146 147 148 149
	if (machine__is_host(machine)) {
		if (perf_event__get_comm_ids(pid, event->comm.comm,
					     sizeof(event->comm.comm),
					     tgid, ppid) != 0) {
			return -1;
		}
	} else {
		*tgid = machine->pid;
	}
150

151 152
	if (*tgid < 0)
		return -1;
153

154
	event->comm.pid = *tgid;
155
	event->comm.header.type = PERF_RECORD_COMM;
156 157

	size = strlen(event->comm.comm) + 1;
158
	size = PERF_ALIGN(size, sizeof(u64));
159
	memset(event->comm.comm + size, 0, machine->id_hdr_size);
160 161
	event->comm.header.size = (sizeof(event->comm) -
				(sizeof(event->comm.comm) - size) +
162
				machine->id_hdr_size);
163
	event->comm.tid = pid;
164 165

	return 0;
166 167 168 169 170 171 172
}

static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
					 union perf_event *event, pid_t pid,
					 perf_event__handler_t process,
					 struct machine *machine)
{
173
	pid_t tgid, ppid;
174

175 176
	if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
		return -1;
177

178 179
	if (process(tool, event, &synth_sample, machine) != 0)
		return -1;
180

181
	return tgid;
182 183
}

184
static int perf_event__synthesize_fork(struct perf_tool *tool,
185 186 187
				       union perf_event *event,
				       pid_t pid, pid_t tgid, pid_t ppid,
				       perf_event__handler_t process,
188 189 190 191
				       struct machine *machine)
{
	memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);

192 193 194 195 196 197 198 199 200 201 202 203
	/*
	 * for main thread set parent to ppid from status file. For other
	 * threads set parent pid to main thread. ie., assume main thread
	 * spawns all threads in a process
	*/
	if (tgid == pid) {
		event->fork.ppid = ppid;
		event->fork.ptid = ppid;
	} else {
		event->fork.ppid = tgid;
		event->fork.ptid = tgid;
	}
204 205 206 207 208 209 210 211 212 213 214 215
	event->fork.pid  = tgid;
	event->fork.tid  = pid;
	event->fork.header.type = PERF_RECORD_FORK;

	event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);

	if (process(tool, event, &synth_sample, machine) != 0)
		return -1;

	return 0;
}

216 217 218 219 220
int perf_event__synthesize_mmap_events(struct perf_tool *tool,
				       union perf_event *event,
				       pid_t pid, pid_t tgid,
				       perf_event__handler_t process,
				       struct machine *machine,
221 222
				       bool mmap_data,
				       unsigned int proc_map_timeout)
223 224 225
{
	char filename[PATH_MAX];
	FILE *fp;
226 227
	unsigned long long t;
	bool truncation = false;
228
	unsigned long long timeout = proc_map_timeout * 1000000ULL;
229
	int rc = 0;
230

231 232 233
	if (machine__is_default_guest(machine))
		return 0;

234 235
	snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
		 machine->root_dir, pid);
236 237 238 239 240 241 242 243 244 245

	fp = fopen(filename, "r");
	if (fp == NULL) {
		/*
		 * We raced with a task exiting - just return:
		 */
		pr_debug("couldn't open %s\n", filename);
		return -1;
	}

246
	event->header.type = PERF_RECORD_MMAP2;
247
	t = rdclock();
248

249
	while (1) {
250 251 252 253
		char bf[BUFSIZ];
		char prot[5];
		char execname[PATH_MAX];
		char anonstr[] = "//anon";
254
		unsigned int ino;
255
		size_t size;
256
		ssize_t n;
257

258 259 260
		if (fgets(bf, sizeof(bf), fp) == NULL)
			break;

261 262 263 264 265
		if ((rdclock() - t) > timeout) {
			pr_warning("Reading %s time out. "
				   "You may want to increase "
				   "the time limit by --proc-map-timeout\n",
				   filename);
266 267 268 269
			truncation = true;
			goto out;
		}

270 271 272
		/* ensure null termination since stack will be reused. */
		strcpy(execname, "");

273
		/* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
274 275 276 277 278 279
		n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n",
		       &event->mmap2.start, &event->mmap2.len, prot,
		       &event->mmap2.pgoff, &event->mmap2.maj,
		       &event->mmap2.min,
		       &ino, execname);

280 281 282
		/*
 		 * Anon maps don't have the execname.
 		 */
283
		if (n < 7)
284
			continue;
285 286 287

		event->mmap2.ino = (u64)ino;

288 289 290
		/*
		 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
		 */
291 292 293 294
		if (machine__is_host(machine))
			event->header.misc = PERF_RECORD_MISC_USER;
		else
			event->header.misc = PERF_RECORD_MISC_GUEST_USER;
295

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
		/* map protection and flags bits */
		event->mmap2.prot = 0;
		event->mmap2.flags = 0;
		if (prot[0] == 'r')
			event->mmap2.prot |= PROT_READ;
		if (prot[1] == 'w')
			event->mmap2.prot |= PROT_WRITE;
		if (prot[2] == 'x')
			event->mmap2.prot |= PROT_EXEC;

		if (prot[3] == 's')
			event->mmap2.flags |= MAP_SHARED;
		else
			event->mmap2.flags |= MAP_PRIVATE;

311 312 313 314 315 316
		if (prot[2] != 'x') {
			if (!mmap_data || prot[0] != 'r')
				continue;

			event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
		}
317

318 319 320 321
out:
		if (truncation)
			event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;

322 323 324 325
		if (!strcmp(execname, ""))
			strcpy(execname, anonstr);

		size = strlen(execname) + 1;
326
		memcpy(event->mmap2.filename, execname, size);
327
		size = PERF_ALIGN(size, sizeof(u64));
328 329 330 331 332 333 334
		event->mmap2.len -= event->mmap.start;
		event->mmap2.header.size = (sizeof(event->mmap2) -
					(sizeof(event->mmap2.filename) - size));
		memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
		event->mmap2.header.size += machine->id_hdr_size;
		event->mmap2.pid = tgid;
		event->mmap2.tid = pid;
335 336 337 338

		if (process(tool, event, &synth_sample, machine) != 0) {
			rc = -1;
			break;
339
		}
340 341 342

		if (truncation)
			break;
343 344 345
	}

	fclose(fp);
346
	return rc;
347 348
}

349
int perf_event__synthesize_modules(struct perf_tool *tool,
350
				   perf_event__handler_t process,
351
				   struct machine *machine)
352
{
353
	int rc = 0;
354
	struct map *pos;
355
	struct map_groups *kmaps = &machine->kmaps;
356
	struct maps *maps = &kmaps->maps[MAP__FUNCTION];
357
	union perf_event *event = zalloc((sizeof(event->mmap) +
358
					  machine->id_hdr_size));
359 360 361 362 363 364 365
	if (event == NULL) {
		pr_debug("Not enough memory synthesizing mmap event "
			 "for kernel modules\n");
		return -1;
	}

	event->header.type = PERF_RECORD_MMAP;
366

367 368 369 370
	/*
	 * kernel uses 0 for user space maps, see kernel/perf_event.c
	 * __perf_event_mmap
	 */
371
	if (machine__is_host(machine))
372
		event->header.misc = PERF_RECORD_MISC_KERNEL;
373
	else
374
		event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
375

376
	for (pos = maps__first(maps); pos; pos = map__next(pos)) {
377 378 379 380 381
		size_t size;

		if (pos->dso->kernel)
			continue;

382
		size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
383 384 385
		event->mmap.header.type = PERF_RECORD_MMAP;
		event->mmap.header.size = (sizeof(event->mmap) -
				        (sizeof(event->mmap.filename) - size));
386 387
		memset(event->mmap.filename + size, 0, machine->id_hdr_size);
		event->mmap.header.size += machine->id_hdr_size;
388 389 390 391 392
		event->mmap.start = pos->start;
		event->mmap.len   = pos->end - pos->start;
		event->mmap.pid   = machine->pid;

		memcpy(event->mmap.filename, pos->dso->long_name,
393
		       pos->dso->long_name_len + 1);
394 395 396 397
		if (process(tool, event, &synth_sample, machine) != 0) {
			rc = -1;
			break;
		}
398 399
	}

400
	free(event);
401
	return rc;
402 403
}

404 405
static int __event__synthesize_thread(union perf_event *comm_event,
				      union perf_event *mmap_event,
406
				      union perf_event *fork_event,
407 408
				      pid_t pid, int full,
					  perf_event__handler_t process,
409
				      struct perf_tool *tool,
410 411 412
				      struct machine *machine,
				      bool mmap_data,
				      unsigned int proc_map_timeout)
413
{
414 415 416
	char filename[PATH_MAX];
	DIR *tasks;
	struct dirent dirent, *next;
417
	pid_t tgid, ppid;
418
	int rc = 0;
419 420 421 422 423 424 425 426 427 428

	/* special case: only send one comm event using passed in pid */
	if (!full) {
		tgid = perf_event__synthesize_comm(tool, comm_event, pid,
						   process, machine);

		if (tgid == -1)
			return -1;

		return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
429 430
							  process, machine, mmap_data,
							  proc_map_timeout);
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	}

	if (machine__is_default_guest(machine))
		return 0;

	snprintf(filename, sizeof(filename), "%s/proc/%d/task",
		 machine->root_dir, pid);

	tasks = opendir(filename);
	if (tasks == NULL) {
		pr_debug("couldn't open %s\n", filename);
		return 0;
	}

	while (!readdir_r(tasks, &dirent, &next) && next) {
		char *end;
		pid_t _pid;

		_pid = strtol(dirent.d_name, &end, 10);
		if (*end)
			continue;

453
		rc = -1;
454 455
		if (perf_event__prepare_comm(comm_event, _pid, machine,
					     &tgid, &ppid) != 0)
456
			break;
457

458
		if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
459
						ppid, process, machine) < 0)
460
			break;
461 462 463 464
		/*
		 * Send the prepared comm event
		 */
		if (process(tool, comm_event, &synth_sample, machine) != 0)
465
			break;
466

467
		rc = 0;
468 469 470
		if (_pid == pid) {
			/* process the parent's maps too */
			rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
471
						process, machine, mmap_data, proc_map_timeout);
472 473
			if (rc)
				break;
474
		}
475 476 477
	}

	closedir(tasks);
478
	return rc;
479 480
}

481
int perf_event__synthesize_thread_map(struct perf_tool *tool,
482
				      struct thread_map *threads,
483
				      perf_event__handler_t process,
484
				      struct machine *machine,
485 486
				      bool mmap_data,
				      unsigned int proc_map_timeout)
487
{
488
	union perf_event *comm_event, *mmap_event, *fork_event;
489
	int err = -1, thread, j;
490

491
	comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
492 493 494
	if (comm_event == NULL)
		goto out;

495
	mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
496 497 498
	if (mmap_event == NULL)
		goto out_free_comm;

499 500 501 502
	fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
	if (fork_event == NULL)
		goto out_free_mmap;

503 504 505
	err = 0;
	for (thread = 0; thread < threads->nr; ++thread) {
		if (__event__synthesize_thread(comm_event, mmap_event,
506
					       fork_event,
507
					       threads->map[thread], 0,
508
					       process, tool, machine,
509
					       mmap_data, proc_map_timeout)) {
510 511 512
			err = -1;
			break;
		}
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

		/*
		 * comm.pid is set to thread group id by
		 * perf_event__synthesize_comm
		 */
		if ((int) comm_event->comm.pid != threads->map[thread]) {
			bool need_leader = true;

			/* is thread group leader in thread_map? */
			for (j = 0; j < threads->nr; ++j) {
				if ((int) comm_event->comm.pid == threads->map[j]) {
					need_leader = false;
					break;
				}
			}

			/* if not, generate events for it */
			if (need_leader &&
531
			    __event__synthesize_thread(comm_event, mmap_event,
532
						       fork_event,
533 534
						       comm_event->comm.pid, 0,
						       process, tool, machine,
535
						       mmap_data, proc_map_timeout)) {
536 537 538 539
				err = -1;
				break;
			}
		}
540
	}
541 542
	free(fork_event);
out_free_mmap:
543 544 545 546 547 548 549
	free(mmap_event);
out_free_comm:
	free(comm_event);
out:
	return err;
}

550
int perf_event__synthesize_threads(struct perf_tool *tool,
551
				   perf_event__handler_t process,
552 553 554
				   struct machine *machine,
				   bool mmap_data,
				   unsigned int proc_map_timeout)
555 556
{
	DIR *proc;
557
	char proc_path[PATH_MAX];
558
	struct dirent dirent, *next;
559
	union perf_event *comm_event, *mmap_event, *fork_event;
560 561
	int err = -1;

562 563 564
	if (machine__is_default_guest(machine))
		return 0;

565
	comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
566 567 568
	if (comm_event == NULL)
		goto out;

569
	mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
570 571
	if (mmap_event == NULL)
		goto out_free_comm;
572

573 574 575 576
	fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
	if (fork_event == NULL)
		goto out_free_mmap;

577 578 579
	snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
	proc = opendir(proc_path);

580
	if (proc == NULL)
581
		goto out_free_fork;
582 583 584 585 586 587 588

	while (!readdir_r(proc, &dirent, &next) && next) {
		char *end;
		pid_t pid = strtol(dirent.d_name, &end, 10);

		if (*end) /* only interested in proper numerical dirents */
			continue;
589 590 591 592
		/*
 		 * We may race with exiting thread, so don't stop just because
 		 * one thread couldn't be synthesized.
 		 */
593
		__event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
594 595
					   1, process, tool, machine, mmap_data,
					   proc_map_timeout);
596 597
	}

598
	err = 0;
599
	closedir(proc);
600 601
out_free_fork:
	free(fork_event);
602 603 604 605 606 607
out_free_mmap:
	free(mmap_event);
out_free_comm:
	free(comm_event);
out:
	return err;
608
}
609

610 611 612 613 614
struct process_symbol_args {
	const char *name;
	u64	   start;
};

615
static int find_symbol_cb(void *arg, const char *name, char type,
616
			  u64 start)
617 618 619
{
	struct process_symbol_args *args = arg;

620 621 622 623 624 625
	/*
	 * Must be a function or at least an alias, as in PARISC64, where "_text" is
	 * an 'A' to the same address as "_stext".
	 */
	if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
	      type == 'A') || strcmp(name, args->name))
626 627 628 629 630 631
		return 0;

	args->start = start;
	return 1;
}

632 633 634 635 636 637 638 639 640 641 642
u64 kallsyms__get_function_start(const char *kallsyms_filename,
				 const char *symbol_name)
{
	struct process_symbol_args args = { .name = symbol_name, };

	if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
		return 0;

	return args.start;
}

643
int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
644
				       perf_event__handler_t process,
645
				       struct machine *machine)
646 647
{
	size_t size;
648
	const char *mmap_name;
649 650
	char name_buff[PATH_MAX];
	struct map *map;
651
	struct kmap *kmap;
652
	int err;
653 654 655 656 657
	union perf_event *event;

	if (machine->vmlinux_maps[0] == NULL)
		return -1;

658 659 660 661 662
	/*
	 * We should get this from /sys/kernel/sections/.text, but till that is
	 * available use this, and after it is use this as a fallback for older
	 * kernels.
	 */
663
	event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
664 665 666 667 668
	if (event == NULL) {
		pr_debug("Not enough memory synthesizing mmap event "
			 "for kernel modules\n");
		return -1;
	}
669

670
	mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
671
	if (machine__is_host(machine)) {
672 673 674 675
		/*
		 * kernel uses PERF_RECORD_MISC_USER for user space maps,
		 * see kernel/perf_event.c __perf_event_mmap
		 */
676
		event->header.misc = PERF_RECORD_MISC_KERNEL;
677
	} else {
678
		event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
679
	}
680

681
	map = machine->vmlinux_maps[MAP__FUNCTION];
682
	kmap = map__kmap(map);
683
	size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
684
			"%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
685
	size = PERF_ALIGN(size, sizeof(u64));
686 687
	event->mmap.header.type = PERF_RECORD_MMAP;
	event->mmap.header.size = (sizeof(event->mmap) -
688
			(sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
689
	event->mmap.pgoff = kmap->ref_reloc_sym->addr;
690 691 692 693
	event->mmap.start = map->start;
	event->mmap.len   = map->end - event->mmap.start;
	event->mmap.pid   = machine->pid;

694
	err = process(tool, event, &synth_sample, machine);
695 696 697
	free(event);

	return err;
698 699
}

700 701
size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
{
702 703 704 705 706 707 708
	const char *s;

	if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
		s = " exec";
	else
		s = "";

709
	return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
710 711
}

712
int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
713
			     union perf_event *event,
714
			     struct perf_sample *sample,
715
			     struct machine *machine)
716
{
717
	return machine__process_comm_event(machine, event, sample);
718 719
}

720
int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
721
			     union perf_event *event,
722
			     struct perf_sample *sample,
723
			     struct machine *machine)
724
{
725
	return machine__process_lost_event(machine, event, sample);
726
}
727

728 729 730 731 732 733 734 735
int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
			    union perf_event *event,
			    struct perf_sample *sample __maybe_unused,
			    struct machine *machine)
{
	return machine__process_aux_event(machine, event);
}

736 737 738 739 740 741 742 743
int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
				     union perf_event *event,
				     struct perf_sample *sample __maybe_unused,
				     struct machine *machine)
{
	return machine__process_itrace_start_event(machine, event);
}

744 745 746 747 748 749 750 751
int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
				     union perf_event *event,
				     struct perf_sample *sample,
				     struct machine *machine)
{
	return machine__process_lost_samples_event(machine, event, sample);
}

752 753
size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
{
754
	return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
755
		       event->mmap.pid, event->mmap.tid, event->mmap.start,
756 757 758
		       event->mmap.len, event->mmap.pgoff,
		       (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
		       event->mmap.filename);
759 760
}

761 762 763
size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
{
	return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
764
			   " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
765 766 767 768
		       event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
		       event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
		       event->mmap2.min, event->mmap2.ino,
		       event->mmap2.ino_generation,
769 770 771 772
		       (event->mmap2.prot & PROT_READ) ? 'r' : '-',
		       (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
		       (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
		       (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
773 774 775
		       event->mmap2.filename);
}

776
int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
777
			     union perf_event *event,
778
			     struct perf_sample *sample,
779
			     struct machine *machine)
780
{
781
	return machine__process_mmap_event(machine, event, sample);
782 783
}

784 785
int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
			     union perf_event *event,
786
			     struct perf_sample *sample,
787 788
			     struct machine *machine)
{
789
	return machine__process_mmap2_event(machine, event, sample);
790 791
}

792 793 794 795 796 797 798
size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
{
	return fprintf(fp, "(%d:%d):(%d:%d)\n",
		       event->fork.pid, event->fork.tid,
		       event->fork.ppid, event->fork.ptid);
}

799
int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
800
			     union perf_event *event,
801
			     struct perf_sample *sample,
802
			     struct machine *machine)
803
{
804
	return machine__process_fork_event(machine, event, sample);
805
}
806

807 808
int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
			     union perf_event *event,
809
			     struct perf_sample *sample,
810 811
			     struct machine *machine)
{
812
	return machine__process_exit_event(machine, event, sample);
813 814
}

815 816 817 818 819 820 821 822 823
size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
{
	return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
		       event->aux.aux_offset, event->aux.aux_size,
		       event->aux.flags,
		       event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
		       event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
}

824 825 826 827 828 829
size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
{
	return fprintf(fp, " pid: %u tid: %u\n",
		       event->itrace_start.pid, event->itrace_start.tid);
}

830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
size_t perf_event__fprintf(union perf_event *event, FILE *fp)
{
	size_t ret = fprintf(fp, "PERF_RECORD_%s",
			     perf_event__name(event->header.type));

	switch (event->header.type) {
	case PERF_RECORD_COMM:
		ret += perf_event__fprintf_comm(event, fp);
		break;
	case PERF_RECORD_FORK:
	case PERF_RECORD_EXIT:
		ret += perf_event__fprintf_task(event, fp);
		break;
	case PERF_RECORD_MMAP:
		ret += perf_event__fprintf_mmap(event, fp);
		break;
846 847 848
	case PERF_RECORD_MMAP2:
		ret += perf_event__fprintf_mmap2(event, fp);
		break;
849 850 851
	case PERF_RECORD_AUX:
		ret += perf_event__fprintf_aux(event, fp);
		break;
852 853 854
	case PERF_RECORD_ITRACE_START:
		ret += perf_event__fprintf_itrace_start(event, fp);
		break;
855 856 857 858 859 860 861
	default:
		ret += fprintf(fp, "\n");
	}

	return ret;
}

862 863
int perf_event__process(struct perf_tool *tool __maybe_unused,
			union perf_event *event,
864
			struct perf_sample *sample,
865
			struct machine *machine)
866
{
867
	return machine__process_event(machine, event, sample);
868 869
}

870
void thread__find_addr_map(struct thread *thread, u8 cpumode,
871
			   enum map_type type, u64 addr,
872
			   struct addr_location *al)
873
{
874
	struct map_groups *mg = thread->mg;
875
	struct machine *machine = mg->machine;
876
	bool load_map = false;
877

878
	al->machine = machine;
879
	al->thread = thread;
880
	al->addr = addr;
881
	al->cpumode = cpumode;
882
	al->filtered = 0;
883

884 885 886 887 888
	if (machine == NULL) {
		al->map = NULL;
		return;
	}

889
	if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
890
		al->level = 'k';
891
		mg = &machine->kmaps;
892
		load_map = true;
893
	} else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
894
		al->level = '.';
895 896
	} else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
		al->level = 'g';
897
		mg = &machine->kmaps;
898
		load_map = true;
899 900
	} else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
		al->level = 'u';
901
	} else {
902
		al->level = 'H';
903
		al->map = NULL;
904 905 906 907

		if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
			cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
			!perf_guest)
908
			al->filtered |= (1 << HIST_FILTER__GUEST);
909 910 911
		if ((cpumode == PERF_RECORD_MISC_USER ||
			cpumode == PERF_RECORD_MISC_KERNEL) &&
			!perf_host)
912
			al->filtered |= (1 << HIST_FILTER__HOST);
913

914 915 916
		return;
	}
try_again:
917
	al->map = map_groups__find(mg, type, al->addr);
918 919 920 921 922 923 924 925 926 927
	if (al->map == NULL) {
		/*
		 * If this is outside of all known maps, and is a negative
		 * address, try to look it up in the kernel dso, as it might be
		 * a vsyscall or vdso (which executes in user-mode).
		 *
		 * XXX This is nasty, we should have a symbol list in the
		 * "[vdso]" dso, but for now lets use the old trick of looking
		 * in the whole kernel symbol list.
		 */
928 929 930
		if (cpumode == PERF_RECORD_MISC_USER && machine &&
		    mg != &machine->kmaps &&
		    machine__kernel_ip(machine, al->addr)) {
931
			mg = &machine->kmaps;
932
			load_map = true;
933 934
			goto try_again;
		}
935 936 937 938 939 940
	} else {
		/*
		 * Kernel maps might be changed when loading symbols so loading
		 * must be done prior to using kernel maps.
		 */
		if (load_map)
941
			map__load(al->map, machine->symbol_filter);
942
		al->addr = al->map->map_ip(al->map, al->addr);
943
	}
944 945
}

946
void thread__find_addr_location(struct thread *thread,
947
				u8 cpumode, enum map_type type, u64 addr,
948
				struct addr_location *al)
949
{
950
	thread__find_addr_map(thread, cpumode, type, addr, al);
951
	if (al->map != NULL)
952
		al->sym = map__find_symbol(al->map, al->addr,
953
					   thread->mg->machine->symbol_filter);
954 955
	else
		al->sym = NULL;
956 957
}

958 959 960 961
/*
 * Callers need to drop the reference to al->thread, obtained in
 * machine__findnew_thread()
 */
962
int perf_event__preprocess_sample(const union perf_event *event,
963
				  struct machine *machine,
964
				  struct addr_location *al,
965
				  struct perf_sample *sample)
966
{
967
	u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
968
	struct thread *thread = machine__findnew_thread(machine, sample->pid,
969
							sample->tid);
970

971 972 973
	if (thread == NULL)
		return -1;

974
	dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
975
	/*
976
	 * Have we already created the kernel maps for this machine?
977 978 979 980 981 982
	 *
	 * This should have happened earlier, when we processed the kernel MMAP
	 * events, but for older perf.data files there was no such thing, so do
	 * it now.
	 */
	if (cpumode == PERF_RECORD_MISC_KERNEL &&
983 984
	    machine->vmlinux_maps[MAP__FUNCTION] == NULL)
		machine__create_kernel_maps(machine);
985

986
	thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al);
987 988 989
	dump_printf(" ...... dso: %s\n",
		    al->map ? al->map->dso->long_name :
			al->level == 'H' ? "[hypervisor]" : "<not found>");
990 991 992 993

	if (thread__is_filtered(thread))
		al->filtered |= (1 << HIST_FILTER__THREAD);

994
	al->sym = NULL;
995
	al->cpu = sample->cpu;
996 997

	if (al->map) {
998 999
		struct dso *dso = al->map->dso;

1000
		if (symbol_conf.dso_list &&
1001 1002 1003 1004
		    (!dso || !(strlist__has_entry(symbol_conf.dso_list,
						  dso->short_name) ||
			       (dso->short_name != dso->long_name &&
				strlist__has_entry(symbol_conf.dso_list,
1005 1006 1007
						   dso->long_name))))) {
			al->filtered |= (1 << HIST_FILTER__DSO);
		}
1008

1009 1010
		al->sym = map__find_symbol(al->map, al->addr,
					   machine->symbol_filter);
1011
	}
1012

1013 1014
	if (symbol_conf.sym_list &&
		(!al->sym || !strlist__has_entry(symbol_conf.sym_list,
1015 1016 1017
						al->sym->name))) {
		al->filtered |= (1 << HIST_FILTER__SYMBOL);
	}
1018 1019

	return 0;
1020
}
1021

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
/*
 * The preprocess_sample method will return with reference counts for the
 * in it, when done using (and perhaps getting ref counts if needing to
 * keep a pointer to one of those entries) it must be paired with
 * addr_location__put(), so that the refcounts can be decremented.
 */
void addr_location__put(struct addr_location *al)
{
	thread__zput(al->thread);
}

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
bool is_bts_event(struct perf_event_attr *attr)
{
	return attr->type == PERF_TYPE_HARDWARE &&
	       (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
	       attr->sample_period == 1;
}

bool sample_addr_correlates_sym(struct perf_event_attr *attr)
{
	if (attr->type == PERF_TYPE_SOFTWARE &&
	    (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
	     attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
	     attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
		return true;

	if (is_bts_event(attr))
		return true;

	return false;
}

void perf_event__preprocess_sample_addr(union perf_event *event,
					struct perf_sample *sample,
					struct thread *thread,
					struct addr_location *al)
{
	u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;

1061
	thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al);
1062
	if (!al->map)
1063
		thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
1064 1065 1066 1067 1068 1069 1070 1071
				      sample->addr, al);

	al->cpu = sample->cpu;
	al->sym = NULL;

	if (al->map)
		al->sym = map__find_symbol(al->map, al->addr, NULL);
}