event.c 21.4 KB
Newer Older
1 2 3
#include <linux/types.h>
#include "event.h"
#include "debug.h"
4
#include "session.h"
5
#include "sort.h"
6
#include "string.h"
7
#include "strlist.h"
8
#include "thread.h"
9

10
static const char *event__name[] = {
11 12 13 14 15 16 17 18 19 20 21 22 23 24
	[0]			 = "TOTAL",
	[PERF_RECORD_MMAP]	 = "MMAP",
	[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",
	[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",
25
	[PERF_RECORD_FINISHED_ROUND]	 = "FINISHED_ROUND",
26 27
};

28 29 30 31 32 33 34 35 36
const char *event__get_event_name(unsigned int id)
{
	if (id >= ARRAY_SIZE(event__name))
		return "INVALID";
	if (!event__name[id])
		return "UNKNOWN";
	return event__name[id];
}

37 38 39 40 41 42 43 44 45
static struct sample_data synth_sample = {
	.pid	   = -1,
	.tid	   = -1,
	.time	   = -1,
	.stream_id = -1,
	.cpu	   = -1,
	.period	   = 1,
};

46
static pid_t event__synthesize_comm(event_t *event, pid_t pid, int full,
47
				    event__handler_t process,
48
				    struct perf_session *session)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
{
	char filename[PATH_MAX];
	char bf[BUFSIZ];
	FILE *fp;
	size_t size = 0;
	DIR *tasks;
	struct dirent dirent, *next;
	pid_t tgid = 0;

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

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

70 71 72 73 74 75 76
	memset(&event->comm, 0, sizeof(event->comm));

	while (!event->comm.comm[0] || !event->comm.pid) {
		if (fgets(bf, sizeof(bf), fp) == NULL) {
			pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
			goto out;
		}
77 78 79 80 81 82

		if (memcmp(bf, "Name:", 5) == 0) {
			char *name = bf + 5;
			while (*name && isspace(*name))
				++name;
			size = strlen(name) - 1;
83
			memcpy(event->comm.comm, name, size++);
84 85 86 87
		} else if (memcmp(bf, "Tgid:", 5) == 0) {
			char *tgids = bf + 5;
			while (*tgids && isspace(*tgids))
				++tgids;
88
			tgid = event->comm.pid = atoi(tgids);
89 90 91
		}
	}

92
	event->comm.header.type = PERF_RECORD_COMM;
93
	size = ALIGN(size, sizeof(u64));
94 95 96 97
	memset(event->comm.comm + size, 0, session->id_hdr_size);
	event->comm.header.size = (sizeof(event->comm) -
				(sizeof(event->comm.comm) - size) +
				session->id_hdr_size);
98
	if (!full) {
99
		event->comm.tid = pid;
100

101 102
		process(event, &synth_sample, session);
		goto out;
103 104 105 106 107 108 109 110 111 112 113 114 115 116
	}

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

	tasks = opendir(filename);
	if (tasks == NULL)
		goto out_race;

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

117
		event->comm.tid = pid;
118

119
		process(event, &synth_sample, session);
120 121
	}

122 123
	closedir(tasks);
out:
124 125
	fclose(fp);

126
	return tgid;
127 128
}

129
static int event__synthesize_mmap_events(event_t *event, pid_t pid, pid_t tgid,
130
					 event__handler_t process,
131
					 struct perf_session *session)
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
{
	char filename[PATH_MAX];
	FILE *fp;

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

	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;
	}

147 148 149 150 151 152
	event->header.type = PERF_RECORD_MMAP;
	/*
	 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
	 */
	event->header.misc = PERF_RECORD_MISC_USER;

153 154 155 156 157 158 159 160
	while (1) {
		char bf[BUFSIZ], *pbf = bf;
		int n;
		size_t size;
		if (fgets(bf, sizeof(bf), fp) == NULL)
			break;

		/* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
161
		n = hex2u64(pbf, &event->mmap.start);
162 163 164
		if (n < 0)
			continue;
		pbf += n + 1;
165
		n = hex2u64(pbf, &event->mmap.len);
166 167 168 169 170 171 172 173 174 175 176 177 178
		if (n < 0)
			continue;
		pbf += n + 3;
		if (*pbf == 'x') { /* vm_exec */
			char *execname = strchr(bf, '/');

			/* Catch VDSO */
			if (execname == NULL)
				execname = strstr(bf, "[vdso]");

			if (execname == NULL)
				continue;

179
			pbf += 3;
180
			n = hex2u64(pbf, &event->mmap.pgoff);
181

182 183
			size = strlen(execname);
			execname[size - 1] = '\0'; /* Remove \n */
184
			memcpy(event->mmap.filename, execname, size);
185
			size = ALIGN(size, sizeof(u64));
186 187 188 189 190 191 192 193 194
			event->mmap.len -= event->mmap.start;
			event->mmap.header.size = (sizeof(event->mmap) -
					        (sizeof(event->mmap.filename) - size));
			memset(event->mmap.filename + size, 0, session->id_hdr_size);
			event->mmap.header.size += session->id_hdr_size;
			event->mmap.pid = tgid;
			event->mmap.tid = pid;

			process(event, &synth_sample, session);
195 196 197 198 199 200 201
		}
	}

	fclose(fp);
	return 0;
}

