1 #include <errno.h> 2 #include <fcntl.h> 3 #include <inttypes.h> 4 #include <linux/compiler.h> 5 #include <linux/kernel.h> 6 #include <linux/types.h> 7 #include <perf/cpumap.h> 8 #include <perf/event.h> 9 #include <stdio.h> 10 #include <sys/types.h> 11 #include <sys/stat.h> 12 #include <unistd.h> 13 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */ 14 #include <linux/perf_event.h> 15 #include <linux/zalloc.h> 16 #include "cpumap.h" 17 #include "dso.h" 18 #include "event.h" 19 #include "debug.h" 20 #include "hist.h" 21 #include "machine.h" 22 #include "sort.h" 23 #include "string2.h" 24 #include "strlist.h" 25 #include "thread.h" 26 #include "thread_map.h" 27 #include "time-utils.h" 28 #include <linux/ctype.h> 29 #include "map.h" 30 #include "util/namespaces.h" 31 #include "symbol.h" 32 #include "symbol/kallsyms.h" 33 #include "asm/bug.h" 34 #include "stat.h" 35 #include "session.h" 36 #include "bpf-event.h" 37 #include "print_binary.h" 38 #include "tool.h" 39 #include "util.h" 40 41 static const char *perf_event__names[] = { 42 [0] = "TOTAL", 43 [PERF_RECORD_MMAP] = "MMAP", 44 [PERF_RECORD_MMAP2] = "MMAP2", 45 [PERF_RECORD_LOST] = "LOST", 46 [PERF_RECORD_COMM] = "COMM", 47 [PERF_RECORD_EXIT] = "EXIT", 48 [PERF_RECORD_THROTTLE] = "THROTTLE", 49 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", 50 [PERF_RECORD_FORK] = "FORK", 51 [PERF_RECORD_READ] = "READ", 52 [PERF_RECORD_SAMPLE] = "SAMPLE", 53 [PERF_RECORD_AUX] = "AUX", 54 [PERF_RECORD_ITRACE_START] = "ITRACE_START", 55 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES", 56 [PERF_RECORD_SWITCH] = "SWITCH", 57 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE", 58 [PERF_RECORD_NAMESPACES] = "NAMESPACES", 59 [PERF_RECORD_KSYMBOL] = "KSYMBOL", 60 [PERF_RECORD_BPF_EVENT] = "BPF_EVENT", 61 [PERF_RECORD_CGROUP] = "CGROUP", 62 [PERF_RECORD_TEXT_POKE] = "TEXT_POKE", 63 [PERF_RECORD_AUX_OUTPUT_HW_ID] = "AUX_OUTPUT_HW_ID", 64 [PERF_RECORD_CALLCHAIN_DEFERRED] = "CALLCHAIN_DEFERRED", 65 [PERF_RECORD_HEADER_ATTR] = "ATTR", 66 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", 67 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA", 68 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID", 69 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND", 70 [PERF_RECORD_ID_INDEX] = "ID_INDEX", 71 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO", 72 [PERF_RECORD_AUXTRACE] = "AUXTRACE", 73 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR", 74 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP", 75 [PERF_RECORD_CPU_MAP] = "CPU_MAP", 76 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG", 77 [PERF_RECORD_STAT] = "STAT", 78 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND", 79 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE", 80 [PERF_RECORD_TIME_CONV] = "TIME_CONV", 81 [PERF_RECORD_HEADER_FEATURE] = "FEATURE", 82 [PERF_RECORD_COMPRESSED] = "COMPRESSED", 83 [PERF_RECORD_FINISHED_INIT] = "FINISHED_INIT", 84 [PERF_RECORD_COMPRESSED2] = "COMPRESSED2", 85 [PERF_RECORD_BPF_METADATA] = "BPF_METADATA", 86 [PERF_RECORD_SCHEDSTAT_CPU] = "SCHEDSTAT_CPU", 87 [PERF_RECORD_SCHEDSTAT_DOMAIN] = "SCHEDSTAT_DOMAIN", 88 }; 89 90 const char *perf_event__name(unsigned int id) 91 { 92 if (id >= ARRAY_SIZE(perf_event__names)) 93 return "INVALID"; 94 if (!perf_event__names[id]) 95 return "UNKNOWN"; 96 return perf_event__names[id]; 97 } 98 99 struct process_symbol_args { 100 const char *name; 101 u64 start; 102 }; 103 104 static int find_func_symbol_cb(void *arg, const char *name, char type, 105 u64 start) 106 { 107 struct process_symbol_args *args = arg; 108 109 /* 110 * Must be a function or at least an alias, as in PARISC64, where "_text" is 111 * an 'A' to the same address as "_stext". 112 */ 113 if (!(kallsyms__is_function(type) || 114 type == 'A') || strcmp(name, args->name)) 115 return 0; 116 117 args->start = start; 118 return 1; 119 } 120 121 static int find_any_symbol_cb(void *arg, const char *name, 122 char type __maybe_unused, u64 start) 123 { 124 struct process_symbol_args *args = arg; 125 126 if (strcmp(name, args->name)) 127 return 0; 128 129 args->start = start; 130 return 1; 131 } 132 133 int kallsyms__get_function_start(const char *kallsyms_filename, 134 const char *symbol_name, u64 *addr) 135 { 136 struct process_symbol_args args = { .name = symbol_name, }; 137 138 if (kallsyms__parse(kallsyms_filename, &args, find_func_symbol_cb) <= 0) 139 return -1; 140 141 *addr = args.start; 142 return 0; 143 } 144 145 int kallsyms__get_symbol_start(const char *kallsyms_filename, 146 const char *symbol_name, u64 *addr) 147 { 148 struct process_symbol_args args = { .name = symbol_name, }; 149 150 if (kallsyms__parse(kallsyms_filename, &args, find_any_symbol_cb) <= 0) 151 return -1; 152 153 *addr = args.start; 154 return 0; 155 } 156 157 void perf_event__read_stat_config(struct perf_stat_config *config, 158 struct perf_record_stat_config *event) 159 { 160 unsigned i; 161 162 for (i = 0; i < event->nr; i++) { 163 164 switch (event->data[i].tag) { 165 #define CASE(__term, __val) \ 166 case PERF_STAT_CONFIG_TERM__##__term: \ 167 config->__val = event->data[i].val; \ 168 break; 169 170 CASE(AGGR_MODE, aggr_mode) 171 CASE(SCALE, scale) 172 CASE(INTERVAL, interval) 173 CASE(AGGR_LEVEL, aggr_level) 174 #undef CASE 175 default: 176 pr_warning("unknown stat config term %" PRI_lu64 "\n", 177 event->data[i].tag); 178 } 179 } 180 } 181 182 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp) 183 { 184 const char *s; 185 186 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC) 187 s = " exec"; 188 else 189 s = ""; 190 191 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid); 192 } 193 194 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp) 195 { 196 size_t ret = 0; 197 struct perf_ns_link_info *ns_link_info; 198 u32 nr_namespaces, idx; 199 200 ns_link_info = event->namespaces.link_info; 201 nr_namespaces = event->namespaces.nr_namespaces; 202 203 ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[", 204 event->namespaces.pid, 205 event->namespaces.tid, 206 nr_namespaces); 207 208 for (idx = 0; idx < nr_namespaces; idx++) { 209 if (idx && (idx % 4 == 0)) 210 ret += fprintf(fp, "\n\t\t "); 211 212 ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx, 213 perf_ns__name(idx), (u64)ns_link_info[idx].dev, 214 (u64)ns_link_info[idx].ino, 215 ((idx + 1) != nr_namespaces) ? ", " : "]\n"); 216 } 217 218 return ret; 219 } 220 221 size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp) 222 { 223 return fprintf(fp, " cgroup: %" PRI_lu64 " %s\n", 224 event->cgroup.id, event->cgroup.path); 225 } 226 227 int perf_event__process_comm(const struct perf_tool *tool __maybe_unused, 228 union perf_event *event, 229 struct perf_sample *sample, 230 struct machine *machine) 231 { 232 return machine__process_comm_event(machine, event, sample); 233 } 234 235 int perf_event__process_namespaces(const struct perf_tool *tool __maybe_unused, 236 union perf_event *event, 237 struct perf_sample *sample, 238 struct machine *machine) 239 { 240 return machine__process_namespaces_event(machine, event, sample); 241 } 242 243 int perf_event__process_cgroup(const struct perf_tool *tool __maybe_unused, 244 union perf_event *event, 245 struct perf_sample *sample, 246 struct machine *machine) 247 { 248 return machine__process_cgroup_event(machine, event, sample); 249 } 250 251 int perf_event__process_lost(const struct perf_tool *tool __maybe_unused, 252 union perf_event *event, 253 struct perf_sample *sample, 254 struct machine *machine) 255 { 256 return machine__process_lost_event(machine, event, sample); 257 } 258 259 int perf_event__process_aux(const struct perf_tool *tool __maybe_unused, 260 union perf_event *event, 261 struct perf_sample *sample __maybe_unused, 262 struct machine *machine) 263 { 264 return machine__process_aux_event(machine, event); 265 } 266 267 int perf_event__process_itrace_start(const struct perf_tool *tool __maybe_unused, 268 union perf_event *event, 269 struct perf_sample *sample __maybe_unused, 270 struct machine *machine) 271 { 272 return machine__process_itrace_start_event(machine, event); 273 } 274 275 int perf_event__process_aux_output_hw_id(const struct perf_tool *tool __maybe_unused, 276 union perf_event *event, 277 struct perf_sample *sample __maybe_unused, 278 struct machine *machine) 279 { 280 return machine__process_aux_output_hw_id_event(machine, event); 281 } 282 283 int perf_event__process_lost_samples(const struct perf_tool *tool __maybe_unused, 284 union perf_event *event, 285 struct perf_sample *sample, 286 struct machine *machine) 287 { 288 return machine__process_lost_samples_event(machine, event, sample); 289 } 290 291 int perf_event__process_switch(const struct perf_tool *tool __maybe_unused, 292 union perf_event *event, 293 struct perf_sample *sample __maybe_unused, 294 struct machine *machine) 295 { 296 return machine__process_switch_event(machine, event); 297 } 298 299 int perf_event__process_ksymbol(const struct perf_tool *tool __maybe_unused, 300 union perf_event *event, 301 struct perf_sample *sample __maybe_unused, 302 struct machine *machine) 303 { 304 return machine__process_ksymbol(machine, event, sample); 305 } 306 307 int perf_event__process_bpf(const struct perf_tool *tool __maybe_unused, 308 union perf_event *event, 309 struct perf_sample *sample, 310 struct machine *machine) 311 { 312 return machine__process_bpf(machine, event, sample); 313 } 314 315 int perf_event__process_text_poke(const struct perf_tool *tool __maybe_unused, 316 union perf_event *event, 317 struct perf_sample *sample, 318 struct machine *machine) 319 { 320 return machine__process_text_poke(machine, event, sample); 321 } 322 323 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp) 324 { 325 return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64 "]: %c %s\n", 326 event->mmap.pid, event->mmap.tid, event->mmap.start, 327 event->mmap.len, event->mmap.pgoff, 328 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x', 329 event->mmap.filename); 330 } 331 332 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp) 333 { 334 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) { 335 char sbuild_id[SBUILD_ID_SIZE]; 336 struct build_id bid; 337 338 build_id__init(&bid, event->mmap2.build_id, 339 event->mmap2.build_id_size); 340 build_id__snprintf(&bid, sbuild_id, sizeof(sbuild_id)); 341 342 return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64 343 " <%s>]: %c%c%c%c %s\n", 344 event->mmap2.pid, event->mmap2.tid, event->mmap2.start, 345 event->mmap2.len, event->mmap2.pgoff, sbuild_id, 346 (event->mmap2.prot & PROT_READ) ? 'r' : '-', 347 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-', 348 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-', 349 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p', 350 event->mmap2.filename); 351 } else { 352 return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64 353 " %02x:%02x %"PRI_lu64" %"PRI_lu64"]: %c%c%c%c %s\n", 354 event->mmap2.pid, event->mmap2.tid, event->mmap2.start, 355 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj, 356 event->mmap2.min, event->mmap2.ino, 357 event->mmap2.ino_generation, 358 (event->mmap2.prot & PROT_READ) ? 'r' : '-', 359 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-', 360 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-', 361 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p', 362 event->mmap2.filename); 363 } 364 } 365 366 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp) 367 { 368 struct perf_thread_map *threads = thread_map__new_event(&event->thread_map); 369 size_t ret; 370 371 ret = fprintf(fp, " nr: "); 372 373 if (threads) 374 ret += thread_map__fprintf(threads, fp); 375 else 376 ret += fprintf(fp, "failed to get threads from event\n"); 377 378 perf_thread_map__put(threads); 379 return ret; 380 } 381 382 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp) 383 { 384 struct perf_cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data); 385 size_t ret; 386 387 ret = fprintf(fp, ": "); 388 389 if (cpus) 390 ret += cpu_map__fprintf(cpus, fp); 391 else 392 ret += fprintf(fp, "failed to get cpumap from event\n"); 393 394 perf_cpu_map__put(cpus); 395 return ret; 396 } 397 398 int perf_event__process_mmap(const struct perf_tool *tool __maybe_unused, 399 union perf_event *event, 400 struct perf_sample *sample, 401 struct machine *machine) 402 { 403 return machine__process_mmap_event(machine, event, sample); 404 } 405 406 int perf_event__process_mmap2(const struct perf_tool *tool __maybe_unused, 407 union perf_event *event, 408 struct perf_sample *sample, 409 struct machine *machine) 410 { 411 return machine__process_mmap2_event(machine, event, sample); 412 } 413 414 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp) 415 { 416 return fprintf(fp, "(%d:%d):(%d:%d)\n", 417 event->fork.pid, event->fork.tid, 418 event->fork.ppid, event->fork.ptid); 419 } 420 421 int perf_event__process_fork(const struct perf_tool *tool __maybe_unused, 422 union perf_event *event, 423 struct perf_sample *sample, 424 struct machine *machine) 425 { 426 return machine__process_fork_event(machine, event, sample); 427 } 428 429 int perf_event__process_exit(const struct perf_tool *tool __maybe_unused, 430 union perf_event *event, 431 struct perf_sample *sample, 432 struct machine *machine) 433 { 434 return machine__process_exit_event(machine, event, sample); 435 } 436 437 int perf_event__exit_del_thread(const struct perf_tool *tool __maybe_unused, 438 union perf_event *event, 439 struct perf_sample *sample __maybe_unused, 440 struct machine *machine) 441 { 442 struct thread *thread = machine__findnew_thread(machine, 443 event->fork.pid, 444 event->fork.tid); 445 446 dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid, 447 event->fork.ppid, event->fork.ptid); 448 449 if (thread) { 450 machine__remove_thread(machine, thread); 451 thread__put(thread); 452 } 453 454 return 0; 455 } 456 457 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp) 458 { 459 return fprintf(fp, " offset: %#"PRI_lx64" size: %#"PRI_lx64" flags: %#"PRI_lx64" [%s%s%s%s]\n", 460 event->aux.aux_offset, event->aux.aux_size, 461 event->aux.flags, 462 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "", 463 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "", 464 event->aux.flags & PERF_AUX_FLAG_PARTIAL ? "P" : "", 465 event->aux.flags & PERF_AUX_FLAG_COLLISION ? "C" : ""); 466 } 467 468 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp) 469 { 470 return fprintf(fp, " pid: %u tid: %u\n", 471 event->itrace_start.pid, event->itrace_start.tid); 472 } 473 474 size_t perf_event__fprintf_aux_output_hw_id(union perf_event *event, FILE *fp) 475 { 476 return fprintf(fp, " hw_id: %#"PRI_lx64"\n", 477 event->aux_output_hw_id.hw_id); 478 } 479 480 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp) 481 { 482 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT; 483 const char *in_out = !out ? "IN " : 484 !(event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT) ? 485 "OUT " : "OUT preempt"; 486 487 if (event->header.type == PERF_RECORD_SWITCH) 488 return fprintf(fp, " %s\n", in_out); 489 490 return fprintf(fp, " %s %s pid/tid: %5d/%-5d\n", 491 in_out, out ? "next" : "prev", 492 event->context_switch.next_prev_pid, 493 event->context_switch.next_prev_tid); 494 } 495 496 static size_t perf_event__fprintf_lost(union perf_event *event, FILE *fp) 497 { 498 return fprintf(fp, " lost %" PRI_lu64 "\n", event->lost.lost); 499 } 500 501 size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp) 502 { 503 return fprintf(fp, " addr %" PRI_lx64 " len %u type %u flags 0x%x name %s\n", 504 event->ksymbol.addr, event->ksymbol.len, 505 event->ksymbol.ksym_type, 506 event->ksymbol.flags, event->ksymbol.name); 507 } 508 509 size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp) 510 { 511 return fprintf(fp, " type %u, flags %u, id %u\n", 512 event->bpf.type, event->bpf.flags, event->bpf.id); 513 } 514 515 size_t perf_event__fprintf_bpf_metadata(union perf_event *event, FILE *fp) 516 { 517 struct perf_record_bpf_metadata *metadata = &event->bpf_metadata; 518 size_t ret; 519 520 ret = fprintf(fp, " prog %s\n", metadata->prog_name); 521 for (__u32 i = 0; i < metadata->nr_entries; i++) { 522 ret += fprintf(fp, " entry %d: %20s = %s\n", i, 523 metadata->entries[i].key, 524 metadata->entries[i].value); 525 } 526 return ret; 527 } 528 529 static int text_poke_printer(enum binary_printer_ops op, unsigned int val, 530 void *extra, FILE *fp) 531 { 532 bool old = *(bool *)extra; 533 534 switch ((int)op) { 535 case BINARY_PRINT_LINE_BEGIN: 536 return fprintf(fp, " %s bytes:", old ? "Old" : "New"); 537 case BINARY_PRINT_NUM_DATA: 538 return fprintf(fp, " %02x", val); 539 case BINARY_PRINT_LINE_END: 540 return fprintf(fp, "\n"); 541 default: 542 return 0; 543 } 544 } 545 546 size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine, FILE *fp) 547 { 548 struct perf_record_text_poke_event *tp = &event->text_poke; 549 size_t ret; 550 bool old; 551 552 ret = fprintf(fp, " %" PRI_lx64 " ", tp->addr); 553 if (machine) { 554 struct addr_location al; 555 556 addr_location__init(&al); 557 al.map = maps__find(machine__kernel_maps(machine), tp->addr); 558 if (al.map && map__load(al.map) >= 0) { 559 al.addr = map__map_ip(al.map, tp->addr); 560 al.sym = map__find_symbol(al.map, al.addr); 561 if (al.sym) 562 ret += symbol__fprintf_symname_offs(al.sym, &al, fp); 563 } 564 addr_location__exit(&al); 565 } 566 ret += fprintf(fp, " old len %u new len %u\n", tp->old_len, tp->new_len); 567 old = true; 568 ret += binary__fprintf(tp->bytes, tp->old_len, 16, text_poke_printer, 569 &old, fp); 570 old = false; 571 ret += binary__fprintf(tp->bytes + tp->old_len, tp->new_len, 16, 572 text_poke_printer, &old, fp); 573 return ret; 574 } 575 576 size_t perf_event__fprintf_schedstat_cpu(union perf_event *event, FILE *fp) 577 { 578 struct perf_record_schedstat_cpu *cs = &event->schedstat_cpu; 579 size_t size = fprintf(fp, "\ncpu%u ", cs->cpu); 580 __u16 version = cs->version; 581 582 #define CPU_FIELD(_type, _name, _desc, _format, _is_pct, _pct_of, _ver) \ 583 size += fprintf(fp, "%" PRIu64 " ", (uint64_t)cs->_ver._name) 584 585 if (version == 15) { 586 #include <perf/schedstat-v15.h> 587 return size; 588 } else if (version == 16) { 589 #include <perf/schedstat-v16.h> 590 return size; 591 } else if (version == 17) { 592 #include <perf/schedstat-v17.h> 593 return size; 594 } 595 #undef CPU_FIELD 596 597 return fprintf(fp, "Unsupported /proc/schedstat version %d.\n", 598 event->schedstat_cpu.version); 599 } 600 601 size_t perf_event__fprintf_schedstat_domain(union perf_event *event, FILE *fp) 602 { 603 struct perf_record_schedstat_domain *ds = &event->schedstat_domain; 604 __u16 version = ds->version; 605 size_t size = fprintf(fp, "\ndomain%u ", ds->domain); 606 607 #define DOMAIN_FIELD(_type, _name, _desc, _format, _is_jiffies, _ver) \ 608 size += fprintf(fp, "%" PRIu64 " ", (uint64_t)ds->_ver._name) 609 610 if (version == 15) { 611 #include <perf/schedstat-v15.h> 612 return size; 613 } else if (version == 16) { 614 #include <perf/schedstat-v16.h> 615 return size; 616 } else if (version == 17) { 617 #include <perf/schedstat-v17.h> 618 return size; 619 } 620 #undef DOMAIN_FIELD 621 622 return fprintf(fp, "Unsupported /proc/schedstat version %d.\n", 623 event->schedstat_domain.version); 624 } 625 626 size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp) 627 { 628 size_t ret = fprintf(fp, "PERF_RECORD_%s", 629 perf_event__name(event->header.type)); 630 631 switch (event->header.type) { 632 case PERF_RECORD_COMM: 633 ret += perf_event__fprintf_comm(event, fp); 634 break; 635 case PERF_RECORD_FORK: 636 case PERF_RECORD_EXIT: 637 ret += perf_event__fprintf_task(event, fp); 638 break; 639 case PERF_RECORD_MMAP: 640 ret += perf_event__fprintf_mmap(event, fp); 641 break; 642 case PERF_RECORD_NAMESPACES: 643 ret += perf_event__fprintf_namespaces(event, fp); 644 break; 645 case PERF_RECORD_CGROUP: 646 ret += perf_event__fprintf_cgroup(event, fp); 647 break; 648 case PERF_RECORD_MMAP2: 649 ret += perf_event__fprintf_mmap2(event, fp); 650 break; 651 case PERF_RECORD_AUX: 652 ret += perf_event__fprintf_aux(event, fp); 653 break; 654 case PERF_RECORD_ITRACE_START: 655 ret += perf_event__fprintf_itrace_start(event, fp); 656 break; 657 case PERF_RECORD_SWITCH: 658 case PERF_RECORD_SWITCH_CPU_WIDE: 659 ret += perf_event__fprintf_switch(event, fp); 660 break; 661 case PERF_RECORD_LOST: 662 ret += perf_event__fprintf_lost(event, fp); 663 break; 664 case PERF_RECORD_KSYMBOL: 665 ret += perf_event__fprintf_ksymbol(event, fp); 666 break; 667 case PERF_RECORD_BPF_EVENT: 668 ret += perf_event__fprintf_bpf(event, fp); 669 break; 670 case PERF_RECORD_TEXT_POKE: 671 ret += perf_event__fprintf_text_poke(event, machine, fp); 672 break; 673 case PERF_RECORD_AUX_OUTPUT_HW_ID: 674 ret += perf_event__fprintf_aux_output_hw_id(event, fp); 675 break; 676 case PERF_RECORD_BPF_METADATA: 677 ret += perf_event__fprintf_bpf_metadata(event, fp); 678 break; 679 default: 680 ret += fprintf(fp, "\n"); 681 } 682 683 return ret; 684 } 685 686 int perf_event__process(const struct perf_tool *tool __maybe_unused, 687 union perf_event *event, 688 struct perf_sample *sample, 689 struct machine *machine) 690 { 691 return machine__process_event(machine, event, sample); 692 } 693 694 struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr, 695 struct addr_location *al) 696 { 697 struct maps *maps = thread__maps(thread); 698 struct machine *machine = maps__machine(maps); 699 bool load_map = false; 700 701 map__zput(al->map); 702 thread__zput(al->thread); 703 al->thread = thread__get(thread); 704 705 al->addr = addr; 706 al->cpumode = cpumode; 707 al->filtered = 0; 708 709 if (machine == NULL) 710 return NULL; 711 712 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) { 713 al->level = 'k'; 714 maps = machine__kernel_maps(machine); 715 load_map = !symbol_conf.lazy_load_kernel_maps; 716 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) { 717 al->level = '.'; 718 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { 719 al->level = 'g'; 720 maps = machine__kernel_maps(machine); 721 load_map = !symbol_conf.lazy_load_kernel_maps; 722 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) { 723 al->level = 'u'; 724 } else { 725 al->level = 'H'; 726 727 if ((cpumode == PERF_RECORD_MISC_GUEST_USER || 728 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) && 729 !perf_guest) 730 al->filtered |= (1 << HIST_FILTER__GUEST); 731 if ((cpumode == PERF_RECORD_MISC_USER || 732 cpumode == PERF_RECORD_MISC_KERNEL) && 733 !perf_host) 734 al->filtered |= (1 << HIST_FILTER__HOST); 735 736 return NULL; 737 } 738 al->map = maps__find(maps, al->addr); 739 if (al->map != NULL) { 740 /* 741 * Kernel maps might be changed when loading symbols so loading 742 * must be done prior to using kernel maps. 743 */ 744 if (load_map) 745 map__load(al->map); 746 al->addr = map__map_ip(al->map, al->addr); 747 } 748 749 return al->map; 750 } 751 752 /* 753 * For branch stacks or branch samples, the sample cpumode might not be correct 754 * because it applies only to the sample 'ip' and not necessary to 'addr' or 755 * branch stack addresses. If possible, use a fallback to deal with those cases. 756 */ 757 struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr, 758 struct addr_location *al) 759 { 760 struct map *map = thread__find_map(thread, cpumode, addr, al); 761 struct machine *machine = maps__machine(thread__maps(thread)); 762 u8 addr_cpumode = machine__addr_cpumode(machine, cpumode, addr); 763 764 if (map || addr_cpumode == cpumode) 765 return map; 766 767 return thread__find_map(thread, addr_cpumode, addr, al); 768 } 769 770 struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode, 771 u64 addr, struct addr_location *al) 772 { 773 al->sym = NULL; 774 if (thread__find_map(thread, cpumode, addr, al)) 775 al->sym = map__find_symbol(al->map, al->addr); 776 return al->sym; 777 } 778 779 struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode, 780 u64 addr, struct addr_location *al) 781 { 782 al->sym = NULL; 783 if (thread__find_map_fb(thread, cpumode, addr, al)) 784 al->sym = map__find_symbol(al->map, al->addr); 785 return al->sym; 786 } 787 788 static bool check_address_range(struct intlist *addr_list, int addr_range, 789 unsigned long addr) 790 { 791 struct int_node *pos; 792 793 intlist__for_each_entry(pos, addr_list) { 794 if (addr >= pos->i && addr < pos->i + addr_range) 795 return true; 796 } 797 798 return false; 799 } 800 801 /* 802 * Callers need to drop the reference to al->thread, obtained in 803 * machine__findnew_thread() 804 */ 805 int machine__resolve(struct machine *machine, struct addr_location *al, 806 struct perf_sample *sample) 807 { 808 struct thread *thread; 809 struct dso *dso; 810 811 if (symbol_conf.guest_code && !machine__is_host(machine)) 812 thread = machine__findnew_guest_code(machine, sample->pid); 813 else 814 thread = machine__findnew_thread(machine, sample->pid, sample->tid); 815 if (thread == NULL) 816 return -1; 817 818 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread__tid(thread)); 819 thread__find_map(thread, sample->cpumode, sample->ip, al); 820 dso = al->map ? map__dso(al->map) : NULL; 821 dump_printf(" ...... dso: %s\n", 822 dso 823 ? dso__long_name(dso) 824 : (al->level == 'H' ? "[hypervisor]" : "<not found>")); 825 826 if (thread__is_filtered(thread)) 827 al->filtered |= (1 << HIST_FILTER__THREAD); 828 829 thread__put(thread); 830 thread = NULL; 831 832 al->sym = NULL; 833 al->cpu = sample->cpu; 834 al->socket = -1; 835 al->srcline = NULL; 836 837 if (al->cpu >= 0) { 838 struct perf_env *env = machine->env; 839 840 if (env && env->cpu) 841 al->socket = env->cpu[al->cpu].socket_id; 842 } 843 844 /* Account for possible out-of-order switch events. */ 845 al->parallelism = max(1, min(machine->parallelism, machine__nr_cpus_avail(machine))); 846 if (test_bit(al->parallelism, symbol_conf.parallelism_filter)) 847 al->filtered |= (1 << HIST_FILTER__PARALLELISM); 848 /* 849 * Multiply it by some const to avoid precision loss or dealing 850 * with floats. The multiplier does not matter otherwise since 851 * we only print it as percents. 852 */ 853 al->latency = sample->period * 1000 / al->parallelism; 854 855 if (al->map) { 856 if (symbol_conf.dso_list && 857 (!dso || !(strlist__has_entry(symbol_conf.dso_list, 858 dso__short_name(dso)) || 859 (dso__short_name(dso) != dso__long_name(dso) && 860 strlist__has_entry(symbol_conf.dso_list, 861 dso__long_name(dso)))))) { 862 al->filtered |= (1 << HIST_FILTER__DSO); 863 } 864 865 al->sym = map__find_symbol(al->map, al->addr); 866 } else if (symbol_conf.dso_list) { 867 al->filtered |= (1 << HIST_FILTER__DSO); 868 } 869 870 if (symbol_conf.sym_list) { 871 int ret = 0; 872 char al_addr_str[32]; 873 size_t sz = sizeof(al_addr_str); 874 875 if (al->sym) { 876 ret = strlist__has_entry(symbol_conf.sym_list, 877 al->sym->name); 878 } 879 if (!ret && al->sym) { 880 snprintf(al_addr_str, sz, "0x%"PRIx64, 881 map__unmap_ip(al->map, al->sym->start)); 882 ret = strlist__has_entry(symbol_conf.sym_list, 883 al_addr_str); 884 } 885 if (!ret && symbol_conf.addr_list && al->map) { 886 unsigned long addr = map__unmap_ip(al->map, al->addr); 887 888 ret = intlist__has_entry(symbol_conf.addr_list, addr); 889 if (!ret && symbol_conf.addr_range) { 890 ret = check_address_range(symbol_conf.addr_list, 891 symbol_conf.addr_range, 892 addr); 893 } 894 } 895 896 if (!ret) 897 al->filtered |= (1 << HIST_FILTER__SYMBOL); 898 } 899 900 return 0; 901 } 902 903 bool is_bts_event(struct perf_event_attr *attr) 904 { 905 return attr->type == PERF_TYPE_HARDWARE && 906 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && 907 attr->sample_period == 1; 908 } 909 910 bool sample_addr_correlates_sym(struct perf_event_attr *attr) 911 { 912 if (attr->type == PERF_TYPE_SOFTWARE && 913 (attr->config == PERF_COUNT_SW_PAGE_FAULTS || 914 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN || 915 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)) 916 return true; 917 918 if (is_bts_event(attr)) 919 return true; 920 921 return false; 922 } 923 924 void thread__resolve(struct thread *thread, struct addr_location *al, 925 struct perf_sample *sample) 926 { 927 thread__find_map_fb(thread, sample->cpumode, sample->addr, al); 928 929 al->cpu = sample->cpu; 930 al->sym = NULL; 931 932 if (al->map) 933 al->sym = map__find_symbol(al->map, al->addr); 934 } 935