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