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