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