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