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