202
int event__synthesize_modules(event__handler_t process,
203
			      struct perf_session *session,
204
			      struct machine *machine)
205 206
{
	struct rb_node *nd;
207
	struct map_groups *kmaps = &machine->kmaps;
208 209 210 211 212 213 214 215 216
	event_t *event = zalloc(sizeof(event->mmap) + session->id_hdr_size);

	if (event == NULL) {
		pr_debug("Not enough memory synthesizing mmap event "
			 "for kernel modules\n");
		return -1;
	}

	event->header.type = PERF_RECORD_MMAP;
217

218 219 220 221
	/*
	 * kernel uses 0 for user space maps, see kernel/perf_event.c
	 * __perf_event_mmap
	 */
222
	if (machine__is_host(machine))
223
		event->header.misc = PERF_RECORD_MISC_KERNEL;
224
	else
225
		event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
226 227

	for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
228 229 230 231 232 233 234 235
	     nd; nd = rb_next(nd)) {
		size_t size;
		struct map *pos = rb_entry(nd, struct map, rb_node);

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

		size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
236 237 238 239 240 241 242 243 244 245
		event->mmap.header.type = PERF_RECORD_MMAP;
		event->mmap.header.size = (sizeof(event->mmap) -
				        (sizeof(event->mmap.filename) - size));
		memset(event->mmap.filename + size, 0, session->id_hdr_size);
		event->mmap.header.size += session->id_hdr_size;
		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,
246
		       pos->dso->long_name_len + 1);
247
		process(event, &synth_sample, session);
248 249
	}

250
	free(event);
251 252 253
	return 0;
}

254 255 256
static int __event__synthesize_thread(event_t *comm_event, event_t *mmap_event,
				      pid_t pid, event__handler_t process,
				      struct perf_session *session)
257
{
258 259
	pid_t tgid = event__synthesize_comm(comm_event, pid, 1, process,
					    session);
260 261
	if (tgid == -1)
		return -1;
262 263
	return event__synthesize_mmap_events(mmap_event, pid, tgid,
					     process, session);
264 265
}

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
int event__synthesize_thread(pid_t pid, event__handler_t process,
			     struct perf_session *session)
{
	event_t *comm_event, *mmap_event;
	int err = -1;

	comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
	if (comm_event == NULL)
		goto out;

	mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
	if (mmap_event == NULL)
		goto out_free_comm;

	err = __event__synthesize_thread(comm_event, mmap_event, pid,
					 process, session);
	free(mmap_event);
out_free_comm:
	free(comm_event);
out:
	return err;
}

int event__synthesize_threads(event__handler_t process,
			      struct perf_session *session)
291 292 293
{
	DIR *proc;
	struct dirent dirent, *next;
294 295 296 297 298 299 300 301 302 303
	event_t *comm_event, *mmap_event;
	int err = -1;

	comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
	if (comm_event == NULL)
		goto out;

	mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
	if (mmap_event == NULL)
		goto out_free_comm;
304 305

	proc = opendir("/proc");
306 307
	if (proc == NULL)
		goto out_free_mmap;
308 309 310 311 312 313 314 315

	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;

316 317
		__event__synthesize_thread(comm_event, mmap_event, pid,
					   process, session);
318 319 320
	}

	closedir(proc);
