1 #include <linux/types.h> 2 #include <sys/mman.h> 3 #include "event.h" 4 #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 [0] = "TOTAL", 16 [PERF_RECORD_MMAP] = "MMAP", 17 [PERF_RECORD_MMAP2] = "MMAP2", 18 [PERF_RECORD_LOST] = "LOST", 19 [PERF_RECORD_COMM] = "COMM", 20 [PERF_RECORD_EXIT] = "EXIT", 21 [PERF_RECORD_THROTTLE] = "THROTTLE", 22 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", 23 [PERF_RECORD_FORK] = "FORK", 24 [PERF_RECORD_READ] = "READ", 25 [PERF_RECORD_SAMPLE] = "SAMPLE", 26 [PERF_RECORD_AUX] = "AUX", 27 [PERF_RECORD_ITRACE_START] = "ITRACE_START", 28 [PERF_RECORD_HEADER_ATTR] = "ATTR", 29 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", 30 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA", 31 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID", 32 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND", 33 [PERF_RECORD_ID_INDEX] = "ID_INDEX", 34 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO", 35 [PERF_RECORD_AUXTRACE] = "AUXTRACE", 36 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR", 37 }; 38 39 const char *perf_event__name(unsigned int id) 40 { 41 if (id >= ARRAY_SIZE(perf_event__names)) 42 return "INVALID"; 43 if (!perf_event__names[id]) 44 return "UNKNOWN"; 45 return perf_event__names[id]; 46 } 47 48 static struct perf_sample synth_sample = { 49 .pid = -1, 50 .tid = -1, 51 .time = -1, 52 .stream_id = -1, 53 .cpu = -1, 54 .period = 1, 55 }; 56 57 /* 58 * Assumes that the first 4095 bytes of /proc/pid/stat contains 59 * the comm, tgid and ppid. 60 */ 61 static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len, 62 pid_t *tgid, pid_t *ppid) 63 { 64 char filename[PATH_MAX]; 65 char bf[4096]; 66 int fd; 67 size_t size = 0, n; 68 char *nl, *name, *tgids, *ppids; 69 70 *tgid = -1; 71 *ppid = -1; 72 73 snprintf(filename, sizeof(filename), "/proc/%d/status", pid); 74 75 fd = open(filename, O_RDONLY); 76 if (fd < 0) { 77 pr_debug("couldn't open %s\n", filename); 78 return -1; 79 } 80 81 n = read(fd, bf, sizeof(bf) - 1); 82 close(fd); 83 if (n <= 0) { 84 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n", 85 pid); 86 return -1; 87 } 88 bf[n] = '\0'; 89 90 name = strstr(bf, "Name:"); 91 tgids = strstr(bf, "Tgid:"); 92 ppids = strstr(bf, "PPid:"); 93 94 if (name) { 95 name += 5; /* strlen("Name:") */ 96 97 while (*name && isspace(*name)) 98 ++name; 99 100 nl = strchr(name, '\n'); 101 if (nl) 102 *nl = '\0'; 103 104 size = strlen(name); 105 if (size >= len) 106 size = len - 1; 107 memcpy(comm, name, size); 108 comm[size] = '\0'; 109 } else { 110 pr_debug("Name: string not found for pid %d\n", pid); 111 } 112 113 if (tgids) { 114 tgids += 5; /* strlen("Tgid:") */ 115 *tgid = atoi(tgids); 116 } else { 117 pr_debug("Tgid: string not found for pid %d\n", pid); 118 } 119 120 if (ppids) { 121 ppids += 5; /* strlen("PPid:") */ 122 *ppid = atoi(ppids); 123 } else { 124 pr_debug("PPid: string not found for pid %d\n", pid); 125 } 126 127 return 0; 128 } 129 130 static int perf_event__prepare_comm(union perf_event *event, pid_t pid, 131 struct machine *machine, 132 pid_t *tgid, pid_t *ppid) 133 { 134 size_t size; 135 136 *ppid = -1; 137 138 memset(&event->comm, 0, sizeof(event->comm)); 139 140 if (machine__is_host(machine)) { 141 if (perf_event__get_comm_ids(pid, event->comm.comm, 142 sizeof(event->comm.comm), 143 tgid, ppid) != 0) { 144 return -1; 145 } 146 } else { 147 *tgid = machine->pid; 148 } 149 150 if (*tgid < 0) 151 return -1; 152 153 event->comm.pid = *tgid; 154 event->comm.header.type = PERF_RECORD_COMM; 155 156 size = strlen(event->comm.comm) + 1; 157 size = PERF_ALIGN(size, sizeof(u64)); 158 memset(event->comm.comm + size, 0, machine->id_hdr_size); 159 event->comm.header.size = (sizeof(event->comm) - 160 (sizeof(event->comm.comm) - size) + 161 machine->id_hdr_size); 162 event->comm.tid = pid; 163 164 return 0; 165 } 166 167 static pid_t perf_event__synthesize_comm(struct perf_tool *tool, 168 union perf_event *event, pid_t pid, 169 perf_event__handler_t process, 170 struct machine *machine) 171 { 172 pid_t tgid, ppid; 173 174 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0) 175 return -1; 176 177 if (process(tool, event, &synth_sample, machine) != 0) 178 return -1; 179 180 return tgid; 181 } 182 183 static int perf_event__synthesize_fork(struct perf_tool *tool, 184 union perf_event *event, 185 pid_t pid, pid_t tgid, pid_t ppid, 186 perf_event__handler_t process, 187 struct machine *machine) 188 { 189 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size); 190 191 /* 192 * for main thread set parent to ppid from status file. For other 193 * threads set parent pid to main thread. ie., assume main thread 194 * spawns all threads in a process 195 */ 196 if (tgid == pid) { 197 event->fork.ppid = ppid; 198 event->fork.ptid = ppid; 199 } else { 200 event->fork.ppid = tgid; 201 event->fork.ptid = tgid; 202 } 203 event->fork.pid = tgid; 204 event->fork.tid = pid; 205 event->fork.header.type = PERF_RECORD_FORK; 206 207 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size); 208 209 if (process(tool, event, &synth_sample, machine) != 0) 210 return -1; 211 212 return 0; 213 } 214 215 int perf_event__synthesize_mmap_events(struct perf_tool *tool, 216 union perf_event *event, 217 pid_t pid, pid_t tgid, 218 perf_event__handler_t process, 219 struct machine *machine, 220 bool mmap_data) 221 { 222 char filename[PATH_MAX]; 223 FILE *fp; 224 int rc = 0; 225 226 if (machine__is_default_guest(machine)) 227 return 0; 228 229 snprintf(filename, sizeof(filename), "%s/proc/%d/maps", 230 machine->root_dir, pid); 231 232 fp = fopen(filename, "r"); 233 if (fp == NULL) { 234 /* 235 * We raced with a task exiting - just return: 236 */ 237 pr_debug("couldn't open %s\n", filename); 238 return -1; 239 } 240 241 event->header.type = PERF_RECORD_MMAP2; 242 243 while (1) { 244 char bf[BUFSIZ]; 245 char prot[5]; 246 char execname[PATH_MAX]; 247 char anonstr[] = "//anon"; 248 unsigned int ino; 249 size_t size; 250 ssize_t n; 251 252 if (fgets(bf, sizeof(bf), fp) == NULL) 253 break; 254 255 /* ensure null termination since stack will be reused. */ 256 strcpy(execname, ""); 257 258 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ 259 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n", 260 &event->mmap2.start, &event->mmap2.len, prot, 261 &event->mmap2.pgoff, &event->mmap2.maj, 262 &event->mmap2.min, 263 &ino, execname); 264 265 /* 266 * Anon maps don't have the execname. 267 */ 268 if (n < 7) 269 continue; 270 271 event->mmap2.ino = (u64)ino; 272 273 /* 274 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c 275 */ 276 if (machine__is_host(machine)) 277 event->header.misc = PERF_RECORD_MISC_USER; 278 else 279 event->header.misc = PERF_RECORD_MISC_GUEST_USER; 280 281 /* map protection and flags bits */ 282 event->mmap2.prot = 0; 283 event->mmap2.flags = 0; 284 if (prot[0] == 'r') 285 event->mmap2.prot |= PROT_READ; 286 if (prot[1] == 'w') 287 event->mmap2.prot |= PROT_WRITE; 288 if (prot[2] == 'x') 289 event->mmap2.prot |= PROT_EXEC; 290 291 if (prot[3] == 's') 292 event->mmap2.flags |= MAP_SHARED; 293 else 294 event->mmap2.flags |= MAP_PRIVATE; 295 296 if (prot[2] != 'x') { 297 if (!mmap_data || prot[0] != 'r') 298 continue; 299 300 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA; 301 } 302 303 if (!strcmp(execname, "")) 304 strcpy(execname, anonstr); 305 306 size = strlen(execname) + 1; 307 memcpy(event->mmap2.filename, execname, size); 308 size = PERF_ALIGN(size, sizeof(u64)); 309 event->mmap2.len -= event->mmap.start; 310 event->mmap2.header.size = (sizeof(event->mmap2) - 311 (sizeof(event->mmap2.filename) - size)); 312 memset(event->mmap2.filename + size, 0, machine->id_hdr_size); 313 event->mmap2.header.size += machine->id_hdr_size; 314 event->mmap2.pid = tgid; 315 event->mmap2.tid = pid; 316 317 if (process(tool, event, &synth_sample, machine) != 0) { 318 rc = -1; 319 break; 320 } 321 } 322 323 fclose(fp); 324 return rc; 325 } 326 327 int perf_event__synthesize_modules(struct perf_tool *tool, 328 perf_event__handler_t process, 329 struct machine *machine) 330 { 331 int rc = 0; 332 struct rb_node *nd; 333 struct map_groups *kmaps = &machine->kmaps; 334 union perf_event *event = zalloc((sizeof(event->mmap) + 335 machine->id_hdr_size)); 336 if (event == NULL) { 337 pr_debug("Not enough memory synthesizing mmap event " 338 "for kernel modules\n"); 339 return -1; 340 } 341 342 event->header.type = PERF_RECORD_MMAP; 343 344 /* 345 * kernel uses 0 for user space maps, see kernel/perf_event.c 346 * __perf_event_mmap 347 */ 348 if (machine__is_host(machine)) 349 event->header.misc = PERF_RECORD_MISC_KERNEL; 350 else 351 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; 352 353 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]); 354 nd; nd = rb_next(nd)) { 355 size_t size; 356 struct map *pos = rb_entry(nd, struct map, rb_node); 357 358 if (pos->dso->kernel) 359 continue; 360 361 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64)); 362 event->mmap.header.type = PERF_RECORD_MMAP; 363 event->mmap.header.size = (sizeof(event->mmap) - 364 (sizeof(event->mmap.filename) - size)); 365 memset(event->mmap.filename + size, 0, machine->id_hdr_size); 366 event->mmap.header.size += machine->id_hdr_size; 367 event->mmap.start = pos->start; 368 event->mmap.len = pos->end - pos->start; 369 event->mmap.pid = machine->pid; 370 371 memcpy(event->mmap.filename, pos->dso->long_name, 372 pos->dso->long_name_len + 1); 373 if (process(tool, event, &synth_sample, machine) != 0) { 374 rc = -1; 375 break; 376 } 377 } 378 379 free(event); 380 return rc; 381 } 382 383 static int __event__synthesize_thread(union perf_event *comm_event, 384 union perf_event *mmap_event, 385 union perf_event *fork_event, 386 pid_t pid, int full, 387 perf_event__handler_t process, 388 struct perf_tool *tool, 389 struct machine *machine, bool mmap_data) 390 { 391 char filename[PATH_MAX]; 392 DIR *tasks; 393 struct dirent dirent, *next; 394 pid_t tgid, ppid; 395 int rc = 0; 396 397 /* special case: only send one comm event using passed in pid */ 398 if (!full) { 399 tgid = perf_event__synthesize_comm(tool, comm_event, pid, 400 process, machine); 401 402 if (tgid == -1) 403 return -1; 404 405 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, 406 process, machine, mmap_data); 407 } 408 409 if (machine__is_default_guest(machine)) 410 return 0; 411 412 snprintf(filename, sizeof(filename), "%s/proc/%d/task", 413 machine->root_dir, pid); 414 415 tasks = opendir(filename); 416 if (tasks == NULL) { 417 pr_debug("couldn't open %s\n", filename); 418 return 0; 419 } 420 421 while (!readdir_r(tasks, &dirent, &next) && next) { 422 char *end; 423 pid_t _pid; 424 425 _pid = strtol(dirent.d_name, &end, 10); 426 if (*end) 427 continue; 428 429 rc = -1; 430 if (perf_event__prepare_comm(comm_event, _pid, machine, 431 &tgid, &ppid) != 0) 432 break; 433 434 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid, 435 ppid, process, machine) < 0) 436 break; 437 /* 438 * Send the prepared comm event 439 */ 440 if (process(tool, comm_event, &synth_sample, machine) != 0) 441 break; 442 443 rc = 0; 444 if (_pid == pid) { 445 /* process the parent's maps too */ 446 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, 447 process, machine, mmap_data); 448 if (rc) 449 break; 450 } 451 } 452 453 closedir(tasks); 454 return rc; 455 } 456 457 int perf_event__synthesize_thread_map(struct perf_tool *tool, 458 struct thread_map *threads, 459 perf_event__handler_t process, 460 struct machine *machine, 461 bool mmap_data) 462 { 463 union perf_event *comm_event, *mmap_event, *fork_event; 464 int err = -1, thread, j; 465 466 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size); 467 if (comm_event == NULL) 468 goto out; 469 470 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size); 471 if (mmap_event == NULL) 472 goto out_free_comm; 473 474 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); 475 if (fork_event == NULL) 476 goto out_free_mmap; 477 478 err = 0; 479 for (thread = 0; thread < threads->nr; ++thread) { 480 if (__event__synthesize_thread(comm_event, mmap_event, 481 fork_event, 482 threads->map[thread], 0, 483 process, tool, machine, 484 mmap_data)) { 485 err = -1; 486 break; 487 } 488 489 /* 490 * comm.pid is set to thread group id by 491 * perf_event__synthesize_comm 492 */ 493 if ((int) comm_event->comm.pid != threads->map[thread]) { 494 bool need_leader = true; 495 496 /* is thread group leader in thread_map? */ 497 for (j = 0; j < threads->nr; ++j) { 498 if ((int) comm_event->comm.pid == threads->map[j]) { 499 need_leader = false; 500 break; 501 } 502 } 503 504 /* if not, generate events for it */ 505 if (need_leader && 506 __event__synthesize_thread(comm_event, mmap_event, 507 fork_event, 508 comm_event->comm.pid, 0, 509 process, tool, machine, 510 mmap_data)) { 511 err = -1; 512 break; 513 } 514 } 515 } 516 free(fork_event); 517 out_free_mmap: 518 free(mmap_event); 519 out_free_comm: 520 free(comm_event); 521 out: 522 return err; 523 } 524 525 int perf_event__synthesize_threads(struct perf_tool *tool, 526 perf_event__handler_t process, 527 struct machine *machine, bool mmap_data) 528 { 529 DIR *proc; 530 char proc_path[PATH_MAX]; 531 struct dirent dirent, *next; 532 union perf_event *comm_event, *mmap_event, *fork_event; 533 int err = -1; 534 535 if (machine__is_default_guest(machine)) 536 return 0; 537 538 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size); 539 if (comm_event == NULL) 540 goto out; 541 542 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size); 543 if (mmap_event == NULL) 544 goto out_free_comm; 545 546 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); 547 if (fork_event == NULL) 548 goto out_free_mmap; 549 550 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir); 551 proc = opendir(proc_path); 552 553 if (proc == NULL) 554 goto out_free_fork; 555 556 while (!readdir_r(proc, &dirent, &next) && next) { 557 char *end; 558 pid_t pid = strtol(dirent.d_name, &end, 10); 559 560 if (*end) /* only interested in proper numerical dirents */ 561 continue; 562 /* 563 * We may race with exiting thread, so don't stop just because 564 * one thread couldn't be synthesized. 565 */ 566 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid, 567 1, process, tool, machine, mmap_data); 568 } 569 570 err = 0; 571 closedir(proc); 572 out_free_fork: 573 free(fork_event); 574 out_free_mmap: 575 free(mmap_event); 576 out_free_comm: 577 free(comm_event); 578 out: 579 return err; 580 } 581 582 struct process_symbol_args { 583 const char *name; 584 u64 start; 585 }; 586 587 static int find_symbol_cb(void *arg, const char *name, char type, 588 u64 start) 589 { 590 struct process_symbol_args *args = arg; 591 592 /* 593 * Must be a function or at least an alias, as in PARISC64, where "_text" is 594 * an 'A' to the same address as "_stext". 595 */ 596 if (!(symbol_type__is_a(type, MAP__FUNCTION) || 597 type == 'A') || strcmp(name, args->name)) 598 return 0; 599 600 args->start = start; 601 return 1; 602 } 603 604 u64 kallsyms__get_function_start(const char *kallsyms_filename, 605 const char *symbol_name) 606 { 607 struct process_symbol_args args = { .name = symbol_name, }; 608 609 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0) 610 return 0; 611 612 return args.start; 613 } 614 615 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool, 616 perf_event__handler_t process, 617 struct machine *machine) 618 { 619 size_t size; 620 const char *mmap_name; 621 char name_buff[PATH_MAX]; 622 struct map *map; 623 struct kmap *kmap; 624 int err; 625 union perf_event *event; 626 627 if (machine->vmlinux_maps[0] == NULL) 628 return -1; 629 630 /* 631 * We should get this from /sys/kernel/sections/.text, but till that is 632 * available use this, and after it is use this as a fallback for older 633 * kernels. 634 */ 635 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size)); 636 if (event == NULL) { 637 pr_debug("Not enough memory synthesizing mmap event " 638 "for kernel modules\n"); 639 return -1; 640 } 641 642 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff)); 643 if (machine__is_host(machine)) { 644 /* 645 * kernel uses PERF_RECORD_MISC_USER for user space maps, 646 * see kernel/perf_event.c __perf_event_mmap 647 */ 648 event->header.misc = PERF_RECORD_MISC_KERNEL; 649 } else { 650 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; 651 } 652 653 map = machine->vmlinux_maps[MAP__FUNCTION]; 654 kmap = map__kmap(map); 655 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename), 656 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1; 657 size = PERF_ALIGN(size, sizeof(u64)); 658 event->mmap.header.type = PERF_RECORD_MMAP; 659 event->mmap.header.size = (sizeof(event->mmap) - 660 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size); 661 event->mmap.pgoff = kmap->ref_reloc_sym->addr; 662 event->mmap.start = map->start; 663 event->mmap.len = map->end - event->mmap.start; 664 event->mmap.pid = machine->pid; 665 666 err = process(tool, event, &synth_sample, machine); 667 free(event); 668 669 return err; 670 } 671 672 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp) 673 { 674 const char *s; 675 676 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC) 677 s = " exec"; 678 else 679 s = ""; 680 681 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid); 682 } 683 684 int perf_event__process_comm(struct perf_tool *tool __maybe_unused, 685 union perf_event *event, 686 struct perf_sample *sample, 687 struct machine *machine) 688 { 689 return machine__process_comm_event(machine, event, sample); 690 } 691 692 int perf_event__process_lost(struct perf_tool *tool __maybe_unused, 693 union perf_event *event, 694 struct perf_sample *sample, 695 struct machine *machine) 696 { 697 return machine__process_lost_event(machine, event, sample); 698 } 699 700 int perf_event__process_aux(struct perf_tool *tool __maybe_unused, 701 union perf_event *event, 702 struct perf_sample *sample __maybe_unused, 703 struct machine *machine) 704 { 705 return machine__process_aux_event(machine, event); 706 } 707 708 int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused, 709 union perf_event *event, 710 struct perf_sample *sample __maybe_unused, 711 struct machine *machine) 712 { 713 return machine__process_itrace_start_event(machine, event); 714 } 715 716 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp) 717 { 718 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n", 719 event->mmap.pid, event->mmap.tid, event->mmap.start, 720 event->mmap.len, event->mmap.pgoff, 721 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x', 722 event->mmap.filename); 723 } 724 725 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp) 726 { 727 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 728 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n", 729 event->mmap2.pid, event->mmap2.tid, event->mmap2.start, 730 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj, 731 event->mmap2.min, event->mmap2.ino, 732 event->mmap2.ino_generation, 733 (event->mmap2.prot & PROT_READ) ? 'r' : '-', 734 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-', 735 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-', 736 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p', 737 event->mmap2.filename); 738 } 739 740 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused, 741 union perf_event *event, 742 struct perf_sample *sample, 743 struct machine *machine) 744 { 745 return machine__process_mmap_event(machine, event, sample); 746 } 747 748 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused, 749 union perf_event *event, 750 struct perf_sample *sample, 751 struct machine *machine) 752 { 753 return machine__process_mmap2_event(machine, event, sample); 754 } 755 756 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp) 757 { 758 return fprintf(fp, "(%d:%d):(%d:%d)\n", 759 event->fork.pid, event->fork.tid, 760 event->fork.ppid, event->fork.ptid); 761 } 762 763 int perf_event__process_fork(struct perf_tool *tool __maybe_unused, 764 union perf_event *event, 765 struct perf_sample *sample, 766 struct machine *machine) 767 { 768 return machine__process_fork_event(machine, event, sample); 769 } 770 771 int perf_event__process_exit(struct perf_tool *tool __maybe_unused, 772 union perf_event *event, 773 struct perf_sample *sample, 774 struct machine *machine) 775 { 776 return machine__process_exit_event(machine, event, sample); 777 } 778 779 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp) 780 { 781 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n", 782 event->aux.aux_offset, event->aux.aux_size, 783 event->aux.flags, 784 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "", 785 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : ""); 786 } 787 788 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp) 789 { 790 return fprintf(fp, " pid: %u tid: %u\n", 791 event->itrace_start.pid, event->itrace_start.tid); 792 } 793 794 size_t perf_event__fprintf(union perf_event *event, FILE *fp) 795 { 796 size_t ret = fprintf(fp, "PERF_RECORD_%s", 797 perf_event__name(event->header.type)); 798 799 switch (event->header.type) { 800 case PERF_RECORD_COMM: 801 ret += perf_event__fprintf_comm(event, fp); 802 break; 803 case PERF_RECORD_FORK: 804 case PERF_RECORD_EXIT: 805 ret += perf_event__fprintf_task(event, fp); 806 break; 807 case PERF_RECORD_MMAP: 808 ret += perf_event__fprintf_mmap(event, fp); 809 break; 810 case PERF_RECORD_MMAP2: 811 ret += perf_event__fprintf_mmap2(event, fp); 812 break; 813 case PERF_RECORD_AUX: 814 ret += perf_event__fprintf_aux(event, fp); 815 break; 816 case PERF_RECORD_ITRACE_START: 817 ret += perf_event__fprintf_itrace_start(event, fp); 818 break; 819 default: 820 ret += fprintf(fp, "\n"); 821 } 822 823 return ret; 824 } 825 826 int perf_event__process(struct perf_tool *tool __maybe_unused, 827 union perf_event *event, 828 struct perf_sample *sample, 829 struct machine *machine) 830 { 831 return machine__process_event(machine, event, sample); 832 } 833 834 void thread__find_addr_map(struct thread *thread, u8 cpumode, 835 enum map_type type, u64 addr, 836 struct addr_location *al) 837 { 838 struct map_groups *mg = thread->mg; 839 struct machine *machine = mg->machine; 840 bool load_map = false; 841 842 al->machine = machine; 843 al->thread = thread; 844 al->addr = addr; 845 al->cpumode = cpumode; 846 al->filtered = 0; 847 848 if (machine == NULL) { 849 al->map = NULL; 850 return; 851 } 852 853 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) { 854 al->level = 'k'; 855 mg = &machine->kmaps; 856 load_map = true; 857 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) { 858 al->level = '.'; 859 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { 860 al->level = 'g'; 861 mg = &machine->kmaps; 862 load_map = true; 863 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) { 864 al->level = 'u'; 865 } else { 866 al->level = 'H'; 867 al->map = NULL; 868 869 if ((cpumode == PERF_RECORD_MISC_GUEST_USER || 870 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) && 871 !perf_guest) 872 al->filtered |= (1 << HIST_FILTER__GUEST); 873 if ((cpumode == PERF_RECORD_MISC_USER || 874 cpumode == PERF_RECORD_MISC_KERNEL) && 875 !perf_host) 876 al->filtered |= (1 << HIST_FILTER__HOST); 877 878 return; 879 } 880 try_again: 881 al->map = map_groups__find(mg, type, al->addr); 882 if (al->map == NULL) { 883 /* 884 * If this is outside of all known maps, and is a negative 885 * address, try to look it up in the kernel dso, as it might be 886 * a vsyscall or vdso (which executes in user-mode). 887 * 888 * XXX This is nasty, we should have a symbol list in the 889 * "[vdso]" dso, but for now lets use the old trick of looking 890 * in the whole kernel symbol list. 891 */ 892 if (cpumode == PERF_RECORD_MISC_USER && machine && 893 mg != &machine->kmaps && 894 machine__kernel_ip(machine, al->addr)) { 895 mg = &machine->kmaps; 896 load_map = true; 897 goto try_again; 898 } 899 } else { 900 /* 901 * Kernel maps might be changed when loading symbols so loading 902 * must be done prior to using kernel maps. 903 */ 904 if (load_map) 905 map__load(al->map, machine->symbol_filter); 906 al->addr = al->map->map_ip(al->map, al->addr); 907 } 908 } 909 910 void thread__find_addr_location(struct thread *thread, 911 u8 cpumode, enum map_type type, u64 addr, 912 struct addr_location *al) 913 { 914 thread__find_addr_map(thread, cpumode, type, addr, al); 915 if (al->map != NULL) 916 al->sym = map__find_symbol(al->map, al->addr, 917 thread->mg->machine->symbol_filter); 918 else 919 al->sym = NULL; 920 } 921 922 int perf_event__preprocess_sample(const union perf_event *event, 923 struct machine *machine, 924 struct addr_location *al, 925 struct perf_sample *sample) 926 { 927 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 928 struct thread *thread = machine__findnew_thread(machine, sample->pid, 929 sample->tid); 930 931 if (thread == NULL) 932 return -1; 933 934 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid); 935 /* 936 * Have we already created the kernel maps for this machine? 937 * 938 * This should have happened earlier, when we processed the kernel MMAP 939 * events, but for older perf.data files there was no such thing, so do 940 * it now. 941 */ 942 if (cpumode == PERF_RECORD_MISC_KERNEL && 943 machine->vmlinux_maps[MAP__FUNCTION] == NULL) 944 machine__create_kernel_maps(machine); 945 946 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al); 947 dump_printf(" ...... dso: %s\n", 948 al->map ? al->map->dso->long_name : 949 al->level == 'H' ? "[hypervisor]" : "<not found>"); 950 951 if (thread__is_filtered(thread)) 952 al->filtered |= (1 << HIST_FILTER__THREAD); 953 954 al->sym = NULL; 955 al->cpu = sample->cpu; 956 957 if (al->map) { 958 struct dso *dso = al->map->dso; 959 960 if (symbol_conf.dso_list && 961 (!dso || !(strlist__has_entry(symbol_conf.dso_list, 962 dso->short_name) || 963 (dso->short_name != dso->long_name && 964 strlist__has_entry(symbol_conf.dso_list, 965 dso->long_name))))) { 966 al->filtered |= (1 << HIST_FILTER__DSO); 967 } 968 969 al->sym = map__find_symbol(al->map, al->addr, 970 machine->symbol_filter); 971 } 972 973 if (symbol_conf.sym_list && 974 (!al->sym || !strlist__has_entry(symbol_conf.sym_list, 975 al->sym->name))) { 976 al->filtered |= (1 << HIST_FILTER__SYMBOL); 977 } 978 979 return 0; 980 } 981 982 bool is_bts_event(struct perf_event_attr *attr) 983 { 984 return attr->type == PERF_TYPE_HARDWARE && 985 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && 986 attr->sample_period == 1; 987 } 988 989 bool sample_addr_correlates_sym(struct perf_event_attr *attr) 990 { 991 if (attr->type == PERF_TYPE_SOFTWARE && 992 (attr->config == PERF_COUNT_SW_PAGE_FAULTS || 993 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN || 994 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)) 995 return true; 996 997 if (is_bts_event(attr)) 998 return true; 999 1000 return false; 1001 } 1002 1003 void perf_event__preprocess_sample_addr(union perf_event *event, 1004 struct perf_sample *sample, 1005 struct thread *thread, 1006 struct addr_location *al) 1007 { 1008 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 1009 1010 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al); 1011 if (!al->map) 1012 thread__find_addr_map(thread, cpumode, MAP__VARIABLE, 1013 sample->addr, al); 1014 1015 al->cpu = sample->cpu; 1016 al->sym = NULL; 1017 1018 if (al->map) 1019 al->sym = map__find_symbol(al->map, al->addr, NULL); 1020 } 1021