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