321 322 323 324 325 326 327
	err = 0;
out_free_mmap:
	free(mmap_event);
out_free_comm:
	free(comm_event);
out:
	return err;
328
}
329

330 331 332 333 334
struct process_symbol_args {
	const char *name;
	u64	   start;
};

335 336
static int find_symbol_cb(void *arg, const char *name, char type,
			  u64 start, u64 end __used)
337 338 339
{
	struct process_symbol_args *args = arg;

340 341 342 343 344 345
	/*
	 * 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))
346 347 348 349 350 351
		return 0;

	args->start = start;
	return 1;
}

352
int event__synthesize_kernel_mmap(event__handler_t process,
353
				  struct perf_session *session,
354
				  struct machine *machine,
355 356 357
				  const char *symbol_name)
{
	size_t size;
358 359 360 361
	const char *filename, *mmap_name;
	char path[PATH_MAX];
	char name_buff[PATH_MAX];
	struct map *map;
362
	int err;
363 364 365 366 367 368
	/*
	 * 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.
	 */
	struct process_symbol_args args = { .name = symbol_name, };
369 370 371 372 373 374 375
	event_t *event = zalloc(sizeof(event->mmap) + session->id_hdr_size);

	if (event == NULL) {
		pr_debug("Not enough memory synthesizing mmap event "
			 "for kernel modules\n");
		return -1;
	}
376

377
	mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
378
	if (machine__is_host(machine)) {
379 380 381 382
		/*
		 * kernel uses PERF_RECORD_MISC_USER for user space maps,
		 * see kernel/perf_event.c __perf_event_mmap
		 */
383
		event->header.misc = PERF_RECORD_MISC_KERNEL;
384 385
		filename = "/proc/kallsyms";
	} else {
386
		event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
387
		if (machine__is_default_guest(machine))
388 389
			filename = (char *) symbol_conf.default_guest_kallsyms;
		else {
390
			sprintf(path, "%s/proc/kallsyms", machine->root_dir);
391 392 393 394 395
			filename = path;
		}
	}

	if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
396 397
		return -ENOENT;

398
	map = machine->vmlinux_maps[MAP__FUNCTION];
399
	size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
400
			"%s%s", mmap_name, symbol_name) + 1;
401
	size = ALIGN(size, sizeof(u64));
402 403 404 405 406 407 408 409 410 411 412 413
	event->mmap.header.type = PERF_RECORD_MMAP;
	event->mmap.header.size = (sizeof(event->mmap) -
			(sizeof(event->mmap.filename) - size) + session->id_hdr_size);
	event->mmap.pgoff = args.start;
	event->mmap.start = map->start;
	event->mmap.len   = map->end - event->mmap.start;
	event->mmap.pid   = machine->pid;

	err = process(event, &synth_sample, session);
	free(event);

	return err;
414 415
}

416
static void thread__comm_adjust(struct thread *self, struct hists *hists)
417 418 419 420 421 422
{
	char *comm = self->comm;

	if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
	    (!symbol_conf.comm_list ||
	     strlist__has_entry(symbol_conf.comm_list, comm))) {
423
		u16 slen = strlen(comm);
424

425 426
		if (hists__new_col_len(hists, HISTC_COMM, slen))
			hists__set_col_len(hists, HISTC_THREAD, slen + 6);
427 428 429
	}
}

430 431
static int thread__set_comm_adjust(struct thread *self, const char *comm,
				   struct hists *hists)
432 433 434 435 436 437
{
	int ret = thread__set_comm(self, comm);

	if (ret)
		return ret;

438
	thread__comm_adjust(self, hists);
439 440 441 442

	return 0;
}

443 444
int event__process_comm(event_t *self, struct sample_data *sample __used,
			struct perf_session *session)
