1 // SPDX-License-Identifier: GPL-2.0 2 #include "builtin.h" 3 4 #include "util/cache.h" 5 #include "util/counts.h" 6 #include "util/debug.h" 7 #include <subcmd/exec-cmd.h> 8 #include "util/header.h" 9 #include <subcmd/parse-options.h> 10 #include "util/perf_regs.h" 11 #include "util/session.h" 12 #include "util/tool.h" 13 #include "util/map.h" 14 #include "util/srcline.h" 15 #include "util/symbol.h" 16 #include "util/thread.h" 17 #include "util/trace-event.h" 18 #include "util/evlist.h" 19 #include "util/evsel.h" 20 #include "util/evswitch.h" 21 #include "util/sort.h" 22 #include "util/data.h" 23 #include "util/auxtrace.h" 24 #include "util/cpumap.h" 25 #include "util/thread_map.h" 26 #include "util/stat.h" 27 #include "util/color.h" 28 #include "util/string2.h" 29 #include "util/thread-stack.h" 30 #include "util/time-utils.h" 31 #include "util/path.h" 32 #include "print_binary.h" 33 #include "archinsn.h" 34 #include <linux/bitmap.h> 35 #include <linux/kernel.h> 36 #include <linux/stringify.h> 37 #include <linux/time64.h> 38 #include <linux/zalloc.h> 39 #include <sys/utsname.h> 40 #include "asm/bug.h" 41 #include "util/mem-events.h" 42 #include "util/dump-insn.h" 43 #include <dirent.h> 44 #include <errno.h> 45 #include <inttypes.h> 46 #include <signal.h> 47 #include <sys/param.h> 48 #include <sys/types.h> 49 #include <sys/stat.h> 50 #include <fcntl.h> 51 #include <unistd.h> 52 #include <subcmd/pager.h> 53 #include <perf/evlist.h> 54 #include "util/record.h" 55 #include "util/util.h" 56 #include "perf.h" 57 58 #include <linux/ctype.h> 59 60 static char const *script_name; 61 static char const *generate_script_lang; 62 static bool reltime; 63 static u64 initial_time; 64 static bool debug_mode; 65 static u64 last_timestamp; 66 static u64 nr_unordered; 67 static bool no_callchain; 68 static bool latency_format; 69 static bool system_wide; 70 static bool print_flags; 71 static const char *cpu_list; 72 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); 73 static struct perf_stat_config stat_config; 74 static int max_blocks; 75 static bool native_arch; 76 77 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH; 78 79 enum perf_output_field { 80 PERF_OUTPUT_COMM = 1U << 0, 81 PERF_OUTPUT_TID = 1U << 1, 82 PERF_OUTPUT_PID = 1U << 2, 83 PERF_OUTPUT_TIME = 1U << 3, 84 PERF_OUTPUT_CPU = 1U << 4, 85 PERF_OUTPUT_EVNAME = 1U << 5, 86 PERF_OUTPUT_TRACE = 1U << 6, 87 PERF_OUTPUT_IP = 1U << 7, 88 PERF_OUTPUT_SYM = 1U << 8, 89 PERF_OUTPUT_DSO = 1U << 9, 90 PERF_OUTPUT_ADDR = 1U << 10, 91 PERF_OUTPUT_SYMOFFSET = 1U << 11, 92 PERF_OUTPUT_SRCLINE = 1U << 12, 93 PERF_OUTPUT_PERIOD = 1U << 13, 94 PERF_OUTPUT_IREGS = 1U << 14, 95 PERF_OUTPUT_BRSTACK = 1U << 15, 96 PERF_OUTPUT_BRSTACKSYM = 1U << 16, 97 PERF_OUTPUT_DATA_SRC = 1U << 17, 98 PERF_OUTPUT_WEIGHT = 1U << 18, 99 PERF_OUTPUT_BPF_OUTPUT = 1U << 19, 100 PERF_OUTPUT_CALLINDENT = 1U << 20, 101 PERF_OUTPUT_INSN = 1U << 21, 102 PERF_OUTPUT_INSNLEN = 1U << 22, 103 PERF_OUTPUT_BRSTACKINSN = 1U << 23, 104 PERF_OUTPUT_BRSTACKOFF = 1U << 24, 105 PERF_OUTPUT_SYNTH = 1U << 25, 106 PERF_OUTPUT_PHYS_ADDR = 1U << 26, 107 PERF_OUTPUT_UREGS = 1U << 27, 108 PERF_OUTPUT_METRIC = 1U << 28, 109 PERF_OUTPUT_MISC = 1U << 29, 110 PERF_OUTPUT_SRCCODE = 1U << 30, 111 PERF_OUTPUT_IPC = 1U << 31, 112 }; 113 114 struct output_option { 115 const char *str; 116 enum perf_output_field field; 117 } all_output_options[] = { 118 {.str = "comm", .field = PERF_OUTPUT_COMM}, 119 {.str = "tid", .field = PERF_OUTPUT_TID}, 120 {.str = "pid", .field = PERF_OUTPUT_PID}, 121 {.str = "time", .field = PERF_OUTPUT_TIME}, 122 {.str = "cpu", .field = PERF_OUTPUT_CPU}, 123 {.str = "event", .field = PERF_OUTPUT_EVNAME}, 124 {.str = "trace", .field = PERF_OUTPUT_TRACE}, 125 {.str = "ip", .field = PERF_OUTPUT_IP}, 126 {.str = "sym", .field = PERF_OUTPUT_SYM}, 127 {.str = "dso", .field = PERF_OUTPUT_DSO}, 128 {.str = "addr", .field = PERF_OUTPUT_ADDR}, 129 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET}, 130 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE}, 131 {.str = "period", .field = PERF_OUTPUT_PERIOD}, 132 {.str = "iregs", .field = PERF_OUTPUT_IREGS}, 133 {.str = "uregs", .field = PERF_OUTPUT_UREGS}, 134 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK}, 135 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM}, 136 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC}, 137 {.str = "weight", .field = PERF_OUTPUT_WEIGHT}, 138 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT}, 139 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT}, 140 {.str = "insn", .field = PERF_OUTPUT_INSN}, 141 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN}, 142 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN}, 143 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF}, 144 {.str = "synth", .field = PERF_OUTPUT_SYNTH}, 145 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR}, 146 {.str = "metric", .field = PERF_OUTPUT_METRIC}, 147 {.str = "misc", .field = PERF_OUTPUT_MISC}, 148 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE}, 149 {.str = "ipc", .field = PERF_OUTPUT_IPC}, 150 }; 151 152 enum { 153 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX, 154 OUTPUT_TYPE_MAX 155 }; 156 157 /* default set to maintain compatibility with current format */ 158 static struct { 159 bool user_set; 160 bool wildcard_set; 161 unsigned int print_ip_opts; 162 u64 fields; 163 u64 invalid_fields; 164 u64 user_set_fields; 165 } output[OUTPUT_TYPE_MAX] = { 166 167 [PERF_TYPE_HARDWARE] = { 168 .user_set = false, 169 170 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | 171 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | 172 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | 173 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET | 174 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD, 175 176 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, 177 }, 178 179 [PERF_TYPE_SOFTWARE] = { 180 .user_set = false, 181 182 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | 183 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | 184 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | 185 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET | 186 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD | 187 PERF_OUTPUT_BPF_OUTPUT, 188 189 .invalid_fields = PERF_OUTPUT_TRACE, 190 }, 191 192 [PERF_TYPE_TRACEPOINT] = { 193 .user_set = false, 194 195 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | 196 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | 197 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE 198 }, 199 200 [PERF_TYPE_HW_CACHE] = { 201 .user_set = false, 202 203 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | 204 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | 205 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | 206 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET | 207 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD, 208 209 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, 210 }, 211 212 [PERF_TYPE_RAW] = { 213 .user_set = false, 214 215 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | 216 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | 217 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | 218 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET | 219 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD | 220 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC | 221 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR, 222 223 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, 224 }, 225 226 [PERF_TYPE_BREAKPOINT] = { 227 .user_set = false, 228 229 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | 230 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | 231 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | 232 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET | 233 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD, 234 235 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, 236 }, 237 238 [OUTPUT_TYPE_SYNTH] = { 239 .user_set = false, 240 241 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | 242 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | 243 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | 244 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET | 245 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH, 246 247 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, 248 }, 249 }; 250 251 struct evsel_script { 252 char *filename; 253 FILE *fp; 254 u64 samples; 255 /* For metric output */ 256 u64 val; 257 int gnum; 258 }; 259 260 static inline struct evsel_script *evsel_script(struct evsel *evsel) 261 { 262 return (struct evsel_script *)evsel->priv; 263 } 264 265 static struct evsel_script *perf_evsel_script__new(struct evsel *evsel, 266 struct perf_data *data) 267 { 268 struct evsel_script *es = zalloc(sizeof(*es)); 269 270 if (es != NULL) { 271 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0) 272 goto out_free; 273 es->fp = fopen(es->filename, "w"); 274 if (es->fp == NULL) 275 goto out_free_filename; 276 } 277 278 return es; 279 out_free_filename: 280 zfree(&es->filename); 281 out_free: 282 free(es); 283 return NULL; 284 } 285 286 static void perf_evsel_script__delete(struct evsel_script *es) 287 { 288 zfree(&es->filename); 289 fclose(es->fp); 290 es->fp = NULL; 291 free(es); 292 } 293 294 static int perf_evsel_script__fprintf(struct evsel_script *es, FILE *fp) 295 { 296 struct stat st; 297 298 fstat(fileno(es->fp), &st); 299 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n", 300 st.st_size / 1024.0 / 1024.0, es->filename, es->samples); 301 } 302 303 static inline int output_type(unsigned int type) 304 { 305 switch (type) { 306 case PERF_TYPE_SYNTH: 307 return OUTPUT_TYPE_SYNTH; 308 default: 309 return type; 310 } 311 } 312 313 static inline unsigned int attr_type(unsigned int type) 314 { 315 switch (type) { 316 case OUTPUT_TYPE_SYNTH: 317 return PERF_TYPE_SYNTH; 318 default: 319 return type; 320 } 321 } 322 323 static bool output_set_by_user(void) 324 { 325 int j; 326 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) { 327 if (output[j].user_set) 328 return true; 329 } 330 return false; 331 } 332 333 static const char *output_field2str(enum perf_output_field field) 334 { 335 int i, imax = ARRAY_SIZE(all_output_options); 336 const char *str = ""; 337 338 for (i = 0; i < imax; ++i) { 339 if (all_output_options[i].field == field) { 340 str = all_output_options[i].str; 341 break; 342 } 343 } 344 return str; 345 } 346 347 #define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x) 348 349 static int perf_evsel__do_check_stype(struct evsel *evsel, 350 u64 sample_type, const char *sample_msg, 351 enum perf_output_field field, 352 bool allow_user_set) 353 { 354 struct perf_event_attr *attr = &evsel->core.attr; 355 int type = output_type(attr->type); 356 const char *evname; 357 358 if (attr->sample_type & sample_type) 359 return 0; 360 361 if (output[type].user_set_fields & field) { 362 if (allow_user_set) 363 return 0; 364 evname = perf_evsel__name(evsel); 365 pr_err("Samples for '%s' event do not have %s attribute set. " 366 "Cannot print '%s' field.\n", 367 evname, sample_msg, output_field2str(field)); 368 return -1; 369 } 370 371 /* user did not ask for it explicitly so remove from the default list */ 372 output[type].fields &= ~field; 373 evname = perf_evsel__name(evsel); 374 pr_debug("Samples for '%s' event do not have %s attribute set. " 375 "Skipping '%s' field.\n", 376 evname, sample_msg, output_field2str(field)); 377 378 return 0; 379 } 380 381 static int perf_evsel__check_stype(struct evsel *evsel, 382 u64 sample_type, const char *sample_msg, 383 enum perf_output_field field) 384 { 385 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field, 386 false); 387 } 388 389 static int perf_evsel__check_attr(struct evsel *evsel, 390 struct perf_session *session) 391 { 392 struct perf_event_attr *attr = &evsel->core.attr; 393 bool allow_user_set; 394 395 if (perf_header__has_feat(&session->header, HEADER_STAT)) 396 return 0; 397 398 allow_user_set = perf_header__has_feat(&session->header, 399 HEADER_AUXTRACE); 400 401 if (PRINT_FIELD(TRACE) && 402 !perf_session__has_traces(session, "record -R")) 403 return -EINVAL; 404 405 if (PRINT_FIELD(IP)) { 406 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", 407 PERF_OUTPUT_IP)) 408 return -EINVAL; 409 } 410 411 if (PRINT_FIELD(ADDR) && 412 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", 413 PERF_OUTPUT_ADDR, allow_user_set)) 414 return -EINVAL; 415 416 if (PRINT_FIELD(DATA_SRC) && 417 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC", 418 PERF_OUTPUT_DATA_SRC)) 419 return -EINVAL; 420 421 if (PRINT_FIELD(WEIGHT) && 422 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT", 423 PERF_OUTPUT_WEIGHT)) 424 return -EINVAL; 425 426 if (PRINT_FIELD(SYM) && 427 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) { 428 pr_err("Display of symbols requested but neither sample IP nor " 429 "sample address\navailable. Hence, no addresses to convert " 430 "to symbols.\n"); 431 return -EINVAL; 432 } 433 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) { 434 pr_err("Display of offsets requested but symbol is not" 435 "selected.\n"); 436 return -EINVAL; 437 } 438 if (PRINT_FIELD(DSO) && 439 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) { 440 pr_err("Display of DSO requested but no address to convert.\n"); 441 return -EINVAL; 442 } 443 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) { 444 pr_err("Display of source line number requested but sample IP is not\n" 445 "selected. Hence, no address to lookup the source line number.\n"); 446 return -EINVAL; 447 } 448 if (PRINT_FIELD(BRSTACKINSN) && 449 !(perf_evlist__combined_branch_type(session->evlist) & 450 PERF_SAMPLE_BRANCH_ANY)) { 451 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n" 452 "Hint: run 'perf record -b ...'\n"); 453 return -EINVAL; 454 } 455 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) && 456 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", 457 PERF_OUTPUT_TID|PERF_OUTPUT_PID)) 458 return -EINVAL; 459 460 if (PRINT_FIELD(TIME) && 461 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", 462 PERF_OUTPUT_TIME)) 463 return -EINVAL; 464 465 if (PRINT_FIELD(CPU) && 466 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", 467 PERF_OUTPUT_CPU, allow_user_set)) 468 return -EINVAL; 469 470 if (PRINT_FIELD(IREGS) && 471 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", 472 PERF_OUTPUT_IREGS)) 473 return -EINVAL; 474 475 if (PRINT_FIELD(UREGS) && 476 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS", 477 PERF_OUTPUT_UREGS)) 478 return -EINVAL; 479 480 if (PRINT_FIELD(PHYS_ADDR) && 481 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR", 482 PERF_OUTPUT_PHYS_ADDR)) 483 return -EINVAL; 484 485 return 0; 486 } 487 488 static void set_print_ip_opts(struct perf_event_attr *attr) 489 { 490 unsigned int type = output_type(attr->type); 491 492 output[type].print_ip_opts = 0; 493 if (PRINT_FIELD(IP)) 494 output[type].print_ip_opts |= EVSEL__PRINT_IP; 495 496 if (PRINT_FIELD(SYM)) 497 output[type].print_ip_opts |= EVSEL__PRINT_SYM; 498 499 if (PRINT_FIELD(DSO)) 500 output[type].print_ip_opts |= EVSEL__PRINT_DSO; 501 502 if (PRINT_FIELD(SYMOFFSET)) 503 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET; 504 505 if (PRINT_FIELD(SRCLINE)) 506 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE; 507 } 508 509 /* 510 * verify all user requested events exist and the samples 511 * have the expected data 512 */ 513 static int perf_session__check_output_opt(struct perf_session *session) 514 { 515 unsigned int j; 516 struct evsel *evsel; 517 518 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) { 519 evsel = perf_session__find_first_evtype(session, attr_type(j)); 520 521 /* 522 * even if fields is set to 0 (ie., show nothing) event must 523 * exist if user explicitly includes it on the command line 524 */ 525 if (!evsel && output[j].user_set && !output[j].wildcard_set && 526 j != OUTPUT_TYPE_SYNTH) { 527 pr_err("%s events do not exist. " 528 "Remove corresponding -F option to proceed.\n", 529 event_type(j)); 530 return -1; 531 } 532 533 if (evsel && output[j].fields && 534 perf_evsel__check_attr(evsel, session)) 535 return -1; 536 537 if (evsel == NULL) 538 continue; 539 540 set_print_ip_opts(&evsel->core.attr); 541 } 542 543 if (!no_callchain) { 544 bool use_callchain = false; 545 bool not_pipe = false; 546 547 evlist__for_each_entry(session->evlist, evsel) { 548 not_pipe = true; 549 if (evsel__has_callchain(evsel)) { 550 use_callchain = true; 551 break; 552 } 553 } 554 if (not_pipe && !use_callchain) 555 symbol_conf.use_callchain = false; 556 } 557 558 /* 559 * set default for tracepoints to print symbols only 560 * if callchains are present 561 */ 562 if (symbol_conf.use_callchain && 563 !output[PERF_TYPE_TRACEPOINT].user_set) { 564 j = PERF_TYPE_TRACEPOINT; 565 566 evlist__for_each_entry(session->evlist, evsel) { 567 if (evsel->core.attr.type != j) 568 continue; 569 570 if (evsel__has_callchain(evsel)) { 571 output[j].fields |= PERF_OUTPUT_IP; 572 output[j].fields |= PERF_OUTPUT_SYM; 573 output[j].fields |= PERF_OUTPUT_SYMOFFSET; 574 output[j].fields |= PERF_OUTPUT_DSO; 575 set_print_ip_opts(&evsel->core.attr); 576 goto out; 577 } 578 } 579 } 580 581 out: 582 return 0; 583 } 584 585 static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask, 586 FILE *fp 587 ) 588 { 589 unsigned i = 0, r; 590 int printed = 0; 591 592 if (!regs || !regs->regs) 593 return 0; 594 595 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi); 596 597 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) { 598 u64 val = regs->regs[i++]; 599 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val); 600 } 601 602 fprintf(fp, "\n"); 603 604 return printed; 605 } 606 607 static int perf_sample__fprintf_iregs(struct perf_sample *sample, 608 struct perf_event_attr *attr, FILE *fp) 609 { 610 return perf_sample__fprintf_regs(&sample->intr_regs, 611 attr->sample_regs_intr, fp); 612 } 613 614 static int perf_sample__fprintf_uregs(struct perf_sample *sample, 615 struct perf_event_attr *attr, FILE *fp) 616 { 617 return perf_sample__fprintf_regs(&sample->user_regs, 618 attr->sample_regs_user, fp); 619 } 620 621 static int perf_sample__fprintf_start(struct perf_sample *sample, 622 struct thread *thread, 623 struct evsel *evsel, 624 u32 type, FILE *fp) 625 { 626 struct perf_event_attr *attr = &evsel->core.attr; 627 unsigned long secs; 628 unsigned long long nsecs; 629 int printed = 0; 630 631 if (PRINT_FIELD(COMM)) { 632 if (latency_format) 633 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread)); 634 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain) 635 printed += fprintf(fp, "%s ", thread__comm_str(thread)); 636 else 637 printed += fprintf(fp, "%16s ", thread__comm_str(thread)); 638 } 639 640 if (PRINT_FIELD(PID) && PRINT_FIELD(TID)) 641 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid); 642 else if (PRINT_FIELD(PID)) 643 printed += fprintf(fp, "%5d ", sample->pid); 644 else if (PRINT_FIELD(TID)) 645 printed += fprintf(fp, "%5d ", sample->tid); 646 647 if (PRINT_FIELD(CPU)) { 648 if (latency_format) 649 printed += fprintf(fp, "%3d ", sample->cpu); 650 else 651 printed += fprintf(fp, "[%03d] ", sample->cpu); 652 } 653 654 if (PRINT_FIELD(MISC)) { 655 int ret = 0; 656 657 #define has(m) \ 658 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m 659 660 if (has(KERNEL)) 661 ret += fprintf(fp, "K"); 662 if (has(USER)) 663 ret += fprintf(fp, "U"); 664 if (has(HYPERVISOR)) 665 ret += fprintf(fp, "H"); 666 if (has(GUEST_KERNEL)) 667 ret += fprintf(fp, "G"); 668 if (has(GUEST_USER)) 669 ret += fprintf(fp, "g"); 670 671 switch (type) { 672 case PERF_RECORD_MMAP: 673 case PERF_RECORD_MMAP2: 674 if (has(MMAP_DATA)) 675 ret += fprintf(fp, "M"); 676 break; 677 case PERF_RECORD_COMM: 678 if (has(COMM_EXEC)) 679 ret += fprintf(fp, "E"); 680 break; 681 case PERF_RECORD_SWITCH: 682 case PERF_RECORD_SWITCH_CPU_WIDE: 683 if (has(SWITCH_OUT)) { 684 ret += fprintf(fp, "S"); 685 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT) 686 ret += fprintf(fp, "p"); 687 } 688 default: 689 break; 690 } 691 692 #undef has 693 694 ret += fprintf(fp, "%*s", 6 - ret, " "); 695 printed += ret; 696 } 697 698 if (PRINT_FIELD(TIME)) { 699 u64 t = sample->time; 700 if (reltime) { 701 if (!initial_time) 702 initial_time = sample->time; 703 t = sample->time - initial_time; 704 } 705 nsecs = t; 706 secs = nsecs / NSEC_PER_SEC; 707 nsecs -= secs * NSEC_PER_SEC; 708 709 if (symbol_conf.nanosecs) 710 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs); 711 else { 712 char sample_time[32]; 713 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time)); 714 printed += fprintf(fp, "%12s: ", sample_time); 715 } 716 } 717 718 return printed; 719 } 720 721 static inline char 722 mispred_str(struct branch_entry *br) 723 { 724 if (!(br->flags.mispred || br->flags.predicted)) 725 return '-'; 726 727 return br->flags.predicted ? 'P' : 'M'; 728 } 729 730 static int perf_sample__fprintf_brstack(struct perf_sample *sample, 731 struct thread *thread, 732 struct perf_event_attr *attr, FILE *fp) 733 { 734 struct branch_stack *br = sample->branch_stack; 735 struct addr_location alf, alt; 736 u64 i, from, to; 737 int printed = 0; 738 739 if (!(br && br->nr)) 740 return 0; 741 742 for (i = 0; i < br->nr; i++) { 743 from = br->entries[i].from; 744 to = br->entries[i].to; 745 746 if (PRINT_FIELD(DSO)) { 747 memset(&alf, 0, sizeof(alf)); 748 memset(&alt, 0, sizeof(alt)); 749 thread__find_map_fb(thread, sample->cpumode, from, &alf); 750 thread__find_map_fb(thread, sample->cpumode, to, &alt); 751 } 752 753 printed += fprintf(fp, " 0x%"PRIx64, from); 754 if (PRINT_FIELD(DSO)) { 755 printed += fprintf(fp, "("); 756 printed += map__fprintf_dsoname(alf.map, fp); 757 printed += fprintf(fp, ")"); 758 } 759 760 printed += fprintf(fp, "/0x%"PRIx64, to); 761 if (PRINT_FIELD(DSO)) { 762 printed += fprintf(fp, "("); 763 printed += map__fprintf_dsoname(alt.map, fp); 764 printed += fprintf(fp, ")"); 765 } 766 767 printed += fprintf(fp, "/%c/%c/%c/%d ", 768 mispred_str( br->entries + i), 769 br->entries[i].flags.in_tx? 'X' : '-', 770 br->entries[i].flags.abort? 'A' : '-', 771 br->entries[i].flags.cycles); 772 } 773 774 return printed; 775 } 776 777 static int perf_sample__fprintf_brstacksym(struct perf_sample *sample, 778 struct thread *thread, 779 struct perf_event_attr *attr, FILE *fp) 780 { 781 struct branch_stack *br = sample->branch_stack; 782 struct addr_location alf, alt; 783 u64 i, from, to; 784 int printed = 0; 785 786 if (!(br && br->nr)) 787 return 0; 788 789 for (i = 0; i < br->nr; i++) { 790 791 memset(&alf, 0, sizeof(alf)); 792 memset(&alt, 0, sizeof(alt)); 793 from = br->entries[i].from; 794 to = br->entries[i].to; 795 796 thread__find_symbol_fb(thread, sample->cpumode, from, &alf); 797 thread__find_symbol_fb(thread, sample->cpumode, to, &alt); 798 799 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp); 800 if (PRINT_FIELD(DSO)) { 801 printed += fprintf(fp, "("); 802 printed += map__fprintf_dsoname(alf.map, fp); 803 printed += fprintf(fp, ")"); 804 } 805 printed += fprintf(fp, "%c", '/'); 806 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp); 807 if (PRINT_FIELD(DSO)) { 808 printed += fprintf(fp, "("); 809 printed += map__fprintf_dsoname(alt.map, fp); 810 printed += fprintf(fp, ")"); 811 } 812 printed += fprintf(fp, "/%c/%c/%c/%d ", 813 mispred_str( br->entries + i), 814 br->entries[i].flags.in_tx? 'X' : '-', 815 br->entries[i].flags.abort? 'A' : '-', 816 br->entries[i].flags.cycles); 817 } 818 819 return printed; 820 } 821 822 static int perf_sample__fprintf_brstackoff(struct perf_sample *sample, 823 struct thread *thread, 824 struct perf_event_attr *attr, FILE *fp) 825 { 826 struct branch_stack *br = sample->branch_stack; 827 struct addr_location alf, alt; 828 u64 i, from, to; 829 int printed = 0; 830 831 if (!(br && br->nr)) 832 return 0; 833 834 for (i = 0; i < br->nr; i++) { 835 836 memset(&alf, 0, sizeof(alf)); 837 memset(&alt, 0, sizeof(alt)); 838 from = br->entries[i].from; 839 to = br->entries[i].to; 840 841 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) && 842 !alf.map->dso->adjust_symbols) 843 from = map__map_ip(alf.map, from); 844 845 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) && 846 !alt.map->dso->adjust_symbols) 847 to = map__map_ip(alt.map, to); 848 849 printed += fprintf(fp, " 0x%"PRIx64, from); 850 if (PRINT_FIELD(DSO)) { 851 printed += fprintf(fp, "("); 852 printed += map__fprintf_dsoname(alf.map, fp); 853 printed += fprintf(fp, ")"); 854 } 855 printed += fprintf(fp, "/0x%"PRIx64, to); 856 if (PRINT_FIELD(DSO)) { 857 printed += fprintf(fp, "("); 858 printed += map__fprintf_dsoname(alt.map, fp); 859 printed += fprintf(fp, ")"); 860 } 861 printed += fprintf(fp, "/%c/%c/%c/%d ", 862 mispred_str(br->entries + i), 863 br->entries[i].flags.in_tx ? 'X' : '-', 864 br->entries[i].flags.abort ? 'A' : '-', 865 br->entries[i].flags.cycles); 866 } 867 868 return printed; 869 } 870 #define MAXBB 16384UL 871 872 static int grab_bb(u8 *buffer, u64 start, u64 end, 873 struct machine *machine, struct thread *thread, 874 bool *is64bit, u8 *cpumode, bool last) 875 { 876 long offset, len; 877 struct addr_location al; 878 bool kernel; 879 880 if (!start || !end) 881 return 0; 882 883 kernel = machine__kernel_ip(machine, start); 884 if (kernel) 885 *cpumode = PERF_RECORD_MISC_KERNEL; 886 else 887 *cpumode = PERF_RECORD_MISC_USER; 888 889 /* 890 * Block overlaps between kernel and user. 891 * This can happen due to ring filtering 892 * On Intel CPUs the entry into the kernel is filtered, 893 * but the exit is not. Let the caller patch it up. 894 */ 895 if (kernel != machine__kernel_ip(machine, end)) { 896 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end); 897 return -ENXIO; 898 } 899 900 memset(&al, 0, sizeof(al)); 901 if (end - start > MAXBB - MAXINSN) { 902 if (last) 903 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end); 904 else 905 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start); 906 return 0; 907 } 908 909 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) { 910 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end); 911 return 0; 912 } 913 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) { 914 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end); 915 return 0; 916 } 917 918 /* Load maps to ensure dso->is_64_bit has been updated */ 919 map__load(al.map); 920 921 offset = al.map->map_ip(al.map, start); 922 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer, 923 end - start + MAXINSN); 924 925 *is64bit = al.map->dso->is_64_bit; 926 if (len <= 0) 927 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n", 928 start, end); 929 return len; 930 } 931 932 static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr) 933 { 934 struct addr_location al; 935 int ret = 0; 936 937 memset(&al, 0, sizeof(al)); 938 thread__find_map(thread, cpumode, addr, &al); 939 if (!al.map) 940 return 0; 941 ret = map__fprintf_srccode(al.map, al.addr, stdout, 942 &thread->srccode_state); 943 if (ret) 944 ret += printf("\n"); 945 return ret; 946 } 947 948 static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en, 949 struct perf_insn *x, u8 *inbuf, int len, 950 int insn, FILE *fp, int *total_cycles) 951 { 952 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip, 953 dump_insn(x, ip, inbuf, len, NULL), 954 en->flags.predicted ? " PRED" : "", 955 en->flags.mispred ? " MISPRED" : "", 956 en->flags.in_tx ? " INTX" : "", 957 en->flags.abort ? " ABORT" : ""); 958 if (en->flags.cycles) { 959 *total_cycles += en->flags.cycles; 960 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles); 961 if (insn) 962 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles); 963 } 964 return printed + fprintf(fp, "\n"); 965 } 966 967 static int ip__fprintf_sym(uint64_t addr, struct thread *thread, 968 u8 cpumode, int cpu, struct symbol **lastsym, 969 struct perf_event_attr *attr, FILE *fp) 970 { 971 struct addr_location al; 972 int off, printed = 0; 973 974 memset(&al, 0, sizeof(al)); 975 976 thread__find_map(thread, cpumode, addr, &al); 977 978 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end) 979 return 0; 980 981 al.cpu = cpu; 982 al.sym = NULL; 983 if (al.map) 984 al.sym = map__find_symbol(al.map, al.addr); 985 986 if (!al.sym) 987 return 0; 988 989 if (al.addr < al.sym->end) 990 off = al.addr - al.sym->start; 991 else 992 off = al.addr - al.map->start - al.sym->start; 993 printed += fprintf(fp, "\t%s", al.sym->name); 994 if (off) 995 printed += fprintf(fp, "%+d", off); 996 printed += fprintf(fp, ":"); 997 if (PRINT_FIELD(SRCLINE)) 998 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp); 999 printed += fprintf(fp, "\n"); 1000 *lastsym = al.sym; 1001 1002 return printed; 1003 } 1004 1005 static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample, 1006 struct thread *thread, 1007 struct perf_event_attr *attr, 1008 struct machine *machine, FILE *fp) 1009 { 1010 struct branch_stack *br = sample->branch_stack; 1011 u64 start, end; 1012 int i, insn, len, nr, ilen, printed = 0; 1013 struct perf_insn x; 1014 u8 buffer[MAXBB]; 1015 unsigned off; 1016 struct symbol *lastsym = NULL; 1017 int total_cycles = 0; 1018 1019 if (!(br && br->nr)) 1020 return 0; 1021 nr = br->nr; 1022 if (max_blocks && nr > max_blocks + 1) 1023 nr = max_blocks + 1; 1024 1025 x.thread = thread; 1026 x.cpu = sample->cpu; 1027 1028 printed += fprintf(fp, "%c", '\n'); 1029 1030 /* Handle first from jump, of which we don't know the entry. */ 1031 len = grab_bb(buffer, br->entries[nr-1].from, 1032 br->entries[nr-1].from, 1033 machine, thread, &x.is64bit, &x.cpumode, false); 1034 if (len > 0) { 1035 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread, 1036 x.cpumode, x.cpu, &lastsym, attr, fp); 1037 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1], 1038 &x, buffer, len, 0, fp, &total_cycles); 1039 if (PRINT_FIELD(SRCCODE)) 1040 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from); 1041 } 1042 1043 /* Print all blocks */ 1044 for (i = nr - 2; i >= 0; i--) { 1045 if (br->entries[i].from || br->entries[i].to) 1046 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i, 1047 br->entries[i].from, 1048 br->entries[i].to); 1049 start = br->entries[i + 1].to; 1050 end = br->entries[i].from; 1051 1052 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false); 1053 /* Patch up missing kernel transfers due to ring filters */ 1054 if (len == -ENXIO && i > 0) { 1055 end = br->entries[--i].from; 1056 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end); 1057 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false); 1058 } 1059 if (len <= 0) 1060 continue; 1061 1062 insn = 0; 1063 for (off = 0;; off += ilen) { 1064 uint64_t ip = start + off; 1065 1066 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp); 1067 if (ip == end) { 1068 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, ++insn, fp, 1069 &total_cycles); 1070 if (PRINT_FIELD(SRCCODE)) 1071 printed += print_srccode(thread, x.cpumode, ip); 1072 break; 1073 } else { 1074 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip, 1075 dump_insn(&x, ip, buffer + off, len - off, &ilen)); 1076 if (ilen == 0) 1077 break; 1078 if (PRINT_FIELD(SRCCODE)) 1079 print_srccode(thread, x.cpumode, ip); 1080 insn++; 1081 } 1082 } 1083 } 1084 1085 /* 1086 * Hit the branch? In this case we are already done, and the target 1087 * has not been executed yet. 1088 */ 1089 if (br->entries[0].from == sample->ip) 1090 goto out; 1091 if (br->entries[0].flags.abort) 1092 goto out; 1093 1094 /* 1095 * Print final block upto sample 1096 * 1097 * Due to pipeline delays the LBRs might be missing a branch 1098 * or two, which can result in very large or negative blocks 1099 * between final branch and sample. When this happens just 1100 * continue walking after the last TO until we hit a branch. 1101 */ 1102 start = br->entries[0].to; 1103 end = sample->ip; 1104 if (end < start) { 1105 /* Missing jump. Scan 128 bytes for the next branch */ 1106 end = start + 128; 1107 } 1108 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true); 1109 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp); 1110 if (len <= 0) { 1111 /* Print at least last IP if basic block did not work */ 1112 len = grab_bb(buffer, sample->ip, sample->ip, 1113 machine, thread, &x.is64bit, &x.cpumode, false); 1114 if (len <= 0) 1115 goto out; 1116 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip, 1117 dump_insn(&x, sample->ip, buffer, len, NULL)); 1118 if (PRINT_FIELD(SRCCODE)) 1119 print_srccode(thread, x.cpumode, sample->ip); 1120 goto out; 1121 } 1122 for (off = 0; off <= end - start; off += ilen) { 1123 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off, 1124 dump_insn(&x, start + off, buffer + off, len - off, &ilen)); 1125 if (ilen == 0) 1126 break; 1127 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) { 1128 /* 1129 * Hit a missing branch. Just stop. 1130 */ 1131 printed += fprintf(fp, "\t... not reaching sample ...\n"); 1132 break; 1133 } 1134 if (PRINT_FIELD(SRCCODE)) 1135 print_srccode(thread, x.cpumode, start + off); 1136 } 1137 out: 1138 return printed; 1139 } 1140 1141 static int perf_sample__fprintf_addr(struct perf_sample *sample, 1142 struct thread *thread, 1143 struct perf_event_attr *attr, FILE *fp) 1144 { 1145 struct addr_location al; 1146 int printed = fprintf(fp, "%16" PRIx64, sample->addr); 1147 1148 if (!sample_addr_correlates_sym(attr)) 1149 goto out; 1150 1151 thread__resolve(thread, &al, sample); 1152 1153 if (PRINT_FIELD(SYM)) { 1154 printed += fprintf(fp, " "); 1155 if (PRINT_FIELD(SYMOFFSET)) 1156 printed += symbol__fprintf_symname_offs(al.sym, &al, fp); 1157 else 1158 printed += symbol__fprintf_symname(al.sym, fp); 1159 } 1160 1161 if (PRINT_FIELD(DSO)) { 1162 printed += fprintf(fp, " ("); 1163 printed += map__fprintf_dsoname(al.map, fp); 1164 printed += fprintf(fp, ")"); 1165 } 1166 out: 1167 return printed; 1168 } 1169 1170 static const char *resolve_branch_sym(struct perf_sample *sample, 1171 struct evsel *evsel, 1172 struct thread *thread, 1173 struct addr_location *al, 1174 u64 *ip) 1175 { 1176 struct addr_location addr_al; 1177 struct perf_event_attr *attr = &evsel->core.attr; 1178 const char *name = NULL; 1179 1180 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) { 1181 if (sample_addr_correlates_sym(attr)) { 1182 thread__resolve(thread, &addr_al, sample); 1183 if (addr_al.sym) 1184 name = addr_al.sym->name; 1185 else 1186 *ip = sample->addr; 1187 } else { 1188 *ip = sample->addr; 1189 } 1190 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) { 1191 if (al->sym) 1192 name = al->sym->name; 1193 else 1194 *ip = sample->ip; 1195 } 1196 return name; 1197 } 1198 1199 static int perf_sample__fprintf_callindent(struct perf_sample *sample, 1200 struct evsel *evsel, 1201 struct thread *thread, 1202 struct addr_location *al, FILE *fp) 1203 { 1204 struct perf_event_attr *attr = &evsel->core.attr; 1205 size_t depth = thread_stack__depth(thread, sample->cpu); 1206 const char *name = NULL; 1207 static int spacing; 1208 int len = 0; 1209 int dlen = 0; 1210 u64 ip = 0; 1211 1212 /* 1213 * The 'return' has already been popped off the stack so the depth has 1214 * to be adjusted to match the 'call'. 1215 */ 1216 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN) 1217 depth += 1; 1218 1219 name = resolve_branch_sym(sample, evsel, thread, al, &ip); 1220 1221 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) { 1222 dlen += fprintf(fp, "("); 1223 dlen += map__fprintf_dsoname(al->map, fp); 1224 dlen += fprintf(fp, ")\t"); 1225 } 1226 1227 if (name) 1228 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name); 1229 else if (ip) 1230 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip); 1231 1232 if (len < 0) 1233 return len; 1234 1235 /* 1236 * Try to keep the output length from changing frequently so that the 1237 * output lines up more nicely. 1238 */ 1239 if (len > spacing || (len && len < spacing - 52)) 1240 spacing = round_up(len + 4, 32); 1241 1242 if (len < spacing) 1243 len += fprintf(fp, "%*s", spacing - len, ""); 1244 1245 return len + dlen; 1246 } 1247 1248 __weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused, 1249 struct thread *thread __maybe_unused, 1250 struct machine *machine __maybe_unused) 1251 { 1252 } 1253 1254 static int perf_sample__fprintf_insn(struct perf_sample *sample, 1255 struct perf_event_attr *attr, 1256 struct thread *thread, 1257 struct machine *machine, FILE *fp) 1258 { 1259 int printed = 0; 1260 1261 if (sample->insn_len == 0 && native_arch) 1262 arch_fetch_insn(sample, thread, machine); 1263 1264 if (PRINT_FIELD(INSNLEN)) 1265 printed += fprintf(fp, " ilen: %d", sample->insn_len); 1266 if (PRINT_FIELD(INSN) && sample->insn_len) { 1267 int i; 1268 1269 printed += fprintf(fp, " insn:"); 1270 for (i = 0; i < sample->insn_len; i++) 1271 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]); 1272 } 1273 if (PRINT_FIELD(BRSTACKINSN)) 1274 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp); 1275 1276 return printed; 1277 } 1278 1279 static int perf_sample__fprintf_ipc(struct perf_sample *sample, 1280 struct perf_event_attr *attr, FILE *fp) 1281 { 1282 unsigned int ipc; 1283 1284 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt) 1285 return 0; 1286 1287 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt; 1288 1289 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ", 1290 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt); 1291 } 1292 1293 static int perf_sample__fprintf_bts(struct perf_sample *sample, 1294 struct evsel *evsel, 1295 struct thread *thread, 1296 struct addr_location *al, 1297 struct machine *machine, FILE *fp) 1298 { 1299 struct perf_event_attr *attr = &evsel->core.attr; 1300 unsigned int type = output_type(attr->type); 1301 bool print_srcline_last = false; 1302 int printed = 0; 1303 1304 if (PRINT_FIELD(CALLINDENT)) 1305 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp); 1306 1307 /* print branch_from information */ 1308 if (PRINT_FIELD(IP)) { 1309 unsigned int print_opts = output[type].print_ip_opts; 1310 struct callchain_cursor *cursor = NULL; 1311 1312 if (symbol_conf.use_callchain && sample->callchain && 1313 thread__resolve_callchain(al->thread, &callchain_cursor, evsel, 1314 sample, NULL, NULL, scripting_max_stack) == 0) 1315 cursor = &callchain_cursor; 1316 1317 if (cursor == NULL) { 1318 printed += fprintf(fp, " "); 1319 if (print_opts & EVSEL__PRINT_SRCLINE) { 1320 print_srcline_last = true; 1321 print_opts &= ~EVSEL__PRINT_SRCLINE; 1322 } 1323 } else 1324 printed += fprintf(fp, "\n"); 1325 1326 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp); 1327 } 1328 1329 /* print branch_to information */ 1330 if (PRINT_FIELD(ADDR) || 1331 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) && 1332 !output[type].user_set)) { 1333 printed += fprintf(fp, " => "); 1334 printed += perf_sample__fprintf_addr(sample, thread, attr, fp); 1335 } 1336 1337 printed += perf_sample__fprintf_ipc(sample, attr, fp); 1338 1339 if (print_srcline_last) 1340 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp); 1341 1342 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp); 1343 printed += fprintf(fp, "\n"); 1344 if (PRINT_FIELD(SRCCODE)) { 1345 int ret = map__fprintf_srccode(al->map, al->addr, stdout, 1346 &thread->srccode_state); 1347 if (ret) { 1348 printed += ret; 1349 printed += printf("\n"); 1350 } 1351 } 1352 return printed; 1353 } 1354 1355 static struct { 1356 u32 flags; 1357 const char *name; 1358 } sample_flags[] = { 1359 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"}, 1360 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"}, 1361 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"}, 1362 {PERF_IP_FLAG_BRANCH, "jmp"}, 1363 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"}, 1364 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"}, 1365 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"}, 1366 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"}, 1367 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"}, 1368 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"}, 1369 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"}, 1370 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"}, 1371 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"}, 1372 {0, NULL} 1373 }; 1374 1375 static const char *sample_flags_to_name(u32 flags) 1376 { 1377 int i; 1378 1379 for (i = 0; sample_flags[i].name ; i++) { 1380 if (sample_flags[i].flags == flags) 1381 return sample_flags[i].name; 1382 } 1383 1384 return NULL; 1385 } 1386 1387 static int perf_sample__fprintf_flags(u32 flags, FILE *fp) 1388 { 1389 const char *chars = PERF_IP_FLAG_CHARS; 1390 const int n = strlen(PERF_IP_FLAG_CHARS); 1391 bool in_tx = flags & PERF_IP_FLAG_IN_TX; 1392 const char *name = NULL; 1393 char str[33]; 1394 int i, pos = 0; 1395 1396 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX); 1397 if (name) 1398 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : ""); 1399 1400 if (flags & PERF_IP_FLAG_TRACE_BEGIN) { 1401 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN)); 1402 if (name) 1403 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : ""); 1404 } 1405 1406 if (flags & PERF_IP_FLAG_TRACE_END) { 1407 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END)); 1408 if (name) 1409 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : ""); 1410 } 1411 1412 for (i = 0; i < n; i++, flags >>= 1) { 1413 if (flags & 1) 1414 str[pos++] = chars[i]; 1415 } 1416 for (; i < 32; i++, flags >>= 1) { 1417 if (flags & 1) 1418 str[pos++] = '?'; 1419 } 1420 str[pos] = 0; 1421 1422 return fprintf(fp, " %-19s ", str); 1423 } 1424 1425 struct printer_data { 1426 int line_no; 1427 bool hit_nul; 1428 bool is_printable; 1429 }; 1430 1431 static int sample__fprintf_bpf_output(enum binary_printer_ops op, 1432 unsigned int val, 1433 void *extra, FILE *fp) 1434 { 1435 unsigned char ch = (unsigned char)val; 1436 struct printer_data *printer_data = extra; 1437 int printed = 0; 1438 1439 switch (op) { 1440 case BINARY_PRINT_DATA_BEGIN: 1441 printed += fprintf(fp, "\n"); 1442 break; 1443 case BINARY_PRINT_LINE_BEGIN: 1444 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" : 1445 " "); 1446 break; 1447 case BINARY_PRINT_ADDR: 1448 printed += fprintf(fp, " %04x:", val); 1449 break; 1450 case BINARY_PRINT_NUM_DATA: 1451 printed += fprintf(fp, " %02x", val); 1452 break; 1453 case BINARY_PRINT_NUM_PAD: 1454 printed += fprintf(fp, " "); 1455 break; 1456 case BINARY_PRINT_SEP: 1457 printed += fprintf(fp, " "); 1458 break; 1459 case BINARY_PRINT_CHAR_DATA: 1460 if (printer_data->hit_nul && ch) 1461 printer_data->is_printable = false; 1462 1463 if (!isprint(ch)) { 1464 printed += fprintf(fp, "%c", '.'); 1465 1466 if (!printer_data->is_printable) 1467 break; 1468 1469 if (ch == '\0') 1470 printer_data->hit_nul = true; 1471 else 1472 printer_data->is_printable = false; 1473 } else { 1474 printed += fprintf(fp, "%c", ch); 1475 } 1476 break; 1477 case BINARY_PRINT_CHAR_PAD: 1478 printed += fprintf(fp, " "); 1479 break; 1480 case BINARY_PRINT_LINE_END: 1481 printed += fprintf(fp, "\n"); 1482 printer_data->line_no++; 1483 break; 1484 case BINARY_PRINT_DATA_END: 1485 default: 1486 break; 1487 } 1488 1489 return printed; 1490 } 1491 1492 static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp) 1493 { 1494 unsigned int nr_bytes = sample->raw_size; 1495 struct printer_data printer_data = {0, false, true}; 1496 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8, 1497 sample__fprintf_bpf_output, &printer_data, fp); 1498 1499 if (printer_data.is_printable && printer_data.hit_nul) 1500 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data)); 1501 1502 return printed; 1503 } 1504 1505 static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp) 1506 { 1507 if (len > 0 && len < spacing) 1508 return fprintf(fp, "%*s", spacing - len, ""); 1509 1510 return 0; 1511 } 1512 1513 static int perf_sample__fprintf_pt_spacing(int len, FILE *fp) 1514 { 1515 return perf_sample__fprintf_spacing(len, 34, fp); 1516 } 1517 1518 static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp) 1519 { 1520 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample); 1521 int len; 1522 1523 if (perf_sample__bad_synth_size(sample, *data)) 1524 return 0; 1525 1526 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ", 1527 data->ip, le64_to_cpu(data->payload)); 1528 return len + perf_sample__fprintf_pt_spacing(len, fp); 1529 } 1530 1531 static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp) 1532 { 1533 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample); 1534 int len; 1535 1536 if (perf_sample__bad_synth_size(sample, *data)) 1537 return 0; 1538 1539 len = fprintf(fp, " hints: %#x extensions: %#x ", 1540 data->hints, data->extensions); 1541 return len + perf_sample__fprintf_pt_spacing(len, fp); 1542 } 1543 1544 static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp) 1545 { 1546 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample); 1547 int len; 1548 1549 if (perf_sample__bad_synth_size(sample, *data)) 1550 return 0; 1551 1552 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ", 1553 data->hw, data->cstate, data->subcstate); 1554 return len + perf_sample__fprintf_pt_spacing(len, fp); 1555 } 1556 1557 static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp) 1558 { 1559 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample); 1560 int len; 1561 1562 if (perf_sample__bad_synth_size(sample, *data)) 1563 return 0; 1564 1565 len = fprintf(fp, " IP: %u ", data->ip); 1566 return len + perf_sample__fprintf_pt_spacing(len, fp); 1567 } 1568 1569 static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp) 1570 { 1571 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample); 1572 int len; 1573 1574 if (perf_sample__bad_synth_size(sample, *data)) 1575 return 0; 1576 1577 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ", 1578 data->deepest_cstate, data->last_cstate, 1579 data->wake_reason); 1580 return len + perf_sample__fprintf_pt_spacing(len, fp); 1581 } 1582 1583 static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp) 1584 { 1585 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample); 1586 unsigned int percent, freq; 1587 int len; 1588 1589 if (perf_sample__bad_synth_size(sample, *data)) 1590 return 0; 1591 1592 freq = (le32_to_cpu(data->freq) + 500) / 1000; 1593 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq); 1594 if (data->max_nonturbo) { 1595 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10; 1596 len += fprintf(fp, "(%3u%%) ", percent); 1597 } 1598 return len + perf_sample__fprintf_pt_spacing(len, fp); 1599 } 1600 1601 static int perf_sample__fprintf_synth(struct perf_sample *sample, 1602 struct evsel *evsel, FILE *fp) 1603 { 1604 switch (evsel->core.attr.config) { 1605 case PERF_SYNTH_INTEL_PTWRITE: 1606 return perf_sample__fprintf_synth_ptwrite(sample, fp); 1607 case PERF_SYNTH_INTEL_MWAIT: 1608 return perf_sample__fprintf_synth_mwait(sample, fp); 1609 case PERF_SYNTH_INTEL_PWRE: 1610 return perf_sample__fprintf_synth_pwre(sample, fp); 1611 case PERF_SYNTH_INTEL_EXSTOP: 1612 return perf_sample__fprintf_synth_exstop(sample, fp); 1613 case PERF_SYNTH_INTEL_PWRX: 1614 return perf_sample__fprintf_synth_pwrx(sample, fp); 1615 case PERF_SYNTH_INTEL_CBR: 1616 return perf_sample__fprintf_synth_cbr(sample, fp); 1617 default: 1618 break; 1619 } 1620 1621 return 0; 1622 } 1623 1624 struct perf_script { 1625 struct perf_tool tool; 1626 struct perf_session *session; 1627 bool show_task_events; 1628 bool show_mmap_events; 1629 bool show_switch_events; 1630 bool show_namespace_events; 1631 bool show_lost_events; 1632 bool show_round_events; 1633 bool show_bpf_events; 1634 bool allocated; 1635 bool per_event_dump; 1636 struct evswitch evswitch; 1637 struct perf_cpu_map *cpus; 1638 struct perf_thread_map *threads; 1639 int name_width; 1640 const char *time_str; 1641 struct perf_time_interval *ptime_range; 1642 int range_size; 1643 int range_num; 1644 }; 1645 1646 static int perf_evlist__max_name_len(struct evlist *evlist) 1647 { 1648 struct evsel *evsel; 1649 int max = 0; 1650 1651 evlist__for_each_entry(evlist, evsel) { 1652 int len = strlen(perf_evsel__name(evsel)); 1653 1654 max = MAX(len, max); 1655 } 1656 1657 return max; 1658 } 1659 1660 static int data_src__fprintf(u64 data_src, FILE *fp) 1661 { 1662 struct mem_info mi = { .data_src.val = data_src }; 1663 char decode[100]; 1664 char out[100]; 1665 static int maxlen; 1666 int len; 1667 1668 perf_script__meminfo_scnprintf(decode, 100, &mi); 1669 1670 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode); 1671 if (maxlen < len) 1672 maxlen = len; 1673 1674 return fprintf(fp, "%-*s", maxlen, out); 1675 } 1676 1677 struct metric_ctx { 1678 struct perf_sample *sample; 1679 struct thread *thread; 1680 struct evsel *evsel; 1681 FILE *fp; 1682 }; 1683 1684 static void script_print_metric(struct perf_stat_config *config __maybe_unused, 1685 void *ctx, const char *color, 1686 const char *fmt, 1687 const char *unit, double val) 1688 { 1689 struct metric_ctx *mctx = ctx; 1690 1691 if (!fmt) 1692 return; 1693 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel, 1694 PERF_RECORD_SAMPLE, mctx->fp); 1695 fputs("\tmetric: ", mctx->fp); 1696 if (color) 1697 color_fprintf(mctx->fp, color, fmt, val); 1698 else 1699 printf(fmt, val); 1700 fprintf(mctx->fp, " %s\n", unit); 1701 } 1702 1703 static void script_new_line(struct perf_stat_config *config __maybe_unused, 1704 void *ctx) 1705 { 1706 struct metric_ctx *mctx = ctx; 1707 1708 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel, 1709 PERF_RECORD_SAMPLE, mctx->fp); 1710 fputs("\tmetric: ", mctx->fp); 1711 } 1712 1713 static void perf_sample__fprint_metric(struct perf_script *script, 1714 struct thread *thread, 1715 struct evsel *evsel, 1716 struct perf_sample *sample, 1717 FILE *fp) 1718 { 1719 struct perf_stat_output_ctx ctx = { 1720 .print_metric = script_print_metric, 1721 .new_line = script_new_line, 1722 .ctx = &(struct metric_ctx) { 1723 .sample = sample, 1724 .thread = thread, 1725 .evsel = evsel, 1726 .fp = fp, 1727 }, 1728 .force_header = false, 1729 }; 1730 struct evsel *ev2; 1731 u64 val; 1732 1733 if (!evsel->stats) 1734 perf_evlist__alloc_stats(script->session->evlist, false); 1735 if (evsel_script(evsel->leader)->gnum++ == 0) 1736 perf_stat__reset_shadow_stats(); 1737 val = sample->period * evsel->scale; 1738 perf_stat__update_shadow_stats(evsel, 1739 val, 1740 sample->cpu, 1741 &rt_stat); 1742 evsel_script(evsel)->val = val; 1743 if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) { 1744 for_each_group_member (ev2, evsel->leader) { 1745 perf_stat__print_shadow_stats(&stat_config, ev2, 1746 evsel_script(ev2)->val, 1747 sample->cpu, 1748 &ctx, 1749 NULL, 1750 &rt_stat); 1751 } 1752 evsel_script(evsel->leader)->gnum = 0; 1753 } 1754 } 1755 1756 static bool show_event(struct perf_sample *sample, 1757 struct evsel *evsel, 1758 struct thread *thread, 1759 struct addr_location *al) 1760 { 1761 int depth = thread_stack__depth(thread, sample->cpu); 1762 1763 if (!symbol_conf.graph_function) 1764 return true; 1765 1766 if (thread->filter) { 1767 if (depth <= thread->filter_entry_depth) { 1768 thread->filter = false; 1769 return false; 1770 } 1771 return true; 1772 } else { 1773 const char *s = symbol_conf.graph_function; 1774 u64 ip; 1775 const char *name = resolve_branch_sym(sample, evsel, thread, al, 1776 &ip); 1777 unsigned nlen; 1778 1779 if (!name) 1780 return false; 1781 nlen = strlen(name); 1782 while (*s) { 1783 unsigned len = strcspn(s, ","); 1784 if (nlen == len && !strncmp(name, s, len)) { 1785 thread->filter = true; 1786 thread->filter_entry_depth = depth; 1787 return true; 1788 } 1789 s += len; 1790 if (*s == ',') 1791 s++; 1792 } 1793 return false; 1794 } 1795 } 1796 1797 static void process_event(struct perf_script *script, 1798 struct perf_sample *sample, struct evsel *evsel, 1799 struct addr_location *al, 1800 struct machine *machine) 1801 { 1802 struct thread *thread = al->thread; 1803 struct perf_event_attr *attr = &evsel->core.attr; 1804 unsigned int type = output_type(attr->type); 1805 struct evsel_script *es = evsel->priv; 1806 FILE *fp = es->fp; 1807 1808 if (output[type].fields == 0) 1809 return; 1810 1811 if (!show_event(sample, evsel, thread, al)) 1812 return; 1813 1814 if (evswitch__discard(&script->evswitch, evsel)) 1815 return; 1816 1817 ++es->samples; 1818 1819 perf_sample__fprintf_start(sample, thread, evsel, 1820 PERF_RECORD_SAMPLE, fp); 1821 1822 if (PRINT_FIELD(PERIOD)) 1823 fprintf(fp, "%10" PRIu64 " ", sample->period); 1824 1825 if (PRINT_FIELD(EVNAME)) { 1826 const char *evname = perf_evsel__name(evsel); 1827 1828 if (!script->name_width) 1829 script->name_width = perf_evlist__max_name_len(script->session->evlist); 1830 1831 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]"); 1832 } 1833 1834 if (print_flags) 1835 perf_sample__fprintf_flags(sample->flags, fp); 1836 1837 if (is_bts_event(attr)) { 1838 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp); 1839 return; 1840 } 1841 1842 if (PRINT_FIELD(TRACE) && sample->raw_data) { 1843 event_format__fprintf(evsel->tp_format, sample->cpu, 1844 sample->raw_data, sample->raw_size, fp); 1845 } 1846 1847 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH)) 1848 perf_sample__fprintf_synth(sample, evsel, fp); 1849 1850 if (PRINT_FIELD(ADDR)) 1851 perf_sample__fprintf_addr(sample, thread, attr, fp); 1852 1853 if (PRINT_FIELD(DATA_SRC)) 1854 data_src__fprintf(sample->data_src, fp); 1855 1856 if (PRINT_FIELD(WEIGHT)) 1857 fprintf(fp, "%16" PRIu64, sample->weight); 1858 1859 if (PRINT_FIELD(IP)) { 1860 struct callchain_cursor *cursor = NULL; 1861 1862 if (symbol_conf.use_callchain && sample->callchain && 1863 thread__resolve_callchain(al->thread, &callchain_cursor, evsel, 1864 sample, NULL, NULL, scripting_max_stack) == 0) 1865 cursor = &callchain_cursor; 1866 1867 fputc(cursor ? '\n' : ' ', fp); 1868 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp); 1869 } 1870 1871 if (PRINT_FIELD(IREGS)) 1872 perf_sample__fprintf_iregs(sample, attr, fp); 1873 1874 if (PRINT_FIELD(UREGS)) 1875 perf_sample__fprintf_uregs(sample, attr, fp); 1876 1877 if (PRINT_FIELD(BRSTACK)) 1878 perf_sample__fprintf_brstack(sample, thread, attr, fp); 1879 else if (PRINT_FIELD(BRSTACKSYM)) 1880 perf_sample__fprintf_brstacksym(sample, thread, attr, fp); 1881 else if (PRINT_FIELD(BRSTACKOFF)) 1882 perf_sample__fprintf_brstackoff(sample, thread, attr, fp); 1883 1884 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT)) 1885 perf_sample__fprintf_bpf_output(sample, fp); 1886 perf_sample__fprintf_insn(sample, attr, thread, machine, fp); 1887 1888 if (PRINT_FIELD(PHYS_ADDR)) 1889 fprintf(fp, "%16" PRIx64, sample->phys_addr); 1890 1891 perf_sample__fprintf_ipc(sample, attr, fp); 1892 1893 fprintf(fp, "\n"); 1894 1895 if (PRINT_FIELD(SRCCODE)) { 1896 if (map__fprintf_srccode(al->map, al->addr, stdout, 1897 &thread->srccode_state)) 1898 printf("\n"); 1899 } 1900 1901 if (PRINT_FIELD(METRIC)) 1902 perf_sample__fprint_metric(script, thread, evsel, sample, fp); 1903 1904 if (verbose) 1905 fflush(fp); 1906 } 1907 1908 static struct scripting_ops *scripting_ops; 1909 1910 static void __process_stat(struct evsel *counter, u64 tstamp) 1911 { 1912 int nthreads = perf_thread_map__nr(counter->core.threads); 1913 int ncpus = perf_evsel__nr_cpus(counter); 1914 int cpu, thread; 1915 static int header_printed; 1916 1917 if (counter->system_wide) 1918 nthreads = 1; 1919 1920 if (!header_printed) { 1921 printf("%3s %8s %15s %15s %15s %15s %s\n", 1922 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT"); 1923 header_printed = 1; 1924 } 1925 1926 for (thread = 0; thread < nthreads; thread++) { 1927 for (cpu = 0; cpu < ncpus; cpu++) { 1928 struct perf_counts_values *counts; 1929 1930 counts = perf_counts(counter->counts, cpu, thread); 1931 1932 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n", 1933 counter->core.cpus->map[cpu], 1934 perf_thread_map__pid(counter->core.threads, thread), 1935 counts->val, 1936 counts->ena, 1937 counts->run, 1938 tstamp, 1939 perf_evsel__name(counter)); 1940 } 1941 } 1942 } 1943 1944 static void process_stat(struct evsel *counter, u64 tstamp) 1945 { 1946 if (scripting_ops && scripting_ops->process_stat) 1947 scripting_ops->process_stat(&stat_config, counter, tstamp); 1948 else 1949 __process_stat(counter, tstamp); 1950 } 1951 1952 static void process_stat_interval(u64 tstamp) 1953 { 1954 if (scripting_ops && scripting_ops->process_stat_interval) 1955 scripting_ops->process_stat_interval(tstamp); 1956 } 1957 1958 static void setup_scripting(void) 1959 { 1960 setup_perl_scripting(); 1961 setup_python_scripting(); 1962 } 1963 1964 static int flush_scripting(void) 1965 { 1966 return scripting_ops ? scripting_ops->flush_script() : 0; 1967 } 1968 1969 static int cleanup_scripting(void) 1970 { 1971 pr_debug("\nperf script stopped\n"); 1972 1973 return scripting_ops ? scripting_ops->stop_script() : 0; 1974 } 1975 1976 static bool filter_cpu(struct perf_sample *sample) 1977 { 1978 if (cpu_list) 1979 return !test_bit(sample->cpu, cpu_bitmap); 1980 return false; 1981 } 1982 1983 static int process_sample_event(struct perf_tool *tool, 1984 union perf_event *event, 1985 struct perf_sample *sample, 1986 struct evsel *evsel, 1987 struct machine *machine) 1988 { 1989 struct perf_script *scr = container_of(tool, struct perf_script, tool); 1990 struct addr_location al; 1991 1992 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num, 1993 sample->time)) { 1994 return 0; 1995 } 1996 1997 if (debug_mode) { 1998 if (sample->time < last_timestamp) { 1999 pr_err("Samples misordered, previous: %" PRIu64 2000 " this: %" PRIu64 "\n", last_timestamp, 2001 sample->time); 2002 nr_unordered++; 2003 } 2004 last_timestamp = sample->time; 2005 return 0; 2006 } 2007 2008 if (machine__resolve(machine, &al, sample) < 0) { 2009 pr_err("problem processing %d event, skipping it.\n", 2010 event->header.type); 2011 return -1; 2012 } 2013 2014 if (al.filtered) 2015 goto out_put; 2016 2017 if (filter_cpu(sample)) 2018 goto out_put; 2019 2020 if (scripting_ops) 2021 scripting_ops->process_event(event, sample, evsel, &al); 2022 else 2023 process_event(scr, sample, evsel, &al, machine); 2024 2025 out_put: 2026 addr_location__put(&al); 2027 return 0; 2028 } 2029 2030 static int process_attr(struct perf_tool *tool, union perf_event *event, 2031 struct evlist **pevlist) 2032 { 2033 struct perf_script *scr = container_of(tool, struct perf_script, tool); 2034 struct evlist *evlist; 2035 struct evsel *evsel, *pos; 2036 int err; 2037 static struct evsel_script *es; 2038 2039 err = perf_event__process_attr(tool, event, pevlist); 2040 if (err) 2041 return err; 2042 2043 evlist = *pevlist; 2044 evsel = perf_evlist__last(*pevlist); 2045 2046 if (!evsel->priv) { 2047 if (scr->per_event_dump) { 2048 evsel->priv = perf_evsel_script__new(evsel, 2049 scr->session->data); 2050 } else { 2051 es = zalloc(sizeof(*es)); 2052 if (!es) 2053 return -ENOMEM; 2054 es->fp = stdout; 2055 evsel->priv = es; 2056 } 2057 } 2058 2059 if (evsel->core.attr.type >= PERF_TYPE_MAX && 2060 evsel->core.attr.type != PERF_TYPE_SYNTH) 2061 return 0; 2062 2063 evlist__for_each_entry(evlist, pos) { 2064 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel) 2065 return 0; 2066 } 2067 2068 set_print_ip_opts(&evsel->core.attr); 2069 2070 if (evsel->core.attr.sample_type) 2071 err = perf_evsel__check_attr(evsel, scr->session); 2072 2073 return err; 2074 } 2075 2076 static int process_comm_event(struct perf_tool *tool, 2077 union perf_event *event, 2078 struct perf_sample *sample, 2079 struct machine *machine) 2080 { 2081 struct thread *thread; 2082 struct perf_script *script = container_of(tool, struct perf_script, tool); 2083 struct perf_session *session = script->session; 2084 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2085 int ret = -1; 2086 2087 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid); 2088 if (thread == NULL) { 2089 pr_debug("problem processing COMM event, skipping it.\n"); 2090 return -1; 2091 } 2092 2093 if (perf_event__process_comm(tool, event, sample, machine) < 0) 2094 goto out; 2095 2096 if (!evsel->core.attr.sample_id_all) { 2097 sample->cpu = 0; 2098 sample->time = 0; 2099 sample->tid = event->comm.tid; 2100 sample->pid = event->comm.pid; 2101 } 2102 if (!filter_cpu(sample)) { 2103 perf_sample__fprintf_start(sample, thread, evsel, 2104 PERF_RECORD_COMM, stdout); 2105 perf_event__fprintf(event, stdout); 2106 } 2107 ret = 0; 2108 out: 2109 thread__put(thread); 2110 return ret; 2111 } 2112 2113 static int process_namespaces_event(struct perf_tool *tool, 2114 union perf_event *event, 2115 struct perf_sample *sample, 2116 struct machine *machine) 2117 { 2118 struct thread *thread; 2119 struct perf_script *script = container_of(tool, struct perf_script, tool); 2120 struct perf_session *session = script->session; 2121 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2122 int ret = -1; 2123 2124 thread = machine__findnew_thread(machine, event->namespaces.pid, 2125 event->namespaces.tid); 2126 if (thread == NULL) { 2127 pr_debug("problem processing NAMESPACES event, skipping it.\n"); 2128 return -1; 2129 } 2130 2131 if (perf_event__process_namespaces(tool, event, sample, machine) < 0) 2132 goto out; 2133 2134 if (!evsel->core.attr.sample_id_all) { 2135 sample->cpu = 0; 2136 sample->time = 0; 2137 sample->tid = event->namespaces.tid; 2138 sample->pid = event->namespaces.pid; 2139 } 2140 if (!filter_cpu(sample)) { 2141 perf_sample__fprintf_start(sample, thread, evsel, 2142 PERF_RECORD_NAMESPACES, stdout); 2143 perf_event__fprintf(event, stdout); 2144 } 2145 ret = 0; 2146 out: 2147 thread__put(thread); 2148 return ret; 2149 } 2150 2151 static int process_fork_event(struct perf_tool *tool, 2152 union perf_event *event, 2153 struct perf_sample *sample, 2154 struct machine *machine) 2155 { 2156 struct thread *thread; 2157 struct perf_script *script = container_of(tool, struct perf_script, tool); 2158 struct perf_session *session = script->session; 2159 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2160 2161 if (perf_event__process_fork(tool, event, sample, machine) < 0) 2162 return -1; 2163 2164 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); 2165 if (thread == NULL) { 2166 pr_debug("problem processing FORK event, skipping it.\n"); 2167 return -1; 2168 } 2169 2170 if (!evsel->core.attr.sample_id_all) { 2171 sample->cpu = 0; 2172 sample->time = event->fork.time; 2173 sample->tid = event->fork.tid; 2174 sample->pid = event->fork.pid; 2175 } 2176 if (!filter_cpu(sample)) { 2177 perf_sample__fprintf_start(sample, thread, evsel, 2178 PERF_RECORD_FORK, stdout); 2179 perf_event__fprintf(event, stdout); 2180 } 2181 thread__put(thread); 2182 2183 return 0; 2184 } 2185 static int process_exit_event(struct perf_tool *tool, 2186 union perf_event *event, 2187 struct perf_sample *sample, 2188 struct machine *machine) 2189 { 2190 int err = 0; 2191 struct thread *thread; 2192 struct perf_script *script = container_of(tool, struct perf_script, tool); 2193 struct perf_session *session = script->session; 2194 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2195 2196 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); 2197 if (thread == NULL) { 2198 pr_debug("problem processing EXIT event, skipping it.\n"); 2199 return -1; 2200 } 2201 2202 if (!evsel->core.attr.sample_id_all) { 2203 sample->cpu = 0; 2204 sample->time = 0; 2205 sample->tid = event->fork.tid; 2206 sample->pid = event->fork.pid; 2207 } 2208 if (!filter_cpu(sample)) { 2209 perf_sample__fprintf_start(sample, thread, evsel, 2210 PERF_RECORD_EXIT, stdout); 2211 perf_event__fprintf(event, stdout); 2212 } 2213 2214 if (perf_event__process_exit(tool, event, sample, machine) < 0) 2215 err = -1; 2216 2217 thread__put(thread); 2218 return err; 2219 } 2220 2221 static int process_mmap_event(struct perf_tool *tool, 2222 union perf_event *event, 2223 struct perf_sample *sample, 2224 struct machine *machine) 2225 { 2226 struct thread *thread; 2227 struct perf_script *script = container_of(tool, struct perf_script, tool); 2228 struct perf_session *session = script->session; 2229 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2230 2231 if (perf_event__process_mmap(tool, event, sample, machine) < 0) 2232 return -1; 2233 2234 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid); 2235 if (thread == NULL) { 2236 pr_debug("problem processing MMAP event, skipping it.\n"); 2237 return -1; 2238 } 2239 2240 if (!evsel->core.attr.sample_id_all) { 2241 sample->cpu = 0; 2242 sample->time = 0; 2243 sample->tid = event->mmap.tid; 2244 sample->pid = event->mmap.pid; 2245 } 2246 if (!filter_cpu(sample)) { 2247 perf_sample__fprintf_start(sample, thread, evsel, 2248 PERF_RECORD_MMAP, stdout); 2249 perf_event__fprintf(event, stdout); 2250 } 2251 thread__put(thread); 2252 return 0; 2253 } 2254 2255 static int process_mmap2_event(struct perf_tool *tool, 2256 union perf_event *event, 2257 struct perf_sample *sample, 2258 struct machine *machine) 2259 { 2260 struct thread *thread; 2261 struct perf_script *script = container_of(tool, struct perf_script, tool); 2262 struct perf_session *session = script->session; 2263 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2264 2265 if (perf_event__process_mmap2(tool, event, sample, machine) < 0) 2266 return -1; 2267 2268 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid); 2269 if (thread == NULL) { 2270 pr_debug("problem processing MMAP2 event, skipping it.\n"); 2271 return -1; 2272 } 2273 2274 if (!evsel->core.attr.sample_id_all) { 2275 sample->cpu = 0; 2276 sample->time = 0; 2277 sample->tid = event->mmap2.tid; 2278 sample->pid = event->mmap2.pid; 2279 } 2280 if (!filter_cpu(sample)) { 2281 perf_sample__fprintf_start(sample, thread, evsel, 2282 PERF_RECORD_MMAP2, stdout); 2283 perf_event__fprintf(event, stdout); 2284 } 2285 thread__put(thread); 2286 return 0; 2287 } 2288 2289 static int process_switch_event(struct perf_tool *tool, 2290 union perf_event *event, 2291 struct perf_sample *sample, 2292 struct machine *machine) 2293 { 2294 struct thread *thread; 2295 struct perf_script *script = container_of(tool, struct perf_script, tool); 2296 struct perf_session *session = script->session; 2297 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2298 2299 if (perf_event__process_switch(tool, event, sample, machine) < 0) 2300 return -1; 2301 2302 if (scripting_ops && scripting_ops->process_switch) 2303 scripting_ops->process_switch(event, sample, machine); 2304 2305 if (!script->show_switch_events) 2306 return 0; 2307 2308 thread = machine__findnew_thread(machine, sample->pid, 2309 sample->tid); 2310 if (thread == NULL) { 2311 pr_debug("problem processing SWITCH event, skipping it.\n"); 2312 return -1; 2313 } 2314 2315 if (!filter_cpu(sample)) { 2316 perf_sample__fprintf_start(sample, thread, evsel, 2317 PERF_RECORD_SWITCH, stdout); 2318 perf_event__fprintf(event, stdout); 2319 } 2320 thread__put(thread); 2321 return 0; 2322 } 2323 2324 static int 2325 process_lost_event(struct perf_tool *tool, 2326 union perf_event *event, 2327 struct perf_sample *sample, 2328 struct machine *machine) 2329 { 2330 struct perf_script *script = container_of(tool, struct perf_script, tool); 2331 struct perf_session *session = script->session; 2332 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2333 struct thread *thread; 2334 2335 thread = machine__findnew_thread(machine, sample->pid, 2336 sample->tid); 2337 if (thread == NULL) 2338 return -1; 2339 2340 if (!filter_cpu(sample)) { 2341 perf_sample__fprintf_start(sample, thread, evsel, 2342 PERF_RECORD_LOST, stdout); 2343 perf_event__fprintf(event, stdout); 2344 } 2345 thread__put(thread); 2346 return 0; 2347 } 2348 2349 static int 2350 process_finished_round_event(struct perf_tool *tool __maybe_unused, 2351 union perf_event *event, 2352 struct ordered_events *oe __maybe_unused) 2353 2354 { 2355 perf_event__fprintf(event, stdout); 2356 return 0; 2357 } 2358 2359 static int 2360 process_bpf_events(struct perf_tool *tool __maybe_unused, 2361 union perf_event *event, 2362 struct perf_sample *sample, 2363 struct machine *machine) 2364 { 2365 struct thread *thread; 2366 struct perf_script *script = container_of(tool, struct perf_script, tool); 2367 struct perf_session *session = script->session; 2368 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2369 2370 if (machine__process_ksymbol(machine, event, sample) < 0) 2371 return -1; 2372 2373 if (!evsel->core.attr.sample_id_all) { 2374 perf_event__fprintf(event, stdout); 2375 return 0; 2376 } 2377 2378 thread = machine__findnew_thread(machine, sample->pid, sample->tid); 2379 if (thread == NULL) { 2380 pr_debug("problem processing MMAP event, skipping it.\n"); 2381 return -1; 2382 } 2383 2384 if (!filter_cpu(sample)) { 2385 perf_sample__fprintf_start(sample, thread, evsel, 2386 event->header.type, stdout); 2387 perf_event__fprintf(event, stdout); 2388 } 2389 2390 thread__put(thread); 2391 return 0; 2392 } 2393 2394 static void sig_handler(int sig __maybe_unused) 2395 { 2396 session_done = 1; 2397 } 2398 2399 static void perf_script__fclose_per_event_dump(struct perf_script *script) 2400 { 2401 struct evlist *evlist = script->session->evlist; 2402 struct evsel *evsel; 2403 2404 evlist__for_each_entry(evlist, evsel) { 2405 if (!evsel->priv) 2406 break; 2407 perf_evsel_script__delete(evsel->priv); 2408 evsel->priv = NULL; 2409 } 2410 } 2411 2412 static int perf_script__fopen_per_event_dump(struct perf_script *script) 2413 { 2414 struct evsel *evsel; 2415 2416 evlist__for_each_entry(script->session->evlist, evsel) { 2417 /* 2418 * Already setup? I.e. we may be called twice in cases like 2419 * Intel PT, one for the intel_pt// and dummy events, then 2420 * for the evsels syntheized from the auxtrace info. 2421 * 2422 * Ses perf_script__process_auxtrace_info. 2423 */ 2424 if (evsel->priv != NULL) 2425 continue; 2426 2427 evsel->priv = perf_evsel_script__new(evsel, script->session->data); 2428 if (evsel->priv == NULL) 2429 goto out_err_fclose; 2430 } 2431 2432 return 0; 2433 2434 out_err_fclose: 2435 perf_script__fclose_per_event_dump(script); 2436 return -1; 2437 } 2438 2439 static int perf_script__setup_per_event_dump(struct perf_script *script) 2440 { 2441 struct evsel *evsel; 2442 static struct evsel_script es_stdout; 2443 2444 if (script->per_event_dump) 2445 return perf_script__fopen_per_event_dump(script); 2446 2447 es_stdout.fp = stdout; 2448 2449 evlist__for_each_entry(script->session->evlist, evsel) 2450 evsel->priv = &es_stdout; 2451 2452 return 0; 2453 } 2454 2455 static void perf_script__exit_per_event_dump_stats(struct perf_script *script) 2456 { 2457 struct evsel *evsel; 2458 2459 evlist__for_each_entry(script->session->evlist, evsel) { 2460 struct evsel_script *es = evsel->priv; 2461 2462 perf_evsel_script__fprintf(es, stdout); 2463 perf_evsel_script__delete(es); 2464 evsel->priv = NULL; 2465 } 2466 } 2467 2468 static int __cmd_script(struct perf_script *script) 2469 { 2470 int ret; 2471 2472 signal(SIGINT, sig_handler); 2473 2474 perf_stat__init_shadow_stats(); 2475 2476 /* override event processing functions */ 2477 if (script->show_task_events) { 2478 script->tool.comm = process_comm_event; 2479 script->tool.fork = process_fork_event; 2480 script->tool.exit = process_exit_event; 2481 } 2482 if (script->show_mmap_events) { 2483 script->tool.mmap = process_mmap_event; 2484 script->tool.mmap2 = process_mmap2_event; 2485 } 2486 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch)) 2487 script->tool.context_switch = process_switch_event; 2488 if (script->show_namespace_events) 2489 script->tool.namespaces = process_namespaces_event; 2490 if (script->show_lost_events) 2491 script->tool.lost = process_lost_event; 2492 if (script->show_round_events) { 2493 script->tool.ordered_events = false; 2494 script->tool.finished_round = process_finished_round_event; 2495 } 2496 if (script->show_bpf_events) { 2497 script->tool.ksymbol = process_bpf_events; 2498 script->tool.bpf = process_bpf_events; 2499 } 2500 2501 if (perf_script__setup_per_event_dump(script)) { 2502 pr_err("Couldn't create the per event dump files\n"); 2503 return -1; 2504 } 2505 2506 ret = perf_session__process_events(script->session); 2507 2508 if (script->per_event_dump) 2509 perf_script__exit_per_event_dump_stats(script); 2510 2511 if (debug_mode) 2512 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered); 2513 2514 return ret; 2515 } 2516 2517 struct script_spec { 2518 struct list_head node; 2519 struct scripting_ops *ops; 2520 char spec[0]; 2521 }; 2522 2523 static LIST_HEAD(script_specs); 2524 2525 static struct script_spec *script_spec__new(const char *spec, 2526 struct scripting_ops *ops) 2527 { 2528 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); 2529 2530 if (s != NULL) { 2531 strcpy(s->spec, spec); 2532 s->ops = ops; 2533 } 2534 2535 return s; 2536 } 2537 2538 static void script_spec__add(struct script_spec *s) 2539 { 2540 list_add_tail(&s->node, &script_specs); 2541 } 2542 2543 static struct script_spec *script_spec__find(const char *spec) 2544 { 2545 struct script_spec *s; 2546 2547 list_for_each_entry(s, &script_specs, node) 2548 if (strcasecmp(s->spec, spec) == 0) 2549 return s; 2550 return NULL; 2551 } 2552 2553 int script_spec_register(const char *spec, struct scripting_ops *ops) 2554 { 2555 struct script_spec *s; 2556 2557 s = script_spec__find(spec); 2558 if (s) 2559 return -1; 2560 2561 s = script_spec__new(spec, ops); 2562 if (!s) 2563 return -1; 2564 else 2565 script_spec__add(s); 2566 2567 return 0; 2568 } 2569 2570 static struct scripting_ops *script_spec__lookup(const char *spec) 2571 { 2572 struct script_spec *s = script_spec__find(spec); 2573 if (!s) 2574 return NULL; 2575 2576 return s->ops; 2577 } 2578 2579 static void list_available_languages(void) 2580 { 2581 struct script_spec *s; 2582 2583 fprintf(stderr, "\n"); 2584 fprintf(stderr, "Scripting language extensions (used in " 2585 "perf script -s [spec:]script.[spec]):\n\n"); 2586 2587 list_for_each_entry(s, &script_specs, node) 2588 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); 2589 2590 fprintf(stderr, "\n"); 2591 } 2592 2593 static int parse_scriptname(const struct option *opt __maybe_unused, 2594 const char *str, int unset __maybe_unused) 2595 { 2596 char spec[PATH_MAX]; 2597 const char *script, *ext; 2598 int len; 2599 2600 if (strcmp(str, "lang") == 0) { 2601 list_available_languages(); 2602 exit(0); 2603 } 2604 2605 script = strchr(str, ':'); 2606 if (script) { 2607 len = script - str; 2608 if (len >= PATH_MAX) { 2609 fprintf(stderr, "invalid language specifier"); 2610 return -1; 2611 } 2612 strncpy(spec, str, len); 2613 spec[len] = '\0'; 2614 scripting_ops = script_spec__lookup(spec); 2615 if (!scripting_ops) { 2616 fprintf(stderr, "invalid language specifier"); 2617 return -1; 2618 } 2619 script++; 2620 } else { 2621 script = str; 2622 ext = strrchr(script, '.'); 2623 if (!ext) { 2624 fprintf(stderr, "invalid script extension"); 2625 return -1; 2626 } 2627 scripting_ops = script_spec__lookup(++ext); 2628 if (!scripting_ops) { 2629 fprintf(stderr, "invalid script extension"); 2630 return -1; 2631 } 2632 } 2633 2634 script_name = strdup(script); 2635 2636 return 0; 2637 } 2638 2639 static int parse_output_fields(const struct option *opt __maybe_unused, 2640 const char *arg, int unset __maybe_unused) 2641 { 2642 char *tok, *strtok_saveptr = NULL; 2643 int i, imax = ARRAY_SIZE(all_output_options); 2644 int j; 2645 int rc = 0; 2646 char *str = strdup(arg); 2647 int type = -1; 2648 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT; 2649 2650 if (!str) 2651 return -ENOMEM; 2652 2653 /* first word can state for which event type the user is specifying 2654 * the fields. If no type exists, the specified fields apply to all 2655 * event types found in the file minus the invalid fields for a type. 2656 */ 2657 tok = strchr(str, ':'); 2658 if (tok) { 2659 *tok = '\0'; 2660 tok++; 2661 if (!strcmp(str, "hw")) 2662 type = PERF_TYPE_HARDWARE; 2663 else if (!strcmp(str, "sw")) 2664 type = PERF_TYPE_SOFTWARE; 2665 else if (!strcmp(str, "trace")) 2666 type = PERF_TYPE_TRACEPOINT; 2667 else if (!strcmp(str, "raw")) 2668 type = PERF_TYPE_RAW; 2669 else if (!strcmp(str, "break")) 2670 type = PERF_TYPE_BREAKPOINT; 2671 else if (!strcmp(str, "synth")) 2672 type = OUTPUT_TYPE_SYNTH; 2673 else { 2674 fprintf(stderr, "Invalid event type in field string.\n"); 2675 rc = -EINVAL; 2676 goto out; 2677 } 2678 2679 if (output[type].user_set) 2680 pr_warning("Overriding previous field request for %s events.\n", 2681 event_type(type)); 2682 2683 /* Don't override defaults for +- */ 2684 if (strchr(tok, '+') || strchr(tok, '-')) 2685 goto parse; 2686 2687 output[type].fields = 0; 2688 output[type].user_set = true; 2689 output[type].wildcard_set = false; 2690 2691 } else { 2692 tok = str; 2693 if (strlen(str) == 0) { 2694 fprintf(stderr, 2695 "Cannot set fields to 'none' for all event types.\n"); 2696 rc = -EINVAL; 2697 goto out; 2698 } 2699 2700 /* Don't override defaults for +- */ 2701 if (strchr(str, '+') || strchr(str, '-')) 2702 goto parse; 2703 2704 if (output_set_by_user()) 2705 pr_warning("Overriding previous field request for all events.\n"); 2706 2707 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) { 2708 output[j].fields = 0; 2709 output[j].user_set = true; 2710 output[j].wildcard_set = true; 2711 } 2712 } 2713 2714 parse: 2715 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) { 2716 if (*tok == '+') { 2717 if (change == SET) 2718 goto out_badmix; 2719 change = ADD; 2720 tok++; 2721 } else if (*tok == '-') { 2722 if (change == SET) 2723 goto out_badmix; 2724 change = REMOVE; 2725 tok++; 2726 } else { 2727 if (change != SET && change != DEFAULT) 2728 goto out_badmix; 2729 change = SET; 2730 } 2731 2732 for (i = 0; i < imax; ++i) { 2733 if (strcmp(tok, all_output_options[i].str) == 0) 2734 break; 2735 } 2736 if (i == imax && strcmp(tok, "flags") == 0) { 2737 print_flags = change == REMOVE ? false : true; 2738 continue; 2739 } 2740 if (i == imax) { 2741 fprintf(stderr, "Invalid field requested.\n"); 2742 rc = -EINVAL; 2743 goto out; 2744 } 2745 2746 if (type == -1) { 2747 /* add user option to all events types for 2748 * which it is valid 2749 */ 2750 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) { 2751 if (output[j].invalid_fields & all_output_options[i].field) { 2752 pr_warning("\'%s\' not valid for %s events. Ignoring.\n", 2753 all_output_options[i].str, event_type(j)); 2754 } else { 2755 if (change == REMOVE) { 2756 output[j].fields &= ~all_output_options[i].field; 2757 output[j].user_set_fields &= ~all_output_options[i].field; 2758 } else { 2759 output[j].fields |= all_output_options[i].field; 2760 output[j].user_set_fields |= all_output_options[i].field; 2761 } 2762 output[j].user_set = true; 2763 output[j].wildcard_set = true; 2764 } 2765 } 2766 } else { 2767 if (output[type].invalid_fields & all_output_options[i].field) { 2768 fprintf(stderr, "\'%s\' not valid for %s events.\n", 2769 all_output_options[i].str, event_type(type)); 2770 2771 rc = -EINVAL; 2772 goto out; 2773 } 2774 if (change == REMOVE) 2775 output[type].fields &= ~all_output_options[i].field; 2776 else 2777 output[type].fields |= all_output_options[i].field; 2778 output[type].user_set = true; 2779 output[type].wildcard_set = true; 2780 } 2781 } 2782 2783 if (type >= 0) { 2784 if (output[type].fields == 0) { 2785 pr_debug("No fields requested for %s type. " 2786 "Events will not be displayed.\n", event_type(type)); 2787 } 2788 } 2789 goto out; 2790 2791 out_badmix: 2792 fprintf(stderr, "Cannot mix +-field with overridden fields\n"); 2793 rc = -EINVAL; 2794 out: 2795 free(str); 2796 return rc; 2797 } 2798 2799 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \ 2800 while ((lang_dirent = readdir(scripts_dir)) != NULL) \ 2801 if ((lang_dirent->d_type == DT_DIR || \ 2802 (lang_dirent->d_type == DT_UNKNOWN && \ 2803 is_directory(scripts_path, lang_dirent))) && \ 2804 (strcmp(lang_dirent->d_name, ".")) && \ 2805 (strcmp(lang_dirent->d_name, ".."))) 2806 2807 #define for_each_script(lang_path, lang_dir, script_dirent) \ 2808 while ((script_dirent = readdir(lang_dir)) != NULL) \ 2809 if (script_dirent->d_type != DT_DIR && \ 2810 (script_dirent->d_type != DT_UNKNOWN || \ 2811 !is_directory(lang_path, script_dirent))) 2812 2813 2814 #define RECORD_SUFFIX "-record" 2815 #define REPORT_SUFFIX "-report" 2816 2817 struct script_desc { 2818 struct list_head node; 2819 char *name; 2820 char *half_liner; 2821 char *args; 2822 }; 2823 2824 static LIST_HEAD(script_descs); 2825 2826 static struct script_desc *script_desc__new(const char *name) 2827 { 2828 struct script_desc *s = zalloc(sizeof(*s)); 2829 2830 if (s != NULL && name) 2831 s->name = strdup(name); 2832 2833 return s; 2834 } 2835 2836 static void script_desc__delete(struct script_desc *s) 2837 { 2838 zfree(&s->name); 2839 zfree(&s->half_liner); 2840 zfree(&s->args); 2841 free(s); 2842 } 2843 2844 static void script_desc__add(struct script_desc *s) 2845 { 2846 list_add_tail(&s->node, &script_descs); 2847 } 2848 2849 static struct script_desc *script_desc__find(const char *name) 2850 { 2851 struct script_desc *s; 2852 2853 list_for_each_entry(s, &script_descs, node) 2854 if (strcasecmp(s->name, name) == 0) 2855 return s; 2856 return NULL; 2857 } 2858 2859 static struct script_desc *script_desc__findnew(const char *name) 2860 { 2861 struct script_desc *s = script_desc__find(name); 2862 2863 if (s) 2864 return s; 2865 2866 s = script_desc__new(name); 2867 if (!s) 2868 return NULL; 2869 2870 script_desc__add(s); 2871 2872 return s; 2873 } 2874 2875 static const char *ends_with(const char *str, const char *suffix) 2876 { 2877 size_t suffix_len = strlen(suffix); 2878 const char *p = str; 2879 2880 if (strlen(str) > suffix_len) { 2881 p = str + strlen(str) - suffix_len; 2882 if (!strncmp(p, suffix, suffix_len)) 2883 return p; 2884 } 2885 2886 return NULL; 2887 } 2888 2889 static int read_script_info(struct script_desc *desc, const char *filename) 2890 { 2891 char line[BUFSIZ], *p; 2892 FILE *fp; 2893 2894 fp = fopen(filename, "r"); 2895 if (!fp) 2896 return -1; 2897 2898 while (fgets(line, sizeof(line), fp)) { 2899 p = skip_spaces(line); 2900 if (strlen(p) == 0) 2901 continue; 2902 if (*p != '#') 2903 continue; 2904 p++; 2905 if (strlen(p) && *p == '!') 2906 continue; 2907 2908 p = skip_spaces(p); 2909 if (strlen(p) && p[strlen(p) - 1] == '\n') 2910 p[strlen(p) - 1] = '\0'; 2911 2912 if (!strncmp(p, "description:", strlen("description:"))) { 2913 p += strlen("description:"); 2914 desc->half_liner = strdup(skip_spaces(p)); 2915 continue; 2916 } 2917 2918 if (!strncmp(p, "args:", strlen("args:"))) { 2919 p += strlen("args:"); 2920 desc->args = strdup(skip_spaces(p)); 2921 continue; 2922 } 2923 } 2924 2925 fclose(fp); 2926 2927 return 0; 2928 } 2929 2930 static char *get_script_root(struct dirent *script_dirent, const char *suffix) 2931 { 2932 char *script_root, *str; 2933 2934 script_root = strdup(script_dirent->d_name); 2935 if (!script_root) 2936 return NULL; 2937 2938 str = (char *)ends_with(script_root, suffix); 2939 if (!str) { 2940 free(script_root); 2941 return NULL; 2942 } 2943 2944 *str = '\0'; 2945 return script_root; 2946 } 2947 2948 static int list_available_scripts(const struct option *opt __maybe_unused, 2949 const char *s __maybe_unused, 2950 int unset __maybe_unused) 2951 { 2952 struct dirent *script_dirent, *lang_dirent; 2953 char scripts_path[MAXPATHLEN]; 2954 DIR *scripts_dir, *lang_dir; 2955 char script_path[MAXPATHLEN]; 2956 char lang_path[MAXPATHLEN]; 2957 struct script_desc *desc; 2958 char first_half[BUFSIZ]; 2959 char *script_root; 2960 2961 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path()); 2962 2963 scripts_dir = opendir(scripts_path); 2964 if (!scripts_dir) { 2965 fprintf(stdout, 2966 "open(%s) failed.\n" 2967 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n", 2968 scripts_path); 2969 exit(-1); 2970 } 2971 2972 for_each_lang(scripts_path, scripts_dir, lang_dirent) { 2973 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, 2974 lang_dirent->d_name); 2975 lang_dir = opendir(lang_path); 2976 if (!lang_dir) 2977 continue; 2978 2979 for_each_script(lang_path, lang_dir, script_dirent) { 2980 script_root = get_script_root(script_dirent, REPORT_SUFFIX); 2981 if (script_root) { 2982 desc = script_desc__findnew(script_root); 2983 scnprintf(script_path, MAXPATHLEN, "%s/%s", 2984 lang_path, script_dirent->d_name); 2985 read_script_info(desc, script_path); 2986 free(script_root); 2987 } 2988 } 2989 } 2990 2991 fprintf(stdout, "List of available trace scripts:\n"); 2992 list_for_each_entry(desc, &script_descs, node) { 2993 sprintf(first_half, "%s %s", desc->name, 2994 desc->args ? desc->args : ""); 2995 fprintf(stdout, " %-36s %s\n", first_half, 2996 desc->half_liner ? desc->half_liner : ""); 2997 } 2998 2999 exit(0); 3000 } 3001 3002 /* 3003 * Some scripts specify the required events in their "xxx-record" file, 3004 * this function will check if the events in perf.data match those 3005 * mentioned in the "xxx-record". 3006 * 3007 * Fixme: All existing "xxx-record" are all in good formats "-e event ", 3008 * which is covered well now. And new parsing code should be added to 3009 * cover the future complexing formats like event groups etc. 3010 */ 3011 static int check_ev_match(char *dir_name, char *scriptname, 3012 struct perf_session *session) 3013 { 3014 char filename[MAXPATHLEN], evname[128]; 3015 char line[BUFSIZ], *p; 3016 struct evsel *pos; 3017 int match, len; 3018 FILE *fp; 3019 3020 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname); 3021 3022 fp = fopen(filename, "r"); 3023 if (!fp) 3024 return -1; 3025 3026 while (fgets(line, sizeof(line), fp)) { 3027 p = skip_spaces(line); 3028 if (*p == '#') 3029 continue; 3030 3031 while (strlen(p)) { 3032 p = strstr(p, "-e"); 3033 if (!p) 3034 break; 3035 3036 p += 2; 3037 p = skip_spaces(p); 3038 len = strcspn(p, " \t"); 3039 if (!len) 3040 break; 3041 3042 snprintf(evname, len + 1, "%s", p); 3043 3044 match = 0; 3045 evlist__for_each_entry(session->evlist, pos) { 3046 if (!strcmp(perf_evsel__name(pos), evname)) { 3047 match = 1; 3048 break; 3049 } 3050 } 3051 3052 if (!match) { 3053 fclose(fp); 3054 return -1; 3055 } 3056 } 3057 } 3058 3059 fclose(fp); 3060 return 0; 3061 } 3062 3063 /* 3064 * Return -1 if none is found, otherwise the actual scripts number. 3065 * 3066 * Currently the only user of this function is the script browser, which 3067 * will list all statically runnable scripts, select one, execute it and 3068 * show the output in a perf browser. 3069 */ 3070 int find_scripts(char **scripts_array, char **scripts_path_array, int num, 3071 int pathlen) 3072 { 3073 struct dirent *script_dirent, *lang_dirent; 3074 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN]; 3075 DIR *scripts_dir, *lang_dir; 3076 struct perf_session *session; 3077 struct perf_data data = { 3078 .path = input_name, 3079 .mode = PERF_DATA_MODE_READ, 3080 }; 3081 char *temp; 3082 int i = 0; 3083 3084 session = perf_session__new(&data, false, NULL); 3085 if (!session) 3086 return -1; 3087 3088 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path()); 3089 3090 scripts_dir = opendir(scripts_path); 3091 if (!scripts_dir) { 3092 perf_session__delete(session); 3093 return -1; 3094 } 3095 3096 for_each_lang(scripts_path, scripts_dir, lang_dirent) { 3097 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path, 3098 lang_dirent->d_name); 3099 #ifndef HAVE_LIBPERL_SUPPORT 3100 if (strstr(lang_path, "perl")) 3101 continue; 3102 #endif 3103 #ifndef HAVE_LIBPYTHON_SUPPORT 3104 if (strstr(lang_path, "python")) 3105 continue; 3106 #endif 3107 3108 lang_dir = opendir(lang_path); 3109 if (!lang_dir) 3110 continue; 3111 3112 for_each_script(lang_path, lang_dir, script_dirent) { 3113 /* Skip those real time scripts: xxxtop.p[yl] */ 3114 if (strstr(script_dirent->d_name, "top.")) 3115 continue; 3116 if (i >= num) 3117 break; 3118 snprintf(scripts_path_array[i], pathlen, "%s/%s", 3119 lang_path, 3120 script_dirent->d_name); 3121 temp = strchr(script_dirent->d_name, '.'); 3122 snprintf(scripts_array[i], 3123 (temp - script_dirent->d_name) + 1, 3124 "%s", script_dirent->d_name); 3125 3126 if (check_ev_match(lang_path, 3127 scripts_array[i], session)) 3128 continue; 3129 3130 i++; 3131 } 3132 closedir(lang_dir); 3133 } 3134 3135 closedir(scripts_dir); 3136 perf_session__delete(session); 3137 return i; 3138 } 3139 3140 static char *get_script_path(const char *script_root, const char *suffix) 3141 { 3142 struct dirent *script_dirent, *lang_dirent; 3143 char scripts_path[MAXPATHLEN]; 3144 char script_path[MAXPATHLEN]; 3145 DIR *scripts_dir, *lang_dir; 3146 char lang_path[MAXPATHLEN]; 3147 char *__script_root; 3148 3149 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path()); 3150 3151 scripts_dir = opendir(scripts_path); 3152 if (!scripts_dir) 3153 return NULL; 3154 3155 for_each_lang(scripts_path, scripts_dir, lang_dirent) { 3156 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, 3157 lang_dirent->d_name); 3158 lang_dir = opendir(lang_path); 3159 if (!lang_dir) 3160 continue; 3161 3162 for_each_script(lang_path, lang_dir, script_dirent) { 3163 __script_root = get_script_root(script_dirent, suffix); 3164 if (__script_root && !strcmp(script_root, __script_root)) { 3165 free(__script_root); 3166 closedir(lang_dir); 3167 closedir(scripts_dir); 3168 scnprintf(script_path, MAXPATHLEN, "%s/%s", 3169 lang_path, script_dirent->d_name); 3170 return strdup(script_path); 3171 } 3172 free(__script_root); 3173 } 3174 closedir(lang_dir); 3175 } 3176 closedir(scripts_dir); 3177 3178 return NULL; 3179 } 3180 3181 static bool is_top_script(const char *script_path) 3182 { 3183 return ends_with(script_path, "top") == NULL ? false : true; 3184 } 3185 3186 static int has_required_arg(char *script_path) 3187 { 3188 struct script_desc *desc; 3189 int n_args = 0; 3190 char *p; 3191 3192 desc = script_desc__new(NULL); 3193 3194 if (read_script_info(desc, script_path)) 3195 goto out; 3196 3197 if (!desc->args) 3198 goto out; 3199 3200 for (p = desc->args; *p; p++) 3201 if (*p == '<') 3202 n_args++; 3203 out: 3204 script_desc__delete(desc); 3205 3206 return n_args; 3207 } 3208 3209 static int have_cmd(int argc, const char **argv) 3210 { 3211 char **__argv = malloc(sizeof(const char *) * argc); 3212 3213 if (!__argv) { 3214 pr_err("malloc failed\n"); 3215 return -1; 3216 } 3217 3218 memcpy(__argv, argv, sizeof(const char *) * argc); 3219 argc = parse_options(argc, (const char **)__argv, record_options, 3220 NULL, PARSE_OPT_STOP_AT_NON_OPTION); 3221 free(__argv); 3222 3223 system_wide = (argc == 0); 3224 3225 return 0; 3226 } 3227 3228 static void script__setup_sample_type(struct perf_script *script) 3229 { 3230 struct perf_session *session = script->session; 3231 u64 sample_type = perf_evlist__combined_sample_type(session->evlist); 3232 3233 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) { 3234 if ((sample_type & PERF_SAMPLE_REGS_USER) && 3235 (sample_type & PERF_SAMPLE_STACK_USER)) { 3236 callchain_param.record_mode = CALLCHAIN_DWARF; 3237 dwarf_callchain_users = true; 3238 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK) 3239 callchain_param.record_mode = CALLCHAIN_LBR; 3240 else 3241 callchain_param.record_mode = CALLCHAIN_FP; 3242 } 3243 } 3244 3245 static int process_stat_round_event(struct perf_session *session, 3246 union perf_event *event) 3247 { 3248 struct perf_record_stat_round *round = &event->stat_round; 3249 struct evsel *counter; 3250 3251 evlist__for_each_entry(session->evlist, counter) { 3252 perf_stat_process_counter(&stat_config, counter); 3253 process_stat(counter, round->time); 3254 } 3255 3256 process_stat_interval(round->time); 3257 return 0; 3258 } 3259 3260 static int process_stat_config_event(struct perf_session *session __maybe_unused, 3261 union perf_event *event) 3262 { 3263 perf_event__read_stat_config(&stat_config, &event->stat_config); 3264 return 0; 3265 } 3266 3267 static int set_maps(struct perf_script *script) 3268 { 3269 struct evlist *evlist = script->session->evlist; 3270 3271 if (!script->cpus || !script->threads) 3272 return 0; 3273 3274 if (WARN_ONCE(script->allocated, "stats double allocation\n")) 3275 return -EINVAL; 3276 3277 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads); 3278 3279 if (perf_evlist__alloc_stats(evlist, true)) 3280 return -ENOMEM; 3281 3282 script->allocated = true; 3283 return 0; 3284 } 3285 3286 static 3287 int process_thread_map_event(struct perf_session *session, 3288 union perf_event *event) 3289 { 3290 struct perf_tool *tool = session->tool; 3291 struct perf_script *script = container_of(tool, struct perf_script, tool); 3292 3293 if (script->threads) { 3294 pr_warning("Extra thread map event, ignoring.\n"); 3295 return 0; 3296 } 3297 3298 script->threads = thread_map__new_event(&event->thread_map); 3299 if (!script->threads) 3300 return -ENOMEM; 3301 3302 return set_maps(script); 3303 } 3304 3305 static 3306 int process_cpu_map_event(struct perf_session *session, 3307 union perf_event *event) 3308 { 3309 struct perf_tool *tool = session->tool; 3310 struct perf_script *script = container_of(tool, struct perf_script, tool); 3311 3312 if (script->cpus) { 3313 pr_warning("Extra cpu map event, ignoring.\n"); 3314 return 0; 3315 } 3316 3317 script->cpus = cpu_map__new_data(&event->cpu_map.data); 3318 if (!script->cpus) 3319 return -ENOMEM; 3320 3321 return set_maps(script); 3322 } 3323 3324 static int process_feature_event(struct perf_session *session, 3325 union perf_event *event) 3326 { 3327 if (event->feat.feat_id < HEADER_LAST_FEATURE) 3328 return perf_event__process_feature(session, event); 3329 return 0; 3330 } 3331 3332 #ifdef HAVE_AUXTRACE_SUPPORT 3333 static int perf_script__process_auxtrace_info(struct perf_session *session, 3334 union perf_event *event) 3335 { 3336 struct perf_tool *tool = session->tool; 3337 3338 int ret = perf_event__process_auxtrace_info(session, event); 3339 3340 if (ret == 0) { 3341 struct perf_script *script = container_of(tool, struct perf_script, tool); 3342 3343 ret = perf_script__setup_per_event_dump(script); 3344 } 3345 3346 return ret; 3347 } 3348 #else 3349 #define perf_script__process_auxtrace_info 0 3350 #endif 3351 3352 static int parse_insn_trace(const struct option *opt __maybe_unused, 3353 const char *str __maybe_unused, 3354 int unset __maybe_unused) 3355 { 3356 parse_output_fields(NULL, "+insn,-event,-period", 0); 3357 itrace_parse_synth_opts(opt, "i0ns", 0); 3358 symbol_conf.nanosecs = true; 3359 return 0; 3360 } 3361 3362 static int parse_xed(const struct option *opt __maybe_unused, 3363 const char *str __maybe_unused, 3364 int unset __maybe_unused) 3365 { 3366 force_pager("xed -F insn: -A -64 | less"); 3367 return 0; 3368 } 3369 3370 static int parse_call_trace(const struct option *opt __maybe_unused, 3371 const char *str __maybe_unused, 3372 int unset __maybe_unused) 3373 { 3374 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0); 3375 itrace_parse_synth_opts(opt, "cewp", 0); 3376 symbol_conf.nanosecs = true; 3377 symbol_conf.pad_output_len_dso = 50; 3378 return 0; 3379 } 3380 3381 static int parse_callret_trace(const struct option *opt __maybe_unused, 3382 const char *str __maybe_unused, 3383 int unset __maybe_unused) 3384 { 3385 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0); 3386 itrace_parse_synth_opts(opt, "crewp", 0); 3387 symbol_conf.nanosecs = true; 3388 return 0; 3389 } 3390 3391 int cmd_script(int argc, const char **argv) 3392 { 3393 bool show_full_info = false; 3394 bool header = false; 3395 bool header_only = false; 3396 bool script_started = false; 3397 char *rec_script_path = NULL; 3398 char *rep_script_path = NULL; 3399 struct perf_session *session; 3400 struct itrace_synth_opts itrace_synth_opts = { 3401 .set = false, 3402 .default_no_sample = true, 3403 }; 3404 struct utsname uts; 3405 char *script_path = NULL; 3406 const char **__argv; 3407 int i, j, err = 0; 3408 struct perf_script script = { 3409 .tool = { 3410 .sample = process_sample_event, 3411 .mmap = perf_event__process_mmap, 3412 .mmap2 = perf_event__process_mmap2, 3413 .comm = perf_event__process_comm, 3414 .namespaces = perf_event__process_namespaces, 3415 .exit = perf_event__process_exit, 3416 .fork = perf_event__process_fork, 3417 .attr = process_attr, 3418 .event_update = perf_event__process_event_update, 3419 .tracing_data = perf_event__process_tracing_data, 3420 .feature = process_feature_event, 3421 .build_id = perf_event__process_build_id, 3422 .id_index = perf_event__process_id_index, 3423 .auxtrace_info = perf_script__process_auxtrace_info, 3424 .auxtrace = perf_event__process_auxtrace, 3425 .auxtrace_error = perf_event__process_auxtrace_error, 3426 .stat = perf_event__process_stat_event, 3427 .stat_round = process_stat_round_event, 3428 .stat_config = process_stat_config_event, 3429 .thread_map = process_thread_map_event, 3430 .cpu_map = process_cpu_map_event, 3431 .ordered_events = true, 3432 .ordering_requires_timestamps = true, 3433 }, 3434 }; 3435 struct perf_data data = { 3436 .mode = PERF_DATA_MODE_READ, 3437 }; 3438 const struct option options[] = { 3439 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 3440 "dump raw trace in ASCII"), 3441 OPT_INCR('v', "verbose", &verbose, 3442 "be more verbose (show symbol address, etc)"), 3443 OPT_BOOLEAN('L', "Latency", &latency_format, 3444 "show latency attributes (irqs/preemption disabled, etc)"), 3445 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", 3446 list_available_scripts), 3447 OPT_CALLBACK('s', "script", NULL, "name", 3448 "script file name (lang:script name, script name, or *)", 3449 parse_scriptname), 3450 OPT_STRING('g', "gen-script", &generate_script_lang, "lang", 3451 "generate perf-script.xx script in specified language"), 3452 OPT_STRING('i', "input", &input_name, "file", "input file name"), 3453 OPT_BOOLEAN('d', "debug-mode", &debug_mode, 3454 "do various checks like samples ordering and lost events"), 3455 OPT_BOOLEAN(0, "header", &header, "Show data header."), 3456 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."), 3457 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, 3458 "file", "vmlinux pathname"), 3459 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, 3460 "file", "kallsyms pathname"), 3461 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain, 3462 "When printing symbols do not display call chain"), 3463 OPT_CALLBACK(0, "symfs", NULL, "directory", 3464 "Look for files with symbols relative to this directory", 3465 symbol__config_symfs), 3466 OPT_CALLBACK('F', "fields", NULL, "str", 3467 "comma separated output fields prepend with 'type:'. " 3468 "+field to add and -field to remove." 3469 "Valid types: hw,sw,trace,raw,synth. " 3470 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso," 3471 "addr,symoff,srcline,period,iregs,uregs,brstack," 3472 "brstacksym,flags,bpf-output,brstackinsn,brstackoff," 3473 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc", 3474 parse_output_fields), 3475 OPT_BOOLEAN('a', "all-cpus", &system_wide, 3476 "system-wide collection from all CPUs"), 3477 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", 3478 "only consider these symbols"), 3479 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL, 3480 "Decode instructions from itrace", parse_insn_trace), 3481 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL, 3482 "Run xed disassembler on output", parse_xed), 3483 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL, 3484 "Decode calls from from itrace", parse_call_trace), 3485 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL, 3486 "Decode calls and returns from itrace", parse_callret_trace), 3487 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]", 3488 "Only print symbols and callees with --call-trace/--call-ret-trace"), 3489 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]", 3490 "Stop display of callgraph at these symbols"), 3491 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"), 3492 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", 3493 "only display events for these comms"), 3494 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]", 3495 "only consider symbols in these pids"), 3496 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]", 3497 "only consider symbols in these tids"), 3498 OPT_UINTEGER(0, "max-stack", &scripting_max_stack, 3499 "Set the maximum stack depth when parsing the callchain, " 3500 "anything beyond the specified depth will be ignored. " 3501 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)), 3502 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"), 3503 OPT_BOOLEAN('I', "show-info", &show_full_info, 3504 "display extended information from perf.data file"), 3505 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path, 3506 "Show the path of [kernel.kallsyms]"), 3507 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events, 3508 "Show the fork/comm/exit events"), 3509 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events, 3510 "Show the mmap events"), 3511 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events, 3512 "Show context switch events (if recorded)"), 3513 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events, 3514 "Show namespace events (if recorded)"), 3515 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events, 3516 "Show lost events (if recorded)"), 3517 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events, 3518 "Show round events (if recorded)"), 3519 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events, 3520 "Show bpf related events (if recorded)"), 3521 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump, 3522 "Dump trace output to files named by the monitored events"), 3523 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"), 3524 OPT_INTEGER(0, "max-blocks", &max_blocks, 3525 "Maximum number of code blocks to dump with brstackinsn"), 3526 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs, 3527 "Use 9 decimal places when displaying time"), 3528 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts", 3529 "Instruction Tracing options\n" ITRACE_HELP, 3530 itrace_parse_synth_opts), 3531 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename, 3532 "Show full source file name path for source lines"), 3533 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle, 3534 "Enable symbol demangling"), 3535 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel, 3536 "Enable kernel symbol demangling"), 3537 OPT_STRING(0, "time", &script.time_str, "str", 3538 "Time span of interest (start,stop)"), 3539 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name, 3540 "Show inline function"), 3541 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory", 3542 "guest mount directory under which every guest os" 3543 " instance has a subdir"), 3544 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name, 3545 "file", "file saving guest os vmlinux"), 3546 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms, 3547 "file", "file saving guest os /proc/kallsyms"), 3548 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules, 3549 "file", "file saving guest os /proc/modules"), 3550 OPTS_EVSWITCH(&script.evswitch), 3551 OPT_END() 3552 }; 3553 const char * const script_subcommands[] = { "record", "report", NULL }; 3554 const char *script_usage[] = { 3555 "perf script [<options>]", 3556 "perf script [<options>] record <script> [<record-options>] <command>", 3557 "perf script [<options>] report <script> [script-args]", 3558 "perf script [<options>] <script> [<record-options>] <command>", 3559 "perf script [<options>] <top-script> [script-args]", 3560 NULL 3561 }; 3562 3563 perf_set_singlethreaded(); 3564 3565 setup_scripting(); 3566 3567 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage, 3568 PARSE_OPT_STOP_AT_NON_OPTION); 3569 3570 if (symbol_conf.guestmount || 3571 symbol_conf.default_guest_vmlinux_name || 3572 symbol_conf.default_guest_kallsyms || 3573 symbol_conf.default_guest_modules) { 3574 /* 3575 * Enable guest sample processing. 3576 */ 3577 perf_guest = true; 3578 } 3579 3580 data.path = input_name; 3581 data.force = symbol_conf.force; 3582 3583 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { 3584 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); 3585 if (!rec_script_path) 3586 return cmd_record(argc, argv); 3587 } 3588 3589 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) { 3590 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX); 3591 if (!rep_script_path) { 3592 fprintf(stderr, 3593 "Please specify a valid report script" 3594 "(see 'perf script -l' for listing)\n"); 3595 return -1; 3596 } 3597 } 3598 3599 if (script.time_str && reltime) { 3600 fprintf(stderr, "Don't combine --reltime with --time\n"); 3601 return -1; 3602 } 3603 3604 if (itrace_synth_opts.callchain && 3605 itrace_synth_opts.callchain_sz > scripting_max_stack) 3606 scripting_max_stack = itrace_synth_opts.callchain_sz; 3607 3608 /* make sure PERF_EXEC_PATH is set for scripts */ 3609 set_argv_exec_path(get_argv_exec_path()); 3610 3611 if (argc && !script_name && !rec_script_path && !rep_script_path) { 3612 int live_pipe[2]; 3613 int rep_args; 3614 pid_t pid; 3615 3616 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX); 3617 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX); 3618 3619 if (!rec_script_path && !rep_script_path) { 3620 usage_with_options_msg(script_usage, options, 3621 "Couldn't find script `%s'\n\n See perf" 3622 " script -l for available scripts.\n", argv[0]); 3623 } 3624 3625 if (is_top_script(argv[0])) { 3626 rep_args = argc - 1; 3627 } else { 3628 int rec_args; 3629 3630 rep_args = has_required_arg(rep_script_path); 3631 rec_args = (argc - 1) - rep_args; 3632 if (rec_args < 0) { 3633 usage_with_options_msg(script_usage, options, 3634 "`%s' script requires options." 3635 "\n\n See perf script -l for available " 3636 "scripts and options.\n", argv[0]); 3637 } 3638 } 3639 3640 if (pipe(live_pipe) < 0) { 3641 perror("failed to create pipe"); 3642 return -1; 3643 } 3644 3645 pid = fork(); 3646 if (pid < 0) { 3647 perror("failed to fork"); 3648 return -1; 3649 } 3650 3651 if (!pid) { 3652 j = 0; 3653 3654 dup2(live_pipe[1], 1); 3655 close(live_pipe[0]); 3656 3657 if (is_top_script(argv[0])) { 3658 system_wide = true; 3659 } else if (!system_wide) { 3660 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) { 3661 err = -1; 3662 goto out; 3663 } 3664 } 3665 3666 __argv = malloc((argc + 6) * sizeof(const char *)); 3667 if (!__argv) { 3668 pr_err("malloc failed\n"); 3669 err = -ENOMEM; 3670 goto out; 3671 } 3672 3673 __argv[j++] = "/bin/sh"; 3674 __argv[j++] = rec_script_path; 3675 if (system_wide) 3676 __argv[j++] = "-a"; 3677 __argv[j++] = "-q"; 3678 __argv[j++] = "-o"; 3679 __argv[j++] = "-"; 3680 for (i = rep_args + 1; i < argc; i++) 3681 __argv[j++] = argv[i]; 3682 __argv[j++] = NULL; 3683 3684 execvp("/bin/sh", (char **)__argv); 3685 free(__argv); 3686 exit(-1); 3687 } 3688 3689 dup2(live_pipe[0], 0); 3690 close(live_pipe[1]); 3691 3692 __argv = malloc((argc + 4) * sizeof(const char *)); 3693 if (!__argv) { 3694 pr_err("malloc failed\n"); 3695 err = -ENOMEM; 3696 goto out; 3697 } 3698 3699 j = 0; 3700 __argv[j++] = "/bin/sh"; 3701 __argv[j++] = rep_script_path; 3702 for (i = 1; i < rep_args + 1; i++) 3703 __argv[j++] = argv[i]; 3704 __argv[j++] = "-i"; 3705 __argv[j++] = "-"; 3706 __argv[j++] = NULL; 3707 3708 execvp("/bin/sh", (char **)__argv); 3709 free(__argv); 3710 exit(-1); 3711 } 3712 3713 if (rec_script_path) 3714 script_path = rec_script_path; 3715 if (rep_script_path) 3716 script_path = rep_script_path; 3717 3718 if (script_path) { 3719 j = 0; 3720 3721 if (!rec_script_path) 3722 system_wide = false; 3723 else if (!system_wide) { 3724 if (have_cmd(argc - 1, &argv[1]) != 0) { 3725 err = -1; 3726 goto out; 3727 } 3728 } 3729 3730 __argv = malloc((argc + 2) * sizeof(const char *)); 3731 if (!__argv) { 3732 pr_err("malloc failed\n"); 3733 err = -ENOMEM; 3734 goto out; 3735 } 3736 3737 __argv[j++] = "/bin/sh"; 3738 __argv[j++] = script_path; 3739 if (system_wide) 3740 __argv[j++] = "-a"; 3741 for (i = 2; i < argc; i++) 3742 __argv[j++] = argv[i]; 3743 __argv[j++] = NULL; 3744 3745 execvp("/bin/sh", (char **)__argv); 3746 free(__argv); 3747 exit(-1); 3748 } 3749 3750 if (!script_name) { 3751 setup_pager(); 3752 use_browser = 0; 3753 } 3754 3755 session = perf_session__new(&data, false, &script.tool); 3756 if (session == NULL) 3757 return -1; 3758 3759 if (header || header_only) { 3760 script.tool.show_feat_hdr = SHOW_FEAT_HEADER; 3761 perf_session__fprintf_info(session, stdout, show_full_info); 3762 if (header_only) 3763 goto out_delete; 3764 } 3765 if (show_full_info) 3766 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO; 3767 3768 if (symbol__init(&session->header.env) < 0) 3769 goto out_delete; 3770 3771 uname(&uts); 3772 if (data.is_pipe || /* assume pipe_mode indicates native_arch */ 3773 !strcmp(uts.machine, session->header.env.arch) || 3774 (!strcmp(uts.machine, "x86_64") && 3775 !strcmp(session->header.env.arch, "i386"))) 3776 native_arch = true; 3777 3778 script.session = session; 3779 script__setup_sample_type(&script); 3780 3781 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) || 3782 symbol_conf.graph_function) 3783 itrace_synth_opts.thread_stack = true; 3784 3785 session->itrace_synth_opts = &itrace_synth_opts; 3786 3787 if (cpu_list) { 3788 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap); 3789 if (err < 0) 3790 goto out_delete; 3791 itrace_synth_opts.cpu_bitmap = cpu_bitmap; 3792 } 3793 3794 if (!no_callchain) 3795 symbol_conf.use_callchain = true; 3796 else 3797 symbol_conf.use_callchain = false; 3798 3799 if (session->tevent.pevent && 3800 tep_set_function_resolver(session->tevent.pevent, 3801 machine__resolve_kernel_addr, 3802 &session->machines.host) < 0) { 3803 pr_err("%s: failed to set libtraceevent function resolver\n", __func__); 3804 err = -1; 3805 goto out_delete; 3806 } 3807 3808 if (generate_script_lang) { 3809 struct stat perf_stat; 3810 int input; 3811 3812 if (output_set_by_user()) { 3813 fprintf(stderr, 3814 "custom fields not supported for generated scripts"); 3815 err = -EINVAL; 3816 goto out_delete; 3817 } 3818 3819 input = open(data.path, O_RDONLY); /* input_name */ 3820 if (input < 0) { 3821 err = -errno; 3822 perror("failed to open file"); 3823 goto out_delete; 3824 } 3825 3826 err = fstat(input, &perf_stat); 3827 if (err < 0) { 3828 perror("failed to stat file"); 3829 goto out_delete; 3830 } 3831 3832 if (!perf_stat.st_size) { 3833 fprintf(stderr, "zero-sized file, nothing to do!\n"); 3834 goto out_delete; 3835 } 3836 3837 scripting_ops = script_spec__lookup(generate_script_lang); 3838 if (!scripting_ops) { 3839 fprintf(stderr, "invalid language specifier"); 3840 err = -ENOENT; 3841 goto out_delete; 3842 } 3843 3844 err = scripting_ops->generate_script(session->tevent.pevent, 3845 "perf-script"); 3846 goto out_delete; 3847 } 3848 3849 if (script_name) { 3850 err = scripting_ops->start_script(script_name, argc, argv); 3851 if (err) 3852 goto out_delete; 3853 pr_debug("perf script started with script %s\n\n", script_name); 3854 script_started = true; 3855 } 3856 3857 3858 err = perf_session__check_output_opt(session); 3859 if (err < 0) 3860 goto out_delete; 3861 3862 if (script.time_str) { 3863 err = perf_time__parse_for_ranges(script.time_str, session, 3864 &script.ptime_range, 3865 &script.range_size, 3866 &script.range_num); 3867 if (err < 0) 3868 goto out_delete; 3869 3870 itrace_synth_opts__set_time_range(&itrace_synth_opts, 3871 script.ptime_range, 3872 script.range_num); 3873 } 3874 3875 err = evswitch__init(&script.evswitch, session->evlist, stderr); 3876 if (err) 3877 goto out_delete; 3878 3879 err = __cmd_script(&script); 3880 3881 flush_scripting(); 3882 3883 out_delete: 3884 if (script.ptime_range) { 3885 itrace_synth_opts__clear_time_range(&itrace_synth_opts); 3886 zfree(&script.ptime_range); 3887 } 3888 3889 perf_evlist__free_stats(session->evlist); 3890 perf_session__delete(session); 3891 3892 if (script_started) 3893 cleanup_scripting(); 3894 out: 3895 return err; 3896 } 3897