1 #include <linux/kernel.h> 2 #include <traceevent/event-parse.h> 3 4 #include <byteswap.h> 5 #include <unistd.h> 6 #include <sys/types.h> 7 #include <sys/mman.h> 8 9 #include "evlist.h" 10 #include "evsel.h" 11 #include "session.h" 12 #include "tool.h" 13 #include "sort.h" 14 #include "util.h" 15 #include "cpumap.h" 16 #include "perf_regs.h" 17 #include "asm/bug.h" 18 19 static int machines__deliver_event(struct machines *machines, 20 struct perf_evlist *evlist, 21 union perf_event *event, 22 struct perf_sample *sample, 23 struct perf_tool *tool, u64 file_offset); 24 25 static int perf_session__open(struct perf_session *session) 26 { 27 struct perf_data_file *file = session->file; 28 29 if (perf_session__read_header(session) < 0) { 30 pr_err("incompatible file format (rerun with -v to learn more)"); 31 return -1; 32 } 33 34 if (perf_data_file__is_pipe(file)) 35 return 0; 36 37 if (!perf_evlist__valid_sample_type(session->evlist)) { 38 pr_err("non matching sample_type"); 39 return -1; 40 } 41 42 if (!perf_evlist__valid_sample_id_all(session->evlist)) { 43 pr_err("non matching sample_id_all"); 44 return -1; 45 } 46 47 if (!perf_evlist__valid_read_format(session->evlist)) { 48 pr_err("non matching read_format"); 49 return -1; 50 } 51 52 return 0; 53 } 54 55 void perf_session__set_id_hdr_size(struct perf_session *session) 56 { 57 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist); 58 59 machines__set_id_hdr_size(&session->machines, id_hdr_size); 60 } 61 62 int perf_session__create_kernel_maps(struct perf_session *session) 63 { 64 int ret = machine__create_kernel_maps(&session->machines.host); 65 66 if (ret >= 0) 67 ret = machines__create_guest_kernel_maps(&session->machines); 68 return ret; 69 } 70 71 static void perf_session__destroy_kernel_maps(struct perf_session *session) 72 { 73 machines__destroy_kernel_maps(&session->machines); 74 } 75 76 static bool perf_session__has_comm_exec(struct perf_session *session) 77 { 78 struct perf_evsel *evsel; 79 80 evlist__for_each(session->evlist, evsel) { 81 if (evsel->attr.comm_exec) 82 return true; 83 } 84 85 return false; 86 } 87 88 static void perf_session__set_comm_exec(struct perf_session *session) 89 { 90 bool comm_exec = perf_session__has_comm_exec(session); 91 92 machines__set_comm_exec(&session->machines, comm_exec); 93 } 94 95 static int ordered_events__deliver_event(struct ordered_events *oe, 96 struct ordered_event *event, 97 struct perf_sample *sample) 98 { 99 return machines__deliver_event(oe->machines, oe->evlist, event->event, 100 sample, oe->tool, event->file_offset); 101 } 102 103 struct perf_session *perf_session__new(struct perf_data_file *file, 104 bool repipe, struct perf_tool *tool) 105 { 106 struct perf_session *session = zalloc(sizeof(*session)); 107 108 if (!session) 109 goto out; 110 111 session->repipe = repipe; 112 machines__init(&session->machines); 113 ordered_events__init(&session->ordered_events, &session->machines, 114 session->evlist, tool, ordered_events__deliver_event); 115 116 if (file) { 117 if (perf_data_file__open(file)) 118 goto out_delete; 119 120 session->file = file; 121 122 if (perf_data_file__is_read(file)) { 123 if (perf_session__open(session) < 0) 124 goto out_close; 125 126 perf_session__set_id_hdr_size(session); 127 perf_session__set_comm_exec(session); 128 } 129 } 130 131 if (!file || perf_data_file__is_write(file)) { 132 /* 133 * In O_RDONLY mode this will be performed when reading the 134 * kernel MMAP event, in perf_event__process_mmap(). 135 */ 136 if (perf_session__create_kernel_maps(session) < 0) 137 pr_warning("Cannot read kernel map\n"); 138 } 139 140 if (tool && tool->ordering_requires_timestamps && 141 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) { 142 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n"); 143 tool->ordered_events = false; 144 } 145 146 return session; 147 148 out_close: 149 perf_data_file__close(file); 150 out_delete: 151 perf_session__delete(session); 152 out: 153 return NULL; 154 } 155 156 static void perf_session__delete_threads(struct perf_session *session) 157 { 158 machine__delete_threads(&session->machines.host); 159 } 160 161 static void perf_session_env__delete(struct perf_session_env *env) 162 { 163 zfree(&env->hostname); 164 zfree(&env->os_release); 165 zfree(&env->version); 166 zfree(&env->arch); 167 zfree(&env->cpu_desc); 168 zfree(&env->cpuid); 169 170 zfree(&env->cmdline); 171 zfree(&env->sibling_cores); 172 zfree(&env->sibling_threads); 173 zfree(&env->numa_nodes); 174 zfree(&env->pmu_mappings); 175 } 176 177 void perf_session__delete(struct perf_session *session) 178 { 179 perf_session__destroy_kernel_maps(session); 180 perf_session__delete_threads(session); 181 perf_session_env__delete(&session->header.env); 182 machines__exit(&session->machines); 183 if (session->file) 184 perf_data_file__close(session->file); 185 free(session); 186 } 187 188 static int process_event_synth_tracing_data_stub(struct perf_tool *tool 189 __maybe_unused, 190 union perf_event *event 191 __maybe_unused, 192 struct perf_session *session 193 __maybe_unused) 194 { 195 dump_printf(": unhandled!\n"); 196 return 0; 197 } 198 199 static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused, 200 union perf_event *event __maybe_unused, 201 struct perf_evlist **pevlist 202 __maybe_unused) 203 { 204 dump_printf(": unhandled!\n"); 205 return 0; 206 } 207 208 static int process_event_sample_stub(struct perf_tool *tool __maybe_unused, 209 union perf_event *event __maybe_unused, 210 struct perf_sample *sample __maybe_unused, 211 struct perf_evsel *evsel __maybe_unused, 212 struct machine *machine __maybe_unused) 213 { 214 dump_printf(": unhandled!\n"); 215 return 0; 216 } 217 218 static int process_event_stub(struct perf_tool *tool __maybe_unused, 219 union perf_event *event __maybe_unused, 220 struct perf_sample *sample __maybe_unused, 221 struct machine *machine __maybe_unused) 222 { 223 dump_printf(": unhandled!\n"); 224 return 0; 225 } 226 227 static int process_build_id_stub(struct perf_tool *tool __maybe_unused, 228 union perf_event *event __maybe_unused, 229 struct perf_session *session __maybe_unused) 230 { 231 dump_printf(": unhandled!\n"); 232 return 0; 233 } 234 235 static int process_finished_round_stub(struct perf_tool *tool __maybe_unused, 236 union perf_event *event __maybe_unused, 237 struct ordered_events *oe __maybe_unused) 238 { 239 dump_printf(": unhandled!\n"); 240 return 0; 241 } 242 243 static int process_finished_round(struct perf_tool *tool, 244 union perf_event *event, 245 struct ordered_events *oe); 246 247 static int process_id_index_stub(struct perf_tool *tool __maybe_unused, 248 union perf_event *event __maybe_unused, 249 struct perf_session *perf_session 250 __maybe_unused) 251 { 252 dump_printf(": unhandled!\n"); 253 return 0; 254 } 255 256 void perf_tool__fill_defaults(struct perf_tool *tool) 257 { 258 if (tool->sample == NULL) 259 tool->sample = process_event_sample_stub; 260 if (tool->mmap == NULL) 261 tool->mmap = process_event_stub; 262 if (tool->mmap2 == NULL) 263 tool->mmap2 = process_event_stub; 264 if (tool->comm == NULL) 265 tool->comm = process_event_stub; 266 if (tool->fork == NULL) 267 tool->fork = process_event_stub; 268 if (tool->exit == NULL) 269 tool->exit = process_event_stub; 270 if (tool->lost == NULL) 271 tool->lost = perf_event__process_lost; 272 if (tool->read == NULL) 273 tool->read = process_event_sample_stub; 274 if (tool->throttle == NULL) 275 tool->throttle = process_event_stub; 276 if (tool->unthrottle == NULL) 277 tool->unthrottle = process_event_stub; 278 if (tool->attr == NULL) 279 tool->attr = process_event_synth_attr_stub; 280 if (tool->tracing_data == NULL) 281 tool->tracing_data = process_event_synth_tracing_data_stub; 282 if (tool->build_id == NULL) 283 tool->build_id = process_build_id_stub; 284 if (tool->finished_round == NULL) { 285 if (tool->ordered_events) 286 tool->finished_round = process_finished_round; 287 else 288 tool->finished_round = process_finished_round_stub; 289 } 290 if (tool->id_index == NULL) 291 tool->id_index = process_id_index_stub; 292 } 293 294 static void swap_sample_id_all(union perf_event *event, void *data) 295 { 296 void *end = (void *) event + event->header.size; 297 int size = end - data; 298 299 BUG_ON(size % sizeof(u64)); 300 mem_bswap_64(data, size); 301 } 302 303 static void perf_event__all64_swap(union perf_event *event, 304 bool sample_id_all __maybe_unused) 305 { 306 struct perf_event_header *hdr = &event->header; 307 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr)); 308 } 309 310 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all) 311 { 312 event->comm.pid = bswap_32(event->comm.pid); 313 event->comm.tid = bswap_32(event->comm.tid); 314 315 if (sample_id_all) { 316 void *data = &event->comm.comm; 317 318 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 319 swap_sample_id_all(event, data); 320 } 321 } 322 323 static void perf_event__mmap_swap(union perf_event *event, 324 bool sample_id_all) 325 { 326 event->mmap.pid = bswap_32(event->mmap.pid); 327 event->mmap.tid = bswap_32(event->mmap.tid); 328 event->mmap.start = bswap_64(event->mmap.start); 329 event->mmap.len = bswap_64(event->mmap.len); 330 event->mmap.pgoff = bswap_64(event->mmap.pgoff); 331 332 if (sample_id_all) { 333 void *data = &event->mmap.filename; 334 335 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 336 swap_sample_id_all(event, data); 337 } 338 } 339 340 static void perf_event__mmap2_swap(union perf_event *event, 341 bool sample_id_all) 342 { 343 event->mmap2.pid = bswap_32(event->mmap2.pid); 344 event->mmap2.tid = bswap_32(event->mmap2.tid); 345 event->mmap2.start = bswap_64(event->mmap2.start); 346 event->mmap2.len = bswap_64(event->mmap2.len); 347 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff); 348 event->mmap2.maj = bswap_32(event->mmap2.maj); 349 event->mmap2.min = bswap_32(event->mmap2.min); 350 event->mmap2.ino = bswap_64(event->mmap2.ino); 351 352 if (sample_id_all) { 353 void *data = &event->mmap2.filename; 354 355 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 356 swap_sample_id_all(event, data); 357 } 358 } 359 static void perf_event__task_swap(union perf_event *event, bool sample_id_all) 360 { 361 event->fork.pid = bswap_32(event->fork.pid); 362 event->fork.tid = bswap_32(event->fork.tid); 363 event->fork.ppid = bswap_32(event->fork.ppid); 364 event->fork.ptid = bswap_32(event->fork.ptid); 365 event->fork.time = bswap_64(event->fork.time); 366 367 if (sample_id_all) 368 swap_sample_id_all(event, &event->fork + 1); 369 } 370 371 static void perf_event__read_swap(union perf_event *event, bool sample_id_all) 372 { 373 event->read.pid = bswap_32(event->read.pid); 374 event->read.tid = bswap_32(event->read.tid); 375 event->read.value = bswap_64(event->read.value); 376 event->read.time_enabled = bswap_64(event->read.time_enabled); 377 event->read.time_running = bswap_64(event->read.time_running); 378 event->read.id = bswap_64(event->read.id); 379 380 if (sample_id_all) 381 swap_sample_id_all(event, &event->read + 1); 382 } 383 384 static void perf_event__throttle_swap(union perf_event *event, 385 bool sample_id_all) 386 { 387 event->throttle.time = bswap_64(event->throttle.time); 388 event->throttle.id = bswap_64(event->throttle.id); 389 event->throttle.stream_id = bswap_64(event->throttle.stream_id); 390 391 if (sample_id_all) 392 swap_sample_id_all(event, &event->throttle + 1); 393 } 394 395 static u8 revbyte(u8 b) 396 { 397 int rev = (b >> 4) | ((b & 0xf) << 4); 398 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2); 399 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1); 400 return (u8) rev; 401 } 402 403 /* 404 * XXX this is hack in attempt to carry flags bitfield 405 * throught endian village. ABI says: 406 * 407 * Bit-fields are allocated from right to left (least to most significant) 408 * on little-endian implementations and from left to right (most to least 409 * significant) on big-endian implementations. 410 * 411 * The above seems to be byte specific, so we need to reverse each 412 * byte of the bitfield. 'Internet' also says this might be implementation 413 * specific and we probably need proper fix and carry perf_event_attr 414 * bitfield flags in separate data file FEAT_ section. Thought this seems 415 * to work for now. 416 */ 417 static void swap_bitfield(u8 *p, unsigned len) 418 { 419 unsigned i; 420 421 for (i = 0; i < len; i++) { 422 *p = revbyte(*p); 423 p++; 424 } 425 } 426 427 /* exported for swapping attributes in file header */ 428 void perf_event__attr_swap(struct perf_event_attr *attr) 429 { 430 attr->type = bswap_32(attr->type); 431 attr->size = bswap_32(attr->size); 432 attr->config = bswap_64(attr->config); 433 attr->sample_period = bswap_64(attr->sample_period); 434 attr->sample_type = bswap_64(attr->sample_type); 435 attr->read_format = bswap_64(attr->read_format); 436 attr->wakeup_events = bswap_32(attr->wakeup_events); 437 attr->bp_type = bswap_32(attr->bp_type); 438 attr->bp_addr = bswap_64(attr->bp_addr); 439 attr->bp_len = bswap_64(attr->bp_len); 440 attr->branch_sample_type = bswap_64(attr->branch_sample_type); 441 attr->sample_regs_user = bswap_64(attr->sample_regs_user); 442 attr->sample_stack_user = bswap_32(attr->sample_stack_user); 443 444 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64)); 445 } 446 447 static void perf_event__hdr_attr_swap(union perf_event *event, 448 bool sample_id_all __maybe_unused) 449 { 450 size_t size; 451 452 perf_event__attr_swap(&event->attr.attr); 453 454 size = event->header.size; 455 size -= (void *)&event->attr.id - (void *)event; 456 mem_bswap_64(event->attr.id, size); 457 } 458 459 static void perf_event__event_type_swap(union perf_event *event, 460 bool sample_id_all __maybe_unused) 461 { 462 event->event_type.event_type.event_id = 463 bswap_64(event->event_type.event_type.event_id); 464 } 465 466 static void perf_event__tracing_data_swap(union perf_event *event, 467 bool sample_id_all __maybe_unused) 468 { 469 event->tracing_data.size = bswap_32(event->tracing_data.size); 470 } 471 472 typedef void (*perf_event__swap_op)(union perf_event *event, 473 bool sample_id_all); 474 475 static perf_event__swap_op perf_event__swap_ops[] = { 476 [PERF_RECORD_MMAP] = perf_event__mmap_swap, 477 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap, 478 [PERF_RECORD_COMM] = perf_event__comm_swap, 479 [PERF_RECORD_FORK] = perf_event__task_swap, 480 [PERF_RECORD_EXIT] = perf_event__task_swap, 481 [PERF_RECORD_LOST] = perf_event__all64_swap, 482 [PERF_RECORD_READ] = perf_event__read_swap, 483 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap, 484 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap, 485 [PERF_RECORD_SAMPLE] = perf_event__all64_swap, 486 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap, 487 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap, 488 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap, 489 [PERF_RECORD_HEADER_BUILD_ID] = NULL, 490 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap, 491 [PERF_RECORD_HEADER_MAX] = NULL, 492 }; 493 494 /* 495 * When perf record finishes a pass on every buffers, it records this pseudo 496 * event. 497 * We record the max timestamp t found in the pass n. 498 * Assuming these timestamps are monotonic across cpus, we know that if 499 * a buffer still has events with timestamps below t, they will be all 500 * available and then read in the pass n + 1. 501 * Hence when we start to read the pass n + 2, we can safely flush every 502 * events with timestamps below t. 503 * 504 * ============ PASS n ================= 505 * CPU 0 | CPU 1 506 * | 507 * cnt1 timestamps | cnt2 timestamps 508 * 1 | 2 509 * 2 | 3 510 * - | 4 <--- max recorded 511 * 512 * ============ PASS n + 1 ============== 513 * CPU 0 | CPU 1 514 * | 515 * cnt1 timestamps | cnt2 timestamps 516 * 3 | 5 517 * 4 | 6 518 * 5 | 7 <---- max recorded 519 * 520 * Flush every events below timestamp 4 521 * 522 * ============ PASS n + 2 ============== 523 * CPU 0 | CPU 1 524 * | 525 * cnt1 timestamps | cnt2 timestamps 526 * 6 | 8 527 * 7 | 9 528 * - | 10 529 * 530 * Flush every events below timestamp 7 531 * etc... 532 */ 533 static int process_finished_round(struct perf_tool *tool __maybe_unused, 534 union perf_event *event __maybe_unused, 535 struct ordered_events *oe) 536 { 537 return ordered_events__flush(oe, OE_FLUSH__ROUND); 538 } 539 540 int perf_session__queue_event(struct perf_session *s, union perf_event *event, 541 struct perf_sample *sample, u64 file_offset) 542 { 543 return ordered_events__queue(&s->ordered_events, event, sample, file_offset); 544 } 545 546 static void callchain__lbr_callstack_printf(struct perf_sample *sample) 547 { 548 struct ip_callchain *callchain = sample->callchain; 549 struct branch_stack *lbr_stack = sample->branch_stack; 550 u64 kernel_callchain_nr = callchain->nr; 551 unsigned int i; 552 553 for (i = 0; i < kernel_callchain_nr; i++) { 554 if (callchain->ips[i] == PERF_CONTEXT_USER) 555 break; 556 } 557 558 if ((i != kernel_callchain_nr) && lbr_stack->nr) { 559 u64 total_nr; 560 /* 561 * LBR callstack can only get user call chain, 562 * i is kernel call chain number, 563 * 1 is PERF_CONTEXT_USER. 564 * 565 * The user call chain is stored in LBR registers. 566 * LBR are pair registers. The caller is stored 567 * in "from" register, while the callee is stored 568 * in "to" register. 569 * For example, there is a call stack 570 * "A"->"B"->"C"->"D". 571 * The LBR registers will recorde like 572 * "C"->"D", "B"->"C", "A"->"B". 573 * So only the first "to" register and all "from" 574 * registers are needed to construct the whole stack. 575 */ 576 total_nr = i + 1 + lbr_stack->nr + 1; 577 kernel_callchain_nr = i + 1; 578 579 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr); 580 581 for (i = 0; i < kernel_callchain_nr; i++) 582 printf("..... %2d: %016" PRIx64 "\n", 583 i, callchain->ips[i]); 584 585 printf("..... %2d: %016" PRIx64 "\n", 586 (int)(kernel_callchain_nr), lbr_stack->entries[0].to); 587 for (i = 0; i < lbr_stack->nr; i++) 588 printf("..... %2d: %016" PRIx64 "\n", 589 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from); 590 } 591 } 592 593 static void callchain__printf(struct perf_evsel *evsel, 594 struct perf_sample *sample) 595 { 596 unsigned int i; 597 struct ip_callchain *callchain = sample->callchain; 598 599 if (has_branch_callstack(evsel)) 600 callchain__lbr_callstack_printf(sample); 601 602 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr); 603 604 for (i = 0; i < callchain->nr; i++) 605 printf("..... %2d: %016" PRIx64 "\n", 606 i, callchain->ips[i]); 607 } 608 609 static void branch_stack__printf(struct perf_sample *sample) 610 { 611 uint64_t i; 612 613 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr); 614 615 for (i = 0; i < sample->branch_stack->nr; i++) 616 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n", 617 i, sample->branch_stack->entries[i].from, 618 sample->branch_stack->entries[i].to); 619 } 620 621 static void regs_dump__printf(u64 mask, u64 *regs) 622 { 623 unsigned rid, i = 0; 624 625 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) { 626 u64 val = regs[i++]; 627 628 printf(".... %-5s 0x%" PRIx64 "\n", 629 perf_reg_name(rid), val); 630 } 631 } 632 633 static const char *regs_abi[] = { 634 [PERF_SAMPLE_REGS_ABI_NONE] = "none", 635 [PERF_SAMPLE_REGS_ABI_32] = "32-bit", 636 [PERF_SAMPLE_REGS_ABI_64] = "64-bit", 637 }; 638 639 static inline const char *regs_dump_abi(struct regs_dump *d) 640 { 641 if (d->abi > PERF_SAMPLE_REGS_ABI_64) 642 return "unknown"; 643 644 return regs_abi[d->abi]; 645 } 646 647 static void regs__printf(const char *type, struct regs_dump *regs) 648 { 649 u64 mask = regs->mask; 650 651 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n", 652 type, 653 mask, 654 regs_dump_abi(regs)); 655 656 regs_dump__printf(mask, regs->regs); 657 } 658 659 static void regs_user__printf(struct perf_sample *sample) 660 { 661 struct regs_dump *user_regs = &sample->user_regs; 662 663 if (user_regs->regs) 664 regs__printf("user", user_regs); 665 } 666 667 static void regs_intr__printf(struct perf_sample *sample) 668 { 669 struct regs_dump *intr_regs = &sample->intr_regs; 670 671 if (intr_regs->regs) 672 regs__printf("intr", intr_regs); 673 } 674 675 static void stack_user__printf(struct stack_dump *dump) 676 { 677 printf("... ustack: size %" PRIu64 ", offset 0x%x\n", 678 dump->size, dump->offset); 679 } 680 681 static void perf_evlist__print_tstamp(struct perf_evlist *evlist, 682 union perf_event *event, 683 struct perf_sample *sample) 684 { 685 u64 sample_type = __perf_evlist__combined_sample_type(evlist); 686 687 if (event->header.type != PERF_RECORD_SAMPLE && 688 !perf_evlist__sample_id_all(evlist)) { 689 fputs("-1 -1 ", stdout); 690 return; 691 } 692 693 if ((sample_type & PERF_SAMPLE_CPU)) 694 printf("%u ", sample->cpu); 695 696 if (sample_type & PERF_SAMPLE_TIME) 697 printf("%" PRIu64 " ", sample->time); 698 } 699 700 static void sample_read__printf(struct perf_sample *sample, u64 read_format) 701 { 702 printf("... sample_read:\n"); 703 704 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 705 printf("...... time enabled %016" PRIx64 "\n", 706 sample->read.time_enabled); 707 708 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 709 printf("...... time running %016" PRIx64 "\n", 710 sample->read.time_running); 711 712 if (read_format & PERF_FORMAT_GROUP) { 713 u64 i; 714 715 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr); 716 717 for (i = 0; i < sample->read.group.nr; i++) { 718 struct sample_read_value *value; 719 720 value = &sample->read.group.values[i]; 721 printf("..... id %016" PRIx64 722 ", value %016" PRIx64 "\n", 723 value->id, value->value); 724 } 725 } else 726 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n", 727 sample->read.one.id, sample->read.one.value); 728 } 729 730 static void dump_event(struct perf_evlist *evlist, union perf_event *event, 731 u64 file_offset, struct perf_sample *sample) 732 { 733 if (!dump_trace) 734 return; 735 736 printf("\n%#" PRIx64 " [%#x]: event: %d\n", 737 file_offset, event->header.size, event->header.type); 738 739 trace_event(event); 740 741 if (sample) 742 perf_evlist__print_tstamp(evlist, event, sample); 743 744 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset, 745 event->header.size, perf_event__name(event->header.type)); 746 } 747 748 static void dump_sample(struct perf_evsel *evsel, union perf_event *event, 749 struct perf_sample *sample) 750 { 751 u64 sample_type; 752 753 if (!dump_trace) 754 return; 755 756 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n", 757 event->header.misc, sample->pid, sample->tid, sample->ip, 758 sample->period, sample->addr); 759 760 sample_type = evsel->attr.sample_type; 761 762 if (sample_type & PERF_SAMPLE_CALLCHAIN) 763 callchain__printf(evsel, sample); 764 765 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !has_branch_callstack(evsel)) 766 branch_stack__printf(sample); 767 768 if (sample_type & PERF_SAMPLE_REGS_USER) 769 regs_user__printf(sample); 770 771 if (sample_type & PERF_SAMPLE_REGS_INTR) 772 regs_intr__printf(sample); 773 774 if (sample_type & PERF_SAMPLE_STACK_USER) 775 stack_user__printf(&sample->user_stack); 776 777 if (sample_type & PERF_SAMPLE_WEIGHT) 778 printf("... weight: %" PRIu64 "\n", sample->weight); 779 780 if (sample_type & PERF_SAMPLE_DATA_SRC) 781 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src); 782 783 if (sample_type & PERF_SAMPLE_TRANSACTION) 784 printf("... transaction: %" PRIx64 "\n", sample->transaction); 785 786 if (sample_type & PERF_SAMPLE_READ) 787 sample_read__printf(sample, evsel->attr.read_format); 788 } 789 790 static struct machine *machines__find_for_cpumode(struct machines *machines, 791 union perf_event *event, 792 struct perf_sample *sample) 793 { 794 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 795 struct machine *machine; 796 797 if (perf_guest && 798 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) || 799 (cpumode == PERF_RECORD_MISC_GUEST_USER))) { 800 u32 pid; 801 802 if (event->header.type == PERF_RECORD_MMAP 803 || event->header.type == PERF_RECORD_MMAP2) 804 pid = event->mmap.pid; 805 else 806 pid = sample->pid; 807 808 machine = machines__find(machines, pid); 809 if (!machine) 810 machine = machines__find(machines, DEFAULT_GUEST_KERNEL_ID); 811 return machine; 812 } 813 814 return &machines->host; 815 } 816 817 static int deliver_sample_value(struct perf_evlist *evlist, 818 struct perf_tool *tool, 819 union perf_event *event, 820 struct perf_sample *sample, 821 struct sample_read_value *v, 822 struct machine *machine) 823 { 824 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id); 825 826 if (sid) { 827 sample->id = v->id; 828 sample->period = v->value - sid->period; 829 sid->period = v->value; 830 } 831 832 if (!sid || sid->evsel == NULL) { 833 ++evlist->stats.nr_unknown_id; 834 return 0; 835 } 836 837 return tool->sample(tool, event, sample, sid->evsel, machine); 838 } 839 840 static int deliver_sample_group(struct perf_evlist *evlist, 841 struct perf_tool *tool, 842 union perf_event *event, 843 struct perf_sample *sample, 844 struct machine *machine) 845 { 846 int ret = -EINVAL; 847 u64 i; 848 849 for (i = 0; i < sample->read.group.nr; i++) { 850 ret = deliver_sample_value(evlist, tool, event, sample, 851 &sample->read.group.values[i], 852 machine); 853 if (ret) 854 break; 855 } 856 857 return ret; 858 } 859 860 static int 861 perf_evlist__deliver_sample(struct perf_evlist *evlist, 862 struct perf_tool *tool, 863 union perf_event *event, 864 struct perf_sample *sample, 865 struct perf_evsel *evsel, 866 struct machine *machine) 867 { 868 /* We know evsel != NULL. */ 869 u64 sample_type = evsel->attr.sample_type; 870 u64 read_format = evsel->attr.read_format; 871 872 /* Standard sample delievery. */ 873 if (!(sample_type & PERF_SAMPLE_READ)) 874 return tool->sample(tool, event, sample, evsel, machine); 875 876 /* For PERF_SAMPLE_READ we have either single or group mode. */ 877 if (read_format & PERF_FORMAT_GROUP) 878 return deliver_sample_group(evlist, tool, event, sample, 879 machine); 880 else 881 return deliver_sample_value(evlist, tool, event, sample, 882 &sample->read.one, machine); 883 } 884 885 static int machines__deliver_event(struct machines *machines, 886 struct perf_evlist *evlist, 887 union perf_event *event, 888 struct perf_sample *sample, 889 struct perf_tool *tool, u64 file_offset) 890 { 891 struct perf_evsel *evsel; 892 struct machine *machine; 893 894 dump_event(evlist, event, file_offset, sample); 895 896 evsel = perf_evlist__id2evsel(evlist, sample->id); 897 898 machine = machines__find_for_cpumode(machines, event, sample); 899 900 switch (event->header.type) { 901 case PERF_RECORD_SAMPLE: 902 dump_sample(evsel, event, sample); 903 if (evsel == NULL) { 904 ++evlist->stats.nr_unknown_id; 905 return 0; 906 } 907 if (machine == NULL) { 908 ++evlist->stats.nr_unprocessable_samples; 909 return 0; 910 } 911 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine); 912 case PERF_RECORD_MMAP: 913 return tool->mmap(tool, event, sample, machine); 914 case PERF_RECORD_MMAP2: 915 return tool->mmap2(tool, event, sample, machine); 916 case PERF_RECORD_COMM: 917 return tool->comm(tool, event, sample, machine); 918 case PERF_RECORD_FORK: 919 return tool->fork(tool, event, sample, machine); 920 case PERF_RECORD_EXIT: 921 return tool->exit(tool, event, sample, machine); 922 case PERF_RECORD_LOST: 923 if (tool->lost == perf_event__process_lost) 924 evlist->stats.total_lost += event->lost.lost; 925 return tool->lost(tool, event, sample, machine); 926 case PERF_RECORD_READ: 927 return tool->read(tool, event, sample, evsel, machine); 928 case PERF_RECORD_THROTTLE: 929 return tool->throttle(tool, event, sample, machine); 930 case PERF_RECORD_UNTHROTTLE: 931 return tool->unthrottle(tool, event, sample, machine); 932 default: 933 ++evlist->stats.nr_unknown_events; 934 return -1; 935 } 936 } 937 938 static s64 perf_session__process_user_event(struct perf_session *session, 939 union perf_event *event, 940 u64 file_offset) 941 { 942 struct ordered_events *oe = &session->ordered_events; 943 struct perf_tool *tool = oe->tool; 944 int fd = perf_data_file__fd(session->file); 945 int err; 946 947 dump_event(session->evlist, event, file_offset, NULL); 948 949 /* These events are processed right away */ 950 switch (event->header.type) { 951 case PERF_RECORD_HEADER_ATTR: 952 err = tool->attr(tool, event, &session->evlist); 953 if (err == 0) { 954 perf_session__set_id_hdr_size(session); 955 perf_session__set_comm_exec(session); 956 } 957 return err; 958 case PERF_RECORD_HEADER_EVENT_TYPE: 959 /* 960 * Depreceated, but we need to handle it for sake 961 * of old data files create in pipe mode. 962 */ 963 return 0; 964 case PERF_RECORD_HEADER_TRACING_DATA: 965 /* setup for reading amidst mmap */ 966 lseek(fd, file_offset, SEEK_SET); 967 return tool->tracing_data(tool, event, session); 968 case PERF_RECORD_HEADER_BUILD_ID: 969 return tool->build_id(tool, event, session); 970 case PERF_RECORD_FINISHED_ROUND: 971 return tool->finished_round(tool, event, oe); 972 case PERF_RECORD_ID_INDEX: 973 return tool->id_index(tool, event, session); 974 default: 975 return -EINVAL; 976 } 977 } 978 979 int perf_session__deliver_synth_event(struct perf_session *session, 980 union perf_event *event, 981 struct perf_sample *sample) 982 { 983 struct perf_evlist *evlist = session->evlist; 984 struct perf_tool *tool = session->ordered_events.tool; 985 986 events_stats__inc(&evlist->stats, event->header.type); 987 988 if (event->header.type >= PERF_RECORD_USER_TYPE_START) 989 return perf_session__process_user_event(session, event, 0); 990 991 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0); 992 } 993 994 static void event_swap(union perf_event *event, bool sample_id_all) 995 { 996 perf_event__swap_op swap; 997 998 swap = perf_event__swap_ops[event->header.type]; 999 if (swap) 1000 swap(event, sample_id_all); 1001 } 1002 1003 int perf_session__peek_event(struct perf_session *session, off_t file_offset, 1004 void *buf, size_t buf_sz, 1005 union perf_event **event_ptr, 1006 struct perf_sample *sample) 1007 { 1008 union perf_event *event; 1009 size_t hdr_sz, rest; 1010 int fd; 1011 1012 if (session->one_mmap && !session->header.needs_swap) { 1013 event = file_offset - session->one_mmap_offset + 1014 session->one_mmap_addr; 1015 goto out_parse_sample; 1016 } 1017 1018 if (perf_data_file__is_pipe(session->file)) 1019 return -1; 1020 1021 fd = perf_data_file__fd(session->file); 1022 hdr_sz = sizeof(struct perf_event_header); 1023 1024 if (buf_sz < hdr_sz) 1025 return -1; 1026 1027 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 || 1028 readn(fd, &buf, hdr_sz) != (ssize_t)hdr_sz) 1029 return -1; 1030 1031 event = (union perf_event *)buf; 1032 1033 if (session->header.needs_swap) 1034 perf_event_header__bswap(&event->header); 1035 1036 if (event->header.size < hdr_sz) 1037 return -1; 1038 1039 rest = event->header.size - hdr_sz; 1040 1041 if (readn(fd, &buf, rest) != (ssize_t)rest) 1042 return -1; 1043 1044 if (session->header.needs_swap) 1045 event_swap(event, perf_evlist__sample_id_all(session->evlist)); 1046 1047 out_parse_sample: 1048 1049 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START && 1050 perf_evlist__parse_sample(session->evlist, event, sample)) 1051 return -1; 1052 1053 *event_ptr = event; 1054 1055 return 0; 1056 } 1057 1058 static s64 perf_session__process_event(struct perf_session *session, 1059 union perf_event *event, u64 file_offset) 1060 { 1061 struct perf_evlist *evlist = session->evlist; 1062 struct perf_tool *tool = session->ordered_events.tool; 1063 struct perf_sample sample; 1064 int ret; 1065 1066 if (session->header.needs_swap) 1067 event_swap(event, perf_evlist__sample_id_all(evlist)); 1068 1069 if (event->header.type >= PERF_RECORD_HEADER_MAX) 1070 return -EINVAL; 1071 1072 events_stats__inc(&evlist->stats, event->header.type); 1073 1074 if (event->header.type >= PERF_RECORD_USER_TYPE_START) 1075 return perf_session__process_user_event(session, event, file_offset); 1076 1077 /* 1078 * For all kernel events we get the sample data 1079 */ 1080 ret = perf_evlist__parse_sample(evlist, event, &sample); 1081 if (ret) 1082 return ret; 1083 1084 if (tool->ordered_events) { 1085 ret = perf_session__queue_event(session, event, &sample, file_offset); 1086 if (ret != -ETIME) 1087 return ret; 1088 } 1089 1090 return machines__deliver_event(&session->machines, evlist, event, 1091 &sample, tool, file_offset); 1092 } 1093 1094 void perf_event_header__bswap(struct perf_event_header *hdr) 1095 { 1096 hdr->type = bswap_32(hdr->type); 1097 hdr->misc = bswap_16(hdr->misc); 1098 hdr->size = bswap_16(hdr->size); 1099 } 1100 1101 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid) 1102 { 1103 return machine__findnew_thread(&session->machines.host, -1, pid); 1104 } 1105 1106 static struct thread *perf_session__register_idle_thread(struct perf_session *session) 1107 { 1108 struct thread *thread; 1109 1110 thread = machine__findnew_thread(&session->machines.host, 0, 0); 1111 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) { 1112 pr_err("problem inserting idle task.\n"); 1113 thread = NULL; 1114 } 1115 1116 return thread; 1117 } 1118 1119 static void perf_tool__warn_about_errors(const struct perf_tool *tool, 1120 const struct events_stats *stats) 1121 { 1122 if (tool->lost == perf_event__process_lost && 1123 stats->nr_events[PERF_RECORD_LOST] != 0) { 1124 ui__warning("Processed %d events and lost %d chunks!\n\n" 1125 "Check IO/CPU overload!\n\n", 1126 stats->nr_events[0], 1127 stats->nr_events[PERF_RECORD_LOST]); 1128 } 1129 1130 if (stats->nr_unknown_events != 0) { 1131 ui__warning("Found %u unknown events!\n\n" 1132 "Is this an older tool processing a perf.data " 1133 "file generated by a more recent tool?\n\n" 1134 "If that is not the case, consider " 1135 "reporting to linux-kernel@vger.kernel.org.\n\n", 1136 stats->nr_unknown_events); 1137 } 1138 1139 if (stats->nr_unknown_id != 0) { 1140 ui__warning("%u samples with id not present in the header\n", 1141 stats->nr_unknown_id); 1142 } 1143 1144 if (stats->nr_invalid_chains != 0) { 1145 ui__warning("Found invalid callchains!\n\n" 1146 "%u out of %u events were discarded for this reason.\n\n" 1147 "Consider reporting to linux-kernel@vger.kernel.org.\n\n", 1148 stats->nr_invalid_chains, 1149 stats->nr_events[PERF_RECORD_SAMPLE]); 1150 } 1151 1152 if (stats->nr_unprocessable_samples != 0) { 1153 ui__warning("%u unprocessable samples recorded.\n" 1154 "Do you have a KVM guest running and not using 'perf kvm'?\n", 1155 stats->nr_unprocessable_samples); 1156 } 1157 1158 if (stats->nr_unordered_events != 0) 1159 ui__warning("%u out of order events recorded.\n", stats->nr_unordered_events); 1160 } 1161 1162 volatile int session_done; 1163 1164 static int __perf_session__process_pipe_events(struct perf_session *session) 1165 { 1166 struct ordered_events *oe = &session->ordered_events; 1167 struct perf_tool *tool = oe->tool; 1168 int fd = perf_data_file__fd(session->file); 1169 union perf_event *event; 1170 uint32_t size, cur_size = 0; 1171 void *buf = NULL; 1172 s64 skip = 0; 1173 u64 head; 1174 ssize_t err; 1175 void *p; 1176 1177 perf_tool__fill_defaults(tool); 1178 1179 head = 0; 1180 cur_size = sizeof(union perf_event); 1181 1182 buf = malloc(cur_size); 1183 if (!buf) 1184 return -errno; 1185 more: 1186 event = buf; 1187 err = readn(fd, event, sizeof(struct perf_event_header)); 1188 if (err <= 0) { 1189 if (err == 0) 1190 goto done; 1191 1192 pr_err("failed to read event header\n"); 1193 goto out_err; 1194 } 1195 1196 if (session->header.needs_swap) 1197 perf_event_header__bswap(&event->header); 1198 1199 size = event->header.size; 1200 if (size < sizeof(struct perf_event_header)) { 1201 pr_err("bad event header size\n"); 1202 goto out_err; 1203 } 1204 1205 if (size > cur_size) { 1206 void *new = realloc(buf, size); 1207 if (!new) { 1208 pr_err("failed to allocate memory to read event\n"); 1209 goto out_err; 1210 } 1211 buf = new; 1212 cur_size = size; 1213 event = buf; 1214 } 1215 p = event; 1216 p += sizeof(struct perf_event_header); 1217 1218 if (size - sizeof(struct perf_event_header)) { 1219 err = readn(fd, p, size - sizeof(struct perf_event_header)); 1220 if (err <= 0) { 1221 if (err == 0) { 1222 pr_err("unexpected end of event stream\n"); 1223 goto done; 1224 } 1225 1226 pr_err("failed to read event data\n"); 1227 goto out_err; 1228 } 1229 } 1230 1231 if ((skip = perf_session__process_event(session, event, head)) < 0) { 1232 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", 1233 head, event->header.size, event->header.type); 1234 err = -EINVAL; 1235 goto out_err; 1236 } 1237 1238 head += size; 1239 1240 if (skip > 0) 1241 head += skip; 1242 1243 if (!session_done()) 1244 goto more; 1245 done: 1246 /* do the final flush for ordered samples */ 1247 err = ordered_events__flush(oe, OE_FLUSH__FINAL); 1248 out_err: 1249 free(buf); 1250 perf_tool__warn_about_errors(tool, &session->evlist->stats); 1251 ordered_events__free(&session->ordered_events); 1252 return err; 1253 } 1254 1255 static union perf_event * 1256 fetch_mmaped_event(struct perf_session *session, 1257 u64 head, size_t mmap_size, char *buf) 1258 { 1259 union perf_event *event; 1260 1261 /* 1262 * Ensure we have enough space remaining to read 1263 * the size of the event in the headers. 1264 */ 1265 if (head + sizeof(event->header) > mmap_size) 1266 return NULL; 1267 1268 event = (union perf_event *)(buf + head); 1269 1270 if (session->header.needs_swap) 1271 perf_event_header__bswap(&event->header); 1272 1273 if (head + event->header.size > mmap_size) { 1274 /* We're not fetching the event so swap back again */ 1275 if (session->header.needs_swap) 1276 perf_event_header__bswap(&event->header); 1277 return NULL; 1278 } 1279 1280 return event; 1281 } 1282 1283 /* 1284 * On 64bit we can mmap the data file in one go. No need for tiny mmap 1285 * slices. On 32bit we use 32MB. 1286 */ 1287 #if BITS_PER_LONG == 64 1288 #define MMAP_SIZE ULLONG_MAX 1289 #define NUM_MMAPS 1 1290 #else 1291 #define MMAP_SIZE (32 * 1024 * 1024ULL) 1292 #define NUM_MMAPS 128 1293 #endif 1294 1295 static int __perf_session__process_events(struct perf_session *session, 1296 u64 data_offset, u64 data_size, 1297 u64 file_size) 1298 { 1299 struct ordered_events *oe = &session->ordered_events; 1300 struct perf_tool *tool = oe->tool; 1301 int fd = perf_data_file__fd(session->file); 1302 u64 head, page_offset, file_offset, file_pos, size; 1303 int err, mmap_prot, mmap_flags, map_idx = 0; 1304 size_t mmap_size; 1305 char *buf, *mmaps[NUM_MMAPS]; 1306 union perf_event *event; 1307 struct ui_progress prog; 1308 s64 skip; 1309 1310 perf_tool__fill_defaults(tool); 1311 1312 page_offset = page_size * (data_offset / page_size); 1313 file_offset = page_offset; 1314 head = data_offset - page_offset; 1315 1316 if (data_size && (data_offset + data_size < file_size)) 1317 file_size = data_offset + data_size; 1318 1319 ui_progress__init(&prog, file_size, "Processing events..."); 1320 1321 mmap_size = MMAP_SIZE; 1322 if (mmap_size > file_size) { 1323 mmap_size = file_size; 1324 session->one_mmap = true; 1325 } 1326 1327 memset(mmaps, 0, sizeof(mmaps)); 1328 1329 mmap_prot = PROT_READ; 1330 mmap_flags = MAP_SHARED; 1331 1332 if (session->header.needs_swap) { 1333 mmap_prot |= PROT_WRITE; 1334 mmap_flags = MAP_PRIVATE; 1335 } 1336 remap: 1337 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd, 1338 file_offset); 1339 if (buf == MAP_FAILED) { 1340 pr_err("failed to mmap file\n"); 1341 err = -errno; 1342 goto out_err; 1343 } 1344 mmaps[map_idx] = buf; 1345 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1); 1346 file_pos = file_offset + head; 1347 if (session->one_mmap) { 1348 session->one_mmap_addr = buf; 1349 session->one_mmap_offset = file_offset; 1350 } 1351 1352 more: 1353 event = fetch_mmaped_event(session, head, mmap_size, buf); 1354 if (!event) { 1355 if (mmaps[map_idx]) { 1356 munmap(mmaps[map_idx], mmap_size); 1357 mmaps[map_idx] = NULL; 1358 } 1359 1360 page_offset = page_size * (head / page_size); 1361 file_offset += page_offset; 1362 head -= page_offset; 1363 goto remap; 1364 } 1365 1366 size = event->header.size; 1367 1368 if (size < sizeof(struct perf_event_header) || 1369 (skip = perf_session__process_event(session, event, file_pos)) < 0) { 1370 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", 1371 file_offset + head, event->header.size, 1372 event->header.type); 1373 err = -EINVAL; 1374 goto out_err; 1375 } 1376 1377 if (skip) 1378 size += skip; 1379 1380 head += size; 1381 file_pos += size; 1382 1383 ui_progress__update(&prog, size); 1384 1385 if (session_done()) 1386 goto out; 1387 1388 if (file_pos < file_size) 1389 goto more; 1390 1391 out: 1392 /* do the final flush for ordered samples */ 1393 err = ordered_events__flush(oe, OE_FLUSH__FINAL); 1394 out_err: 1395 ui_progress__finish(); 1396 perf_tool__warn_about_errors(tool, &session->evlist->stats); 1397 ordered_events__free(&session->ordered_events); 1398 session->one_mmap = false; 1399 return err; 1400 } 1401 1402 int perf_session__process_events(struct perf_session *session) 1403 { 1404 u64 size = perf_data_file__size(session->file); 1405 int err; 1406 1407 if (perf_session__register_idle_thread(session) == NULL) 1408 return -ENOMEM; 1409 1410 if (!perf_data_file__is_pipe(session->file)) 1411 err = __perf_session__process_events(session, 1412 session->header.data_offset, 1413 session->header.data_size, size); 1414 else 1415 err = __perf_session__process_pipe_events(session); 1416 1417 return err; 1418 } 1419 1420 bool perf_session__has_traces(struct perf_session *session, const char *msg) 1421 { 1422 struct perf_evsel *evsel; 1423 1424 evlist__for_each(session->evlist, evsel) { 1425 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) 1426 return true; 1427 } 1428 1429 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); 1430 return false; 1431 } 1432 1433 int maps__set_kallsyms_ref_reloc_sym(struct map **maps, 1434 const char *symbol_name, u64 addr) 1435 { 1436 char *bracket; 1437 enum map_type i; 1438 struct ref_reloc_sym *ref; 1439 1440 ref = zalloc(sizeof(struct ref_reloc_sym)); 1441 if (ref == NULL) 1442 return -ENOMEM; 1443 1444 ref->name = strdup(symbol_name); 1445 if (ref->name == NULL) { 1446 free(ref); 1447 return -ENOMEM; 1448 } 1449 1450 bracket = strchr(ref->name, ']'); 1451 if (bracket) 1452 *bracket = '\0'; 1453 1454 ref->addr = addr; 1455 1456 for (i = 0; i < MAP__NR_TYPES; ++i) { 1457 struct kmap *kmap = map__kmap(maps[i]); 1458 kmap->ref_reloc_sym = ref; 1459 } 1460 1461 return 0; 1462 } 1463 1464 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp) 1465 { 1466 return machines__fprintf_dsos(&session->machines, fp); 1467 } 1468 1469 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp, 1470 bool (skip)(struct dso *dso, int parm), int parm) 1471 { 1472 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm); 1473 } 1474 1475 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp) 1476 { 1477 size_t ret = fprintf(fp, "Aggregated stats:\n"); 1478 1479 ret += events_stats__fprintf(&session->evlist->stats, fp); 1480 return ret; 1481 } 1482 1483 size_t perf_session__fprintf(struct perf_session *session, FILE *fp) 1484 { 1485 /* 1486 * FIXME: Here we have to actually print all the machines in this 1487 * session, not just the host... 1488 */ 1489 return machine__fprintf(&session->machines.host, fp); 1490 } 1491 1492 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session, 1493 unsigned int type) 1494 { 1495 struct perf_evsel *pos; 1496 1497 evlist__for_each(session->evlist, pos) { 1498 if (pos->attr.type == type) 1499 return pos; 1500 } 1501 return NULL; 1502 } 1503 1504 void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample, 1505 struct addr_location *al, 1506 unsigned int print_opts, unsigned int stack_depth) 1507 { 1508 struct callchain_cursor_node *node; 1509 int print_ip = print_opts & PRINT_IP_OPT_IP; 1510 int print_sym = print_opts & PRINT_IP_OPT_SYM; 1511 int print_dso = print_opts & PRINT_IP_OPT_DSO; 1512 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET; 1513 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE; 1514 int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE; 1515 char s = print_oneline ? ' ' : '\t'; 1516 1517 if (symbol_conf.use_callchain && sample->callchain) { 1518 struct addr_location node_al; 1519 1520 if (thread__resolve_callchain(al->thread, evsel, 1521 sample, NULL, NULL, 1522 PERF_MAX_STACK_DEPTH) != 0) { 1523 if (verbose) 1524 error("Failed to resolve callchain. Skipping\n"); 1525 return; 1526 } 1527 callchain_cursor_commit(&callchain_cursor); 1528 1529 if (print_symoffset) 1530 node_al = *al; 1531 1532 while (stack_depth) { 1533 u64 addr = 0; 1534 1535 node = callchain_cursor_current(&callchain_cursor); 1536 if (!node) 1537 break; 1538 1539 if (node->sym && node->sym->ignore) 1540 goto next; 1541 1542 if (print_ip) 1543 printf("%c%16" PRIx64, s, node->ip); 1544 1545 if (node->map) 1546 addr = node->map->map_ip(node->map, node->ip); 1547 1548 if (print_sym) { 1549 printf(" "); 1550 if (print_symoffset) { 1551 node_al.addr = addr; 1552 node_al.map = node->map; 1553 symbol__fprintf_symname_offs(node->sym, &node_al, stdout); 1554 } else 1555 symbol__fprintf_symname(node->sym, stdout); 1556 } 1557 1558 if (print_dso) { 1559 printf(" ("); 1560 map__fprintf_dsoname(node->map, stdout); 1561 printf(")"); 1562 } 1563 1564 if (print_srcline) 1565 map__fprintf_srcline(node->map, addr, "\n ", 1566 stdout); 1567 1568 if (!print_oneline) 1569 printf("\n"); 1570 1571 stack_depth--; 1572 next: 1573 callchain_cursor_advance(&callchain_cursor); 1574 } 1575 1576 } else { 1577 if (al->sym && al->sym->ignore) 1578 return; 1579 1580 if (print_ip) 1581 printf("%16" PRIx64, sample->ip); 1582 1583 if (print_sym) { 1584 printf(" "); 1585 if (print_symoffset) 1586 symbol__fprintf_symname_offs(al->sym, al, 1587 stdout); 1588 else 1589 symbol__fprintf_symname(al->sym, stdout); 1590 } 1591 1592 if (print_dso) { 1593 printf(" ("); 1594 map__fprintf_dsoname(al->map, stdout); 1595 printf(")"); 1596 } 1597 1598 if (print_srcline) 1599 map__fprintf_srcline(al->map, al->addr, "\n ", stdout); 1600 } 1601 } 1602 1603 int perf_session__cpu_bitmap(struct perf_session *session, 1604 const char *cpu_list, unsigned long *cpu_bitmap) 1605 { 1606 int i, err = -1; 1607 struct cpu_map *map; 1608 1609 for (i = 0; i < PERF_TYPE_MAX; ++i) { 1610 struct perf_evsel *evsel; 1611 1612 evsel = perf_session__find_first_evtype(session, i); 1613 if (!evsel) 1614 continue; 1615 1616 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) { 1617 pr_err("File does not contain CPU events. " 1618 "Remove -c option to proceed.\n"); 1619 return -1; 1620 } 1621 } 1622 1623 map = cpu_map__new(cpu_list); 1624 if (map == NULL) { 1625 pr_err("Invalid cpu_list\n"); 1626 return -1; 1627 } 1628 1629 for (i = 0; i < map->nr; i++) { 1630 int cpu = map->map[i]; 1631 1632 if (cpu >= MAX_NR_CPUS) { 1633 pr_err("Requested CPU %d too large. " 1634 "Consider raising MAX_NR_CPUS\n", cpu); 1635 goto out_delete_map; 1636 } 1637 1638 set_bit(cpu, cpu_bitmap); 1639 } 1640 1641 err = 0; 1642 1643 out_delete_map: 1644 cpu_map__delete(map); 1645 return err; 1646 } 1647 1648 void perf_session__fprintf_info(struct perf_session *session, FILE *fp, 1649 bool full) 1650 { 1651 struct stat st; 1652 int fd, ret; 1653 1654 if (session == NULL || fp == NULL) 1655 return; 1656 1657 fd = perf_data_file__fd(session->file); 1658 1659 ret = fstat(fd, &st); 1660 if (ret == -1) 1661 return; 1662 1663 fprintf(fp, "# ========\n"); 1664 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime)); 1665 perf_header__fprintf_info(session, fp, full); 1666 fprintf(fp, "# ========\n#\n"); 1667 } 1668 1669 1670 int __perf_session__set_tracepoints_handlers(struct perf_session *session, 1671 const struct perf_evsel_str_handler *assocs, 1672 size_t nr_assocs) 1673 { 1674 struct perf_evsel *evsel; 1675 size_t i; 1676 int err; 1677 1678 for (i = 0; i < nr_assocs; i++) { 1679 /* 1680 * Adding a handler for an event not in the session, 1681 * just ignore it. 1682 */ 1683 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name); 1684 if (evsel == NULL) 1685 continue; 1686 1687 err = -EEXIST; 1688 if (evsel->handler != NULL) 1689 goto out; 1690 evsel->handler = assocs[i].handler; 1691 } 1692 1693 err = 0; 1694 out: 1695 return err; 1696 } 1697 1698 int perf_event__process_id_index(struct perf_tool *tool __maybe_unused, 1699 union perf_event *event, 1700 struct perf_session *session) 1701 { 1702 struct perf_evlist *evlist = session->evlist; 1703 struct id_index_event *ie = &event->id_index; 1704 size_t i, nr, max_nr; 1705 1706 max_nr = (ie->header.size - sizeof(struct id_index_event)) / 1707 sizeof(struct id_index_entry); 1708 nr = ie->nr; 1709 if (nr > max_nr) 1710 return -EINVAL; 1711 1712 if (dump_trace) 1713 fprintf(stdout, " nr: %zu\n", nr); 1714 1715 for (i = 0; i < nr; i++) { 1716 struct id_index_entry *e = &ie->entries[i]; 1717 struct perf_sample_id *sid; 1718 1719 if (dump_trace) { 1720 fprintf(stdout, " ... id: %"PRIu64, e->id); 1721 fprintf(stdout, " idx: %"PRIu64, e->idx); 1722 fprintf(stdout, " cpu: %"PRId64, e->cpu); 1723 fprintf(stdout, " tid: %"PRId64"\n", e->tid); 1724 } 1725 1726 sid = perf_evlist__id2sid(evlist, e->id); 1727 if (!sid) 1728 return -ENOENT; 1729 sid->idx = e->idx; 1730 sid->cpu = e->cpu; 1731 sid->tid = e->tid; 1732 } 1733 return 0; 1734 } 1735 1736 int perf_event__synthesize_id_index(struct perf_tool *tool, 1737 perf_event__handler_t process, 1738 struct perf_evlist *evlist, 1739 struct machine *machine) 1740 { 1741 union perf_event *ev; 1742 struct perf_evsel *evsel; 1743 size_t nr = 0, i = 0, sz, max_nr, n; 1744 int err; 1745 1746 pr_debug2("Synthesizing id index\n"); 1747 1748 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) / 1749 sizeof(struct id_index_entry); 1750 1751 evlist__for_each(evlist, evsel) 1752 nr += evsel->ids; 1753 1754 n = nr > max_nr ? max_nr : nr; 1755 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry); 1756 ev = zalloc(sz); 1757 if (!ev) 1758 return -ENOMEM; 1759 1760 ev->id_index.header.type = PERF_RECORD_ID_INDEX; 1761 ev->id_index.header.size = sz; 1762 ev->id_index.nr = n; 1763 1764 evlist__for_each(evlist, evsel) { 1765 u32 j; 1766 1767 for (j = 0; j < evsel->ids; j++) { 1768 struct id_index_entry *e; 1769 struct perf_sample_id *sid; 1770 1771 if (i >= n) { 1772 err = process(tool, ev, NULL, machine); 1773 if (err) 1774 goto out_err; 1775 nr -= n; 1776 i = 0; 1777 } 1778 1779 e = &ev->id_index.entries[i++]; 1780 1781 e->id = evsel->id[j]; 1782 1783 sid = perf_evlist__id2sid(evlist, e->id); 1784 if (!sid) { 1785 free(ev); 1786 return -ENOENT; 1787 } 1788 1789 e->idx = sid->idx; 1790 e->cpu = sid->cpu; 1791 e->tid = sid->tid; 1792 } 1793 } 1794 1795 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry); 1796 ev->id_index.header.size = sz; 1797 ev->id_index.nr = nr; 1798 1799 err = process(tool, ev, NULL, machine); 1800 out_err: 1801 free(ev); 1802 1803 return err; 1804 } 1805