445
{
446
	struct thread *thread = perf_session__findnew(session, self->comm.tid);
447

448
	dump_printf(": %s:%d\n", self->comm.comm, self->comm.tid);
449

450 451
	if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm,
						      &session->hists)) {
452 453 454 455 456 457 458
		dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
		return -1;
	}

	return 0;
}

459 460
int event__process_lost(event_t *self, struct sample_data *sample __used,
			struct perf_session *session)
461
{
462 463
	dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
		    self->lost.id, self->lost.lost);
464
	session->hists.stats.total_lost += self->lost.lost;
465 466 467
	return 0;
}

468 469 470 471 472 473 474 475 476
static void event_set_kernel_mmap_len(struct map **maps, event_t *self)
{
	maps[MAP__FUNCTION]->start = self->mmap.start;
	maps[MAP__FUNCTION]->end   = self->mmap.start + self->mmap.len;
	/*
	 * Be a bit paranoid here, some perf.data file came with
	 * a zero sized synthesized MMAP event for the kernel.
	 */
	if (maps[MAP__FUNCTION]->end == 0)
477
		maps[MAP__FUNCTION]->end = ~0ULL;
478 479 480 481
}

static int event__process_kernel_mmap(event_t *self,
			struct perf_session *session)
482
{
483
	struct map *map;
484
	char kmmap_prefix[PATH_MAX];
485
	struct machine *machine;
486 487 488
	enum dso_kernel_type kernel_type;
	bool is_kernel_mmap;

489 490 491
	machine = perf_session__findnew_machine(session, self->mmap.pid);
	if (!machine) {
		pr_err("Can't find id %d's machine\n", self->mmap.pid);
492 493
		goto out_problem;
	}
494

495
	machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
496
	if (machine__is_host(machine))
497 498 499
		kernel_type = DSO_TYPE_KERNEL;
	else
		kernel_type = DSO_TYPE_GUEST_KERNEL;
500

501 502 503 504 505
	is_kernel_mmap = memcmp(self->mmap.filename,
				kmmap_prefix,
				strlen(kmmap_prefix)) == 0;
	if (self->mmap.filename[0] == '/' ||
	    (!is_kernel_mmap && self->mmap.filename[0] == '[')) {
506

507 508
		char short_module_name[1024];
		char *name, *dot;
509

510 511
		if (self->mmap.filename[0] == '/') {
			name = strrchr(self->mmap.filename, '/');
512 513 514 515 516 517 518 519
			if (name == NULL)
				goto out_problem;

			++name; /* skip / */
			dot = strrchr(name, '.');
			if (dot == NULL)
				goto out_problem;
			snprintf(short_module_name, sizeof(short_module_name),
520
					"[%.*s]", (int)(dot - name), name);
521
			strxfrchar(short_module_name, '-', '_');
522 523 524
		} else
			strcpy(short_module_name, self->mmap.filename);

525 526
		map = machine__new_module(machine, self->mmap.start,
					  self->mmap.filename);
527 528 529 530 531 532 533 534
		if (map == NULL)
			goto out_problem;

		name = strdup(short_module_name);
		if (name == NULL)
			goto out_problem;

		map->dso->short_name = name;
535
		map->dso->sname_alloc = 1;
536 537 538 539 540 541 542 543
		map->end = map->start + self->mmap.len;
	} else if (is_kernel_mmap) {
		const char *symbol_name = (self->mmap.filename +
				strlen(kmmap_prefix));
		/*
		 * Should be there already, from the build-id table in
		 * the header.
		 */
544 545
		struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
						     kmmap_prefix);
546 547 548 549
		if (kernel == NULL)
			goto out_problem;

		kernel->kernel = kernel_type;
550
		if (__machine__create_kernel_maps(machine, kernel) < 0)
551 552
			goto out_problem;

553 554 555 556 557
		event_set_kernel_mmap_len(machine->vmlinux_maps, self);
		perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
							 symbol_name,
							 self->mmap.pgoff);
		if (machine__is_default_guest(machine)) {
558
			/*
559
			 * preload dso of guest kernel and modules
560
			 */
561 562
			dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
				  NULL);
563 564 565 566 567 568
		}
	}
	return 0;
out_problem:
	return -1;
}
569

