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