1 #include "builtin.h" 2 3 #include "perf.h" 4 #include "util/cache.h" 5 #include "util/debug.h" 6 #include "util/exec_cmd.h" 7 #include "util/header.h" 8 #include "util/parse-options.h" 9 #include "util/perf_regs.h" 10 #include "util/session.h" 11 #include "util/tool.h" 12 #include "util/symbol.h" 13 #include "util/thread.h" 14 #include "util/trace-event.h" 15 #include "util/util.h" 16 #include "util/evlist.h" 17 #include "util/evsel.h" 18 #include "util/sort.h" 19 #include "util/data.h" 20 #include "util/auxtrace.h" 21 #include <linux/bitmap.h> 22 23 static char const *script_name; 24 static char const *generate_script_lang; 25 static bool debug_mode; 26 static u64 last_timestamp; 27 static u64 nr_unordered; 28 static bool no_callchain; 29 static bool latency_format; 30 static bool system_wide; 31 static bool print_flags; 32 static bool nanosecs; 33 static const char *cpu_list; 34 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); 35 36 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH; 37 38 enum perf_output_field { 39 PERF_OUTPUT_COMM = 1U << 0, 40 PERF_OUTPUT_TID = 1U << 1, 41 PERF_OUTPUT_PID = 1U << 2, 42 PERF_OUTPUT_TIME = 1U << 3, 43 PERF_OUTPUT_CPU = 1U << 4, 44 PERF_OUTPUT_EVNAME = 1U << 5, 45 PERF_OUTPUT_TRACE = 1U << 6, 46 PERF_OUTPUT_IP = 1U << 7, 47 PERF_OUTPUT_SYM = 1U << 8, 48 PERF_OUTPUT_DSO = 1U << 9, 49 PERF_OUTPUT_ADDR = 1U << 10, 50 PERF_OUTPUT_SYMOFFSET = 1U << 11, 51 PERF_OUTPUT_SRCLINE = 1U << 12, 52 PERF_OUTPUT_PERIOD = 1U << 13, 53 PERF_OUTPUT_IREGS = 1U << 14, 54 PERF_OUTPUT_BRSTACK = 1U << 15, 55 PERF_OUTPUT_BRSTACKSYM = 1U << 16, 56 }; 57 58 struct output_option { 59 const char *str; 60 enum perf_output_field field; 61 } all_output_options[] = { 62 {.str = "comm", .field = PERF_OUTPUT_COMM}, 63 {.str = "tid", .field = PERF_OUTPUT_TID}, 64 {.str = "pid", .field = PERF_OUTPUT_PID}, 65 {.str = "time", .field = PERF_OUTPUT_TIME}, 66 {.str = "cpu", .field = PERF_OUTPUT_CPU}, 67 {.str = "event", .field = PERF_OUTPUT_EVNAME}, 68 {.str = "trace", .field = PERF_OUTPUT_TRACE}, 69 {.str = "ip", .field = PERF_OUTPUT_IP}, 70 {.str = "sym", .field = PERF_OUTPUT_SYM}, 71 {.str = "dso", .field = PERF_OUTPUT_DSO}, 72 {.str = "addr", .field = PERF_OUTPUT_ADDR}, 73 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET}, 74 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE}, 75 {.str = "period", .field = PERF_OUTPUT_PERIOD}, 76 {.str = "iregs", .field = PERF_OUTPUT_IREGS}, 77 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK}, 78 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM}, 79 }; 80 81 /* default set to maintain compatibility with current format */ 82 static struct { 83 bool user_set; 84 bool wildcard_set; 85 unsigned int print_ip_opts; 86 u64 fields; 87 u64 invalid_fields; 88 } output[PERF_TYPE_MAX] = { 89 90 [PERF_TYPE_HARDWARE] = { 91 .user_set = false, 92 93 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | 94 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | 95 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | 96 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | 97 PERF_OUTPUT_PERIOD, 98 99 .invalid_fields = PERF_OUTPUT_TRACE, 100 }, 101 102 [PERF_TYPE_SOFTWARE] = { 103 .user_set = false, 104 105 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | 106 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | 107 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | 108 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | 109 PERF_OUTPUT_PERIOD, 110 111 .invalid_fields = PERF_OUTPUT_TRACE, 112 }, 113 114 [PERF_TYPE_TRACEPOINT] = { 115 .user_set = false, 116 117 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | 118 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | 119 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE, 120 }, 121 122 [PERF_TYPE_RAW] = { 123 .user_set = false, 124 125 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | 126 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | 127 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | 128 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | 129 PERF_OUTPUT_PERIOD, 130 131 .invalid_fields = PERF_OUTPUT_TRACE, 132 }, 133 }; 134 135 static bool output_set_by_user(void) 136 { 137 int j; 138 for (j = 0; j < PERF_TYPE_MAX; ++j) { 139 if (output[j].user_set) 140 return true; 141 } 142 return false; 143 } 144 145 static const char *output_field2str(enum perf_output_field field) 146 { 147 int i, imax = ARRAY_SIZE(all_output_options); 148 const char *str = ""; 149 150 for (i = 0; i < imax; ++i) { 151 if (all_output_options[i].field == field) { 152 str = all_output_options[i].str; 153 break; 154 } 155 } 156 return str; 157 } 158 159 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x) 160 161 static int perf_evsel__do_check_stype(struct perf_evsel *evsel, 162 u64 sample_type, const char *sample_msg, 163 enum perf_output_field field, 164 bool allow_user_set) 165 { 166 struct perf_event_attr *attr = &evsel->attr; 167 int type = attr->type; 168 const char *evname; 169 170 if (attr->sample_type & sample_type) 171 return 0; 172 173 if (output[type].user_set) { 174 if (allow_user_set) 175 return 0; 176 evname = perf_evsel__name(evsel); 177 pr_err("Samples for '%s' event do not have %s attribute set. " 178 "Cannot print '%s' field.\n", 179 evname, sample_msg, output_field2str(field)); 180 return -1; 181 } 182 183 /* user did not ask for it explicitly so remove from the default list */ 184 output[type].fields &= ~field; 185 evname = perf_evsel__name(evsel); 186 pr_debug("Samples for '%s' event do not have %s attribute set. " 187 "Skipping '%s' field.\n", 188 evname, sample_msg, output_field2str(field)); 189 190 return 0; 191 } 192 193 static int perf_evsel__check_stype(struct perf_evsel *evsel, 194 u64 sample_type, const char *sample_msg, 195 enum perf_output_field field) 196 { 197 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field, 198 false); 199 } 200 201 static int perf_evsel__check_attr(struct perf_evsel *evsel, 202 struct perf_session *session) 203 { 204 struct perf_event_attr *attr = &evsel->attr; 205 bool allow_user_set; 206 207 allow_user_set = perf_header__has_feat(&session->header, 208 HEADER_AUXTRACE); 209 210 if (PRINT_FIELD(TRACE) && 211 !perf_session__has_traces(session, "record -R")) 212 return -EINVAL; 213 214 if (PRINT_FIELD(IP)) { 215 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", 216 PERF_OUTPUT_IP)) 217 return -EINVAL; 218 } 219 220 if (PRINT_FIELD(ADDR) && 221 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", 222 PERF_OUTPUT_ADDR, allow_user_set)) 223 return -EINVAL; 224 225 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) { 226 pr_err("Display of symbols requested but neither sample IP nor " 227 "sample address\nis selected. Hence, no addresses to convert " 228 "to symbols.\n"); 229 return -EINVAL; 230 } 231 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) { 232 pr_err("Display of offsets requested but symbol is not" 233 "selected.\n"); 234 return -EINVAL; 235 } 236 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) { 237 pr_err("Display of DSO requested but neither sample IP nor " 238 "sample address\nis selected. Hence, no addresses to convert " 239 "to DSO.\n"); 240 return -EINVAL; 241 } 242 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) { 243 pr_err("Display of source line number requested but sample IP is not\n" 244 "selected. Hence, no address to lookup the source line number.\n"); 245 return -EINVAL; 246 } 247 248 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) && 249 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", 250 PERF_OUTPUT_TID|PERF_OUTPUT_PID)) 251 return -EINVAL; 252 253 if (PRINT_FIELD(TIME) && 254 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", 255 PERF_OUTPUT_TIME)) 256 return -EINVAL; 257 258 if (PRINT_FIELD(CPU) && 259 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", 260 PERF_OUTPUT_CPU, allow_user_set)) 261 return -EINVAL; 262 263 if (PRINT_FIELD(PERIOD) && 264 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD", 265 PERF_OUTPUT_PERIOD)) 266 return -EINVAL; 267 268 if (PRINT_FIELD(IREGS) && 269 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", 270 PERF_OUTPUT_IREGS)) 271 return -EINVAL; 272 273 return 0; 274 } 275 276 static void set_print_ip_opts(struct perf_event_attr *attr) 277 { 278 unsigned int type = attr->type; 279 280 output[type].print_ip_opts = 0; 281 if (PRINT_FIELD(IP)) 282 output[type].print_ip_opts |= PRINT_IP_OPT_IP; 283 284 if (PRINT_FIELD(SYM)) 285 output[type].print_ip_opts |= PRINT_IP_OPT_SYM; 286 287 if (PRINT_FIELD(DSO)) 288 output[type].print_ip_opts |= PRINT_IP_OPT_DSO; 289 290 if (PRINT_FIELD(SYMOFFSET)) 291 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET; 292 293 if (PRINT_FIELD(SRCLINE)) 294 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE; 295 } 296 297 /* 298 * verify all user requested events exist and the samples 299 * have the expected data 300 */ 301 static int perf_session__check_output_opt(struct perf_session *session) 302 { 303 int j; 304 struct perf_evsel *evsel; 305 306 for (j = 0; j < PERF_TYPE_MAX; ++j) { 307 evsel = perf_session__find_first_evtype(session, j); 308 309 /* 310 * even if fields is set to 0 (ie., show nothing) event must 311 * exist if user explicitly includes it on the command line 312 */ 313 if (!evsel && output[j].user_set && !output[j].wildcard_set) { 314 pr_err("%s events do not exist. " 315 "Remove corresponding -f option to proceed.\n", 316 event_type(j)); 317 return -1; 318 } 319 320 if (evsel && output[j].fields && 321 perf_evsel__check_attr(evsel, session)) 322 return -1; 323 324 if (evsel == NULL) 325 continue; 326 327 set_print_ip_opts(&evsel->attr); 328 } 329 330 if (!no_callchain) { 331 bool use_callchain = false; 332 333 evlist__for_each(session->evlist, evsel) { 334 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { 335 use_callchain = true; 336 break; 337 } 338 } 339 if (!use_callchain) 340 symbol_conf.use_callchain = false; 341 } 342 343 /* 344 * set default for tracepoints to print symbols only 345 * if callchains are present 346 */ 347 if (symbol_conf.use_callchain && 348 !output[PERF_TYPE_TRACEPOINT].user_set) { 349 struct perf_event_attr *attr; 350 351 j = PERF_TYPE_TRACEPOINT; 352 evsel = perf_session__find_first_evtype(session, j); 353 if (evsel == NULL) 354 goto out; 355 356 attr = &evsel->attr; 357 358 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) { 359 output[j].fields |= PERF_OUTPUT_IP; 360 output[j].fields |= PERF_OUTPUT_SYM; 361 output[j].fields |= PERF_OUTPUT_DSO; 362 set_print_ip_opts(attr); 363 } 364 } 365 366 out: 367 return 0; 368 } 369 370 static void print_sample_iregs(union perf_event *event __maybe_unused, 371 struct perf_sample *sample, 372 struct thread *thread __maybe_unused, 373 struct perf_event_attr *attr) 374 { 375 struct regs_dump *regs = &sample->intr_regs; 376 uint64_t mask = attr->sample_regs_intr; 377 unsigned i = 0, r; 378 379 if (!regs) 380 return; 381 382 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) { 383 u64 val = regs->regs[i++]; 384 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val); 385 } 386 } 387 388 static void print_sample_start(struct perf_sample *sample, 389 struct thread *thread, 390 struct perf_evsel *evsel) 391 { 392 struct perf_event_attr *attr = &evsel->attr; 393 unsigned long secs; 394 unsigned long usecs; 395 unsigned long long nsecs; 396 397 if (PRINT_FIELD(COMM)) { 398 if (latency_format) 399 printf("%8.8s ", thread__comm_str(thread)); 400 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain) 401 printf("%s ", thread__comm_str(thread)); 402 else 403 printf("%16s ", thread__comm_str(thread)); 404 } 405 406 if (PRINT_FIELD(PID) && PRINT_FIELD(TID)) 407 printf("%5d/%-5d ", sample->pid, sample->tid); 408 else if (PRINT_FIELD(PID)) 409 printf("%5d ", sample->pid); 410 else if (PRINT_FIELD(TID)) 411 printf("%5d ", sample->tid); 412 413 if (PRINT_FIELD(CPU)) { 414 if (latency_format) 415 printf("%3d ", sample->cpu); 416 else 417 printf("[%03d] ", sample->cpu); 418 } 419 420 if (PRINT_FIELD(TIME)) { 421 nsecs = sample->time; 422 secs = nsecs / NSECS_PER_SEC; 423 nsecs -= secs * NSECS_PER_SEC; 424 usecs = nsecs / NSECS_PER_USEC; 425 if (nanosecs) 426 printf("%5lu.%09llu: ", secs, nsecs); 427 else 428 printf("%5lu.%06lu: ", secs, usecs); 429 } 430 } 431 432 static inline char 433 mispred_str(struct branch_entry *br) 434 { 435 if (!(br->flags.mispred || br->flags.predicted)) 436 return '-'; 437 438 return br->flags.predicted ? 'P' : 'M'; 439 } 440 441 static void print_sample_brstack(union perf_event *event __maybe_unused, 442 struct perf_sample *sample, 443 struct thread *thread __maybe_unused, 444 struct perf_event_attr *attr __maybe_unused) 445 { 446 struct branch_stack *br = sample->branch_stack; 447 u64 i; 448 449 if (!(br && br->nr)) 450 return; 451 452 for (i = 0; i < br->nr; i++) { 453 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ", 454 br->entries[i].from, 455 br->entries[i].to, 456 mispred_str( br->entries + i), 457 br->entries[i].flags.in_tx? 'X' : '-', 458 br->entries[i].flags.abort? 'A' : '-', 459 br->entries[i].flags.cycles); 460 } 461 } 462 463 static void print_sample_brstacksym(union perf_event *event __maybe_unused, 464 struct perf_sample *sample, 465 struct thread *thread __maybe_unused, 466 struct perf_event_attr *attr __maybe_unused) 467 { 468 struct branch_stack *br = sample->branch_stack; 469 struct addr_location alf, alt; 470 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 471 u64 i, from, to; 472 473 if (!(br && br->nr)) 474 return; 475 476 for (i = 0; i < br->nr; i++) { 477 478 memset(&alf, 0, sizeof(alf)); 479 memset(&alt, 0, sizeof(alt)); 480 from = br->entries[i].from; 481 to = br->entries[i].to; 482 483 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, from, &alf); 484 if (alf.map) 485 alf.sym = map__find_symbol(alf.map, alf.addr, NULL); 486 487 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, to, &alt); 488 if (alt.map) 489 alt.sym = map__find_symbol(alt.map, alt.addr, NULL); 490 491 symbol__fprintf_symname_offs(alf.sym, &alf, stdout); 492 putchar('/'); 493 symbol__fprintf_symname_offs(alt.sym, &alt, stdout); 494 printf("/%c/%c/%c/%d ", 495 mispred_str( br->entries + i), 496 br->entries[i].flags.in_tx? 'X' : '-', 497 br->entries[i].flags.abort? 'A' : '-', 498 br->entries[i].flags.cycles); 499 } 500 } 501 502 503 static void print_sample_addr(union perf_event *event, 504 struct perf_sample *sample, 505 struct thread *thread, 506 struct perf_event_attr *attr) 507 { 508 struct addr_location al; 509 510 printf("%16" PRIx64, sample->addr); 511 512 if (!sample_addr_correlates_sym(attr)) 513 return; 514 515 perf_event__preprocess_sample_addr(event, sample, thread, &al); 516 517 if (PRINT_FIELD(SYM)) { 518 printf(" "); 519 if (PRINT_FIELD(SYMOFFSET)) 520 symbol__fprintf_symname_offs(al.sym, &al, stdout); 521 else 522 symbol__fprintf_symname(al.sym, stdout); 523 } 524 525 if (PRINT_FIELD(DSO)) { 526 printf(" ("); 527 map__fprintf_dsoname(al.map, stdout); 528 printf(")"); 529 } 530 } 531 532 static void print_sample_bts(union perf_event *event, 533 struct perf_sample *sample, 534 struct perf_evsel *evsel, 535 struct thread *thread, 536 struct addr_location *al) 537 { 538 struct perf_event_attr *attr = &evsel->attr; 539 bool print_srcline_last = false; 540 541 /* print branch_from information */ 542 if (PRINT_FIELD(IP)) { 543 unsigned int print_opts = output[attr->type].print_ip_opts; 544 545 if (symbol_conf.use_callchain && sample->callchain) { 546 printf("\n"); 547 } else { 548 printf(" "); 549 if (print_opts & PRINT_IP_OPT_SRCLINE) { 550 print_srcline_last = true; 551 print_opts &= ~PRINT_IP_OPT_SRCLINE; 552 } 553 } 554 perf_evsel__print_ip(evsel, sample, al, print_opts, 555 scripting_max_stack); 556 } 557 558 /* print branch_to information */ 559 if (PRINT_FIELD(ADDR) || 560 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) && 561 !output[attr->type].user_set)) { 562 printf(" => "); 563 print_sample_addr(event, sample, thread, attr); 564 } 565 566 if (print_srcline_last) 567 map__fprintf_srcline(al->map, al->addr, "\n ", stdout); 568 569 printf("\n"); 570 } 571 572 static void print_sample_flags(u32 flags) 573 { 574 const char *chars = PERF_IP_FLAG_CHARS; 575 const int n = strlen(PERF_IP_FLAG_CHARS); 576 char str[33]; 577 int i, pos = 0; 578 579 for (i = 0; i < n; i++, flags >>= 1) { 580 if (flags & 1) 581 str[pos++] = chars[i]; 582 } 583 for (; i < 32; i++, flags >>= 1) { 584 if (flags & 1) 585 str[pos++] = '?'; 586 } 587 str[pos] = 0; 588 printf(" %-4s ", str); 589 } 590 591 struct perf_script { 592 struct perf_tool tool; 593 struct perf_session *session; 594 bool show_task_events; 595 bool show_mmap_events; 596 bool show_switch_events; 597 }; 598 599 static void process_event(struct perf_script *script __maybe_unused, union perf_event *event, 600 struct perf_sample *sample, struct perf_evsel *evsel, 601 struct addr_location *al) 602 { 603 struct thread *thread = al->thread; 604 struct perf_event_attr *attr = &evsel->attr; 605 606 if (output[attr->type].fields == 0) 607 return; 608 609 print_sample_start(sample, thread, evsel); 610 611 if (PRINT_FIELD(PERIOD)) 612 printf("%10" PRIu64 " ", sample->period); 613 614 if (PRINT_FIELD(EVNAME)) { 615 const char *evname = perf_evsel__name(evsel); 616 printf("%s: ", evname ? evname : "[unknown]"); 617 } 618 619 if (print_flags) 620 print_sample_flags(sample->flags); 621 622 if (is_bts_event(attr)) { 623 print_sample_bts(event, sample, evsel, thread, al); 624 return; 625 } 626 627 if (PRINT_FIELD(TRACE)) 628 event_format__print(evsel->tp_format, sample->cpu, 629 sample->raw_data, sample->raw_size); 630 if (PRINT_FIELD(ADDR)) 631 print_sample_addr(event, sample, thread, attr); 632 633 if (PRINT_FIELD(IP)) { 634 if (!symbol_conf.use_callchain) 635 printf(" "); 636 else 637 printf("\n"); 638 639 perf_evsel__print_ip(evsel, sample, al, 640 output[attr->type].print_ip_opts, 641 scripting_max_stack); 642 } 643 644 if (PRINT_FIELD(IREGS)) 645 print_sample_iregs(event, sample, thread, attr); 646 647 if (PRINT_FIELD(BRSTACK)) 648 print_sample_brstack(event, sample, thread, attr); 649 else if (PRINT_FIELD(BRSTACKSYM)) 650 print_sample_brstacksym(event, sample, thread, attr); 651 652 printf("\n"); 653 } 654 655 static struct scripting_ops *scripting_ops; 656 657 static void setup_scripting(void) 658 { 659 setup_perl_scripting(); 660 setup_python_scripting(); 661 } 662 663 static int flush_scripting(void) 664 { 665 return scripting_ops ? scripting_ops->flush_script() : 0; 666 } 667 668 static int cleanup_scripting(void) 669 { 670 pr_debug("\nperf script stopped\n"); 671 672 return scripting_ops ? scripting_ops->stop_script() : 0; 673 } 674 675 static int process_sample_event(struct perf_tool *tool, 676 union perf_event *event, 677 struct perf_sample *sample, 678 struct perf_evsel *evsel, 679 struct machine *machine) 680 { 681 struct perf_script *scr = container_of(tool, struct perf_script, tool); 682 struct addr_location al; 683 684 if (debug_mode) { 685 if (sample->time < last_timestamp) { 686 pr_err("Samples misordered, previous: %" PRIu64 687 " this: %" PRIu64 "\n", last_timestamp, 688 sample->time); 689 nr_unordered++; 690 } 691 last_timestamp = sample->time; 692 return 0; 693 } 694 695 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) { 696 pr_err("problem processing %d event, skipping it.\n", 697 event->header.type); 698 return -1; 699 } 700 701 if (al.filtered) 702 goto out_put; 703 704 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) 705 goto out_put; 706 707 if (scripting_ops) 708 scripting_ops->process_event(event, sample, evsel, &al); 709 else 710 process_event(scr, event, sample, evsel, &al); 711 712 out_put: 713 addr_location__put(&al); 714 return 0; 715 } 716 717 static int process_attr(struct perf_tool *tool, union perf_event *event, 718 struct perf_evlist **pevlist) 719 { 720 struct perf_script *scr = container_of(tool, struct perf_script, tool); 721 struct perf_evlist *evlist; 722 struct perf_evsel *evsel, *pos; 723 int err; 724 725 err = perf_event__process_attr(tool, event, pevlist); 726 if (err) 727 return err; 728 729 evlist = *pevlist; 730 evsel = perf_evlist__last(*pevlist); 731 732 if (evsel->attr.type >= PERF_TYPE_MAX) 733 return 0; 734 735 evlist__for_each(evlist, pos) { 736 if (pos->attr.type == evsel->attr.type && pos != evsel) 737 return 0; 738 } 739 740 set_print_ip_opts(&evsel->attr); 741 742 if (evsel->attr.sample_type) 743 err = perf_evsel__check_attr(evsel, scr->session); 744 745 return err; 746 } 747 748 static int process_comm_event(struct perf_tool *tool, 749 union perf_event *event, 750 struct perf_sample *sample, 751 struct machine *machine) 752 { 753 struct thread *thread; 754 struct perf_script *script = container_of(tool, struct perf_script, tool); 755 struct perf_session *session = script->session; 756 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 757 int ret = -1; 758 759 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid); 760 if (thread == NULL) { 761 pr_debug("problem processing COMM event, skipping it.\n"); 762 return -1; 763 } 764 765 if (perf_event__process_comm(tool, event, sample, machine) < 0) 766 goto out; 767 768 if (!evsel->attr.sample_id_all) { 769 sample->cpu = 0; 770 sample->time = 0; 771 sample->tid = event->comm.tid; 772 sample->pid = event->comm.pid; 773 } 774 print_sample_start(sample, thread, evsel); 775 perf_event__fprintf(event, stdout); 776 ret = 0; 777 out: 778 thread__put(thread); 779 return ret; 780 } 781 782 static int process_fork_event(struct perf_tool *tool, 783 union perf_event *event, 784 struct perf_sample *sample, 785 struct machine *machine) 786 { 787 struct thread *thread; 788 struct perf_script *script = container_of(tool, struct perf_script, tool); 789 struct perf_session *session = script->session; 790 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 791 792 if (perf_event__process_fork(tool, event, sample, machine) < 0) 793 return -1; 794 795 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); 796 if (thread == NULL) { 797 pr_debug("problem processing FORK event, skipping it.\n"); 798 return -1; 799 } 800 801 if (!evsel->attr.sample_id_all) { 802 sample->cpu = 0; 803 sample->time = event->fork.time; 804 sample->tid = event->fork.tid; 805 sample->pid = event->fork.pid; 806 } 807 print_sample_start(sample, thread, evsel); 808 perf_event__fprintf(event, stdout); 809 thread__put(thread); 810 811 return 0; 812 } 813 static int process_exit_event(struct perf_tool *tool, 814 union perf_event *event, 815 struct perf_sample *sample, 816 struct machine *machine) 817 { 818 int err = 0; 819 struct thread *thread; 820 struct perf_script *script = container_of(tool, struct perf_script, tool); 821 struct perf_session *session = script->session; 822 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 823 824 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); 825 if (thread == NULL) { 826 pr_debug("problem processing EXIT event, skipping it.\n"); 827 return -1; 828 } 829 830 if (!evsel->attr.sample_id_all) { 831 sample->cpu = 0; 832 sample->time = 0; 833 sample->tid = event->fork.tid; 834 sample->pid = event->fork.pid; 835 } 836 print_sample_start(sample, thread, evsel); 837 perf_event__fprintf(event, stdout); 838 839 if (perf_event__process_exit(tool, event, sample, machine) < 0) 840 err = -1; 841 842 thread__put(thread); 843 return err; 844 } 845 846 static int process_mmap_event(struct perf_tool *tool, 847 union perf_event *event, 848 struct perf_sample *sample, 849 struct machine *machine) 850 { 851 struct thread *thread; 852 struct perf_script *script = container_of(tool, struct perf_script, tool); 853 struct perf_session *session = script->session; 854 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 855 856 if (perf_event__process_mmap(tool, event, sample, machine) < 0) 857 return -1; 858 859 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid); 860 if (thread == NULL) { 861 pr_debug("problem processing MMAP event, skipping it.\n"); 862 return -1; 863 } 864 865 if (!evsel->attr.sample_id_all) { 866 sample->cpu = 0; 867 sample->time = 0; 868 sample->tid = event->mmap.tid; 869 sample->pid = event->mmap.pid; 870 } 871 print_sample_start(sample, thread, evsel); 872 perf_event__fprintf(event, stdout); 873 thread__put(thread); 874 return 0; 875 } 876 877 static int process_mmap2_event(struct perf_tool *tool, 878 union perf_event *event, 879 struct perf_sample *sample, 880 struct machine *machine) 881 { 882 struct thread *thread; 883 struct perf_script *script = container_of(tool, struct perf_script, tool); 884 struct perf_session *session = script->session; 885 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 886 887 if (perf_event__process_mmap2(tool, event, sample, machine) < 0) 888 return -1; 889 890 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid); 891 if (thread == NULL) { 892 pr_debug("problem processing MMAP2 event, skipping it.\n"); 893 return -1; 894 } 895 896 if (!evsel->attr.sample_id_all) { 897 sample->cpu = 0; 898 sample->time = 0; 899 sample->tid = event->mmap2.tid; 900 sample->pid = event->mmap2.pid; 901 } 902 print_sample_start(sample, thread, evsel); 903 perf_event__fprintf(event, stdout); 904 thread__put(thread); 905 return 0; 906 } 907 908 static int process_switch_event(struct perf_tool *tool, 909 union perf_event *event, 910 struct perf_sample *sample, 911 struct machine *machine) 912 { 913 struct thread *thread; 914 struct perf_script *script = container_of(tool, struct perf_script, tool); 915 struct perf_session *session = script->session; 916 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 917 918 if (perf_event__process_switch(tool, event, sample, machine) < 0) 919 return -1; 920 921 thread = machine__findnew_thread(machine, sample->pid, 922 sample->tid); 923 if (thread == NULL) { 924 pr_debug("problem processing SWITCH event, skipping it.\n"); 925 return -1; 926 } 927 928 print_sample_start(sample, thread, evsel); 929 perf_event__fprintf(event, stdout); 930 thread__put(thread); 931 return 0; 932 } 933 934 static void sig_handler(int sig __maybe_unused) 935 { 936 session_done = 1; 937 } 938 939 static int __cmd_script(struct perf_script *script) 940 { 941 int ret; 942 943 signal(SIGINT, sig_handler); 944 945 /* override event processing functions */ 946 if (script->show_task_events) { 947 script->tool.comm = process_comm_event; 948 script->tool.fork = process_fork_event; 949 script->tool.exit = process_exit_event; 950 } 951 if (script->show_mmap_events) { 952 script->tool.mmap = process_mmap_event; 953 script->tool.mmap2 = process_mmap2_event; 954 } 955 if (script->show_switch_events) 956 script->tool.context_switch = process_switch_event; 957 958 ret = perf_session__process_events(script->session); 959 960 if (debug_mode) 961 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered); 962 963 return ret; 964 } 965 966 struct script_spec { 967 struct list_head node; 968 struct scripting_ops *ops; 969 char spec[0]; 970 }; 971 972 static LIST_HEAD(script_specs); 973 974 static struct script_spec *script_spec__new(const char *spec, 975 struct scripting_ops *ops) 976 { 977 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); 978 979 if (s != NULL) { 980 strcpy(s->spec, spec); 981 s->ops = ops; 982 } 983 984 return s; 985 } 986 987 static void script_spec__add(struct script_spec *s) 988 { 989 list_add_tail(&s->node, &script_specs); 990 } 991 992 static struct script_spec *script_spec__find(const char *spec) 993 { 994 struct script_spec *s; 995 996 list_for_each_entry(s, &script_specs, node) 997 if (strcasecmp(s->spec, spec) == 0) 998 return s; 999 return NULL; 1000 } 1001 1002 static struct script_spec *script_spec__findnew(const char *spec, 1003 struct scripting_ops *ops) 1004 { 1005 struct script_spec *s = script_spec__find(spec); 1006 1007 if (s) 1008 return s; 1009 1010 s = script_spec__new(spec, ops); 1011 if (!s) 1012 return NULL; 1013 1014 script_spec__add(s); 1015 1016 return s; 1017 } 1018 1019 int script_spec_register(const char *spec, struct scripting_ops *ops) 1020 { 1021 struct script_spec *s; 1022 1023 s = script_spec__find(spec); 1024 if (s) 1025 return -1; 1026 1027 s = script_spec__findnew(spec, ops); 1028 if (!s) 1029 return -1; 1030 1031 return 0; 1032 } 1033 1034 static struct scripting_ops *script_spec__lookup(const char *spec) 1035 { 1036 struct script_spec *s = script_spec__find(spec); 1037 if (!s) 1038 return NULL; 1039 1040 return s->ops; 1041 } 1042 1043 static void list_available_languages(void) 1044 { 1045 struct script_spec *s; 1046 1047 fprintf(stderr, "\n"); 1048 fprintf(stderr, "Scripting language extensions (used in " 1049 "perf script -s [spec:]script.[spec]):\n\n"); 1050 1051 list_for_each_entry(s, &script_specs, node) 1052 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); 1053 1054 fprintf(stderr, "\n"); 1055 } 1056 1057 static int parse_scriptname(const struct option *opt __maybe_unused, 1058 const char *str, int unset __maybe_unused) 1059 { 1060 char spec[PATH_MAX]; 1061 const char *script, *ext; 1062 int len; 1063 1064 if (strcmp(str, "lang") == 0) { 1065 list_available_languages(); 1066 exit(0); 1067 } 1068 1069 script = strchr(str, ':'); 1070 if (script) { 1071 len = script - str; 1072 if (len >= PATH_MAX) { 1073 fprintf(stderr, "invalid language specifier"); 1074 return -1; 1075 } 1076 strncpy(spec, str, len); 1077 spec[len] = '\0'; 1078 scripting_ops = script_spec__lookup(spec); 1079 if (!scripting_ops) { 1080 fprintf(stderr, "invalid language specifier"); 1081 return -1; 1082 } 1083 script++; 1084 } else { 1085 script = str; 1086 ext = strrchr(script, '.'); 1087 if (!ext) { 1088 fprintf(stderr, "invalid script extension"); 1089 return -1; 1090 } 1091 scripting_ops = script_spec__lookup(++ext); 1092 if (!scripting_ops) { 1093 fprintf(stderr, "invalid script extension"); 1094 return -1; 1095 } 1096 } 1097 1098 script_name = strdup(script); 1099 1100 return 0; 1101 } 1102 1103 static int parse_output_fields(const struct option *opt __maybe_unused, 1104 const char *arg, int unset __maybe_unused) 1105 { 1106 char *tok; 1107 int i, imax = ARRAY_SIZE(all_output_options); 1108 int j; 1109 int rc = 0; 1110 char *str = strdup(arg); 1111 int type = -1; 1112 1113 if (!str) 1114 return -ENOMEM; 1115 1116 /* first word can state for which event type the user is specifying 1117 * the fields. If no type exists, the specified fields apply to all 1118 * event types found in the file minus the invalid fields for a type. 1119 */ 1120 tok = strchr(str, ':'); 1121 if (tok) { 1122 *tok = '\0'; 1123 tok++; 1124 if (!strcmp(str, "hw")) 1125 type = PERF_TYPE_HARDWARE; 1126 else if (!strcmp(str, "sw")) 1127 type = PERF_TYPE_SOFTWARE; 1128 else if (!strcmp(str, "trace")) 1129 type = PERF_TYPE_TRACEPOINT; 1130 else if (!strcmp(str, "raw")) 1131 type = PERF_TYPE_RAW; 1132 else { 1133 fprintf(stderr, "Invalid event type in field string.\n"); 1134 rc = -EINVAL; 1135 goto out; 1136 } 1137 1138 if (output[type].user_set) 1139 pr_warning("Overriding previous field request for %s events.\n", 1140 event_type(type)); 1141 1142 output[type].fields = 0; 1143 output[type].user_set = true; 1144 output[type].wildcard_set = false; 1145 1146 } else { 1147 tok = str; 1148 if (strlen(str) == 0) { 1149 fprintf(stderr, 1150 "Cannot set fields to 'none' for all event types.\n"); 1151 rc = -EINVAL; 1152 goto out; 1153 } 1154 1155 if (output_set_by_user()) 1156 pr_warning("Overriding previous field request for all events.\n"); 1157 1158 for (j = 0; j < PERF_TYPE_MAX; ++j) { 1159 output[j].fields = 0; 1160 output[j].user_set = true; 1161 output[j].wildcard_set = true; 1162 } 1163 } 1164 1165 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) { 1166 for (i = 0; i < imax; ++i) { 1167 if (strcmp(tok, all_output_options[i].str) == 0) 1168 break; 1169 } 1170 if (i == imax && strcmp(tok, "flags") == 0) { 1171 print_flags = true; 1172 continue; 1173 } 1174 if (i == imax) { 1175 fprintf(stderr, "Invalid field requested.\n"); 1176 rc = -EINVAL; 1177 goto out; 1178 } 1179 1180 if (type == -1) { 1181 /* add user option to all events types for 1182 * which it is valid 1183 */ 1184 for (j = 0; j < PERF_TYPE_MAX; ++j) { 1185 if (output[j].invalid_fields & all_output_options[i].field) { 1186 pr_warning("\'%s\' not valid for %s events. Ignoring.\n", 1187 all_output_options[i].str, event_type(j)); 1188 } else 1189 output[j].fields |= all_output_options[i].field; 1190 } 1191 } else { 1192 if (output[type].invalid_fields & all_output_options[i].field) { 1193 fprintf(stderr, "\'%s\' not valid for %s events.\n", 1194 all_output_options[i].str, event_type(type)); 1195 1196 rc = -EINVAL; 1197 goto out; 1198 } 1199 output[type].fields |= all_output_options[i].field; 1200 } 1201 } 1202 1203 if (type >= 0) { 1204 if (output[type].fields == 0) { 1205 pr_debug("No fields requested for %s type. " 1206 "Events will not be displayed.\n", event_type(type)); 1207 } 1208 } 1209 1210 out: 1211 free(str); 1212 return rc; 1213 } 1214 1215 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */ 1216 static int is_directory(const char *base_path, const struct dirent *dent) 1217 { 1218 char path[PATH_MAX]; 1219 struct stat st; 1220 1221 sprintf(path, "%s/%s", base_path, dent->d_name); 1222 if (stat(path, &st)) 1223 return 0; 1224 1225 return S_ISDIR(st.st_mode); 1226 } 1227 1228 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\ 1229 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \ 1230 lang_next) \ 1231 if ((lang_dirent.d_type == DT_DIR || \ 1232 (lang_dirent.d_type == DT_UNKNOWN && \ 1233 is_directory(scripts_path, &lang_dirent))) && \ 1234 (strcmp(lang_dirent.d_name, ".")) && \ 1235 (strcmp(lang_dirent.d_name, ".."))) 1236 1237 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\ 1238 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \ 1239 script_next) \ 1240 if (script_dirent.d_type != DT_DIR && \ 1241 (script_dirent.d_type != DT_UNKNOWN || \ 1242 !is_directory(lang_path, &script_dirent))) 1243 1244 1245 #define RECORD_SUFFIX "-record" 1246 #define REPORT_SUFFIX "-report" 1247 1248 struct script_desc { 1249 struct list_head node; 1250 char *name; 1251 char *half_liner; 1252 char *args; 1253 }; 1254 1255 static LIST_HEAD(script_descs); 1256 1257 static struct script_desc *script_desc__new(const char *name) 1258 { 1259 struct script_desc *s = zalloc(sizeof(*s)); 1260 1261 if (s != NULL && name) 1262 s->name = strdup(name); 1263 1264 return s; 1265 } 1266 1267 static void script_desc__delete(struct script_desc *s) 1268 { 1269 zfree(&s->name); 1270 zfree(&s->half_liner); 1271 zfree(&s->args); 1272 free(s); 1273 } 1274 1275 static void script_desc__add(struct script_desc *s) 1276 { 1277 list_add_tail(&s->node, &script_descs); 1278 } 1279 1280 static struct script_desc *script_desc__find(const char *name) 1281 { 1282 struct script_desc *s; 1283 1284 list_for_each_entry(s, &script_descs, node) 1285 if (strcasecmp(s->name, name) == 0) 1286 return s; 1287 return NULL; 1288 } 1289 1290 static struct script_desc *script_desc__findnew(const char *name) 1291 { 1292 struct script_desc *s = script_desc__find(name); 1293 1294 if (s) 1295 return s; 1296 1297 s = script_desc__new(name); 1298 if (!s) 1299 goto out_delete_desc; 1300 1301 script_desc__add(s); 1302 1303 return s; 1304 1305 out_delete_desc: 1306 script_desc__delete(s); 1307 1308 return NULL; 1309 } 1310 1311 static const char *ends_with(const char *str, const char *suffix) 1312 { 1313 size_t suffix_len = strlen(suffix); 1314 const char *p = str; 1315 1316 if (strlen(str) > suffix_len) { 1317 p = str + strlen(str) - suffix_len; 1318 if (!strncmp(p, suffix, suffix_len)) 1319 return p; 1320 } 1321 1322 return NULL; 1323 } 1324 1325 static int read_script_info(struct script_desc *desc, const char *filename) 1326 { 1327 char line[BUFSIZ], *p; 1328 FILE *fp; 1329 1330 fp = fopen(filename, "r"); 1331 if (!fp) 1332 return -1; 1333 1334 while (fgets(line, sizeof(line), fp)) { 1335 p = ltrim(line); 1336 if (strlen(p) == 0) 1337 continue; 1338 if (*p != '#') 1339 continue; 1340 p++; 1341 if (strlen(p) && *p == '!') 1342 continue; 1343 1344 p = ltrim(p); 1345 if (strlen(p) && p[strlen(p) - 1] == '\n') 1346 p[strlen(p) - 1] = '\0'; 1347 1348 if (!strncmp(p, "description:", strlen("description:"))) { 1349 p += strlen("description:"); 1350 desc->half_liner = strdup(ltrim(p)); 1351 continue; 1352 } 1353 1354 if (!strncmp(p, "args:", strlen("args:"))) { 1355 p += strlen("args:"); 1356 desc->args = strdup(ltrim(p)); 1357 continue; 1358 } 1359 } 1360 1361 fclose(fp); 1362 1363 return 0; 1364 } 1365 1366 static char *get_script_root(struct dirent *script_dirent, const char *suffix) 1367 { 1368 char *script_root, *str; 1369 1370 script_root = strdup(script_dirent->d_name); 1371 if (!script_root) 1372 return NULL; 1373 1374 str = (char *)ends_with(script_root, suffix); 1375 if (!str) { 1376 free(script_root); 1377 return NULL; 1378 } 1379 1380 *str = '\0'; 1381 return script_root; 1382 } 1383 1384 static int list_available_scripts(const struct option *opt __maybe_unused, 1385 const char *s __maybe_unused, 1386 int unset __maybe_unused) 1387 { 1388 struct dirent *script_next, *lang_next, script_dirent, lang_dirent; 1389 char scripts_path[MAXPATHLEN]; 1390 DIR *scripts_dir, *lang_dir; 1391 char script_path[MAXPATHLEN]; 1392 char lang_path[MAXPATHLEN]; 1393 struct script_desc *desc; 1394 char first_half[BUFSIZ]; 1395 char *script_root; 1396 1397 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); 1398 1399 scripts_dir = opendir(scripts_path); 1400 if (!scripts_dir) 1401 return -1; 1402 1403 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { 1404 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, 1405 lang_dirent.d_name); 1406 lang_dir = opendir(lang_path); 1407 if (!lang_dir) 1408 continue; 1409 1410 for_each_script(lang_path, lang_dir, script_dirent, script_next) { 1411 script_root = get_script_root(&script_dirent, REPORT_SUFFIX); 1412 if (script_root) { 1413 desc = script_desc__findnew(script_root); 1414 snprintf(script_path, MAXPATHLEN, "%s/%s", 1415 lang_path, script_dirent.d_name); 1416 read_script_info(desc, script_path); 1417 free(script_root); 1418 } 1419 } 1420 } 1421 1422 fprintf(stdout, "List of available trace scripts:\n"); 1423 list_for_each_entry(desc, &script_descs, node) { 1424 sprintf(first_half, "%s %s", desc->name, 1425 desc->args ? desc->args : ""); 1426 fprintf(stdout, " %-36s %s\n", first_half, 1427 desc->half_liner ? desc->half_liner : ""); 1428 } 1429 1430 exit(0); 1431 } 1432 1433 /* 1434 * Some scripts specify the required events in their "xxx-record" file, 1435 * this function will check if the events in perf.data match those 1436 * mentioned in the "xxx-record". 1437 * 1438 * Fixme: All existing "xxx-record" are all in good formats "-e event ", 1439 * which is covered well now. And new parsing code should be added to 1440 * cover the future complexing formats like event groups etc. 1441 */ 1442 static int check_ev_match(char *dir_name, char *scriptname, 1443 struct perf_session *session) 1444 { 1445 char filename[MAXPATHLEN], evname[128]; 1446 char line[BUFSIZ], *p; 1447 struct perf_evsel *pos; 1448 int match, len; 1449 FILE *fp; 1450 1451 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname); 1452 1453 fp = fopen(filename, "r"); 1454 if (!fp) 1455 return -1; 1456 1457 while (fgets(line, sizeof(line), fp)) { 1458 p = ltrim(line); 1459 if (*p == '#') 1460 continue; 1461 1462 while (strlen(p)) { 1463 p = strstr(p, "-e"); 1464 if (!p) 1465 break; 1466 1467 p += 2; 1468 p = ltrim(p); 1469 len = strcspn(p, " \t"); 1470 if (!len) 1471 break; 1472 1473 snprintf(evname, len + 1, "%s", p); 1474 1475 match = 0; 1476 evlist__for_each(session->evlist, pos) { 1477 if (!strcmp(perf_evsel__name(pos), evname)) { 1478 match = 1; 1479 break; 1480 } 1481 } 1482 1483 if (!match) { 1484 fclose(fp); 1485 return -1; 1486 } 1487 } 1488 } 1489 1490 fclose(fp); 1491 return 0; 1492 } 1493 1494 /* 1495 * Return -1 if none is found, otherwise the actual scripts number. 1496 * 1497 * Currently the only user of this function is the script browser, which 1498 * will list all statically runnable scripts, select one, execute it and 1499 * show the output in a perf browser. 1500 */ 1501 int find_scripts(char **scripts_array, char **scripts_path_array) 1502 { 1503 struct dirent *script_next, *lang_next, script_dirent, lang_dirent; 1504 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN]; 1505 DIR *scripts_dir, *lang_dir; 1506 struct perf_session *session; 1507 struct perf_data_file file = { 1508 .path = input_name, 1509 .mode = PERF_DATA_MODE_READ, 1510 }; 1511 char *temp; 1512 int i = 0; 1513 1514 session = perf_session__new(&file, false, NULL); 1515 if (!session) 1516 return -1; 1517 1518 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); 1519 1520 scripts_dir = opendir(scripts_path); 1521 if (!scripts_dir) { 1522 perf_session__delete(session); 1523 return -1; 1524 } 1525 1526 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { 1527 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path, 1528 lang_dirent.d_name); 1529 #ifdef NO_LIBPERL 1530 if (strstr(lang_path, "perl")) 1531 continue; 1532 #endif 1533 #ifdef NO_LIBPYTHON 1534 if (strstr(lang_path, "python")) 1535 continue; 1536 #endif 1537 1538 lang_dir = opendir(lang_path); 1539 if (!lang_dir) 1540 continue; 1541 1542 for_each_script(lang_path, lang_dir, script_dirent, script_next) { 1543 /* Skip those real time scripts: xxxtop.p[yl] */ 1544 if (strstr(script_dirent.d_name, "top.")) 1545 continue; 1546 sprintf(scripts_path_array[i], "%s/%s", lang_path, 1547 script_dirent.d_name); 1548 temp = strchr(script_dirent.d_name, '.'); 1549 snprintf(scripts_array[i], 1550 (temp - script_dirent.d_name) + 1, 1551 "%s", script_dirent.d_name); 1552 1553 if (check_ev_match(lang_path, 1554 scripts_array[i], session)) 1555 continue; 1556 1557 i++; 1558 } 1559 closedir(lang_dir); 1560 } 1561 1562 closedir(scripts_dir); 1563 perf_session__delete(session); 1564 return i; 1565 } 1566 1567 static char *get_script_path(const char *script_root, const char *suffix) 1568 { 1569 struct dirent *script_next, *lang_next, script_dirent, lang_dirent; 1570 char scripts_path[MAXPATHLEN]; 1571 char script_path[MAXPATHLEN]; 1572 DIR *scripts_dir, *lang_dir; 1573 char lang_path[MAXPATHLEN]; 1574 char *__script_root; 1575 1576 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); 1577 1578 scripts_dir = opendir(scripts_path); 1579 if (!scripts_dir) 1580 return NULL; 1581 1582 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { 1583 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, 1584 lang_dirent.d_name); 1585 lang_dir = opendir(lang_path); 1586 if (!lang_dir) 1587 continue; 1588 1589 for_each_script(lang_path, lang_dir, script_dirent, script_next) { 1590 __script_root = get_script_root(&script_dirent, suffix); 1591 if (__script_root && !strcmp(script_root, __script_root)) { 1592 free(__script_root); 1593 closedir(lang_dir); 1594 closedir(scripts_dir); 1595 snprintf(script_path, MAXPATHLEN, "%s/%s", 1596 lang_path, script_dirent.d_name); 1597 return strdup(script_path); 1598 } 1599 free(__script_root); 1600 } 1601 closedir(lang_dir); 1602 } 1603 closedir(scripts_dir); 1604 1605 return NULL; 1606 } 1607 1608 static bool is_top_script(const char *script_path) 1609 { 1610 return ends_with(script_path, "top") == NULL ? false : true; 1611 } 1612 1613 static int has_required_arg(char *script_path) 1614 { 1615 struct script_desc *desc; 1616 int n_args = 0; 1617 char *p; 1618 1619 desc = script_desc__new(NULL); 1620 1621 if (read_script_info(desc, script_path)) 1622 goto out; 1623 1624 if (!desc->args) 1625 goto out; 1626 1627 for (p = desc->args; *p; p++) 1628 if (*p == '<') 1629 n_args++; 1630 out: 1631 script_desc__delete(desc); 1632 1633 return n_args; 1634 } 1635 1636 static int have_cmd(int argc, const char **argv) 1637 { 1638 char **__argv = malloc(sizeof(const char *) * argc); 1639 1640 if (!__argv) { 1641 pr_err("malloc failed\n"); 1642 return -1; 1643 } 1644 1645 memcpy(__argv, argv, sizeof(const char *) * argc); 1646 argc = parse_options(argc, (const char **)__argv, record_options, 1647 NULL, PARSE_OPT_STOP_AT_NON_OPTION); 1648 free(__argv); 1649 1650 system_wide = (argc == 0); 1651 1652 return 0; 1653 } 1654 1655 static void script__setup_sample_type(struct perf_script *script) 1656 { 1657 struct perf_session *session = script->session; 1658 u64 sample_type = perf_evlist__combined_sample_type(session->evlist); 1659 1660 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) { 1661 if ((sample_type & PERF_SAMPLE_REGS_USER) && 1662 (sample_type & PERF_SAMPLE_STACK_USER)) 1663 callchain_param.record_mode = CALLCHAIN_DWARF; 1664 else if (sample_type & PERF_SAMPLE_BRANCH_STACK) 1665 callchain_param.record_mode = CALLCHAIN_LBR; 1666 else 1667 callchain_param.record_mode = CALLCHAIN_FP; 1668 } 1669 } 1670 1671 int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused) 1672 { 1673 bool show_full_info = false; 1674 bool header = false; 1675 bool header_only = false; 1676 bool script_started = false; 1677 char *rec_script_path = NULL; 1678 char *rep_script_path = NULL; 1679 struct perf_session *session; 1680 struct itrace_synth_opts itrace_synth_opts = { .set = false, }; 1681 char *script_path = NULL; 1682 const char **__argv; 1683 int i, j, err = 0; 1684 struct perf_script script = { 1685 .tool = { 1686 .sample = process_sample_event, 1687 .mmap = perf_event__process_mmap, 1688 .mmap2 = perf_event__process_mmap2, 1689 .comm = perf_event__process_comm, 1690 .exit = perf_event__process_exit, 1691 .fork = perf_event__process_fork, 1692 .attr = process_attr, 1693 .tracing_data = perf_event__process_tracing_data, 1694 .build_id = perf_event__process_build_id, 1695 .id_index = perf_event__process_id_index, 1696 .auxtrace_info = perf_event__process_auxtrace_info, 1697 .auxtrace = perf_event__process_auxtrace, 1698 .auxtrace_error = perf_event__process_auxtrace_error, 1699 .ordered_events = true, 1700 .ordering_requires_timestamps = true, 1701 }, 1702 }; 1703 struct perf_data_file file = { 1704 .mode = PERF_DATA_MODE_READ, 1705 }; 1706 const struct option options[] = { 1707 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 1708 "dump raw trace in ASCII"), 1709 OPT_INCR('v', "verbose", &verbose, 1710 "be more verbose (show symbol address, etc)"), 1711 OPT_BOOLEAN('L', "Latency", &latency_format, 1712 "show latency attributes (irqs/preemption disabled, etc)"), 1713 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", 1714 list_available_scripts), 1715 OPT_CALLBACK('s', "script", NULL, "name", 1716 "script file name (lang:script name, script name, or *)", 1717 parse_scriptname), 1718 OPT_STRING('g', "gen-script", &generate_script_lang, "lang", 1719 "generate perf-script.xx script in specified language"), 1720 OPT_STRING('i', "input", &input_name, "file", "input file name"), 1721 OPT_BOOLEAN('d', "debug-mode", &debug_mode, 1722 "do various checks like samples ordering and lost events"), 1723 OPT_BOOLEAN(0, "header", &header, "Show data header."), 1724 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."), 1725 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, 1726 "file", "vmlinux pathname"), 1727 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, 1728 "file", "kallsyms pathname"), 1729 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain, 1730 "When printing symbols do not display call chain"), 1731 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", 1732 "Look for files with symbols relative to this directory"), 1733 OPT_CALLBACK('F', "fields", NULL, "str", 1734 "comma separated output fields prepend with 'type:'. " 1735 "Valid types: hw,sw,trace,raw. " 1736 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso," 1737 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields), 1738 OPT_BOOLEAN('a', "all-cpus", &system_wide, 1739 "system-wide collection from all CPUs"), 1740 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", 1741 "only consider these symbols"), 1742 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"), 1743 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", 1744 "only display events for these comms"), 1745 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]", 1746 "only consider symbols in these pids"), 1747 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]", 1748 "only consider symbols in these tids"), 1749 OPT_BOOLEAN('I', "show-info", &show_full_info, 1750 "display extended information from perf.data file"), 1751 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path, 1752 "Show the path of [kernel.kallsyms]"), 1753 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events, 1754 "Show the fork/comm/exit events"), 1755 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events, 1756 "Show the mmap events"), 1757 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events, 1758 "Show context switch events (if recorded)"), 1759 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"), 1760 OPT_BOOLEAN(0, "ns", &nanosecs, 1761 "Use 9 decimal places when displaying time"), 1762 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts", 1763 "Instruction Tracing options", 1764 itrace_parse_synth_opts), 1765 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename, 1766 "Show full source file name path for source lines"), 1767 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle, 1768 "Enable symbol demangling"), 1769 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel, 1770 "Enable kernel symbol demangling"), 1771 1772 OPT_END() 1773 }; 1774 const char * const script_subcommands[] = { "record", "report", NULL }; 1775 const char *script_usage[] = { 1776 "perf script [<options>]", 1777 "perf script [<options>] record <script> [<record-options>] <command>", 1778 "perf script [<options>] report <script> [script-args]", 1779 "perf script [<options>] <script> [<record-options>] <command>", 1780 "perf script [<options>] <top-script> [script-args]", 1781 NULL 1782 }; 1783 1784 setup_scripting(); 1785 1786 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage, 1787 PARSE_OPT_STOP_AT_NON_OPTION); 1788 1789 file.path = input_name; 1790 1791 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { 1792 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); 1793 if (!rec_script_path) 1794 return cmd_record(argc, argv, NULL); 1795 } 1796 1797 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) { 1798 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX); 1799 if (!rep_script_path) { 1800 fprintf(stderr, 1801 "Please specify a valid report script" 1802 "(see 'perf script -l' for listing)\n"); 1803 return -1; 1804 } 1805 } 1806 1807 if (itrace_synth_opts.callchain && 1808 itrace_synth_opts.callchain_sz > scripting_max_stack) 1809 scripting_max_stack = itrace_synth_opts.callchain_sz; 1810 1811 /* make sure PERF_EXEC_PATH is set for scripts */ 1812 perf_set_argv_exec_path(perf_exec_path()); 1813 1814 if (argc && !script_name && !rec_script_path && !rep_script_path) { 1815 int live_pipe[2]; 1816 int rep_args; 1817 pid_t pid; 1818 1819 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX); 1820 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX); 1821 1822 if (!rec_script_path && !rep_script_path) { 1823 usage_with_options_msg(script_usage, options, 1824 "Couldn't find script `%s'\n\n See perf" 1825 " script -l for available scripts.\n", argv[0]); 1826 } 1827 1828 if (is_top_script(argv[0])) { 1829 rep_args = argc - 1; 1830 } else { 1831 int rec_args; 1832 1833 rep_args = has_required_arg(rep_script_path); 1834 rec_args = (argc - 1) - rep_args; 1835 if (rec_args < 0) { 1836 usage_with_options_msg(script_usage, options, 1837 "`%s' script requires options." 1838 "\n\n See perf script -l for available " 1839 "scripts and options.\n", argv[0]); 1840 } 1841 } 1842 1843 if (pipe(live_pipe) < 0) { 1844 perror("failed to create pipe"); 1845 return -1; 1846 } 1847 1848 pid = fork(); 1849 if (pid < 0) { 1850 perror("failed to fork"); 1851 return -1; 1852 } 1853 1854 if (!pid) { 1855 j = 0; 1856 1857 dup2(live_pipe[1], 1); 1858 close(live_pipe[0]); 1859 1860 if (is_top_script(argv[0])) { 1861 system_wide = true; 1862 } else if (!system_wide) { 1863 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) { 1864 err = -1; 1865 goto out; 1866 } 1867 } 1868 1869 __argv = malloc((argc + 6) * sizeof(const char *)); 1870 if (!__argv) { 1871 pr_err("malloc failed\n"); 1872 err = -ENOMEM; 1873 goto out; 1874 } 1875 1876 __argv[j++] = "/bin/sh"; 1877 __argv[j++] = rec_script_path; 1878 if (system_wide) 1879 __argv[j++] = "-a"; 1880 __argv[j++] = "-q"; 1881 __argv[j++] = "-o"; 1882 __argv[j++] = "-"; 1883 for (i = rep_args + 1; i < argc; i++) 1884 __argv[j++] = argv[i]; 1885 __argv[j++] = NULL; 1886 1887 execvp("/bin/sh", (char **)__argv); 1888 free(__argv); 1889 exit(-1); 1890 } 1891 1892 dup2(live_pipe[0], 0); 1893 close(live_pipe[1]); 1894 1895 __argv = malloc((argc + 4) * sizeof(const char *)); 1896 if (!__argv) { 1897 pr_err("malloc failed\n"); 1898 err = -ENOMEM; 1899 goto out; 1900 } 1901 1902 j = 0; 1903 __argv[j++] = "/bin/sh"; 1904 __argv[j++] = rep_script_path; 1905 for (i = 1; i < rep_args + 1; i++) 1906 __argv[j++] = argv[i]; 1907 __argv[j++] = "-i"; 1908 __argv[j++] = "-"; 1909 __argv[j++] = NULL; 1910 1911 execvp("/bin/sh", (char **)__argv); 1912 free(__argv); 1913 exit(-1); 1914 } 1915 1916 if (rec_script_path) 1917 script_path = rec_script_path; 1918 if (rep_script_path) 1919 script_path = rep_script_path; 1920 1921 if (script_path) { 1922 j = 0; 1923 1924 if (!rec_script_path) 1925 system_wide = false; 1926 else if (!system_wide) { 1927 if (have_cmd(argc - 1, &argv[1]) != 0) { 1928 err = -1; 1929 goto out; 1930 } 1931 } 1932 1933 __argv = malloc((argc + 2) * sizeof(const char *)); 1934 if (!__argv) { 1935 pr_err("malloc failed\n"); 1936 err = -ENOMEM; 1937 goto out; 1938 } 1939 1940 __argv[j++] = "/bin/sh"; 1941 __argv[j++] = script_path; 1942 if (system_wide) 1943 __argv[j++] = "-a"; 1944 for (i = 2; i < argc; i++) 1945 __argv[j++] = argv[i]; 1946 __argv[j++] = NULL; 1947 1948 execvp("/bin/sh", (char **)__argv); 1949 free(__argv); 1950 exit(-1); 1951 } 1952 1953 if (!script_name) 1954 setup_pager(); 1955 1956 session = perf_session__new(&file, false, &script.tool); 1957 if (session == NULL) 1958 return -1; 1959 1960 if (header || header_only) { 1961 perf_session__fprintf_info(session, stdout, show_full_info); 1962 if (header_only) 1963 goto out_delete; 1964 } 1965 1966 if (symbol__init(&session->header.env) < 0) 1967 goto out_delete; 1968 1969 script.session = session; 1970 script__setup_sample_type(&script); 1971 1972 session->itrace_synth_opts = &itrace_synth_opts; 1973 1974 if (cpu_list) { 1975 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap); 1976 if (err < 0) 1977 goto out_delete; 1978 } 1979 1980 if (!no_callchain) 1981 symbol_conf.use_callchain = true; 1982 else 1983 symbol_conf.use_callchain = false; 1984 1985 if (session->tevent.pevent && 1986 pevent_set_function_resolver(session->tevent.pevent, 1987 machine__resolve_kernel_addr, 1988 &session->machines.host) < 0) { 1989 pr_err("%s: failed to set libtraceevent function resolver\n", __func__); 1990 return -1; 1991 } 1992 1993 if (generate_script_lang) { 1994 struct stat perf_stat; 1995 int input; 1996 1997 if (output_set_by_user()) { 1998 fprintf(stderr, 1999 "custom fields not supported for generated scripts"); 2000 err = -EINVAL; 2001 goto out_delete; 2002 } 2003 2004 input = open(file.path, O_RDONLY); /* input_name */ 2005 if (input < 0) { 2006 err = -errno; 2007 perror("failed to open file"); 2008 goto out_delete; 2009 } 2010 2011 err = fstat(input, &perf_stat); 2012 if (err < 0) { 2013 perror("failed to stat file"); 2014 goto out_delete; 2015 } 2016 2017 if (!perf_stat.st_size) { 2018 fprintf(stderr, "zero-sized file, nothing to do!\n"); 2019 goto out_delete; 2020 } 2021 2022 scripting_ops = script_spec__lookup(generate_script_lang); 2023 if (!scripting_ops) { 2024 fprintf(stderr, "invalid language specifier"); 2025 err = -ENOENT; 2026 goto out_delete; 2027 } 2028 2029 err = scripting_ops->generate_script(session->tevent.pevent, 2030 "perf-script"); 2031 goto out_delete; 2032 } 2033 2034 if (script_name) { 2035 err = scripting_ops->start_script(script_name, argc, argv); 2036 if (err) 2037 goto out_delete; 2038 pr_debug("perf script started with script %s\n\n", script_name); 2039 script_started = true; 2040 } 2041 2042 2043 err = perf_session__check_output_opt(session); 2044 if (err < 0) 2045 goto out_delete; 2046 2047 err = __cmd_script(&script); 2048 2049 flush_scripting(); 2050 2051 out_delete: 2052 perf_session__delete(session); 2053 2054 if (script_started) 2055 cleanup_scripting(); 2056 out: 2057 return err; 2058 } 2059