570 571
int event__process_mmap(event_t *self, struct sample_data *sample __used,
			struct perf_session *session)
572
{
573
	struct machine *machine;
574 575 576 577
	struct thread *thread;
	struct map *map;
	u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
	int ret = 0;
578

579
	dump_printf(" %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
580 581 582 583 584 585 586 587
			self->mmap.pid, self->mmap.tid, self->mmap.start,
			self->mmap.len, self->mmap.pgoff, self->mmap.filename);

	if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
	    cpumode == PERF_RECORD_MISC_KERNEL) {
		ret = event__process_kernel_mmap(self, session);
		if (ret < 0)
			goto out_problem;
588 589 590
		return 0;
	}

591
	machine = perf_session__find_host_machine(session);
592 593 594
	if (machine == NULL)
		goto out_problem;
	thread = perf_session__findnew(session, self->mmap.pid);
595 596
	if (thread == NULL)
		goto out_problem;
597
	map = map__new(&machine->user_dsos, self->mmap.start,
598 599
			self->mmap.len, self->mmap.pgoff,
			self->mmap.pid, self->mmap.filename,
600
			MAP__FUNCTION);
601
	if (map == NULL)
602 603 604 605
		goto out_problem;

	thread__insert_map(thread, map);
	return 0;
606

607 608
out_problem:
	dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
609 610 611
	return 0;
}

612 613
int event__process_task(event_t *self, struct sample_data *sample __used,
			struct perf_session *session)
614
{
615 616
	struct thread *thread = perf_session__findnew(session, self->fork.tid);
	struct thread *parent = perf_session__findnew(session, self->fork.ptid);
617 618 619 620

	dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
		    self->fork.ppid, self->fork.ptid);

621 622
	if (self->header.type == PERF_RECORD_EXIT) {
		perf_session__remove_thread(session, thread);
623
		return 0;
624
	}
625 626 627 628 629 630 631 632 633

	if (thread == NULL || parent == NULL ||
	    thread__fork(thread, parent) < 0) {
		dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
		return -1;
	}

	return 0;
}
634

635 636
int event__process(event_t *event, struct sample_data *sample,
		   struct perf_session *session)
637 638 639
{
	switch (event->header.type) {
	case PERF_RECORD_COMM:
640
		event__process_comm(event, sample, session);
641 642
		break;
	case PERF_RECORD_MMAP:
643
		event__process_mmap(event, sample, session);
644 645 646
		break;
	case PERF_RECORD_FORK:
	case PERF_RECORD_EXIT:
647
		event__process_task(event, sample, session);
648 649 650 651 652 653 654 655
		break;
	default:
		break;
	}

	return 0;
}

656 657
void thread__find_addr_map(struct thread *self,
			   struct perf_session *session, u8 cpumode,
658
			   enum map_type type, pid_t pid, u64 addr,
659
			   struct addr_location *al)
660
{
661
	struct map_groups *mg = &self->mg;
662
	struct machine *machine = NULL;
663

664
	al->thread = self;
665
	al->addr = addr;
666 667
	al->cpumode = cpumode;
	al->filtered = false;
668

669
	if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
670
		al->level = 'k';
671
		machine = perf_session__find_host_machine(session);
672 673 674 675
		if (machine == NULL) {
			al->map = NULL;
			return;
		}
676
		mg = &machine->kmaps;
677
	} else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
678
		al->level = '.';
679
		machine = perf_session__find_host_machine(session);
680 681
	} else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
		al->level = 'g';
682
		machine = perf_session__find_machine(session, pid);
683
		if (machine == NULL) {
684 685 686
			al->map = NULL;
			return;
		}
687
		mg = &machine->kmaps;
688 689 690 691 692 693 694 695 696
	} else {
		/*
		 * 'u' means guest os user space.
		 * TODO: We don't support guest user space. Might support late.
		 */
		if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
			al->level = 'u';
		else
			al->level = 'H';
697
		al->map = NULL;
698 699 700 701 702 703 704 705 706 707

		if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
			cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
			!perf_guest)
			al->filtered = true;
		if ((cpumode == PERF_RECORD_MISC_USER ||
			cpumode == PERF_RECORD_MISC_KERNEL) &&
			!perf_host)
			al->filtered = true;

