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