blob: aaf2922643a0d21f5562b9934829ba3a92150e9e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002#include "builtin.h"
3
Arnaldo Carvalho de Melo0f31c012019-08-21 14:00:17 -03004#include "util/counts.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02005#include "util/debug.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -03006#include "util/dso.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06007#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02008#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06009#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +020010#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010013#include "util/map.h"
Arnaldo Carvalho de Melo97b9d862019-08-22 17:10:08 -030014#include "util/srcline.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020015#include "util/symbol.h"
16#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010017#include "util/trace-event.h"
David Ahern1424dc92011-03-09 22:23:28 -070018#include "util/evlist.h"
19#include "util/evsel.h"
Arnaldo Carvalho de Meloca125272019-09-24 15:41:51 -030020#include "util/evsel_fprintf.h"
Arnaldo Carvalho de Melod2360442019-08-15 10:37:24 -030021#include "util/evswitch.h"
Feng Tang36385be2012-09-07 16:42:24 +080022#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020023#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030024#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010025#include "util/cpumap.h"
26#include "util/thread_map.h"
27#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080028#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030029#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030030#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070031#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010032#include "util/path.h"
Kan Liang6b9bae62020-12-16 10:57:57 -080033#include "util/event.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030034#include "ui/ui.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030035#include "print_binary.h"
Andi Kleen3ab481a2019-03-05 06:47:45 -080036#include "archinsn.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100037#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030038#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030039#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030040#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030041#include <linux/zalloc.h>
Andi Kleen3ab481a2019-03-05 06:47:45 -080042#include <sys/utsname.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010043#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010044#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080045#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030046#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030047#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030048#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030049#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030050#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030051#include <sys/types.h>
52#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030053#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030054#include <unistd.h>
Andi Kleenb585ebd2018-09-20 11:05:36 -070055#include <subcmd/pager.h>
Jiri Olsa453fa032019-07-21 13:24:43 +020056#include <perf/evlist.h>
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053057#include <linux/err.h>
Adrian Hunter291961f2021-06-27 16:18:09 +030058#include "util/dlfilter.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030059#include "util/record.h"
Arnaldo Carvalho de Melo2da39f12019-08-27 11:51:18 -030060#include "util/util.h"
Arnaldo Carvalho de Meloc1a604d2019-08-29 15:20:59 -030061#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020062
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030063#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030064
Tom Zanussi956ffd02009-11-25 01:15:46 -060065static char const *script_name;
66static char const *generate_script_lang;
Andi Kleen90b10f42019-03-14 15:50:00 -070067static bool reltime;
Hagen Paul Pfeifer26567ed2020-02-04 18:37:09 +010068static bool deltatime;
Andi Kleen90b10f42019-03-14 15:50:00 -070069static u64 initial_time;
Hagen Paul Pfeifer26567ed2020-02-04 18:37:09 +010070static u64 previous_time;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020071static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020072static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020073static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070074static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090075static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010076static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030077static bool print_flags;
Anton Blanchard5d67be92011-07-04 21:57:50 +100078static const char *cpu_list;
79static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010080static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080081static int max_blocks;
Andi Kleen3ab481a2019-03-05 06:47:45 -080082static bool native_arch;
Adrian Hunter291961f2021-06-27 16:18:09 +030083static struct dlfilter *dlfilter;
Tom Zanussi956ffd02009-11-25 01:15:46 -060084
Adrian Hunter44cbe722015-09-25 16:15:50 +030085unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030086
David Ahern745f43e2011-03-09 22:23:26 -070087enum perf_output_field {
Jiri Olsa60e5eeb2020-08-05 11:34:43 +020088 PERF_OUTPUT_COMM = 1ULL << 0,
89 PERF_OUTPUT_TID = 1ULL << 1,
90 PERF_OUTPUT_PID = 1ULL << 2,
91 PERF_OUTPUT_TIME = 1ULL << 3,
92 PERF_OUTPUT_CPU = 1ULL << 4,
93 PERF_OUTPUT_EVNAME = 1ULL << 5,
94 PERF_OUTPUT_TRACE = 1ULL << 6,
95 PERF_OUTPUT_IP = 1ULL << 7,
96 PERF_OUTPUT_SYM = 1ULL << 8,
97 PERF_OUTPUT_DSO = 1ULL << 9,
98 PERF_OUTPUT_ADDR = 1ULL << 10,
99 PERF_OUTPUT_SYMOFFSET = 1ULL << 11,
100 PERF_OUTPUT_SRCLINE = 1ULL << 12,
101 PERF_OUTPUT_PERIOD = 1ULL << 13,
102 PERF_OUTPUT_IREGS = 1ULL << 14,
103 PERF_OUTPUT_BRSTACK = 1ULL << 15,
104 PERF_OUTPUT_BRSTACKSYM = 1ULL << 16,
105 PERF_OUTPUT_DATA_SRC = 1ULL << 17,
106 PERF_OUTPUT_WEIGHT = 1ULL << 18,
107 PERF_OUTPUT_BPF_OUTPUT = 1ULL << 19,
108 PERF_OUTPUT_CALLINDENT = 1ULL << 20,
109 PERF_OUTPUT_INSN = 1ULL << 21,
110 PERF_OUTPUT_INSNLEN = 1ULL << 22,
111 PERF_OUTPUT_BRSTACKINSN = 1ULL << 23,
112 PERF_OUTPUT_BRSTACKOFF = 1ULL << 24,
113 PERF_OUTPUT_SYNTH = 1ULL << 25,
114 PERF_OUTPUT_PHYS_ADDR = 1ULL << 26,
115 PERF_OUTPUT_UREGS = 1ULL << 27,
116 PERF_OUTPUT_METRIC = 1ULL << 28,
117 PERF_OUTPUT_MISC = 1ULL << 29,
118 PERF_OUTPUT_SRCCODE = 1ULL << 30,
119 PERF_OUTPUT_IPC = 1ULL << 31,
Jiri Olsae534bfb2020-08-05 11:34:44 +0200120 PERF_OUTPUT_TOD = 1ULL << 32,
Kan Liang6b9bae62020-12-16 10:57:57 -0800121 PERF_OUTPUT_DATA_PAGE_SIZE = 1ULL << 33,
Stephane Eranianc513de82021-01-05 11:57:50 -0800122 PERF_OUTPUT_CODE_PAGE_SIZE = 1ULL << 34,
Jiri Olsae534bfb2020-08-05 11:34:44 +0200123};
124
125struct perf_script {
126 struct perf_tool tool;
127 struct perf_session *session;
128 bool show_task_events;
129 bool show_mmap_events;
130 bool show_switch_events;
131 bool show_namespace_events;
132 bool show_lost_events;
133 bool show_round_events;
134 bool show_bpf_events;
135 bool show_cgroup_events;
136 bool show_text_poke_events;
137 bool allocated;
138 bool per_event_dump;
139 bool stitch_lbr;
140 struct evswitch evswitch;
141 struct perf_cpu_map *cpus;
142 struct perf_thread_map *threads;
143 int name_width;
144 const char *time_str;
145 struct perf_time_interval *ptime_range;
146 int range_size;
147 int range_num;
David Ahern745f43e2011-03-09 22:23:26 -0700148};
149
150struct output_option {
151 const char *str;
152 enum perf_output_field field;
153} all_output_options[] = {
154 {.str = "comm", .field = PERF_OUTPUT_COMM},
155 {.str = "tid", .field = PERF_OUTPUT_TID},
156 {.str = "pid", .field = PERF_OUTPUT_PID},
157 {.str = "time", .field = PERF_OUTPUT_TIME},
158 {.str = "cpu", .field = PERF_OUTPUT_CPU},
159 {.str = "event", .field = PERF_OUTPUT_EVNAME},
160 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600161 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700162 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600163 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600164 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900165 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200166 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200167 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200168 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700169 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200170 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
171 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100172 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
173 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000174 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300175 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300176 {.str = "insn", .field = PERF_OUTPUT_INSN},
177 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800178 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700179 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300180 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400181 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800182 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100183 {.str = "misc", .field = PERF_OUTPUT_MISC},
Andi Kleendd2e18e2018-12-03 16:18:48 -0800184 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300185 {.str = "ipc", .field = PERF_OUTPUT_IPC},
Jiri Olsae534bfb2020-08-05 11:34:44 +0200186 {.str = "tod", .field = PERF_OUTPUT_TOD},
Kan Liang6b9bae62020-12-16 10:57:57 -0800187 {.str = "data_page_size", .field = PERF_OUTPUT_DATA_PAGE_SIZE},
Stephane Eranianc513de82021-01-05 11:57:50 -0800188 {.str = "code_page_size", .field = PERF_OUTPUT_CODE_PAGE_SIZE},
David Ahern745f43e2011-03-09 22:23:26 -0700189};
190
Adrian Hunter14057202017-06-21 13:17:19 +0300191enum {
192 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
Jin Yao8adc0a02020-12-09 08:58:28 +0800193 OUTPUT_TYPE_OTHER,
Adrian Hunter14057202017-06-21 13:17:19 +0300194 OUTPUT_TYPE_MAX
195};
196
David Ahern745f43e2011-03-09 22:23:26 -0700197/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600198static struct {
199 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600200 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400201 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600202 u64 fields;
203 u64 invalid_fields;
Andi Kleen4b6ac812019-02-24 07:37:12 -0800204 u64 user_set_fields;
Adrian Hunterb5164082020-05-27 21:02:50 +0300205 u64 user_unset_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300206} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700207
David Ahern2c9e45f72011-03-17 10:03:21 -0600208 [PERF_TYPE_HARDWARE] = {
209 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700210
David Ahern2c9e45f72011-03-17 10:03:21 -0600211 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
212 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600213 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530214 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
215 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600216
Wang Nan30372f02016-02-24 11:20:45 +0000217 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600218 },
219
220 [PERF_TYPE_SOFTWARE] = {
221 .user_set = false,
222
223 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
224 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600225 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530226 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
227 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
228 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600229
230 .invalid_fields = PERF_OUTPUT_TRACE,
231 },
232
233 [PERF_TYPE_TRACEPOINT] = {
234 .user_set = false,
235
236 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
237 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000238 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600239 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700240
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530241 [PERF_TYPE_HW_CACHE] = {
242 .user_set = false,
243
244 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
245 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
246 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
247 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
248 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
249
250 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
251 },
252
Arun Sharma0817a6a2011-04-14 10:38:18 -0700253 [PERF_TYPE_RAW] = {
254 .user_set = false,
255
256 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
257 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600258 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530259 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
260 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
261 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
Kan Liang6b9bae62020-12-16 10:57:57 -0800262 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR |
Stephane Eranianc513de82021-01-05 11:57:50 -0800263 PERF_OUTPUT_DATA_PAGE_SIZE | PERF_OUTPUT_CODE_PAGE_SIZE,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700264
Wang Nan30372f02016-02-24 11:20:45 +0000265 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700266 },
Wang Nan27cfef02015-12-08 02:25:43 +0000267
268 [PERF_TYPE_BREAKPOINT] = {
269 .user_set = false,
270
271 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
272 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
273 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530274 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
275 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000276
Wang Nan30372f02016-02-24 11:20:45 +0000277 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000278 },
Adrian Hunter14057202017-06-21 13:17:19 +0300279
280 [OUTPUT_TYPE_SYNTH] = {
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 |
Sandipan Das7903a702018-05-17 12:03:26 +0530286 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
287 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300288
289 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
290 },
Jin Yao8adc0a02020-12-09 08:58:28 +0800291
292 [OUTPUT_TYPE_OTHER] = {
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_PERIOD,
300
301 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
302 },
David Ahern1424dc92011-03-09 22:23:28 -0700303};
David Ahern745f43e2011-03-09 22:23:26 -0700304
Jiri Olsa32dcd022019-07-21 13:23:51 +0200305struct evsel_script {
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300306 char *filename;
307 FILE *fp;
308 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800309 /* For metric output */
310 u64 val;
311 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300312};
313
Jiri Olsa32dcd022019-07-21 13:23:51 +0200314static inline struct evsel_script *evsel_script(struct evsel *evsel)
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800315{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200316 return (struct evsel_script *)evsel->priv;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800317}
318
Arnaldo Carvalho de Melo297e69b2021-03-09 08:59:21 -0300319static struct evsel_script *evsel_script__new(struct evsel *evsel, struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300320{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200321 struct evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300322
323 if (es != NULL) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300324 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300325 goto out_free;
326 es->fp = fopen(es->filename, "w");
327 if (es->fp == NULL)
328 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300329 }
330
331 return es;
332out_free_filename:
333 zfree(&es->filename);
334out_free:
335 free(es);
336 return NULL;
337}
338
Arnaldo Carvalho de Melo297e69b2021-03-09 08:59:21 -0300339static void evsel_script__delete(struct evsel_script *es)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300340{
341 zfree(&es->filename);
342 fclose(es->fp);
343 es->fp = NULL;
344 free(es);
345}
346
Arnaldo Carvalho de Melo297e69b2021-03-09 08:59:21 -0300347static int evsel_script__fprintf(struct evsel_script *es, FILE *fp)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300348{
349 struct stat st;
350
351 fstat(fileno(es->fp), &st);
352 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
353 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
354}
355
Adrian Hunter14057202017-06-21 13:17:19 +0300356static inline int output_type(unsigned int type)
357{
358 switch (type) {
359 case PERF_TYPE_SYNTH:
360 return OUTPUT_TYPE_SYNTH;
361 default:
Jin Yao8adc0a02020-12-09 08:58:28 +0800362 if (type < PERF_TYPE_MAX)
363 return type;
Adrian Hunter14057202017-06-21 13:17:19 +0300364 }
Jin Yao8adc0a02020-12-09 08:58:28 +0800365
366 return OUTPUT_TYPE_OTHER;
Adrian Hunter14057202017-06-21 13:17:19 +0300367}
368
369static inline unsigned int attr_type(unsigned int type)
370{
371 switch (type) {
372 case OUTPUT_TYPE_SYNTH:
373 return PERF_TYPE_SYNTH;
374 default:
375 return type;
376 }
377}
378
David Ahern2c9e45f72011-03-17 10:03:21 -0600379static bool output_set_by_user(void)
380{
381 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300382 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600383 if (output[j].user_set)
384 return true;
385 }
386 return false;
387}
David Ahern745f43e2011-03-09 22:23:26 -0700388
David Ahern9cbdb702011-04-06 21:54:20 -0600389static const char *output_field2str(enum perf_output_field field)
390{
391 int i, imax = ARRAY_SIZE(all_output_options);
392 const char *str = "";
393
394 for (i = 0; i < imax; ++i) {
395 if (all_output_options[i].field == field) {
396 str = all_output_options[i].str;
397 break;
398 }
399 }
400 return str;
401}
402
Adrian Hunter14057202017-06-21 13:17:19 +0300403#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700404
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300405static int evsel__do_check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
406 enum perf_output_field field, bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700407{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200408 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300409 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600410 const char *evname;
411
412 if (attr->sample_type & sample_type)
413 return 0;
414
Andi Kleen4b6ac812019-02-24 07:37:12 -0800415 if (output[type].user_set_fields & field) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300416 if (allow_user_set)
417 return 0;
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300418 evname = evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600419 pr_err("Samples for '%s' event do not have %s attribute set. "
420 "Cannot print '%s' field.\n",
421 evname, sample_msg, output_field2str(field));
422 return -1;
423 }
424
425 /* user did not ask for it explicitly so remove from the default list */
426 output[type].fields &= ~field;
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300427 evname = evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600428 pr_debug("Samples for '%s' event do not have %s attribute set. "
429 "Skipping '%s' field.\n",
430 evname, sample_msg, output_field2str(field));
431
432 return 0;
433}
434
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300435static int evsel__check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
436 enum perf_output_field field)
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300437{
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300438 return evsel__do_check_stype(evsel, sample_type, sample_msg, field, false);
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300439}
440
Arnaldo Carvalho de Meloafdd63f2020-06-10 11:39:16 -0300441static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)
David Ahern9cbdb702011-04-06 21:54:20 -0600442{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200443 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300444 bool allow_user_set;
445
Jiri Olsae099eba2016-01-05 22:09:09 +0100446 if (perf_header__has_feat(&session->header, HEADER_STAT))
447 return 0;
448
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300449 allow_user_set = perf_header__has_feat(&session->header,
450 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600451
David Ahern1424dc92011-03-09 22:23:28 -0700452 if (PRINT_FIELD(TRACE) &&
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300453 !perf_session__has_traces(session, "record -R"))
David Ahern1424dc92011-03-09 22:23:28 -0700454 return -EINVAL;
455
David Ahern787bef12011-05-27 14:28:43 -0600456 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300457 if (evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700458 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700459 }
David Ahern7cec0922011-05-30 13:08:23 -0600460
461 if (PRINT_FIELD(ADDR) &&
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300462 evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600463 return -EINVAL;
464
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100465 if (PRINT_FIELD(DATA_SRC) &&
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300466 evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC", PERF_OUTPUT_DATA_SRC))
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100467 return -EINVAL;
468
469 if (PRINT_FIELD(WEIGHT) &&
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300470 evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT", PERF_OUTPUT_WEIGHT))
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100471 return -EINVAL;
472
Andi Kleen37fed3d2018-09-18 05:32:09 -0700473 if (PRINT_FIELD(SYM) &&
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300474 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600475 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700476 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600477 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600478 return -EINVAL;
479 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900480 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
481 pr_err("Display of offsets requested but symbol is not"
482 "selected.\n");
483 return -EINVAL;
484 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700485 if (PRINT_FIELD(DSO) &&
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300486 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
Andi Kleen37fed3d2018-09-18 05:32:09 -0700487 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600488 return -EINVAL;
489 }
Andi Kleendd2e18e2018-12-03 16:18:48 -0800490 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
Adrian Huntercc8fae12013-12-06 09:42:57 +0200491 pr_err("Display of source line number requested but sample IP is not\n"
492 "selected. Hence, no address to lookup the source line number.\n");
493 return -EINVAL;
494 }
Adrian Hunter0cd032d2019-11-27 11:53:21 +0200495 if (PRINT_FIELD(BRSTACKINSN) && !allow_user_set &&
Arnaldo Carvalho de Melo92c7d7c2020-06-17 09:31:25 -0300496 !(evlist__combined_branch_type(session->evlist) & PERF_SAMPLE_BRANCH_ANY)) {
Andi Kleen48d02a12017-02-23 15:46:34 -0800497 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
498 "Hint: run 'perf record -b ...'\n");
499 return -EINVAL;
500 }
David Ahern1424dc92011-03-09 22:23:28 -0700501 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300502 evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700503 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700504
505 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300506 evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700507 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700508
509 if (PRINT_FIELD(CPU) &&
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300510 evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700511 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600512
Stephane Eranianfc36f942015-08-31 18:41:10 +0200513 if (PRINT_FIELD(IREGS) &&
Adrian Hunteradd07cc2020-06-30 16:39:34 +0300514 evsel__do_check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", PERF_OUTPUT_IREGS, allow_user_set))
Stephane Eranianfc36f942015-08-31 18:41:10 +0200515 return -EINVAL;
516
Andi Kleenb1491ac2017-09-05 11:40:57 -0700517 if (PRINT_FIELD(UREGS) &&
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300518 evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS", PERF_OUTPUT_UREGS))
Andi Kleenb1491ac2017-09-05 11:40:57 -0700519 return -EINVAL;
520
Kan Liang49d58f02017-08-29 13:11:11 -0400521 if (PRINT_FIELD(PHYS_ADDR) &&
Arnaldo Carvalho de Meloec98b6d2020-05-04 13:57:01 -0300522 evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR", PERF_OUTPUT_PHYS_ADDR))
Kan Liang49d58f02017-08-29 13:11:11 -0400523 return -EINVAL;
524
Kan Liang6b9bae62020-12-16 10:57:57 -0800525 if (PRINT_FIELD(DATA_PAGE_SIZE) &&
526 evsel__check_stype(evsel, PERF_SAMPLE_DATA_PAGE_SIZE, "DATA_PAGE_SIZE", PERF_OUTPUT_DATA_PAGE_SIZE))
527 return -EINVAL;
528
Stephane Eranianc513de82021-01-05 11:57:50 -0800529 if (PRINT_FIELD(CODE_PAGE_SIZE) &&
530 evsel__check_stype(evsel, PERF_SAMPLE_CODE_PAGE_SIZE, "CODE_PAGE_SIZE", PERF_OUTPUT_CODE_PAGE_SIZE))
531 return -EINVAL;
532
David Ahern9cbdb702011-04-06 21:54:20 -0600533 return 0;
534}
535
Adrian Hunter7ea95722013-11-01 15:51:30 +0200536static void set_print_ip_opts(struct perf_event_attr *attr)
537{
Adrian Hunter14057202017-06-21 13:17:19 +0300538 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200539
540 output[type].print_ip_opts = 0;
541 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300542 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200543
544 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300545 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200546
547 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300548 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200549
550 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300551 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200552
553 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300554 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200555}
556
David Ahern9cbdb702011-04-06 21:54:20 -0600557/*
558 * verify all user requested events exist and the samples
559 * have the expected data
560 */
561static int perf_session__check_output_opt(struct perf_session *session)
562{
Jiri Olsae534bfb2020-08-05 11:34:44 +0200563 bool tod = false;
He Kuang40f20e52016-05-16 04:51:19 +0000564 unsigned int j;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200565 struct evsel *evsel;
David Ahern9cbdb702011-04-06 21:54:20 -0600566
Adrian Hunter14057202017-06-21 13:17:19 +0300567 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
568 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600569
570 /*
571 * even if fields is set to 0 (ie., show nothing) event must
572 * exist if user explicitly includes it on the command line
573 */
Adrian Hunter14057202017-06-21 13:17:19 +0300574 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
575 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600576 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300577 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600578 event_type(j));
579 return -1;
580 }
581
582 if (evsel && output[j].fields &&
Arnaldo Carvalho de Meloafdd63f2020-06-10 11:39:16 -0300583 evsel__check_attr(evsel, session))
David Ahern9cbdb702011-04-06 21:54:20 -0600584 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400585
586 if (evsel == NULL)
587 continue;
588
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200589 set_print_ip_opts(&evsel->core.attr);
Jiri Olsae534bfb2020-08-05 11:34:44 +0200590 tod |= output[j].fields & PERF_OUTPUT_TOD;
David Ahern1424dc92011-03-09 22:23:28 -0700591 }
592
Adrian Hunter98526ee2014-07-31 09:00:59 +0300593 if (!no_callchain) {
594 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000595 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300596
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300597 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000598 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300599 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300600 use_callchain = true;
601 break;
602 }
603 }
He Kuang71ac8992016-08-04 11:25:43 +0000604 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300605 symbol_conf.use_callchain = false;
606 }
607
David Ahern80b8b492013-11-19 21:07:37 -0700608 /*
609 * set default for tracepoints to print symbols only
610 * if callchains are present
611 */
612 if (symbol_conf.use_callchain &&
613 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700614 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700615
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300616 evlist__for_each_entry(session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200617 if (evsel->core.attr.type != j)
He Kuang40f20e52016-05-16 04:51:19 +0000618 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700619
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300620 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000621 output[j].fields |= PERF_OUTPUT_IP;
622 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530623 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000624 output[j].fields |= PERF_OUTPUT_DSO;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200625 set_print_ip_opts(&evsel->core.attr);
He Kuang40f20e52016-05-16 04:51:19 +0000626 goto out;
627 }
David Ahern80b8b492013-11-19 21:07:37 -0700628 }
629 }
630
Jiri Olsae534bfb2020-08-05 11:34:44 +0200631 if (tod && !session->header.env.clock.enabled) {
632 pr_err("Can't provide 'tod' time, missing clock data. "
633 "Please record with -k/--clockid option.\n");
634 return -1;
635 }
David Ahern80b8b492013-11-19 21:07:37 -0700636out:
David Ahern1424dc92011-03-09 22:23:28 -0700637 return 0;
638}
David Ahern745f43e2011-03-09 22:23:26 -0700639
Milian Wolff9add8fe2018-11-07 23:34:37 +0100640static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
Jiri Olsae534bfb2020-08-05 11:34:44 +0200641 FILE *fp)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200642{
Andi Kleenb1491ac2017-09-05 11:40:57 -0700643 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300644 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700645
646 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300647 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700648
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300649 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700650
651 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
652 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300653 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700654 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300655
656 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700657}
658
Jiri Olsae534bfb2020-08-05 11:34:44 +0200659#define DEFAULT_TOD_FMT "%F %H:%M:%S"
660
661static char*
662tod_scnprintf(struct perf_script *script, char *buf, int buflen,
663 u64 timestamp)
664{
665 u64 tod_ns, clockid_ns;
666 struct perf_env *env;
667 unsigned long nsec;
668 struct tm ltime;
669 char date[64];
670 time_t sec;
671
672 buf[0] = '\0';
673 if (buflen < 64 || !script)
674 return buf;
675
676 env = &script->session->header.env;
677 if (!env->clock.enabled) {
678 scnprintf(buf, buflen, "disabled");
679 return buf;
680 }
681
682 clockid_ns = env->clock.clockid_ns;
683 tod_ns = env->clock.tod_ns;
684
685 if (timestamp > clockid_ns)
686 tod_ns += timestamp - clockid_ns;
687 else
688 tod_ns -= clockid_ns - timestamp;
689
690 sec = (time_t) (tod_ns / NSEC_PER_SEC);
691 nsec = tod_ns - sec * NSEC_PER_SEC;
692
693 if (localtime_r(&sec, &ltime) == NULL) {
694 scnprintf(buf, buflen, "failed");
695 } else {
696 strftime(date, sizeof(date), DEFAULT_TOD_FMT, &ltime);
697
698 if (symbol_conf.nanosecs) {
699 snprintf(buf, buflen, "%s.%09lu", date, nsec);
700 } else {
701 snprintf(buf, buflen, "%s.%06lu",
702 date, nsec / NSEC_PER_USEC);
703 }
704 }
705
706 return buf;
707}
708
Milian Wolff9add8fe2018-11-07 23:34:37 +0100709static int perf_sample__fprintf_iregs(struct perf_sample *sample,
710 struct perf_event_attr *attr, FILE *fp)
711{
712 return perf_sample__fprintf_regs(&sample->intr_regs,
713 attr->sample_regs_intr, fp);
714}
715
716static int perf_sample__fprintf_uregs(struct perf_sample *sample,
717 struct perf_event_attr *attr, FILE *fp)
718{
719 return perf_sample__fprintf_regs(&sample->user_regs,
720 attr->sample_regs_user, fp);
721}
722
Jiri Olsae534bfb2020-08-05 11:34:44 +0200723static int perf_sample__fprintf_start(struct perf_script *script,
724 struct perf_sample *sample,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300725 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200726 struct evsel *evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100727 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700728{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200729 struct perf_event_attr *attr = &evsel->core.attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700730 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700731 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300732 int printed = 0;
Jiri Olsae534bfb2020-08-05 11:34:44 +0200733 char tstr[128];
David Ahernc70c94b2011-03-09 22:23:25 -0700734
David Ahern745f43e2011-03-09 22:23:26 -0700735 if (PRINT_FIELD(COMM)) {
Adrian Hunterfc183802020-09-09 11:49:22 +0300736 const char *comm = thread ? thread__comm_str(thread) : ":-1";
737
David Ahern745f43e2011-03-09 22:23:26 -0700738 if (latency_format)
Adrian Hunterfc183802020-09-09 11:49:22 +0300739 printed += fprintf(fp, "%8.8s ", comm);
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300740 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Adrian Hunterfc183802020-09-09 11:49:22 +0300741 printed += fprintf(fp, "%s ", comm);
David Ahern745f43e2011-03-09 22:23:26 -0700742 else
Adrian Hunterfc183802020-09-09 11:49:22 +0300743 printed += fprintf(fp, "%16s ", comm);
David Ahern745f43e2011-03-09 22:23:26 -0700744 }
David Ahernc70c94b2011-03-09 22:23:25 -0700745
David Ahern745f43e2011-03-09 22:23:26 -0700746 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300747 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700748 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300749 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700750 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300751 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700752
David Ahern745f43e2011-03-09 22:23:26 -0700753 if (PRINT_FIELD(CPU)) {
754 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300755 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700756 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300757 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700758 }
David Ahernc70c94b2011-03-09 22:23:25 -0700759
Jiri Olsa28a0b392018-01-07 17:03:52 +0100760 if (PRINT_FIELD(MISC)) {
761 int ret = 0;
762
763 #define has(m) \
764 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
765
766 if (has(KERNEL))
767 ret += fprintf(fp, "K");
768 if (has(USER))
769 ret += fprintf(fp, "U");
770 if (has(HYPERVISOR))
771 ret += fprintf(fp, "H");
772 if (has(GUEST_KERNEL))
773 ret += fprintf(fp, "G");
774 if (has(GUEST_USER))
775 ret += fprintf(fp, "g");
776
777 switch (type) {
778 case PERF_RECORD_MMAP:
779 case PERF_RECORD_MMAP2:
780 if (has(MMAP_DATA))
781 ret += fprintf(fp, "M");
782 break;
783 case PERF_RECORD_COMM:
784 if (has(COMM_EXEC))
785 ret += fprintf(fp, "E");
786 break;
787 case PERF_RECORD_SWITCH:
788 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300789 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100790 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300791 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
792 ret += fprintf(fp, "p");
793 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100794 default:
795 break;
796 }
797
798 #undef has
799
800 ret += fprintf(fp, "%*s", 6 - ret, " ");
801 printed += ret;
802 }
803
Jiri Olsae534bfb2020-08-05 11:34:44 +0200804 if (PRINT_FIELD(TOD)) {
805 tod_scnprintf(script, tstr, sizeof(tstr), sample->time);
806 printed += fprintf(fp, "%s ", tstr);
807 }
808
David Ahern745f43e2011-03-09 22:23:26 -0700809 if (PRINT_FIELD(TIME)) {
Andi Kleen90b10f42019-03-14 15:50:00 -0700810 u64 t = sample->time;
811 if (reltime) {
812 if (!initial_time)
813 initial_time = sample->time;
814 t = sample->time - initial_time;
Hagen Paul Pfeifer26567ed2020-02-04 18:37:09 +0100815 } else if (deltatime) {
816 if (previous_time)
817 t = sample->time - previous_time;
818 else {
819 t = 0;
820 }
821 previous_time = sample->time;
Andi Kleen90b10f42019-03-14 15:50:00 -0700822 }
823 nsecs = t;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300824 secs = nsecs / NSEC_PER_SEC;
825 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900826
Andi Kleen52bab882019-03-05 06:47:47 -0800827 if (symbol_conf.nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300828 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900829 else {
830 char sample_time[32];
Andi Kleen90b10f42019-03-14 15:50:00 -0700831 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300832 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900833 }
David Ahern745f43e2011-03-09 22:23:26 -0700834 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300835
836 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700837}
838
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200839static inline char
840mispred_str(struct branch_entry *br)
841{
842 if (!(br->flags.mispred || br->flags.predicted))
843 return '-';
844
845 return br->flags.predicted ? 'P' : 'M';
846}
847
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300848static int perf_sample__fprintf_brstack(struct perf_sample *sample,
849 struct thread *thread,
850 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200851{
852 struct branch_stack *br = sample->branch_stack;
Kan Liang42bbabe2020-02-28 08:30:00 -0800853 struct branch_entry *entries = perf_sample__branch_entries(sample);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700854 struct addr_location alf, alt;
855 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300856 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200857
858 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300859 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200860
861 for (i = 0; i < br->nr; i++) {
Kan Liang42bbabe2020-02-28 08:30:00 -0800862 from = entries[i].from;
863 to = entries[i].to;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700864
865 if (PRINT_FIELD(DSO)) {
866 memset(&alf, 0, sizeof(alf));
867 memset(&alt, 0, sizeof(alt));
Adrian Hunter692d0e62018-11-06 23:07:12 +0200868 thread__find_map_fb(thread, sample->cpumode, from, &alf);
869 thread__find_map_fb(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700870 }
871
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300872 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700873 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300874 printed += fprintf(fp, "(");
875 printed += map__fprintf_dsoname(alf.map, fp);
876 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700877 }
878
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300879 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700880 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300881 printed += fprintf(fp, "(");
882 printed += map__fprintf_dsoname(alt.map, fp);
883 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700884 }
885
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300886 printed += fprintf(fp, "/%c/%c/%c/%d ",
Kan Liang42bbabe2020-02-28 08:30:00 -0800887 mispred_str(entries + i),
888 entries[i].flags.in_tx ? 'X' : '-',
889 entries[i].flags.abort ? 'A' : '-',
890 entries[i].flags.cycles);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200891 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300892
893 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200894}
895
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300896static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
897 struct thread *thread,
898 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200899{
900 struct branch_stack *br = sample->branch_stack;
Kan Liang42bbabe2020-02-28 08:30:00 -0800901 struct branch_entry *entries = perf_sample__branch_entries(sample);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200902 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200903 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300904 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200905
906 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300907 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200908
909 for (i = 0; i < br->nr; i++) {
910
911 memset(&alf, 0, sizeof(alf));
912 memset(&alt, 0, sizeof(alt));
Kan Liang42bbabe2020-02-28 08:30:00 -0800913 from = entries[i].from;
914 to = entries[i].to;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200915
Adrian Hunter692d0e62018-11-06 23:07:12 +0200916 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
917 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200918
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300919 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700920 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300921 printed += fprintf(fp, "(");
922 printed += map__fprintf_dsoname(alf.map, fp);
923 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700924 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300925 printed += fprintf(fp, "%c", '/');
926 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700927 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300928 printed += fprintf(fp, "(");
929 printed += map__fprintf_dsoname(alt.map, fp);
930 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700931 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300932 printed += fprintf(fp, "/%c/%c/%c/%d ",
Kan Liang42bbabe2020-02-28 08:30:00 -0800933 mispred_str(entries + i),
934 entries[i].flags.in_tx ? 'X' : '-',
935 entries[i].flags.abort ? 'A' : '-',
936 entries[i].flags.cycles);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200937 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300938
939 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200940}
941
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300942static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
943 struct thread *thread,
944 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700945{
946 struct branch_stack *br = sample->branch_stack;
Kan Liang42bbabe2020-02-28 08:30:00 -0800947 struct branch_entry *entries = perf_sample__branch_entries(sample);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700948 struct addr_location alf, alt;
949 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300950 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700951
952 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300953 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700954
955 for (i = 0; i < br->nr; i++) {
956
957 memset(&alf, 0, sizeof(alf));
958 memset(&alt, 0, sizeof(alt));
Kan Liang42bbabe2020-02-28 08:30:00 -0800959 from = entries[i].from;
960 to = entries[i].to;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700961
Adrian Hunter692d0e62018-11-06 23:07:12 +0200962 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300963 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700964 from = map__map_ip(alf.map, from);
965
Adrian Hunter692d0e62018-11-06 23:07:12 +0200966 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300967 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700968 to = map__map_ip(alt.map, to);
969
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300970 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700971 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300972 printed += fprintf(fp, "(");
973 printed += map__fprintf_dsoname(alf.map, fp);
974 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700975 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300976 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700977 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300978 printed += fprintf(fp, "(");
979 printed += map__fprintf_dsoname(alt.map, fp);
980 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700981 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300982 printed += fprintf(fp, "/%c/%c/%c/%d ",
Kan Liang42bbabe2020-02-28 08:30:00 -0800983 mispred_str(entries + i),
984 entries[i].flags.in_tx ? 'X' : '-',
985 entries[i].flags.abort ? 'A' : '-',
986 entries[i].flags.cycles);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700987 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300988
989 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700990}
Andi Kleen48d02a12017-02-23 15:46:34 -0800991#define MAXBB 16384UL
992
993static int grab_bb(u8 *buffer, u64 start, u64 end,
994 struct machine *machine, struct thread *thread,
995 bool *is64bit, u8 *cpumode, bool last)
996{
997 long offset, len;
998 struct addr_location al;
999 bool kernel;
1000
1001 if (!start || !end)
1002 return 0;
1003
1004 kernel = machine__kernel_ip(machine, start);
1005 if (kernel)
1006 *cpumode = PERF_RECORD_MISC_KERNEL;
1007 else
1008 *cpumode = PERF_RECORD_MISC_USER;
1009
1010 /*
1011 * Block overlaps between kernel and user.
1012 * This can happen due to ring filtering
1013 * On Intel CPUs the entry into the kernel is filtered,
1014 * but the exit is not. Let the caller patch it up.
1015 */
1016 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -03001017 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -08001018 return -ENXIO;
1019 }
1020
1021 memset(&al, 0, sizeof(al));
1022 if (end - start > MAXBB - MAXINSN) {
1023 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -03001024 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -08001025 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -03001026 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -08001027 return 0;
1028 }
1029
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -03001030 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -03001031 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -08001032 return 0;
1033 }
1034 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -03001035 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -08001036 return 0;
1037 }
1038
1039 /* Load maps to ensure dso->is_64_bit has been updated */
1040 map__load(al.map);
1041
1042 offset = al.map->map_ip(al.map, start);
1043 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
1044 end - start + MAXINSN);
1045
1046 *is64bit = al.map->dso->is_64_bit;
1047 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -03001048 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -08001049 start, end);
1050 return len;
1051}
1052
Arnaldo Carvalho de Melo540a63e2019-11-22 12:39:06 -03001053static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struct srccode_state *state)
1054{
1055 char *srcfile;
1056 int ret = 0;
1057 unsigned line;
1058 int len;
1059 char *srccode;
1060
1061 if (!map || !map->dso)
1062 return 0;
1063 srcfile = get_srcline_split(map->dso,
1064 map__rip_2objdump(map, addr),
1065 &line);
1066 if (!srcfile)
1067 return 0;
1068
1069 /* Avoid redundant printing */
1070 if (state &&
1071 state->srcfile &&
1072 !strcmp(state->srcfile, srcfile) &&
1073 state->line == line) {
1074 free(srcfile);
1075 return 0;
1076 }
1077
1078 srccode = find_sourceline(srcfile, line, &len);
1079 if (!srccode)
1080 goto out_free_line;
1081
1082 ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
1083
1084 if (state) {
1085 state->srcfile = srcfile;
1086 state->line = line;
1087 }
1088 return ret;
1089
1090out_free_line:
1091 free(srcfile);
1092 return ret;
1093}
1094
Andi Kleendd2e18e2018-12-03 16:18:48 -08001095static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
1096{
1097 struct addr_location al;
1098 int ret = 0;
1099
1100 memset(&al, 0, sizeof(al));
1101 thread__find_map(thread, cpumode, addr, &al);
1102 if (!al.map)
1103 return 0;
1104 ret = map__fprintf_srccode(al.map, al.addr, stdout,
1105 &thread->srccode_state);
1106 if (ret)
1107 ret += printf("\n");
1108 return ret;
1109}
1110
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001111static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
1112 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -07001113 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -08001114{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001115 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
1116 dump_insn(x, ip, inbuf, len, NULL),
1117 en->flags.predicted ? " PRED" : "",
1118 en->flags.mispred ? " MISPRED" : "",
1119 en->flags.in_tx ? " INTX" : "",
1120 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -08001121 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -07001122 *total_cycles += en->flags.cycles;
1123 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -08001124 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001125 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -08001126 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001127 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -08001128}
1129
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001130static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
1131 u8 cpumode, int cpu, struct symbol **lastsym,
1132 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -08001133{
1134 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001135 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001136
1137 memset(&al, 0, sizeof(al));
1138
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -03001139 thread__find_map(thread, cpumode, addr, &al);
1140
Andi Kleen48d02a12017-02-23 15:46:34 -08001141 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001142 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001143
1144 al.cpu = cpu;
1145 al.sym = NULL;
1146 if (al.map)
1147 al.sym = map__find_symbol(al.map, al.addr);
1148
1149 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001150 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001151
1152 if (al.addr < al.sym->end)
1153 off = al.addr - al.sym->start;
1154 else
1155 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001156 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -08001157 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001158 printed += fprintf(fp, "%+d", off);
1159 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -08001160 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001161 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
1162 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -08001163 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001164
1165 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001166}
1167
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001168static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1169 struct thread *thread,
1170 struct perf_event_attr *attr,
1171 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -08001172{
1173 struct branch_stack *br = sample->branch_stack;
Kan Liang42bbabe2020-02-28 08:30:00 -08001174 struct branch_entry *entries = perf_sample__branch_entries(sample);
Andi Kleen48d02a12017-02-23 15:46:34 -08001175 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001176 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001177 struct perf_insn x;
1178 u8 buffer[MAXBB];
1179 unsigned off;
1180 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -07001181 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001182
1183 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001184 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001185 nr = br->nr;
1186 if (max_blocks && nr > max_blocks + 1)
1187 nr = max_blocks + 1;
1188
1189 x.thread = thread;
1190 x.cpu = sample->cpu;
1191
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001192 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -08001193
1194 /* Handle first from jump, of which we don't know the entry. */
Kan Liang42bbabe2020-02-28 08:30:00 -08001195 len = grab_bb(buffer, entries[nr-1].from,
1196 entries[nr-1].from,
Andi Kleen48d02a12017-02-23 15:46:34 -08001197 machine, thread, &x.is64bit, &x.cpumode, false);
1198 if (len > 0) {
Kan Liang42bbabe2020-02-28 08:30:00 -08001199 printed += ip__fprintf_sym(entries[nr - 1].from, thread,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001200 x.cpumode, x.cpu, &lastsym, attr, fp);
Kan Liang42bbabe2020-02-28 08:30:00 -08001201 printed += ip__fprintf_jump(entries[nr - 1].from, &entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001202 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001203 if (PRINT_FIELD(SRCCODE))
Kan Liang42bbabe2020-02-28 08:30:00 -08001204 printed += print_srccode(thread, x.cpumode, entries[nr - 1].from);
Andi Kleen48d02a12017-02-23 15:46:34 -08001205 }
1206
1207 /* Print all blocks */
1208 for (i = nr - 2; i >= 0; i--) {
Kan Liang42bbabe2020-02-28 08:30:00 -08001209 if (entries[i].from || entries[i].to)
Andi Kleen48d02a12017-02-23 15:46:34 -08001210 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
Kan Liang42bbabe2020-02-28 08:30:00 -08001211 entries[i].from,
1212 entries[i].to);
1213 start = entries[i + 1].to;
1214 end = entries[i].from;
Andi Kleen48d02a12017-02-23 15:46:34 -08001215
1216 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1217 /* Patch up missing kernel transfers due to ring filters */
1218 if (len == -ENXIO && i > 0) {
Kan Liang42bbabe2020-02-28 08:30:00 -08001219 end = entries[--i].from;
Andi Kleen48d02a12017-02-23 15:46:34 -08001220 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1221 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1222 }
1223 if (len <= 0)
1224 continue;
1225
1226 insn = 0;
Andi Kleene98df282019-09-27 16:35:44 -07001227 for (off = 0; off < (unsigned)len; off += ilen) {
Andi Kleen48d02a12017-02-23 15:46:34 -08001228 uint64_t ip = start + off;
1229
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001230 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001231 if (ip == end) {
Kan Liang42bbabe2020-02-28 08:30:00 -08001232 printed += ip__fprintf_jump(ip, &entries[i], &x, buffer + off, len - off, ++insn, fp,
Andi Kleenfe571202018-09-24 10:07:32 -07001233 &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001234 if (PRINT_FIELD(SRCCODE))
1235 printed += print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001236 break;
1237 } else {
Andi Kleene98df282019-09-27 16:35:44 -07001238 ilen = 0;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001239 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1240 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001241 if (ilen == 0)
1242 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001243 if (PRINT_FIELD(SRCCODE))
1244 print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001245 insn++;
1246 }
1247 }
Adrian Hunter51726722019-11-27 11:56:31 +02001248 if (off != end - start)
Andi Kleene98df282019-09-27 16:35:44 -07001249 printed += fprintf(fp, "\tmismatch of LBR data and executable\n");
Andi Kleen48d02a12017-02-23 15:46:34 -08001250 }
1251
1252 /*
1253 * Hit the branch? In this case we are already done, and the target
1254 * has not been executed yet.
1255 */
Kan Liang42bbabe2020-02-28 08:30:00 -08001256 if (entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001257 goto out;
Kan Liang42bbabe2020-02-28 08:30:00 -08001258 if (entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001259 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001260
1261 /*
1262 * Print final block upto sample
Andi Kleen61f61152018-11-19 21:06:17 -08001263 *
1264 * Due to pipeline delays the LBRs might be missing a branch
1265 * or two, which can result in very large or negative blocks
1266 * between final branch and sample. When this happens just
1267 * continue walking after the last TO until we hit a branch.
Andi Kleen48d02a12017-02-23 15:46:34 -08001268 */
Kan Liang42bbabe2020-02-28 08:30:00 -08001269 start = entries[0].to;
Andi Kleen48d02a12017-02-23 15:46:34 -08001270 end = sample->ip;
Andi Kleen61f61152018-11-19 21:06:17 -08001271 if (end < start) {
1272 /* Missing jump. Scan 128 bytes for the next branch */
1273 end = start + 128;
1274 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001275 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001276 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001277 if (len <= 0) {
1278 /* Print at least last IP if basic block did not work */
1279 len = grab_bb(buffer, sample->ip, sample->ip,
1280 machine, thread, &x.is64bit, &x.cpumode, false);
1281 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001282 goto out;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001283 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001284 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001285 if (PRINT_FIELD(SRCCODE))
1286 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001287 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001288 }
1289 for (off = 0; off <= end - start; off += ilen) {
Andi Kleene98df282019-09-27 16:35:44 -07001290 ilen = 0;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001291 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1292 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001293 if (ilen == 0)
1294 break;
Andi Kleen61f61152018-11-19 21:06:17 -08001295 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1296 /*
1297 * Hit a missing branch. Just stop.
1298 */
1299 printed += fprintf(fp, "\t... not reaching sample ...\n");
1300 break;
1301 }
Andi Kleendd2e18e2018-12-03 16:18:48 -08001302 if (PRINT_FIELD(SRCCODE))
1303 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001304 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001305out:
1306 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001307}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001308
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001309static int perf_sample__fprintf_addr(struct perf_sample *sample,
1310 struct thread *thread,
1311 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001312{
1313 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001314 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001315
1316 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001317 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001318
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001319 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001320
1321 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001322 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001323 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001324 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001325 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001326 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001327 }
1328
1329 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001330 printed += fprintf(fp, " (");
1331 printed += map__fprintf_dsoname(al.map, fp);
1332 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001333 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001334out:
1335 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001336}
1337
Andi Kleen99f753f2018-09-20 11:05:39 -07001338static const char *resolve_branch_sym(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001339 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001340 struct thread *thread,
1341 struct addr_location *al,
Adrian Hunterb743b862021-06-21 18:05:06 +03001342 struct addr_location *addr_al,
Andi Kleen99f753f2018-09-20 11:05:39 -07001343 u64 *ip)
1344{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001345 struct perf_event_attr *attr = &evsel->core.attr;
Andi Kleen99f753f2018-09-20 11:05:39 -07001346 const char *name = NULL;
1347
1348 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1349 if (sample_addr_correlates_sym(attr)) {
Adrian Hunterb743b862021-06-21 18:05:06 +03001350 if (!addr_al->thread)
1351 thread__resolve(thread, addr_al, sample);
1352 if (addr_al->sym)
1353 name = addr_al->sym->name;
Andi Kleen99f753f2018-09-20 11:05:39 -07001354 else
1355 *ip = sample->addr;
1356 } else {
1357 *ip = sample->addr;
1358 }
1359 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1360 if (al->sym)
1361 name = al->sym->name;
1362 else
1363 *ip = sample->ip;
1364 }
1365 return name;
1366}
1367
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001368static int perf_sample__fprintf_callindent(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001369 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001370 struct thread *thread,
Adrian Hunterb743b862021-06-21 18:05:06 +03001371 struct addr_location *al,
1372 struct addr_location *addr_al,
1373 FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001374{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001375 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter256d92b2018-12-21 14:06:19 +02001376 size_t depth = thread_stack__depth(thread, sample->cpu);
Adrian Huntere2167082016-06-23 16:40:58 +03001377 const char *name = NULL;
1378 static int spacing;
1379 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001380 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001381 u64 ip = 0;
1382
1383 /*
1384 * The 'return' has already been popped off the stack so the depth has
1385 * to be adjusted to match the 'call'.
1386 */
1387 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1388 depth += 1;
1389
Adrian Hunterb743b862021-06-21 18:05:06 +03001390 name = resolve_branch_sym(sample, evsel, thread, al, addr_al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001391
Andi Kleena78cdee2018-09-18 05:32:10 -07001392 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1393 dlen += fprintf(fp, "(");
1394 dlen += map__fprintf_dsoname(al->map, fp);
1395 dlen += fprintf(fp, ")\t");
1396 }
1397
Adrian Huntere2167082016-06-23 16:40:58 +03001398 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001399 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001400 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001401 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001402
1403 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001404 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001405
1406 /*
1407 * Try to keep the output length from changing frequently so that the
1408 * output lines up more nicely.
1409 */
1410 if (len > spacing || (len && len < spacing - 52))
1411 spacing = round_up(len + 4, 32);
1412
1413 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001414 len += fprintf(fp, "%*s", spacing - len, "");
1415
Andi Kleena78cdee2018-09-18 05:32:10 -07001416 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001417}
1418
Andi Kleen3ab481a2019-03-05 06:47:45 -08001419__weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1420 struct thread *thread __maybe_unused,
1421 struct machine *machine __maybe_unused)
1422{
1423}
1424
Adrian Hunterd9ae9c92021-05-30 22:23:01 +03001425void script_fetch_insn(struct perf_sample *sample, struct thread *thread,
1426 struct machine *machine)
1427{
1428 if (sample->insn_len == 0 && native_arch)
1429 arch_fetch_insn(sample, thread, machine);
1430}
1431
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001432static int perf_sample__fprintf_insn(struct perf_sample *sample,
1433 struct perf_event_attr *attr,
1434 struct thread *thread,
1435 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001436{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001437 int printed = 0;
1438
Adrian Hunterd9ae9c92021-05-30 22:23:01 +03001439 script_fetch_insn(sample, thread, machine);
Andi Kleen3ab481a2019-03-05 06:47:45 -08001440
Andi Kleen224e2c92016-10-07 16:42:27 +03001441 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001442 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen3ab481a2019-03-05 06:47:45 -08001443 if (PRINT_FIELD(INSN) && sample->insn_len) {
Andi Kleen224e2c92016-10-07 16:42:27 +03001444 int i;
1445
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001446 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001447 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001448 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001449 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001450 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001451 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1452
1453 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001454}
1455
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001456static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1457 struct perf_event_attr *attr, FILE *fp)
1458{
1459 unsigned int ipc;
1460
1461 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1462 return 0;
1463
1464 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1465
1466 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1467 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1468}
1469
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001470static int perf_sample__fprintf_bts(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001471 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001472 struct thread *thread,
1473 struct addr_location *al,
Adrian Hunterb743b862021-06-21 18:05:06 +03001474 struct addr_location *addr_al,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001475 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001476{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001477 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001478 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001479 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001480 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001481
Adrian Huntere2167082016-06-23 16:40:58 +03001482 if (PRINT_FIELD(CALLINDENT))
Adrian Hunterb743b862021-06-21 18:05:06 +03001483 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, addr_al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001484
Akihiro Nagai95582592012-01-30 13:43:09 +09001485 /* print branch_from information */
1486 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001487 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001488 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001489
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001490 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001491 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001492 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001493 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001494
1495 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001496 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001497 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001498 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001499 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001500 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001501 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001502 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001503
Arnaldo Carvalho de Melo9620bc362019-09-25 15:06:59 -03001504 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor,
1505 symbol_conf.bt_stop_list, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001506 }
1507
Akihiro Nagai95582592012-01-30 13:43:09 +09001508 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001509 if (PRINT_FIELD(ADDR) ||
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001510 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001511 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001512 printed += fprintf(fp, " => ");
1513 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001514 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001515
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001516 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1517
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001518 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001519 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001520
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001521 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001522 printed += fprintf(fp, "\n");
1523 if (PRINT_FIELD(SRCCODE)) {
1524 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1525 &thread->srccode_state);
1526 if (ret) {
1527 printed += ret;
1528 printed += printf("\n");
1529 }
1530 }
1531 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001532}
1533
Adrian Hunter055cd332016-06-23 16:40:56 +03001534static struct {
1535 u32 flags;
1536 const char *name;
1537} sample_flags[] = {
1538 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1539 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1540 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1541 {PERF_IP_FLAG_BRANCH, "jmp"},
1542 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1543 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1544 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1545 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1546 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1547 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1548 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1549 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1550 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
Adrian Hunterc025d462021-02-18 11:57:51 +02001551 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMENTRY, "vmentry"},
1552 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMEXIT, "vmexit"},
Adrian Hunter055cd332016-06-23 16:40:56 +03001553 {0, NULL}
1554};
1555
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001556static const char *sample_flags_to_name(u32 flags)
1557{
1558 int i;
1559
1560 for (i = 0; sample_flags[i].name ; i++) {
1561 if (sample_flags[i].flags == flags)
1562 return sample_flags[i].name;
1563 }
1564
1565 return NULL;
1566}
1567
Adrian Hunter54cd8b02021-05-25 12:51:06 +03001568int perf_sample__sprintf_flags(u32 flags, char *str, size_t sz)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001569{
1570 const char *chars = PERF_IP_FLAG_CHARS;
Adrian Hunter54cd8b02021-05-25 12:51:06 +03001571 const size_t n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001572 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1573 const char *name = NULL;
Adrian Hunter54cd8b02021-05-25 12:51:06 +03001574 size_t i, pos = 0;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001575
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001576 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1577 if (name)
Adrian Hunter54cd8b02021-05-25 12:51:06 +03001578 return snprintf(str, sz, "%-15s%4s", name, in_tx ? "(x)" : "");
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001579
1580 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1581 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1582 if (name)
Adrian Hunter54cd8b02021-05-25 12:51:06 +03001583 return snprintf(str, sz, "tr strt %-7s%4s", name, in_tx ? "(x)" : "");
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001584 }
1585
1586 if (flags & PERF_IP_FLAG_TRACE_END) {
1587 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1588 if (name)
Adrian Hunter54cd8b02021-05-25 12:51:06 +03001589 return snprintf(str, sz, "tr end %-7s%4s", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001590 }
1591
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001592 for (i = 0; i < n; i++, flags >>= 1) {
Adrian Hunter54cd8b02021-05-25 12:51:06 +03001593 if ((flags & 1) && pos < sz)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001594 str[pos++] = chars[i];
1595 }
1596 for (; i < 32; i++, flags >>= 1) {
Adrian Hunter54cd8b02021-05-25 12:51:06 +03001597 if ((flags & 1) && pos < sz)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001598 str[pos++] = '?';
1599 }
Adrian Hunter54cd8b02021-05-25 12:51:06 +03001600 if (pos < sz)
1601 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001602
Adrian Hunter54cd8b02021-05-25 12:51:06 +03001603 return pos;
1604}
1605
1606static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
1607{
1608 char str[SAMPLE_FLAGS_BUF_SIZE];
1609
1610 perf_sample__sprintf_flags(flags, str, sizeof(str));
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001611 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001612}
1613
Wang Nan30372f02016-02-24 11:20:45 +00001614struct printer_data {
1615 int line_no;
1616 bool hit_nul;
1617 bool is_printable;
1618};
1619
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001620static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1621 unsigned int val,
1622 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001623{
1624 unsigned char ch = (unsigned char)val;
1625 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001626 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001627
1628 switch (op) {
1629 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001630 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001631 break;
1632 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001633 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001634 " ");
1635 break;
1636 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001637 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001638 break;
1639 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001640 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001641 break;
1642 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001643 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001644 break;
1645 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001646 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001647 break;
1648 case BINARY_PRINT_CHAR_DATA:
1649 if (printer_data->hit_nul && ch)
1650 printer_data->is_printable = false;
1651
1652 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001653 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001654
1655 if (!printer_data->is_printable)
1656 break;
1657
1658 if (ch == '\0')
1659 printer_data->hit_nul = true;
1660 else
1661 printer_data->is_printable = false;
1662 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001663 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001664 }
1665 break;
1666 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001667 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001668 break;
1669 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001670 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001671 printer_data->line_no++;
1672 break;
1673 case BINARY_PRINT_DATA_END:
1674 default:
1675 break;
1676 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001677
1678 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001679}
1680
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001681static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001682{
1683 unsigned int nr_bytes = sample->raw_size;
1684 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001685 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1686 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001687
1688 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001689 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1690
1691 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001692}
1693
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001694static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001695{
1696 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001697 return fprintf(fp, "%*s", spacing - len, "");
1698
1699 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001700}
1701
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001702static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001703{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001704 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001705}
1706
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001707static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001708{
1709 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1710 int len;
1711
1712 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001713 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001714
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001715 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001716 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001717 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001718}
1719
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001720static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001721{
1722 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1723 int len;
1724
1725 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001726 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001727
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001728 len = fprintf(fp, " hints: %#x extensions: %#x ",
1729 data->hints, data->extensions);
1730 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001731}
1732
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001733static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001734{
1735 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1736 int len;
1737
1738 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001739 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001740
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001741 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1742 data->hw, data->cstate, data->subcstate);
1743 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001744}
1745
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001746static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001747{
1748 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1749 int len;
1750
1751 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001752 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001753
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001754 len = fprintf(fp, " IP: %u ", data->ip);
1755 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001756}
1757
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001758static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001759{
1760 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1761 int len;
1762
1763 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001764 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001765
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001766 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001767 data->deepest_cstate, data->last_cstate,
1768 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001769 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001770}
1771
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001772static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001773{
1774 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1775 unsigned int percent, freq;
1776 int len;
1777
1778 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001779 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001780
1781 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001782 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001783 if (data->max_nonturbo) {
1784 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001785 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001786 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001787 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001788}
1789
Adrian Hunterc840cbf2021-02-05 19:53:50 +02001790static int perf_sample__fprintf_synth_psb(struct perf_sample *sample, FILE *fp)
1791{
1792 struct perf_synth_intel_psb *data = perf_sample__synth_ptr(sample);
1793 int len;
1794
1795 if (perf_sample__bad_synth_size(sample, *data))
1796 return 0;
1797
1798 len = fprintf(fp, " psb offs: %#" PRIx64, data->offset);
1799 return len + perf_sample__fprintf_pt_spacing(len, fp);
1800}
1801
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001802static int perf_sample__fprintf_synth(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001803 struct evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001804{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001805 switch (evsel->core.attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001806 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001807 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001808 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001809 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001810 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001811 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001812 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001813 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001814 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001815 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001816 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001817 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunterc840cbf2021-02-05 19:53:50 +02001818 case PERF_SYNTH_INTEL_PSB:
1819 return perf_sample__fprintf_synth_psb(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001820 default:
1821 break;
1822 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001823
1824 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001825}
1826
Arnaldo Carvalho de Meloafdd63f2020-06-10 11:39:16 -03001827static int evlist__max_name_len(struct evlist *evlist)
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001828{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001829 struct evsel *evsel;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001830 int max = 0;
1831
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001832 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03001833 int len = strlen(evsel__name(evsel));
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001834
1835 max = MAX(len, max);
1836 }
1837
1838 return max;
1839}
1840
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001841static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001842{
1843 struct mem_info mi = { .data_src.val = data_src };
1844 char decode[100];
1845 char out[100];
1846 static int maxlen;
1847 int len;
1848
1849 perf_script__meminfo_scnprintf(decode, 100, &mi);
1850
1851 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1852 if (maxlen < len)
1853 maxlen = len;
1854
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001855 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001856}
1857
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001858struct metric_ctx {
1859 struct perf_sample *sample;
1860 struct thread *thread;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001861 struct evsel *evsel;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001862 FILE *fp;
1863};
1864
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001865static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1866 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001867 const char *fmt,
1868 const char *unit, double val)
1869{
1870 struct metric_ctx *mctx = ctx;
1871
1872 if (!fmt)
1873 return;
Jiri Olsae534bfb2020-08-05 11:34:44 +02001874 perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001875 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001876 fputs("\tmetric: ", mctx->fp);
1877 if (color)
1878 color_fprintf(mctx->fp, color, fmt, val);
1879 else
1880 printf(fmt, val);
1881 fprintf(mctx->fp, " %s\n", unit);
1882}
1883
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001884static void script_new_line(struct perf_stat_config *config __maybe_unused,
1885 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001886{
1887 struct metric_ctx *mctx = ctx;
1888
Jiri Olsae534bfb2020-08-05 11:34:44 +02001889 perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001890 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001891 fputs("\tmetric: ", mctx->fp);
1892}
1893
1894static void perf_sample__fprint_metric(struct perf_script *script,
1895 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001896 struct evsel *evsel,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001897 struct perf_sample *sample,
1898 FILE *fp)
1899{
1900 struct perf_stat_output_ctx ctx = {
1901 .print_metric = script_print_metric,
1902 .new_line = script_new_line,
1903 .ctx = &(struct metric_ctx) {
1904 .sample = sample,
1905 .thread = thread,
1906 .evsel = evsel,
1907 .fp = fp,
1908 },
1909 .force_header = false,
1910 };
Jiri Olsa32dcd022019-07-21 13:23:51 +02001911 struct evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001912 u64 val;
1913
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001914 if (!evsel->stats)
Arnaldo Carvalho de Melo53f5e902020-11-30 09:31:04 -03001915 evlist__alloc_stats(script->session->evlist, false);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001916 if (evsel_script(evsel->leader)->gnum++ == 0)
1917 perf_stat__reset_shadow_stats();
1918 val = sample->period * evsel->scale;
1919 perf_stat__update_shadow_stats(evsel,
1920 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001921 sample->cpu,
1922 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001923 evsel_script(evsel)->val = val;
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001924 if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) {
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001925 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001926 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001927 evsel_script(ev2)->val,
1928 sample->cpu,
1929 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001930 NULL,
1931 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001932 }
1933 evsel_script(evsel->leader)->gnum = 0;
1934 }
1935}
1936
Andi Kleen99f753f2018-09-20 11:05:39 -07001937static bool show_event(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001938 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001939 struct thread *thread,
Adrian Hunterb743b862021-06-21 18:05:06 +03001940 struct addr_location *al,
1941 struct addr_location *addr_al)
Andi Kleen99f753f2018-09-20 11:05:39 -07001942{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001943 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001944
1945 if (!symbol_conf.graph_function)
1946 return true;
1947
1948 if (thread->filter) {
1949 if (depth <= thread->filter_entry_depth) {
1950 thread->filter = false;
1951 return false;
1952 }
1953 return true;
1954 } else {
1955 const char *s = symbol_conf.graph_function;
1956 u64 ip;
Adrian Hunterb743b862021-06-21 18:05:06 +03001957 const char *name = resolve_branch_sym(sample, evsel, thread, al, addr_al,
Andi Kleen99f753f2018-09-20 11:05:39 -07001958 &ip);
1959 unsigned nlen;
1960
1961 if (!name)
1962 return false;
1963 nlen = strlen(name);
1964 while (*s) {
1965 unsigned len = strcspn(s, ",");
1966 if (nlen == len && !strncmp(name, s, len)) {
1967 thread->filter = true;
1968 thread->filter_entry_depth = depth;
1969 return true;
1970 }
1971 s += len;
1972 if (*s == ',')
1973 s++;
1974 }
1975 return false;
1976 }
1977}
1978
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001979static void process_event(struct perf_script *script,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001980 struct perf_sample *sample, struct evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001981 struct addr_location *al,
Adrian Hunterb743b862021-06-21 18:05:06 +03001982 struct addr_location *addr_al,
Andi Kleen48d02a12017-02-23 15:46:34 -08001983 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001984{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001985 struct thread *thread = al->thread;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001986 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001987 unsigned int type = output_type(attr->type);
Jiri Olsa32dcd022019-07-21 13:23:51 +02001988 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001989 FILE *fp = es->fp;
Kan Liang6b9bae62020-12-16 10:57:57 -08001990 char str[PAGE_SIZE_NAME_LEN];
David Ahern1424dc92011-03-09 22:23:28 -07001991
Adrian Hunter14057202017-06-21 13:17:19 +03001992 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001993 return;
1994
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001995 ++es->samples;
1996
Jiri Olsae534bfb2020-08-05 11:34:44 +02001997 perf_sample__fprintf_start(script, sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001998 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001999
Jiri Olsa535aeaae2014-08-25 16:45:42 +02002000 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03002001 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02002002
Namhyung Kime944d3d2013-11-18 14:34:52 +09002003 if (PRINT_FIELD(EVNAME)) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002004 const char *evname = evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01002005
2006 if (!script->name_width)
Arnaldo Carvalho de Meloafdd63f2020-06-10 11:39:16 -03002007 script->name_width = evlist__max_name_len(script->session->evlist);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01002008
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03002009 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09002010 }
2011
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002012 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002013 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002014
Akihiro Nagai95582592012-01-30 13:43:09 +09002015 if (is_bts_event(attr)) {
Adrian Hunterb743b862021-06-21 18:05:06 +03002016 perf_sample__fprintf_bts(sample, evsel, thread, al, addr_al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09002017 return;
2018 }
2019
Andi Kleen96167162019-01-17 11:48:34 -08002020 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03002021 event_format__fprintf(evsel->tp_format, sample->cpu,
2022 sample->raw_data, sample->raw_size, fp);
2023 }
Adrian Hunter47e78082017-05-26 11:17:22 +03002024
2025 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002026 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03002027
David Ahern7cec0922011-05-30 13:08:23 -06002028 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002029 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06002030
Jiri Olsa94ddddf2016-02-15 09:34:51 +01002031 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002032 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01002033
2034 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002035 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01002036
David Ahern787bef12011-05-27 14:28:43 -06002037 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07002038 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04002039
Kan Liang680d1252020-03-19 13:25:14 -07002040 if (script->stitch_lbr)
2041 al->thread->lbr_stitch_enable = true;
2042
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03002043 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07002044 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03002045 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07002046 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03002047
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002048 fputc(cursor ? '\n' : ' ', fp);
Arnaldo Carvalho de Melo9620bc362019-09-25 15:06:59 -03002049 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor,
2050 symbol_conf.bt_stop_list, fp);
David Ahernc0230b22011-03-09 22:23:27 -07002051 }
2052
Stephane Eranianfc36f942015-08-31 18:41:10 +02002053 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002054 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02002055
Andi Kleenb1491ac2017-09-05 11:40:57 -07002056 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002057 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07002058
Stephane Eraniandc323ce2015-08-31 18:41:13 +02002059 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002060 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02002061 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002062 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07002063 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002064 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02002065
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03002066 if (evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002067 perf_sample__fprintf_bpf_output(sample, fp);
2068 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04002069
2070 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03002071 fprintf(fp, "%16" PRIx64, sample->phys_addr);
Adrian Hunter68fb45b2019-05-20 14:37:14 +03002072
Kan Liang6b9bae62020-12-16 10:57:57 -08002073 if (PRINT_FIELD(DATA_PAGE_SIZE))
2074 fprintf(fp, " %s", get_page_size_name(sample->data_page_size, str));
2075
Stephane Eranianc513de82021-01-05 11:57:50 -08002076 if (PRINT_FIELD(CODE_PAGE_SIZE))
2077 fprintf(fp, " %s", get_page_size_name(sample->code_page_size, str));
2078
Adrian Hunter68fb45b2019-05-20 14:37:14 +03002079 perf_sample__fprintf_ipc(sample, attr, fp);
2080
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03002081 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08002082
Andi Kleendd2e18e2018-12-03 16:18:48 -08002083 if (PRINT_FIELD(SRCCODE)) {
2084 if (map__fprintf_srccode(al->map, al->addr, stdout,
2085 &thread->srccode_state))
2086 printf("\n");
2087 }
2088
Andi Kleen4bd1bef2017-11-17 13:43:00 -08002089 if (PRINT_FIELD(METRIC))
2090 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02002091
2092 if (verbose)
2093 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07002094}
2095
Tom Zanussi956ffd02009-11-25 01:15:46 -06002096static struct scripting_ops *scripting_ops;
2097
Jiri Olsa32dcd022019-07-21 13:23:51 +02002098static void __process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsa36e33c52016-01-06 11:49:56 +01002099{
Jiri Olsaa2f354e2019-08-22 13:11:41 +02002100 int nthreads = perf_thread_map__nr(counter->core.threads);
Arnaldo Carvalho de Melo5eb88f02020-04-29 15:45:09 -03002101 int ncpus = evsel__nr_cpus(counter);
Jiri Olsa36e33c52016-01-06 11:49:56 +01002102 int cpu, thread;
2103 static int header_printed;
2104
Jiri Olsa648b5af2019-08-06 11:35:19 +02002105 if (counter->core.system_wide)
Jiri Olsa36e33c52016-01-06 11:49:56 +01002106 nthreads = 1;
2107
2108 if (!header_printed) {
2109 printf("%3s %8s %15s %15s %15s %15s %s\n",
2110 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
2111 header_printed = 1;
2112 }
2113
2114 for (thread = 0; thread < nthreads; thread++) {
2115 for (cpu = 0; cpu < ncpus; cpu++) {
2116 struct perf_counts_values *counts;
2117
2118 counts = perf_counts(counter->counts, cpu, thread);
2119
2120 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
Jiri Olsad400bd32019-07-21 13:24:37 +02002121 counter->core.cpus->map[cpu],
Jiri Olsaa2f354e2019-08-22 13:11:41 +02002122 perf_thread_map__pid(counter->core.threads, thread),
Jiri Olsa36e33c52016-01-06 11:49:56 +01002123 counts->val,
2124 counts->ena,
2125 counts->run,
2126 tstamp,
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002127 evsel__name(counter));
Jiri Olsa36e33c52016-01-06 11:49:56 +01002128 }
2129 }
2130}
2131
Jiri Olsa32dcd022019-07-21 13:23:51 +02002132static void process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsae099eba2016-01-05 22:09:09 +01002133{
2134 if (scripting_ops && scripting_ops->process_stat)
2135 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01002136 else
2137 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01002138}
2139
2140static void process_stat_interval(u64 tstamp)
2141{
2142 if (scripting_ops && scripting_ops->process_stat_interval)
2143 scripting_ops->process_stat_interval(tstamp);
2144}
2145
Tom Zanussi956ffd02009-11-25 01:15:46 -06002146static void setup_scripting(void)
2147{
Tom Zanussi16c632d2009-11-25 01:15:48 -06002148 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06002149 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002150}
2151
Adrian Hunterd445dd22014-08-15 22:08:37 +03002152static int flush_scripting(void)
2153{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002154 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03002155}
2156
Tom Zanussi956ffd02009-11-25 01:15:46 -06002157static int cleanup_scripting(void)
2158{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002159 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05002160
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002161 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002162}
2163
Andi Kleene87e5482019-03-11 07:44:52 -07002164static bool filter_cpu(struct perf_sample *sample)
2165{
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002166 if (cpu_list && sample->cpu != (u32)-1)
Andi Kleene87e5482019-03-11 07:44:52 -07002167 return !test_bit(sample->cpu, cpu_bitmap);
2168 return false;
2169}
2170
Jiri Olsa809e9422015-11-26 18:55:21 +01002171static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002172 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002173 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002174 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002175 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002176{
Jiri Olsa809e9422015-11-26 18:55:21 +01002177 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07002178 struct addr_location al;
Adrian Hunterb743b862021-06-21 18:05:06 +03002179 struct addr_location addr_al;
Adrian Hunter291961f2021-06-27 16:18:09 +03002180 int ret = 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002181
Jin Yao2ab046c2017-12-08 21:13:46 +08002182 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
2183 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07002184 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08002185 }
David Aherna91f4c42016-11-29 10:15:43 -07002186
David Ahern1424dc92011-03-09 22:23:28 -07002187 if (debug_mode) {
2188 if (sample->time < last_timestamp) {
2189 pr_err("Samples misordered, previous: %" PRIu64
2190 " this: %" PRIu64 "\n", last_timestamp,
2191 sample->time);
2192 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02002193 }
David Ahern1424dc92011-03-09 22:23:28 -07002194 last_timestamp = sample->time;
2195 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002196 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002197
Adrian Hunter93000412021-06-21 18:05:04 +03002198 if (filter_cpu(sample))
2199 return 0;
2200
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03002201 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07002202 pr_err("problem processing %d event, skipping it.\n",
2203 event->header.type);
2204 return -1;
2205 }
2206
2207 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002208 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07002209
Adrian Hunterb743b862021-06-21 18:05:06 +03002210 /* Set thread to NULL to indicate addr_al is not initialized */
2211 addr_al.thread = NULL;
2212
2213 if (!show_event(sample, evsel, al.thread, &al, &addr_al))
Adrian Hunter4371fbc2021-06-21 18:05:05 +03002214 goto out_put;
2215
2216 if (evswitch__discard(&scr->evswitch, evsel))
2217 goto out_put;
2218
Adrian Hunter291961f2021-06-27 16:18:09 +03002219 ret = dlfilter__filter_event(dlfilter, event, sample, evsel, machine, &al, &addr_al);
2220 if (ret) {
2221 if (ret > 0)
2222 ret = 0;
2223 goto out_put;
2224 }
2225
Adrian Hunter3f8e0092021-05-25 12:51:05 +03002226 if (scripting_ops) {
2227 struct addr_location *addr_al_ptr = NULL;
Adrian Hunter3f8e0092021-05-25 12:51:05 +03002228
2229 if ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
2230 sample_addr_correlates_sym(&evsel->core.attr)) {
Adrian Hunterb743b862021-06-21 18:05:06 +03002231 if (!addr_al.thread)
2232 thread__resolve(al.thread, &addr_al, sample);
Adrian Hunter3f8e0092021-05-25 12:51:05 +03002233 addr_al_ptr = &addr_al;
2234 }
2235 scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr);
2236 } else {
Adrian Hunterb743b862021-06-21 18:05:06 +03002237 process_event(scr, sample, evsel, &al, &addr_al, machine);
Adrian Hunter3f8e0092021-05-25 12:51:05 +03002238 }
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002239
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002240out_put:
2241 addr_location__put(&al);
Adrian Hunter291961f2021-06-27 16:18:09 +03002242 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002243}
2244
Adrian Hunter7ea95722013-11-01 15:51:30 +02002245static int process_attr(struct perf_tool *tool, union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02002246 struct evlist **pevlist)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002247{
2248 struct perf_script *scr = container_of(tool, struct perf_script, tool);
Jiri Olsa63503db2019-07-21 13:23:52 +02002249 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002250 struct evsel *evsel, *pos;
Jiri Olsa0d71a2b2020-05-07 11:50:23 +02002251 u64 sample_type;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002252 int err;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002253 static struct evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002254
2255 err = perf_event__process_attr(tool, event, pevlist);
2256 if (err)
2257 return err;
2258
2259 evlist = *pevlist;
Jiri Olsa515dbe42019-09-03 10:39:52 +02002260 evsel = evlist__last(*pevlist);
Adrian Hunter7ea95722013-11-01 15:51:30 +02002261
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302262 if (!evsel->priv) {
2263 if (scr->per_event_dump) {
Arnaldo Carvalho de Melo297e69b2021-03-09 08:59:21 -03002264 evsel->priv = evsel_script__new(evsel, scr->session->data);
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302265 } else {
2266 es = zalloc(sizeof(*es));
2267 if (!es)
2268 return -ENOMEM;
2269 es->fp = stdout;
2270 evsel->priv = es;
2271 }
2272 }
2273
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002274 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2275 evsel->core.attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002276 return 0;
2277
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002278 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002279 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002280 return 0;
2281 }
2282
Jiri Olsa0d71a2b2020-05-07 11:50:23 +02002283 if (evsel->core.attr.sample_type) {
Arnaldo Carvalho de Meloafdd63f2020-06-10 11:39:16 -03002284 err = evsel__check_attr(evsel, scr->session);
Jiri Olsa0d71a2b2020-05-07 11:50:23 +02002285 if (err)
2286 return err;
2287 }
Jiri Olsad2b5a312015-10-16 12:41:25 +02002288
Jiri Olsa0d71a2b2020-05-07 11:50:23 +02002289 /*
2290 * Check if we need to enable callchains based
2291 * on events sample_type.
2292 */
Arnaldo Carvalho de Melob3c2cc22020-06-17 09:24:21 -03002293 sample_type = evlist__combined_sample_type(evlist);
Jiri Olsa0d71a2b2020-05-07 11:50:23 +02002294 callchain_param_setup(sample_type);
Jiri Olsa53fb1892020-05-07 11:50:24 +02002295
Adrian Hunterb5164082020-05-27 21:02:50 +03002296 /* Enable fields for callchain entries */
2297 if (symbol_conf.use_callchain &&
2298 (sample_type & PERF_SAMPLE_CALLCHAIN ||
2299 sample_type & PERF_SAMPLE_BRANCH_STACK ||
2300 (sample_type & PERF_SAMPLE_REGS_USER &&
2301 sample_type & PERF_SAMPLE_STACK_USER))) {
2302 int type = output_type(evsel->core.attr.type);
2303
2304 if (!(output[type].user_unset_fields & PERF_OUTPUT_IP))
2305 output[type].fields |= PERF_OUTPUT_IP;
2306 if (!(output[type].user_unset_fields & PERF_OUTPUT_SYM))
2307 output[type].fields |= PERF_OUTPUT_SYM;
Jiri Olsa53fb1892020-05-07 11:50:24 +02002308 }
2309 set_print_ip_opts(&evsel->core.attr);
Jiri Olsa0d71a2b2020-05-07 11:50:23 +02002310 return 0;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002311}
2312
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002313static int print_event_with_time(struct perf_tool *tool,
2314 union perf_event *event,
2315 struct perf_sample *sample,
2316 struct machine *machine,
2317 pid_t pid, pid_t tid, u64 timestamp)
2318{
2319 struct perf_script *script = container_of(tool, struct perf_script, tool);
2320 struct perf_session *session = script->session;
Arnaldo Carvalho de Melo3ccf8a72020-11-30 14:17:57 -03002321 struct evsel *evsel = evlist__id2evsel(session->evlist, sample->id);
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002322 struct thread *thread = NULL;
2323
2324 if (evsel && !evsel->core.attr.sample_id_all) {
2325 sample->cpu = 0;
2326 sample->time = timestamp;
2327 sample->pid = pid;
2328 sample->tid = tid;
2329 }
2330
2331 if (filter_cpu(sample))
2332 return 0;
2333
2334 if (tid != -1)
2335 thread = machine__findnew_thread(machine, pid, tid);
2336
Adrian Hunterfc183802020-09-09 11:49:22 +03002337 if (evsel) {
Jiri Olsae534bfb2020-08-05 11:34:44 +02002338 perf_sample__fprintf_start(script, sample, thread, evsel,
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002339 event->header.type, stdout);
2340 }
2341
Adrian Hunter7eeb9852020-05-12 15:19:22 +03002342 perf_event__fprintf(event, machine, stdout);
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002343
2344 thread__put(thread);
2345
2346 return 0;
2347}
2348
2349static int print_event(struct perf_tool *tool, union perf_event *event,
2350 struct perf_sample *sample, struct machine *machine,
2351 pid_t pid, pid_t tid)
2352{
2353 return print_event_with_time(tool, event, sample, machine, pid, tid, 0);
2354}
2355
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002356static int process_comm_event(struct perf_tool *tool,
2357 union perf_event *event,
2358 struct perf_sample *sample,
2359 struct machine *machine)
2360{
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002361 if (perf_event__process_comm(tool, event, sample, machine) < 0)
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002362 return -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002363
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002364 return print_event(tool, event, sample, machine, event->comm.pid,
2365 event->comm.tid);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002366}
2367
Hari Bathini96a44bb2017-03-08 02:12:06 +05302368static int process_namespaces_event(struct perf_tool *tool,
2369 union perf_event *event,
2370 struct perf_sample *sample,
2371 struct machine *machine)
2372{
Hari Bathini96a44bb2017-03-08 02:12:06 +05302373 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002374 return -1;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302375
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002376 return print_event(tool, event, sample, machine, event->namespaces.pid,
2377 event->namespaces.tid);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302378}
2379
Namhyung Kim160d4af2020-03-25 21:45:36 +09002380static int process_cgroup_event(struct perf_tool *tool,
2381 union perf_event *event,
2382 struct perf_sample *sample,
2383 struct machine *machine)
2384{
Namhyung Kim160d4af2020-03-25 21:45:36 +09002385 if (perf_event__process_cgroup(tool, event, sample, machine) < 0)
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002386 return -1;
Namhyung Kim160d4af2020-03-25 21:45:36 +09002387
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002388 return print_event(tool, event, sample, machine, sample->pid,
2389 sample->tid);
Namhyung Kim160d4af2020-03-25 21:45:36 +09002390}
2391
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002392static int process_fork_event(struct perf_tool *tool,
2393 union perf_event *event,
2394 struct perf_sample *sample,
2395 struct machine *machine)
2396{
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002397 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2398 return -1;
2399
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002400 return print_event_with_time(tool, event, sample, machine,
2401 event->fork.pid, event->fork.tid,
2402 event->fork.time);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002403}
2404static int process_exit_event(struct perf_tool *tool,
2405 union perf_event *event,
2406 struct perf_sample *sample,
2407 struct machine *machine)
2408{
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002409 /* Print before 'exit' deletes anything */
2410 if (print_event_with_time(tool, event, sample, machine, event->fork.pid,
2411 event->fork.tid, event->fork.time))
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002412 return -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002413
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002414 return perf_event__process_exit(tool, event, sample, machine);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002415}
2416
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002417static int process_mmap_event(struct perf_tool *tool,
2418 union perf_event *event,
2419 struct perf_sample *sample,
2420 struct machine *machine)
2421{
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002422 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2423 return -1;
2424
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002425 return print_event(tool, event, sample, machine, event->mmap.pid,
2426 event->mmap.tid);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002427}
2428
2429static int process_mmap2_event(struct perf_tool *tool,
2430 union perf_event *event,
2431 struct perf_sample *sample,
2432 struct machine *machine)
2433{
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002434 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2435 return -1;
2436
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002437 return print_event(tool, event, sample, machine, event->mmap2.pid,
2438 event->mmap2.tid);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002439}
2440
Adrian Hunter7c148982015-07-21 12:44:06 +03002441static int process_switch_event(struct perf_tool *tool,
2442 union perf_event *event,
2443 struct perf_sample *sample,
2444 struct machine *machine)
2445{
Adrian Hunter7c148982015-07-21 12:44:06 +03002446 struct perf_script *script = container_of(tool, struct perf_script, tool);
Adrian Hunter7c148982015-07-21 12:44:06 +03002447
2448 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2449 return -1;
2450
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002451 if (scripting_ops && scripting_ops->process_switch)
2452 scripting_ops->process_switch(event, sample, machine);
2453
2454 if (!script->show_switch_events)
2455 return 0;
2456
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002457 return print_event(tool, event, sample, machine, sample->pid,
2458 sample->tid);
Adrian Hunter7c148982015-07-21 12:44:06 +03002459}
2460
Adrian Hunter2ede9212021-05-25 12:51:11 +03002461static int process_auxtrace_error(struct perf_session *session,
2462 union perf_event *event)
2463{
2464 if (scripting_ops && scripting_ops->process_auxtrace_error) {
2465 scripting_ops->process_auxtrace_error(session, event);
2466 return 0;
2467 }
2468
2469 return perf_event__process_auxtrace_error(session, event);
2470}
2471
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002472static int
2473process_lost_event(struct perf_tool *tool,
2474 union perf_event *event,
2475 struct perf_sample *sample,
2476 struct machine *machine)
2477{
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002478 return print_event(tool, event, sample, machine, sample->pid,
2479 sample->tid);
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002480}
2481
Jiri Olsa3233b372018-02-06 19:17:59 +01002482static int
2483process_finished_round_event(struct perf_tool *tool __maybe_unused,
2484 union perf_event *event,
2485 struct ordered_events *oe __maybe_unused)
2486
2487{
Adrian Hunter7eeb9852020-05-12 15:19:22 +03002488 perf_event__fprintf(event, NULL, stdout);
Jiri Olsa3233b372018-02-06 19:17:59 +01002489 return 0;
2490}
2491
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002492static int
2493process_bpf_events(struct perf_tool *tool __maybe_unused,
2494 union perf_event *event,
2495 struct perf_sample *sample,
2496 struct machine *machine)
2497{
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002498 if (machine__process_ksymbol(machine, event, sample) < 0)
2499 return -1;
2500
Adrian Hunter1a2725f2020-04-02 17:15:48 +03002501 return print_event(tool, event, sample, machine, sample->pid,
2502 sample->tid);
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002503}
2504
Adrian Hunter92ecf3a2020-05-12 15:19:21 +03002505static int process_text_poke_events(struct perf_tool *tool,
2506 union perf_event *event,
2507 struct perf_sample *sample,
2508 struct machine *machine)
2509{
2510 if (perf_event__process_text_poke(tool, event, sample, machine) < 0)
2511 return -1;
2512
2513 return print_event(tool, event, sample, machine, sample->pid,
2514 sample->tid);
2515}
2516
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002517static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002518{
2519 session_done = 1;
2520}
2521
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002522static void perf_script__fclose_per_event_dump(struct perf_script *script)
2523{
Jiri Olsa63503db2019-07-21 13:23:52 +02002524 struct evlist *evlist = script->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002525 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002526
2527 evlist__for_each_entry(evlist, evsel) {
2528 if (!evsel->priv)
2529 break;
Arnaldo Carvalho de Melo297e69b2021-03-09 08:59:21 -03002530 evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002531 evsel->priv = NULL;
2532 }
2533}
2534
2535static int perf_script__fopen_per_event_dump(struct perf_script *script)
2536{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002537 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002538
2539 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002540 /*
2541 * Already setup? I.e. we may be called twice in cases like
2542 * Intel PT, one for the intel_pt// and dummy events, then
Ingo Molnar4d39c892021-03-23 17:09:15 +01002543 * for the evsels synthesized from the auxtrace info.
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002544 *
2545 * Ses perf_script__process_auxtrace_info.
2546 */
2547 if (evsel->priv != NULL)
2548 continue;
2549
Arnaldo Carvalho de Melo297e69b2021-03-09 08:59:21 -03002550 evsel->priv = evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002551 if (evsel->priv == NULL)
2552 goto out_err_fclose;
2553 }
2554
2555 return 0;
2556
2557out_err_fclose:
2558 perf_script__fclose_per_event_dump(script);
2559 return -1;
2560}
2561
2562static int perf_script__setup_per_event_dump(struct perf_script *script)
2563{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002564 struct evsel *evsel;
2565 static struct evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002566
2567 if (script->per_event_dump)
2568 return perf_script__fopen_per_event_dump(script);
2569
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002570 es_stdout.fp = stdout;
2571
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002572 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002573 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002574
2575 return 0;
2576}
2577
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002578static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2579{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002580 struct evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002581
2582 evlist__for_each_entry(script->session->evlist, evsel) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002583 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002584
Arnaldo Carvalho de Melo297e69b2021-03-09 08:59:21 -03002585 evsel_script__fprintf(es, stdout);
2586 evsel_script__delete(es);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002587 evsel->priv = NULL;
2588 }
2589}
2590
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002591static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002592{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002593 int ret;
2594
Tom Zanussic239da32010-04-01 23:59:18 -05002595 signal(SIGINT, sig_handler);
2596
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002597 perf_stat__init_shadow_stats();
2598
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002599 /* override event processing functions */
2600 if (script->show_task_events) {
2601 script->tool.comm = process_comm_event;
2602 script->tool.fork = process_fork_event;
2603 script->tool.exit = process_exit_event;
2604 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002605 if (script->show_mmap_events) {
2606 script->tool.mmap = process_mmap_event;
2607 script->tool.mmap2 = process_mmap2_event;
2608 }
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002609 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
Adrian Hunter7c148982015-07-21 12:44:06 +03002610 script->tool.context_switch = process_switch_event;
Adrian Hunter2ede9212021-05-25 12:51:11 +03002611 if (scripting_ops && scripting_ops->process_auxtrace_error)
2612 script->tool.auxtrace_error = process_auxtrace_error;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302613 if (script->show_namespace_events)
2614 script->tool.namespaces = process_namespaces_event;
Namhyung Kim160d4af2020-03-25 21:45:36 +09002615 if (script->show_cgroup_events)
2616 script->tool.cgroup = process_cgroup_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002617 if (script->show_lost_events)
2618 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002619 if (script->show_round_events) {
2620 script->tool.ordered_events = false;
2621 script->tool.finished_round = process_finished_round_event;
2622 }
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002623 if (script->show_bpf_events) {
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -03002624 script->tool.ksymbol = process_bpf_events;
2625 script->tool.bpf = process_bpf_events;
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002626 }
Adrian Hunter92ecf3a2020-05-12 15:19:21 +03002627 if (script->show_text_poke_events) {
2628 script->tool.ksymbol = process_bpf_events;
2629 script->tool.text_poke = process_text_poke_events;
2630 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002631
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002632 if (perf_script__setup_per_event_dump(script)) {
2633 pr_err("Couldn't create the per event dump files\n");
2634 return -1;
2635 }
2636
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002637 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002638
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002639 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002640 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002641
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002642 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002643 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002644
2645 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002646}
2647
Tom Zanussi956ffd02009-11-25 01:15:46 -06002648struct script_spec {
2649 struct list_head node;
2650 struct scripting_ops *ops;
Gustavo A. R. Silva6549a8c2020-05-15 12:29:26 -05002651 char spec[];
Tom Zanussi956ffd02009-11-25 01:15:46 -06002652};
2653
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002654static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002655
2656static struct script_spec *script_spec__new(const char *spec,
2657 struct scripting_ops *ops)
2658{
2659 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2660
2661 if (s != NULL) {
2662 strcpy(s->spec, spec);
2663 s->ops = ops;
2664 }
2665
2666 return s;
2667}
2668
Tom Zanussi956ffd02009-11-25 01:15:46 -06002669static void script_spec__add(struct script_spec *s)
2670{
2671 list_add_tail(&s->node, &script_specs);
2672}
2673
2674static struct script_spec *script_spec__find(const char *spec)
2675{
2676 struct script_spec *s;
2677
2678 list_for_each_entry(s, &script_specs, node)
2679 if (strcasecmp(s->spec, spec) == 0)
2680 return s;
2681 return NULL;
2682}
2683
Tom Zanussi956ffd02009-11-25 01:15:46 -06002684int script_spec_register(const char *spec, struct scripting_ops *ops)
2685{
2686 struct script_spec *s;
2687
2688 s = script_spec__find(spec);
2689 if (s)
2690 return -1;
2691
Taeung Song8560bae2016-02-26 00:13:10 +09002692 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002693 if (!s)
2694 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002695 else
2696 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002697
2698 return 0;
2699}
2700
2701static struct scripting_ops *script_spec__lookup(const char *spec)
2702{
2703 struct script_spec *s = script_spec__find(spec);
2704 if (!s)
2705 return NULL;
2706
2707 return s->ops;
2708}
2709
2710static void list_available_languages(void)
2711{
2712 struct script_spec *s;
2713
2714 fprintf(stderr, "\n");
2715 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002716 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002717
2718 list_for_each_entry(s, &script_specs, node)
2719 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2720
2721 fprintf(stderr, "\n");
2722}
2723
Adrian Hunter6ea4b5d2021-05-24 09:57:18 +03002724/* Find script file relative to current directory or exec path */
2725static char *find_script(const char *script)
2726{
2727 char path[PATH_MAX];
2728
2729 if (!scripting_ops) {
2730 const char *ext = strrchr(script, '.');
2731
2732 if (!ext)
2733 return NULL;
2734
2735 scripting_ops = script_spec__lookup(++ext);
2736 if (!scripting_ops)
2737 return NULL;
2738 }
2739
2740 if (access(script, R_OK)) {
2741 char *exec_path = get_argv_exec_path();
2742
2743 if (!exec_path)
2744 return NULL;
2745 snprintf(path, sizeof(path), "%s/scripts/%s/%s",
2746 exec_path, scripting_ops->dirname, script);
2747 free(exec_path);
2748 script = path;
2749 if (access(script, R_OK))
2750 return NULL;
2751 }
2752 return strdup(script);
2753}
2754
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002755static int parse_scriptname(const struct option *opt __maybe_unused,
2756 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002757{
2758 char spec[PATH_MAX];
2759 const char *script, *ext;
2760 int len;
2761
Tom Zanussif526d682010-01-27 02:27:52 -06002762 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002763 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002764 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002765 }
2766
2767 script = strchr(str, ':');
2768 if (script) {
2769 len = script - str;
2770 if (len >= PATH_MAX) {
2771 fprintf(stderr, "invalid language specifier");
2772 return -1;
2773 }
2774 strncpy(spec, str, len);
2775 spec[len] = '\0';
2776 scripting_ops = script_spec__lookup(spec);
2777 if (!scripting_ops) {
2778 fprintf(stderr, "invalid language specifier");
2779 return -1;
2780 }
2781 script++;
2782 } else {
2783 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002784 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002785 if (!ext) {
2786 fprintf(stderr, "invalid script extension");
2787 return -1;
2788 }
2789 scripting_ops = script_spec__lookup(++ext);
2790 if (!scripting_ops) {
2791 fprintf(stderr, "invalid script extension");
2792 return -1;
2793 }
2794 }
2795
Adrian Hunter6ea4b5d2021-05-24 09:57:18 +03002796 script_name = find_script(script);
2797 if (!script_name)
2798 script_name = strdup(script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002799
2800 return 0;
2801}
2802
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002803static int parse_output_fields(const struct option *opt __maybe_unused,
2804 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002805{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002806 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002807 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002808 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002809 int rc = 0;
2810 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002811 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002812 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002813
2814 if (!str)
2815 return -ENOMEM;
2816
David Ahern2c9e45f72011-03-17 10:03:21 -06002817 /* first word can state for which event type the user is specifying
2818 * the fields. If no type exists, the specified fields apply to all
2819 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002820 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002821 tok = strchr(str, ':');
2822 if (tok) {
2823 *tok = '\0';
2824 tok++;
2825 if (!strcmp(str, "hw"))
2826 type = PERF_TYPE_HARDWARE;
2827 else if (!strcmp(str, "sw"))
2828 type = PERF_TYPE_SOFTWARE;
2829 else if (!strcmp(str, "trace"))
2830 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002831 else if (!strcmp(str, "raw"))
2832 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002833 else if (!strcmp(str, "break"))
2834 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002835 else if (!strcmp(str, "synth"))
2836 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002837 else {
2838 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002839 rc = -EINVAL;
2840 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002841 }
2842
2843 if (output[type].user_set)
2844 pr_warning("Overriding previous field request for %s events.\n",
2845 event_type(type));
2846
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002847 /* Don't override defaults for +- */
2848 if (strchr(tok, '+') || strchr(tok, '-'))
2849 goto parse;
2850
David Ahern2c9e45f72011-03-17 10:03:21 -06002851 output[type].fields = 0;
2852 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002853 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002854
2855 } else {
2856 tok = str;
2857 if (strlen(str) == 0) {
2858 fprintf(stderr,
2859 "Cannot set fields to 'none' for all event types.\n");
2860 rc = -EINVAL;
2861 goto out;
2862 }
2863
Andi Kleen36ce5652017-06-02 08:48:10 -07002864 /* Don't override defaults for +- */
2865 if (strchr(str, '+') || strchr(str, '-'))
2866 goto parse;
2867
David Ahern2c9e45f72011-03-17 10:03:21 -06002868 if (output_set_by_user())
2869 pr_warning("Overriding previous field request for all events.\n");
2870
Adrian Hunter14057202017-06-21 13:17:19 +03002871 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002872 output[j].fields = 0;
2873 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002874 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002875 }
David Ahern1424dc92011-03-09 22:23:28 -07002876 }
2877
Andi Kleen36ce5652017-06-02 08:48:10 -07002878parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002879 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002880 if (*tok == '+') {
2881 if (change == SET)
2882 goto out_badmix;
2883 change = ADD;
2884 tok++;
2885 } else if (*tok == '-') {
2886 if (change == SET)
2887 goto out_badmix;
2888 change = REMOVE;
2889 tok++;
2890 } else {
2891 if (change != SET && change != DEFAULT)
2892 goto out_badmix;
2893 change = SET;
2894 }
2895
David Ahern745f43e2011-03-09 22:23:26 -07002896 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002897 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002898 break;
David Ahern745f43e2011-03-09 22:23:26 -07002899 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002900 if (i == imax && strcmp(tok, "flags") == 0) {
Yang Li85247112021-02-08 16:45:36 +08002901 print_flags = change != REMOVE;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002902 continue;
2903 }
David Ahern745f43e2011-03-09 22:23:26 -07002904 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002905 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002906 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002907 goto out;
2908 }
2909
2910 if (type == -1) {
2911 /* add user option to all events types for
2912 * which it is valid
2913 */
Adrian Hunter14057202017-06-21 13:17:19 +03002914 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002915 if (output[j].invalid_fields & all_output_options[i].field) {
2916 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2917 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002918 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002919 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002920 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002921 output[j].user_set_fields &= ~all_output_options[i].field;
Adrian Hunterb5164082020-05-27 21:02:50 +03002922 output[j].user_unset_fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002923 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002924 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002925 output[j].user_set_fields |= all_output_options[i].field;
Adrian Hunterb5164082020-05-27 21:02:50 +03002926 output[j].user_unset_fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002927 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002928 output[j].user_set = true;
2929 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002930 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002931 }
2932 } else {
2933 if (output[type].invalid_fields & all_output_options[i].field) {
2934 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2935 all_output_options[i].str, event_type(type));
2936
2937 rc = -EINVAL;
2938 goto out;
2939 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002940 if (change == REMOVE)
2941 output[type].fields &= ~all_output_options[i].field;
2942 else
2943 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002944 output[type].user_set = true;
2945 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002946 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002947 }
2948
2949 if (type >= 0) {
2950 if (output[type].fields == 0) {
2951 pr_debug("No fields requested for %s type. "
2952 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002953 }
David Ahern1424dc92011-03-09 22:23:28 -07002954 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002955 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002956
Andi Kleen36ce5652017-06-02 08:48:10 -07002957out_badmix:
2958 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2959 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002960out:
David Ahern745f43e2011-03-09 22:23:26 -07002961 free(str);
2962 return rc;
2963}
2964
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002965#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2966 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2967 if ((lang_dirent->d_type == DT_DIR || \
2968 (lang_dirent->d_type == DT_UNKNOWN && \
2969 is_directory(scripts_path, lang_dirent))) && \
2970 (strcmp(lang_dirent->d_name, ".")) && \
2971 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002972
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002973#define for_each_script(lang_path, lang_dir, script_dirent) \
2974 while ((script_dirent = readdir(lang_dir)) != NULL) \
2975 if (script_dirent->d_type != DT_DIR && \
2976 (script_dirent->d_type != DT_UNKNOWN || \
2977 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002978
2979
2980#define RECORD_SUFFIX "-record"
2981#define REPORT_SUFFIX "-report"
2982
2983struct script_desc {
2984 struct list_head node;
2985 char *name;
2986 char *half_liner;
2987 char *args;
2988};
2989
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002990static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002991
2992static struct script_desc *script_desc__new(const char *name)
2993{
2994 struct script_desc *s = zalloc(sizeof(*s));
2995
Tom Zanussib5b87312010-11-10 08:16:51 -06002996 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002997 s->name = strdup(name);
2998
2999 return s;
3000}
3001
3002static void script_desc__delete(struct script_desc *s)
3003{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03003004 zfree(&s->name);
3005 zfree(&s->half_liner);
3006 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003007 free(s);
3008}
3009
3010static void script_desc__add(struct script_desc *s)
3011{
3012 list_add_tail(&s->node, &script_descs);
3013}
3014
3015static struct script_desc *script_desc__find(const char *name)
3016{
3017 struct script_desc *s;
3018
3019 list_for_each_entry(s, &script_descs, node)
3020 if (strcasecmp(s->name, name) == 0)
3021 return s;
3022 return NULL;
3023}
3024
3025static struct script_desc *script_desc__findnew(const char *name)
3026{
3027 struct script_desc *s = script_desc__find(name);
3028
3029 if (s)
3030 return s;
3031
3032 s = script_desc__new(name);
3033 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03003034 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003035
3036 script_desc__add(s);
3037
3038 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003039}
3040
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003041static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003042{
3043 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003044 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003045
3046 if (strlen(str) > suffix_len) {
3047 p = str + strlen(str) - suffix_len;
3048 if (!strncmp(p, suffix, suffix_len))
3049 return p;
3050 }
3051
3052 return NULL;
3053}
3054
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003055static int read_script_info(struct script_desc *desc, const char *filename)
3056{
3057 char line[BUFSIZ], *p;
3058 FILE *fp;
3059
3060 fp = fopen(filename, "r");
3061 if (!fp)
3062 return -1;
3063
3064 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003065 p = skip_spaces(line);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003066 if (strlen(p) == 0)
3067 continue;
3068 if (*p != '#')
3069 continue;
3070 p++;
3071 if (strlen(p) && *p == '!')
3072 continue;
3073
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003074 p = skip_spaces(p);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003075 if (strlen(p) && p[strlen(p) - 1] == '\n')
3076 p[strlen(p) - 1] = '\0';
3077
3078 if (!strncmp(p, "description:", strlen("description:"))) {
3079 p += strlen("description:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003080 desc->half_liner = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003081 continue;
3082 }
3083
3084 if (!strncmp(p, "args:", strlen("args:"))) {
3085 p += strlen("args:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003086 desc->args = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003087 continue;
3088 }
3089 }
3090
3091 fclose(fp);
3092
3093 return 0;
3094}
3095
Robert Richter38efb532011-11-25 11:38:40 +01003096static char *get_script_root(struct dirent *script_dirent, const char *suffix)
3097{
3098 char *script_root, *str;
3099
3100 script_root = strdup(script_dirent->d_name);
3101 if (!script_root)
3102 return NULL;
3103
3104 str = (char *)ends_with(script_root, suffix);
3105 if (!str) {
3106 free(script_root);
3107 return NULL;
3108 }
3109
3110 *str = '\0';
3111 return script_root;
3112}
3113
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003114static int list_available_scripts(const struct option *opt __maybe_unused,
3115 const char *s __maybe_unused,
3116 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003117{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003118 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003119 char scripts_path[MAXPATHLEN];
3120 DIR *scripts_dir, *lang_dir;
3121 char script_path[MAXPATHLEN];
3122 char lang_path[MAXPATHLEN];
3123 struct script_desc *desc;
3124 char first_half[BUFSIZ];
3125 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003126
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003127 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003128
3129 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00003130 if (!scripts_dir) {
3131 fprintf(stdout,
3132 "open(%s) failed.\n"
3133 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
3134 scripts_path);
3135 exit(-1);
3136 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003137
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003138 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003139 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3140 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003141 lang_dir = opendir(lang_path);
3142 if (!lang_dir)
3143 continue;
3144
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003145 for_each_script(lang_path, lang_dir, script_dirent) {
3146 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01003147 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003148 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01003149 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3150 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003151 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01003152 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003153 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003154 }
3155 }
3156
3157 fprintf(stdout, "List of available trace scripts:\n");
3158 list_for_each_entry(desc, &script_descs, node) {
3159 sprintf(first_half, "%s %s", desc->name,
3160 desc->args ? desc->args : "");
3161 fprintf(stdout, " %-36s %s\n", first_half,
3162 desc->half_liner ? desc->half_liner : "");
3163 }
3164
3165 exit(0);
3166}
3167
Feng Tange5f37052012-09-07 16:42:26 +08003168/*
Feng Tang49e639e2012-10-30 11:56:03 +08003169 * Some scripts specify the required events in their "xxx-record" file,
3170 * this function will check if the events in perf.data match those
3171 * mentioned in the "xxx-record".
3172 *
3173 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3174 * which is covered well now. And new parsing code should be added to
Ingo Molnar4d39c892021-03-23 17:09:15 +01003175 * cover the future complex formats like event groups etc.
Feng Tang49e639e2012-10-30 11:56:03 +08003176 */
3177static int check_ev_match(char *dir_name, char *scriptname,
3178 struct perf_session *session)
3179{
3180 char filename[MAXPATHLEN], evname[128];
3181 char line[BUFSIZ], *p;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003182 struct evsel *pos;
Feng Tang49e639e2012-10-30 11:56:03 +08003183 int match, len;
3184 FILE *fp;
3185
Jiri Olsa77f18152018-03-19 09:29:01 +01003186 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08003187
3188 fp = fopen(filename, "r");
3189 if (!fp)
3190 return -1;
3191
3192 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003193 p = skip_spaces(line);
Feng Tang49e639e2012-10-30 11:56:03 +08003194 if (*p == '#')
3195 continue;
3196
3197 while (strlen(p)) {
3198 p = strstr(p, "-e");
3199 if (!p)
3200 break;
3201
3202 p += 2;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003203 p = skip_spaces(p);
Feng Tang49e639e2012-10-30 11:56:03 +08003204 len = strcspn(p, " \t");
3205 if (!len)
3206 break;
3207
3208 snprintf(evname, len + 1, "%s", p);
3209
3210 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003211 evlist__for_each_entry(session->evlist, pos) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03003212 if (!strcmp(evsel__name(pos), evname)) {
Feng Tang49e639e2012-10-30 11:56:03 +08003213 match = 1;
3214 break;
3215 }
3216 }
3217
3218 if (!match) {
3219 fclose(fp);
3220 return -1;
3221 }
3222 }
3223 }
3224
3225 fclose(fp);
3226 return 0;
3227}
3228
3229/*
Feng Tange5f37052012-09-07 16:42:26 +08003230 * Return -1 if none is found, otherwise the actual scripts number.
3231 *
3232 * Currently the only user of this function is the script browser, which
3233 * will list all statically runnable scripts, select one, execute it and
3234 * show the output in a perf browser.
3235 */
Andi Kleen905e4af2019-03-11 07:45:01 -07003236int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3237 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08003238{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003239 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08003240 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08003241 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08003242 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003243 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003244 .path = input_name,
3245 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003246 };
Feng Tange5f37052012-09-07 16:42:26 +08003247 char *temp;
3248 int i = 0;
3249
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003250 session = perf_session__new(&data, false, NULL);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05303251 if (IS_ERR(session))
3252 return PTR_ERR(session);
Feng Tang49e639e2012-10-30 11:56:03 +08003253
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003254 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003255
3256 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003257 if (!scripts_dir) {
3258 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003259 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003260 }
Feng Tange5f37052012-09-07 16:42:26 +08003261
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003262 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003263 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3264 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003265#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003266 if (strstr(lang_path, "perl"))
3267 continue;
3268#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003269#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003270 if (strstr(lang_path, "python"))
3271 continue;
3272#endif
3273
3274 lang_dir = opendir(lang_path);
3275 if (!lang_dir)
3276 continue;
3277
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003278 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003279 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003280 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003281 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003282 if (i >= num)
3283 break;
3284 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3285 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003286 script_dirent->d_name);
3287 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003288 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003289 (temp - script_dirent->d_name) + 1,
3290 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003291
3292 if (check_ev_match(lang_path,
3293 scripts_array[i], session))
3294 continue;
3295
Feng Tange5f37052012-09-07 16:42:26 +08003296 i++;
3297 }
Feng Tang49e639e2012-10-30 11:56:03 +08003298 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003299 }
3300
Feng Tang49e639e2012-10-30 11:56:03 +08003301 closedir(scripts_dir);
3302 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003303 return i;
3304}
3305
Tom Zanussi38752942009-12-15 02:53:39 -06003306static char *get_script_path(const char *script_root, const char *suffix)
3307{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003308 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003309 char scripts_path[MAXPATHLEN];
3310 char script_path[MAXPATHLEN];
3311 DIR *scripts_dir, *lang_dir;
3312 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003313 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003314
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003315 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003316
3317 scripts_dir = opendir(scripts_path);
3318 if (!scripts_dir)
3319 return NULL;
3320
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003321 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003322 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3323 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003324 lang_dir = opendir(lang_path);
3325 if (!lang_dir)
3326 continue;
3327
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003328 for_each_script(lang_path, lang_dir, script_dirent) {
3329 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003330 if (__script_root && !strcmp(script_root, __script_root)) {
3331 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003332 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003333 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3334 lang_path, script_dirent->d_name);
Andreas Gerstmayr27486a82020-04-02 14:43:38 +02003335 closedir(lang_dir);
Robert Richter38efb532011-11-25 11:38:40 +01003336 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003337 }
3338 free(__script_root);
3339 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003340 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003341 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003342 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003343
Robert Richter38efb532011-11-25 11:38:40 +01003344 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003345}
3346
Tom Zanussib5b87312010-11-10 08:16:51 -06003347static bool is_top_script(const char *script_path)
3348{
Yang Li85247112021-02-08 16:45:36 +08003349 return ends_with(script_path, "top") != NULL;
Tom Zanussib5b87312010-11-10 08:16:51 -06003350}
3351
3352static int has_required_arg(char *script_path)
3353{
3354 struct script_desc *desc;
3355 int n_args = 0;
3356 char *p;
3357
3358 desc = script_desc__new(NULL);
3359
3360 if (read_script_info(desc, script_path))
3361 goto out;
3362
3363 if (!desc->args)
3364 goto out;
3365
3366 for (p = desc->args; *p; p++)
3367 if (*p == '<')
3368 n_args++;
3369out:
3370 script_desc__delete(desc);
3371
3372 return n_args;
3373}
3374
David Ahernd54b1a92012-08-26 12:24:46 -06003375static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003376{
3377 char **__argv = malloc(sizeof(const char *) * argc);
3378
David Ahernd54b1a92012-08-26 12:24:46 -06003379 if (!__argv) {
3380 pr_err("malloc failed\n");
3381 return -1;
3382 }
3383
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003384 memcpy(__argv, argv, sizeof(const char *) * argc);
3385 argc = parse_options(argc, (const char **)__argv, record_options,
3386 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3387 free(__argv);
3388
David Ahernd54b1a92012-08-26 12:24:46 -06003389 system_wide = (argc == 0);
3390
3391 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003392}
3393
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003394static void script__setup_sample_type(struct perf_script *script)
3395{
3396 struct perf_session *session = script->session;
Arnaldo Carvalho de Melob3c2cc22020-06-17 09:24:21 -03003397 u64 sample_type = evlist__combined_sample_type(session->evlist);
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003398
3399 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3400 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003401 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003402 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003403 dwarf_callchain_users = true;
3404 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003405 callchain_param.record_mode = CALLCHAIN_LBR;
3406 else
3407 callchain_param.record_mode = CALLCHAIN_FP;
3408 }
Kan Liang680d1252020-03-19 13:25:14 -07003409
3410 if (script->stitch_lbr && (callchain_param.record_mode != CALLCHAIN_LBR)) {
3411 pr_warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
3412 "Please apply --call-graph lbr when recording.\n");
3413 script->stitch_lbr = false;
3414 }
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003415}
3416
Jiri Olsa89f16882018-09-13 14:54:03 +02003417static int process_stat_round_event(struct perf_session *session,
3418 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003419{
Jiri Olsa72932372019-08-28 15:57:16 +02003420 struct perf_record_stat_round *round = &event->stat_round;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003421 struct evsel *counter;
Jiri Olsae099eba2016-01-05 22:09:09 +01003422
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003423 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003424 perf_stat_process_counter(&stat_config, counter);
3425 process_stat(counter, round->time);
3426 }
3427
3428 process_stat_interval(round->time);
3429 return 0;
3430}
3431
Jiri Olsa89f16882018-09-13 14:54:03 +02003432static int process_stat_config_event(struct perf_session *session __maybe_unused,
3433 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003434{
3435 perf_event__read_stat_config(&stat_config, &event->stat_config);
3436 return 0;
3437}
3438
Jiri Olsacfc88742016-01-05 22:09:06 +01003439static int set_maps(struct perf_script *script)
3440{
Jiri Olsa63503db2019-07-21 13:23:52 +02003441 struct evlist *evlist = script->session->evlist;
Jiri Olsacfc88742016-01-05 22:09:06 +01003442
3443 if (!script->cpus || !script->threads)
3444 return 0;
3445
3446 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3447 return -EINVAL;
3448
Jiri Olsa453fa032019-07-21 13:24:43 +02003449 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
Jiri Olsacfc88742016-01-05 22:09:06 +01003450
Arnaldo Carvalho de Melo53f5e902020-11-30 09:31:04 -03003451 if (evlist__alloc_stats(evlist, true))
Jiri Olsacfc88742016-01-05 22:09:06 +01003452 return -ENOMEM;
3453
3454 script->allocated = true;
3455 return 0;
3456}
3457
3458static
Jiri Olsa89f16882018-09-13 14:54:03 +02003459int process_thread_map_event(struct perf_session *session,
3460 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003461{
Jiri Olsa89f16882018-09-13 14:54:03 +02003462 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003463 struct perf_script *script = container_of(tool, struct perf_script, tool);
3464
3465 if (script->threads) {
3466 pr_warning("Extra thread map event, ignoring.\n");
3467 return 0;
3468 }
3469
3470 script->threads = thread_map__new_event(&event->thread_map);
3471 if (!script->threads)
3472 return -ENOMEM;
3473
3474 return set_maps(script);
3475}
3476
3477static
Jiri Olsa89f16882018-09-13 14:54:03 +02003478int process_cpu_map_event(struct perf_session *session,
3479 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003480{
Jiri Olsa89f16882018-09-13 14:54:03 +02003481 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003482 struct perf_script *script = container_of(tool, struct perf_script, tool);
3483
3484 if (script->cpus) {
3485 pr_warning("Extra cpu map event, ignoring.\n");
3486 return 0;
3487 }
3488
3489 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3490 if (!script->cpus)
3491 return -ENOMEM;
3492
3493 return set_maps(script);
3494}
3495
Jiri Olsa89f16882018-09-13 14:54:03 +02003496static int process_feature_event(struct perf_session *session,
3497 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303498{
3499 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003500 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303501 return 0;
3502}
3503
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003504#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003505static int perf_script__process_auxtrace_info(struct perf_session *session,
3506 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003507{
Jiri Olsa89f16882018-09-13 14:54:03 +02003508 struct perf_tool *tool = session->tool;
3509
3510 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003511
3512 if (ret == 0) {
3513 struct perf_script *script = container_of(tool, struct perf_script, tool);
3514
3515 ret = perf_script__setup_per_event_dump(script);
3516 }
3517
3518 return ret;
3519}
3520#else
3521#define perf_script__process_auxtrace_info 0
3522#endif
3523
Andi Kleenb585ebd2018-09-20 11:05:36 -07003524static int parse_insn_trace(const struct option *opt __maybe_unused,
3525 const char *str __maybe_unused,
3526 int unset __maybe_unused)
3527{
3528 parse_output_fields(NULL, "+insn,-event,-period", 0);
3529 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003530 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003531 return 0;
3532}
3533
3534static int parse_xed(const struct option *opt __maybe_unused,
3535 const char *str __maybe_unused,
3536 int unset __maybe_unused)
3537{
Andi Kleen8c3e05c2020-05-21 19:09:14 -07003538 if (isatty(1))
3539 force_pager("xed -F insn: -A -64 | less");
3540 else
3541 force_pager("xed -F insn: -A -64");
Andi Kleenb585ebd2018-09-20 11:05:36 -07003542 return 0;
3543}
3544
Andi Kleend1b15522018-09-20 11:05:38 -07003545static int parse_call_trace(const struct option *opt __maybe_unused,
3546 const char *str __maybe_unused,
3547 int unset __maybe_unused)
3548{
3549 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3550 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003551 symbol_conf.nanosecs = true;
Jiri Olsa1c492422019-05-08 15:20:05 +02003552 symbol_conf.pad_output_len_dso = 50;
Andi Kleend1b15522018-09-20 11:05:38 -07003553 return 0;
3554}
3555
3556static int parse_callret_trace(const struct option *opt __maybe_unused,
3557 const char *str __maybe_unused,
3558 int unset __maybe_unused)
3559{
3560 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3561 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003562 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003563 return 0;
3564}
3565
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003566int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003567{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003568 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003569 bool header = false;
3570 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003571 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003572 char *rec_script_path = NULL;
3573 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003574 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003575 struct itrace_synth_opts itrace_synth_opts = {
3576 .set = false,
3577 .default_no_sample = true,
3578 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003579 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003580 char *script_path = NULL;
Adrian Hunter291961f2021-06-27 16:18:09 +03003581 const char *dlfilter_file = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003582 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003583 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003584 struct perf_script script = {
3585 .tool = {
3586 .sample = process_sample_event,
3587 .mmap = perf_event__process_mmap,
3588 .mmap2 = perf_event__process_mmap2,
3589 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303590 .namespaces = perf_event__process_namespaces,
Namhyung Kim160d4af2020-03-25 21:45:36 +09003591 .cgroup = perf_event__process_cgroup,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003592 .exit = perf_event__process_exit,
3593 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003594 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003595 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003596 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303597 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003598 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003599 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003600 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003601 .auxtrace = perf_event__process_auxtrace,
3602 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003603 .stat = perf_event__process_stat_event,
3604 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003605 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003606 .thread_map = process_thread_map_event,
3607 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003608 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003609 .ordering_requires_timestamps = true,
3610 },
3611 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003612 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003613 .mode = PERF_DATA_MODE_READ,
3614 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003615 const struct option options[] = {
3616 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3617 "dump raw trace in ASCII"),
3618 OPT_INCR('v', "verbose", &verbose,
3619 "be more verbose (show symbol address, etc)"),
3620 OPT_BOOLEAN('L', "Latency", &latency_format,
3621 "show latency attributes (irqs/preemption disabled, etc)"),
3622 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3623 list_available_scripts),
3624 OPT_CALLBACK('s', "script", NULL, "name",
3625 "script file name (lang:script name, script name, or *)",
3626 parse_scriptname),
3627 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3628 "generate perf-script.xx script in specified language"),
Adrian Hunter291961f2021-06-27 16:18:09 +03003629 OPT_STRING(0, "dlfilter", &dlfilter_file, "file", "filter .so file name"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003630 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3631 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3632 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003633 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3634 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003635 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3636 "file", "vmlinux pathname"),
3637 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3638 "file", "kallsyms pathname"),
3639 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3640 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003641 OPT_CALLBACK(0, "symfs", NULL, "directory",
3642 "Look for files with symbols relative to this directory",
3643 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003644 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003645 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003646 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003647 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003648 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303649 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3650 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
Kan Liang6b9bae62020-12-16 10:57:57 -08003651 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc,tod,"
Stephane Eranianc513de82021-01-05 11:57:50 -08003652 "data_page_size,code_page_size",
Andi Kleen48d02a12017-02-23 15:46:34 -08003653 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003654 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3655 "system-wide collection from all CPUs"),
Jin Yao4b799a92021-01-25 07:27:50 +08003656 OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
3657 "only consider symbols in these DSOs"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003658 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3659 "only consider these symbols"),
Jin Yao61d9fc42021-02-07 16:09:35 +08003660 OPT_INTEGER(0, "addr-range", &symbol_conf.addr_range,
3661 "Use with -S to list traced records within address range"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003662 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3663 "Decode instructions from itrace", parse_insn_trace),
3664 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3665 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003666 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3667 "Decode calls from from itrace", parse_call_trace),
3668 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3669 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003670 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3671 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003672 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3673 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003674 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3675 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3676 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003677 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3678 "only consider symbols in these pids"),
3679 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3680 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003681 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3682 "Set the maximum stack depth when parsing the callchain, "
3683 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003684 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Andi Kleen90b10f42019-03-14 15:50:00 -07003685 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
Hagen Paul Pfeifer26567ed2020-02-04 18:37:09 +01003686 OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to previous event"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003687 OPT_BOOLEAN('I', "show-info", &show_full_info,
3688 "display extended information from perf.data file"),
3689 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3690 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003691 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3692 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003693 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3694 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003695 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3696 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303697 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3698 "Show namespace events (if recorded)"),
Namhyung Kim160d4af2020-03-25 21:45:36 +09003699 OPT_BOOLEAN('\0', "show-cgroup-events", &script.show_cgroup_events,
3700 "Show cgroup events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003701 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3702 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003703 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3704 "Show round events (if recorded)"),
Jiri Olsa490c8cc2019-05-08 15:20:08 +02003705 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3706 "Show bpf related events (if recorded)"),
Adrian Hunter92ecf3a2020-05-12 15:19:21 +03003707 OPT_BOOLEAN('\0', "show-text-poke-events", &script.show_text_poke_events,
3708 "Show text poke related events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003709 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3710 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003711 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003712 OPT_INTEGER(0, "max-blocks", &max_blocks,
3713 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003714 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003715 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003716 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003717 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003718 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003719 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3720 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003721 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3722 "Enable symbol demangling"),
3723 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3724 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003725 OPT_STRING(0, "time", &script.time_str, "str",
3726 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003727 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3728 "Show inline function"),
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003729 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3730 "guest mount directory under which every guest os"
3731 " instance has a subdir"),
3732 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3733 "file", "file saving guest os vmlinux"),
3734 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3735 "file", "file saving guest os /proc/kallsyms"),
3736 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3737 "file", "file saving guest os /proc/modules"),
Kan Liang680d1252020-03-19 13:25:14 -07003738 OPT_BOOLEAN('\0', "stitch-lbr", &script.stitch_lbr,
3739 "Enable LBR callgraph stitching approach"),
Arnaldo Carvalho de Meloadd3a712019-08-15 11:21:21 -03003740 OPTS_EVSWITCH(&script.evswitch),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003741 OPT_END()
3742 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003743 const char * const script_subcommands[] = { "record", "report", NULL };
3744 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003745 "perf script [<options>]",
3746 "perf script [<options>] record <script> [<record-options>] <command>",
3747 "perf script [<options>] report <script> [script-args]",
3748 "perf script [<options>] <script> [<record-options>] <command>",
3749 "perf script [<options>] <top-script> [script-args]",
3750 NULL
3751 };
Tom Zanussi38752942009-12-15 02:53:39 -06003752
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003753 perf_set_singlethreaded();
3754
Tom Zanussib5b87312010-11-10 08:16:51 -06003755 setup_scripting();
3756
Yunlong Song40cae2b2015-03-18 21:35:54 +08003757 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003758 PARSE_OPT_STOP_AT_NON_OPTION);
3759
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003760 if (symbol_conf.guestmount ||
3761 symbol_conf.default_guest_vmlinux_name ||
3762 symbol_conf.default_guest_kallsyms ||
3763 symbol_conf.default_guest_modules) {
3764 /*
3765 * Enable guest sample processing.
3766 */
3767 perf_guest = true;
3768 }
3769
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003770 data.path = input_name;
3771 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003772
Tom Zanussib5b87312010-11-10 08:16:51 -06003773 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3774 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3775 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003776 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003777 }
3778
Tom Zanussib5b87312010-11-10 08:16:51 -06003779 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3780 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3781 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003782 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003783 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003784 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003785 return -1;
3786 }
Tom Zanussi38752942009-12-15 02:53:39 -06003787 }
3788
Hagen Paul Pfeifer26567ed2020-02-04 18:37:09 +01003789 if (reltime && deltatime) {
3790 fprintf(stderr,
3791 "reltime and deltatime - the two don't get along well. "
3792 "Please limit to --reltime or --deltatime.\n");
3793 return -1;
3794 }
3795
Adrian Hunter1c5c25b2020-04-01 13:16:05 +03003796 if ((itrace_synth_opts.callchain || itrace_synth_opts.add_callchain) &&
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003797 itrace_synth_opts.callchain_sz > scripting_max_stack)
3798 scripting_max_stack = itrace_synth_opts.callchain_sz;
3799
Ben Hutchings44e668c2010-10-10 16:10:03 +01003800 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003801 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003802
Tom Zanussib5b87312010-11-10 08:16:51 -06003803 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003804 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003805 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003806 pid_t pid;
3807
Tom Zanussib5b87312010-11-10 08:16:51 -06003808 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3809 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3810
3811 if (!rec_script_path && !rep_script_path) {
Adrian Hunter6ea4b5d2021-05-24 09:57:18 +03003812 script_name = find_script(argv[0]);
3813 if (script_name) {
3814 argc -= 1;
3815 argv += 1;
3816 goto script_found;
3817 }
Namhyung Kimc7118362015-10-25 00:49:27 +09003818 usage_with_options_msg(script_usage, options,
3819 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003820 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003821 }
3822
Tom Zanussib5b87312010-11-10 08:16:51 -06003823 if (is_top_script(argv[0])) {
3824 rep_args = argc - 1;
3825 } else {
3826 int rec_args;
3827
3828 rep_args = has_required_arg(rep_script_path);
3829 rec_args = (argc - 1) - rep_args;
3830 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003831 usage_with_options_msg(script_usage, options,
3832 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003833 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003834 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003835 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003836 }
3837
3838 if (pipe(live_pipe) < 0) {
3839 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003840 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003841 }
3842
3843 pid = fork();
3844 if (pid < 0) {
3845 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003846 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003847 }
3848
3849 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003850 j = 0;
3851
Tom Zanussia0cccc22010-04-01 23:59:25 -05003852 dup2(live_pipe[1], 1);
3853 close(live_pipe[0]);
3854
Robert Richter317df652011-11-25 15:05:25 +01003855 if (is_top_script(argv[0])) {
3856 system_wide = true;
3857 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003858 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3859 err = -1;
3860 goto out;
3861 }
Robert Richter317df652011-11-25 15:05:25 +01003862 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003863
3864 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003865 if (!__argv) {
3866 pr_err("malloc failed\n");
3867 err = -ENOMEM;
3868 goto out;
3869 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003870
Tom Zanussib5b87312010-11-10 08:16:51 -06003871 __argv[j++] = "/bin/sh";
3872 __argv[j++] = rec_script_path;
3873 if (system_wide)
3874 __argv[j++] = "-a";
3875 __argv[j++] = "-q";
3876 __argv[j++] = "-o";
3877 __argv[j++] = "-";
3878 for (i = rep_args + 1; i < argc; i++)
3879 __argv[j++] = argv[i];
3880 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003881
3882 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003883 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003884 exit(-1);
3885 }
3886
3887 dup2(live_pipe[0], 0);
3888 close(live_pipe[1]);
3889
Tom Zanussib5b87312010-11-10 08:16:51 -06003890 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003891 if (!__argv) {
3892 pr_err("malloc failed\n");
3893 err = -ENOMEM;
3894 goto out;
3895 }
3896
Tom Zanussib5b87312010-11-10 08:16:51 -06003897 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003898 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003899 __argv[j++] = rep_script_path;
3900 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003901 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003902 __argv[j++] = "-i";
3903 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003904 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003905
3906 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003907 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003908 exit(-1);
3909 }
Adrian Hunter6ea4b5d2021-05-24 09:57:18 +03003910script_found:
Tom Zanussib5b87312010-11-10 08:16:51 -06003911 if (rec_script_path)
3912 script_path = rec_script_path;
3913 if (rep_script_path)
3914 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003915
Tom Zanussib5b87312010-11-10 08:16:51 -06003916 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003917 j = 0;
3918
Robert Richter317df652011-11-25 15:05:25 +01003919 if (!rec_script_path)
3920 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003921 else if (!system_wide) {
3922 if (have_cmd(argc - 1, &argv[1]) != 0) {
3923 err = -1;
3924 goto out;
3925 }
3926 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003927
3928 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003929 if (!__argv) {
3930 pr_err("malloc failed\n");
3931 err = -ENOMEM;
3932 goto out;
3933 }
3934
Tom Zanussib5b87312010-11-10 08:16:51 -06003935 __argv[j++] = "/bin/sh";
3936 __argv[j++] = script_path;
3937 if (system_wide)
3938 __argv[j++] = "-a";
3939 for (i = 2; i < argc; i++)
3940 __argv[j++] = argv[i];
3941 __argv[j++] = NULL;
3942
3943 execvp("/bin/sh", (char **)__argv);
3944 free(__argv);
3945 exit(-1);
3946 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003947
Adrian Hunter291961f2021-06-27 16:18:09 +03003948 if (dlfilter_file) {
3949 dlfilter = dlfilter__new(dlfilter_file);
3950 if (!dlfilter)
3951 return -1;
3952 }
3953
Milian Wolffc1c9b962018-10-21 21:14:23 +02003954 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003955 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003956 use_browser = 0;
3957 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003958
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003959 session = perf_session__new(&data, false, &script.tool);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05303960 if (IS_ERR(session))
3961 return PTR_ERR(session);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003962
Jiri Olsae90debd2013-12-09 11:02:50 +01003963 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003964 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003965 perf_session__fprintf_info(session, stdout, show_full_info);
3966 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003967 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003968 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003969 if (show_full_info)
3970 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003971
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003972 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003973 goto out_delete;
3974
Andi Kleen3ab481a2019-03-05 06:47:45 -08003975 uname(&uts);
Song Liu9d491692019-06-20 18:44:38 -07003976 if (data.is_pipe || /* assume pipe_mode indicates native_arch */
3977 !strcmp(uts.machine, session->header.env.arch) ||
Andi Kleen3ab481a2019-03-05 06:47:45 -08003978 (!strcmp(uts.machine, "x86_64") &&
3979 !strcmp(session->header.env.arch, "i386")))
3980 native_arch = true;
3981
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003982 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003983 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003984
Andi Kleen99f753f2018-09-20 11:05:39 -07003985 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3986 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003987 itrace_synth_opts.thread_stack = true;
3988
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003989 session->itrace_synth_opts = &itrace_synth_opts;
3990
Anton Blanchard5d67be92011-07-04 21:57:50 +10003991 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003992 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3993 if (err < 0)
3994 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003995 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003996 }
3997
David Ahern1424dc92011-03-09 22:23:28 -07003998 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003999 symbol_conf.use_callchain = true;
4000 else
4001 symbol_conf.use_callchain = false;
4002
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03004003 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04004004 tep_set_function_resolver(session->tevent.pevent,
4005 machine__resolve_kernel_addr,
4006 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03004007 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02004008 err = -1;
4009 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03004010 }
4011
Tom Zanussi956ffd02009-11-25 01:15:46 -06004012 if (generate_script_lang) {
4013 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07004014 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06004015
David Ahern2c9e45f72011-03-17 10:03:21 -06004016 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07004017 fprintf(stderr,
4018 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09004019 err = -EINVAL;
4020 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07004021 }
4022
Jiri Olsa2d4f2792019-02-21 10:41:30 +01004023 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06004024 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09004025 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06004026 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09004027 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06004028 }
4029
4030 err = fstat(input, &perf_stat);
4031 if (err < 0) {
4032 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09004033 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06004034 }
4035
4036 if (!perf_stat.st_size) {
4037 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09004038 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06004039 }
4040
4041 scripting_ops = script_spec__lookup(generate_script_lang);
4042 if (!scripting_ops) {
4043 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09004044 err = -ENOENT;
4045 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06004046 }
4047
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01004048 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03004049 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09004050 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06004051 }
4052
Adrian Hunter291961f2021-06-27 16:18:09 +03004053 err = dlfilter__start(dlfilter, session);
4054 if (err)
4055 goto out_delete;
4056
Tom Zanussi956ffd02009-11-25 01:15:46 -06004057 if (script_name) {
Adrian Hunter67e50ce2021-05-30 22:22:59 +03004058 err = scripting_ops->start_script(script_name, argc, argv, session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06004059 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09004060 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01004061 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09004062 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06004063 }
4064
David Ahern9cbdb702011-04-06 21:54:20 -06004065
4066 err = perf_session__check_output_opt(session);
4067 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09004068 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06004069
Jin Yao284c4e12019-03-01 18:13:06 +08004070 if (script.time_str) {
Andi Kleenb3509b62019-10-11 11:21:39 -07004071 err = perf_time__parse_for_ranges_reltime(script.time_str, session,
Jin Yao284c4e12019-03-01 18:13:06 +08004072 &script.ptime_range,
4073 &script.range_size,
Andi Kleenb3509b62019-10-11 11:21:39 -07004074 &script.range_num,
4075 reltime);
Jin Yao284c4e12019-03-01 18:13:06 +08004076 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08004077 goto out_delete;
Adrian Hunter400ae982019-06-04 16:00:00 +03004078
4079 itrace_synth_opts__set_time_range(&itrace_synth_opts,
4080 script.ptime_range,
4081 script.range_num);
David Aherna91f4c42016-11-29 10:15:43 -07004082 }
4083
Arnaldo Carvalho de Melo124e02b2019-08-15 11:31:29 -03004084 err = evswitch__init(&script.evswitch, session->evlist, stderr);
4085 if (err)
4086 goto out_delete;
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03004087
Milian Wolffb13b04d2020-06-13 01:03:33 +02004088 if (zstd_init(&(session->zstd_data), 0) < 0)
4089 pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
4090
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03004091 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06004092
Adrian Hunterd445dd22014-08-15 22:08:37 +03004093 flush_scripting();
4094
Namhyung Kim6cc870f2014-08-12 15:40:33 +09004095out_delete:
Adrian Hunter400ae982019-06-04 16:00:00 +03004096 if (script.ptime_range) {
4097 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
Jin Yao284c4e12019-03-01 18:13:06 +08004098 zfree(&script.ptime_range);
Adrian Hunter400ae982019-06-04 16:00:00 +03004099 }
Jin Yaocc2ef582018-01-10 23:00:33 +08004100
Arnaldo Carvalho de Melo53f5e902020-11-30 09:31:04 -03004101 evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02004102 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09004103
4104 if (script_started)
4105 cleanup_scripting();
Adrian Hunter291961f2021-06-27 16:18:09 +03004106 dlfilter__cleanup(dlfilter);
Tom Zanussi956ffd02009-11-25 01:15:46 -06004107out:
4108 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02004109}