708 709 710
		return;
	}
try_again:
711
	al->map = map_groups__find(mg, type, al->addr);
712 713 714 715 716 717 718 719 720 721
	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.
		 */
722
		if ((long long)al->addr < 0 &&
723 724 725
		    cpumode == PERF_RECORD_MISC_KERNEL &&
		    machine && mg != &machine->kmaps) {
			mg = &machine->kmaps;
726 727
			goto try_again;
		}
728
	} else
729
		al->addr = al->map->map_ip(al->map, al->addr);
730 731 732 733
}

void thread__find_addr_location(struct thread *self,
				struct perf_session *session, u8 cpumode,
734
				enum map_type type, pid_t pid, u64 addr,
735 736 737
				struct addr_location *al,
				symbol_filter_t filter)
{
738
	thread__find_addr_map(self, session, cpumode, type, pid, addr, al);
739
	if (al->map != NULL)
740
		al->sym = map__find_symbol(al->map, al->addr, filter);
741 742
	else
		al->sym = NULL;
743 744
}

745
static void dso__calc_col_width(struct dso *self, struct hists *hists)
746 747 748 749
{
	if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
	    (!symbol_conf.dso_list ||
	     strlist__has_entry(symbol_conf.dso_list, self->name))) {
750 751
		u16 slen = dso__name_len(self);
		hists__new_col_len(hists, HISTC_DSO, slen);
752 753 754 755 756
	}

	self->slen_calculated = 1;
}

757
int event__preprocess_sample(const event_t *self, struct perf_session *session,
758 759
			     struct addr_location *al, struct sample_data *data,
			     symbol_filter_t filter)
760 761
{
	u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
762
	struct thread *thread = perf_session__findnew(session, self->ip.pid);
763

764 765 766
	if (thread == NULL)
		return -1;

767 768 769 770
	if (symbol_conf.comm_list &&
	    !strlist__has_entry(symbol_conf.comm_list, thread->comm))
		goto out_filtered;

771
	dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
772 773 774 775 776 777 778 779 780 781
	/*
	 * Have we already created the kernel maps for the host machine?
	 *
	 * 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 &&
	    session->host_machine.vmlinux_maps[MAP__FUNCTION] == NULL)
		machine__create_kernel_maps(&session->host_machine);
782

783
	thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
784
			      self->ip.pid, self->ip.ip, al);
785 786 787
	dump_printf(" ...... dso: %s\n",
		    al->map ? al->map->dso->long_name :
			al->level == 'H' ? "[hypervisor]" : "<not found>");
788
	al->sym = NULL;
A
Arun Sharma 已提交
789
	al->cpu = data->cpu;
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805

	if (al->map) {
		if (symbol_conf.dso_list &&
		    (!al->map || !al->map->dso ||
		     !(strlist__has_entry(symbol_conf.dso_list,
					  al->map->dso->short_name) ||
		       (al->map->dso->short_name != al->map->dso->long_name &&
			strlist__has_entry(symbol_conf.dso_list,
					   al->map->dso->long_name)))))
			goto out_filtered;
		/*
		 * We have to do this here as we may have a dso with no symbol
		 * hit that has a name longer than the ones with symbols
		 * sampled.
		 */
		if (!sort_dso.elide && !al->map->dso->slen_calculated)
806
			dso__calc_col_width(al->map->dso, &session->hists);
807 808

		al->sym = map__find_symbol(al->map, al->addr, filter);
809 810 811
	} else {
		const unsigned int unresolved_col_width = BITS_PER_LONG / 4;

812
		if (hists__col_len(&session->hists, HISTC_DSO) < unresolved_col_width &&
813 814
		    !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
		    !symbol_conf.dso_list)
815 816
			hists__set_col_len(&session->hists, HISTC_DSO,
					   unresolved_col_width);
817
	}
818 819 820 821 822 823 824 825 826

	if (symbol_conf.sym_list && al->sym &&
	    !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
		goto out_filtered;

	return 0;

out_filtered:
	al->filtered = true;
827 828
	return 0;
}