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