blob: b5bc85bd0bbea48aec2f745f5b94c42a7d0fec4f [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
Andrea Gelminib7eead82010-08-05 15:51:38 +02004#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02005#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02006#include "util/debug.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"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020013#include "util/symbol.h"
14#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010015#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020016#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070017#include "util/evlist.h"
18#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080019#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020020#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030021#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010022#include "util/cpumap.h"
23#include "util/thread_map.h"
24#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080025#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030026#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030027#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070028#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010029#include "util/path.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030030#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100031#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030032#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030033#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030034#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010035#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010036#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080037#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030038#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030039#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030040#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030041#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030042#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030043#include <sys/types.h>
44#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030045#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030046#include <unistd.h>
Andi Kleenb585ebd2018-09-20 11:05:36 -070047#include <subcmd/pager.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020048
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030049#include "sane_ctype.h"
50
Tom Zanussi956ffd02009-11-25 01:15:46 -060051static char const *script_name;
52static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020053static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020054static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020055static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070056static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090057static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010058static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030059static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030060static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100061static const char *cpu_list;
62static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010063static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080064static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060065
Adrian Hunter44cbe722015-09-25 16:15:50 +030066unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030067
David Ahern745f43e2011-03-09 22:23:26 -070068enum perf_output_field {
69 PERF_OUTPUT_COMM = 1U << 0,
70 PERF_OUTPUT_TID = 1U << 1,
71 PERF_OUTPUT_PID = 1U << 2,
72 PERF_OUTPUT_TIME = 1U << 3,
73 PERF_OUTPUT_CPU = 1U << 4,
74 PERF_OUTPUT_EVNAME = 1U << 5,
75 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060076 PERF_OUTPUT_IP = 1U << 7,
77 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060078 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060079 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090080 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020081 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020082 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020083 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020084 PERF_OUTPUT_BRSTACK = 1U << 15,
85 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010086 PERF_OUTPUT_DATA_SRC = 1U << 17,
87 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000088 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030089 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030090 PERF_OUTPUT_INSN = 1U << 21,
91 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080092 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070093 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030094 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -040095 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -070096 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -080097 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +010098 PERF_OUTPUT_MISC = 1U << 29,
David Ahern745f43e2011-03-09 22:23:26 -070099};
100
101struct output_option {
102 const char *str;
103 enum perf_output_field field;
104} all_output_options[] = {
105 {.str = "comm", .field = PERF_OUTPUT_COMM},
106 {.str = "tid", .field = PERF_OUTPUT_TID},
107 {.str = "pid", .field = PERF_OUTPUT_PID},
108 {.str = "time", .field = PERF_OUTPUT_TIME},
109 {.str = "cpu", .field = PERF_OUTPUT_CPU},
110 {.str = "event", .field = PERF_OUTPUT_EVNAME},
111 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600112 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700113 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600114 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600115 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900116 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200117 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200118 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200119 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700120 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200121 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
122 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100123 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
124 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000125 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300126 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300127 {.str = "insn", .field = PERF_OUTPUT_INSN},
128 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800129 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700130 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300131 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400132 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800133 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100134 {.str = "misc", .field = PERF_OUTPUT_MISC},
David Ahern745f43e2011-03-09 22:23:26 -0700135};
136
Adrian Hunter14057202017-06-21 13:17:19 +0300137enum {
138 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
139 OUTPUT_TYPE_MAX
140};
141
David Ahern745f43e2011-03-09 22:23:26 -0700142/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600143static struct {
144 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600145 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400146 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600147 u64 fields;
148 u64 invalid_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300149} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700150
David Ahern2c9e45f72011-03-17 10:03:21 -0600151 [PERF_TYPE_HARDWARE] = {
152 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700153
David Ahern2c9e45f72011-03-17 10:03:21 -0600154 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
155 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600156 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a7082018-05-17 12:03:26 +0530157 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
158 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600159
Wang Nan30372f02016-02-24 11:20:45 +0000160 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600161 },
162
163 [PERF_TYPE_SOFTWARE] = {
164 .user_set = false,
165
166 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
167 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600168 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a7082018-05-17 12:03:26 +0530169 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
170 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
171 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600172
173 .invalid_fields = PERF_OUTPUT_TRACE,
174 },
175
176 [PERF_TYPE_TRACEPOINT] = {
177 .user_set = false,
178
179 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
180 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000181 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600182 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700183
Seeteena Thoufeekfad76d432018-06-08 16:32:28 +0530184 [PERF_TYPE_HW_CACHE] = {
185 .user_set = false,
186
187 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
188 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
189 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
190 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
191 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
192
193 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
194 },
195
Arun Sharma0817a6a2011-04-14 10:38:18 -0700196 [PERF_TYPE_RAW] = {
197 .user_set = false,
198
199 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
200 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600201 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a7082018-05-17 12:03:26 +0530202 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
203 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
204 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
205 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700206
Wang Nan30372f02016-02-24 11:20:45 +0000207 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700208 },
Wang Nan27cfef02015-12-08 02:25:43 +0000209
210 [PERF_TYPE_BREAKPOINT] = {
211 .user_set = false,
212
213 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
214 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
215 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a7082018-05-17 12:03:26 +0530216 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
217 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000218
Wang Nan30372f02016-02-24 11:20:45 +0000219 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000220 },
Adrian Hunter14057202017-06-21 13:17:19 +0300221
222 [OUTPUT_TYPE_SYNTH] = {
223 .user_set = false,
224
225 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
226 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
227 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a7082018-05-17 12:03:26 +0530228 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
229 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300230
231 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
232 },
David Ahern1424dc92011-03-09 22:23:28 -0700233};
David Ahern745f43e2011-03-09 22:23:26 -0700234
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300235struct perf_evsel_script {
236 char *filename;
237 FILE *fp;
238 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800239 /* For metric output */
240 u64 val;
241 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300242};
243
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800244static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
245{
246 return (struct perf_evsel_script *)evsel->priv;
247}
248
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300249static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100250 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300251{
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800252 struct perf_evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300253
254 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100255 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300256 goto out_free;
257 es->fp = fopen(es->filename, "w");
258 if (es->fp == NULL)
259 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300260 }
261
262 return es;
263out_free_filename:
264 zfree(&es->filename);
265out_free:
266 free(es);
267 return NULL;
268}
269
270static void perf_evsel_script__delete(struct perf_evsel_script *es)
271{
272 zfree(&es->filename);
273 fclose(es->fp);
274 es->fp = NULL;
275 free(es);
276}
277
278static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
279{
280 struct stat st;
281
282 fstat(fileno(es->fp), &st);
283 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
284 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
285}
286
Adrian Hunter14057202017-06-21 13:17:19 +0300287static inline int output_type(unsigned int type)
288{
289 switch (type) {
290 case PERF_TYPE_SYNTH:
291 return OUTPUT_TYPE_SYNTH;
292 default:
293 return type;
294 }
295}
296
297static inline unsigned int attr_type(unsigned int type)
298{
299 switch (type) {
300 case OUTPUT_TYPE_SYNTH:
301 return PERF_TYPE_SYNTH;
302 default:
303 return type;
304 }
305}
306
David Ahern2c9e45f72011-03-17 10:03:21 -0600307static bool output_set_by_user(void)
308{
309 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300310 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600311 if (output[j].user_set)
312 return true;
313 }
314 return false;
315}
David Ahern745f43e2011-03-09 22:23:26 -0700316
David Ahern9cbdb702011-04-06 21:54:20 -0600317static const char *output_field2str(enum perf_output_field field)
318{
319 int i, imax = ARRAY_SIZE(all_output_options);
320 const char *str = "";
321
322 for (i = 0; i < imax; ++i) {
323 if (all_output_options[i].field == field) {
324 str = all_output_options[i].str;
325 break;
326 }
327 }
328 return str;
329}
330
Adrian Hunter14057202017-06-21 13:17:19 +0300331#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700332
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300333static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
334 u64 sample_type, const char *sample_msg,
335 enum perf_output_field field,
336 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700337{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300338 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300339 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600340 const char *evname;
341
342 if (attr->sample_type & sample_type)
343 return 0;
344
345 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300346 if (allow_user_set)
347 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300348 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600349 pr_err("Samples for '%s' event do not have %s attribute set. "
350 "Cannot print '%s' field.\n",
351 evname, sample_msg, output_field2str(field));
352 return -1;
353 }
354
355 /* user did not ask for it explicitly so remove from the default list */
356 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300357 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600358 pr_debug("Samples for '%s' event do not have %s attribute set. "
359 "Skipping '%s' field.\n",
360 evname, sample_msg, output_field2str(field));
361
362 return 0;
363}
364
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300365static int perf_evsel__check_stype(struct perf_evsel *evsel,
366 u64 sample_type, const char *sample_msg,
367 enum perf_output_field field)
368{
369 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
370 false);
371}
372
David Ahern9cbdb702011-04-06 21:54:20 -0600373static int perf_evsel__check_attr(struct perf_evsel *evsel,
374 struct perf_session *session)
375{
376 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300377 bool allow_user_set;
378
Jiri Olsae099eba2016-01-05 22:09:09 +0100379 if (perf_header__has_feat(&session->header, HEADER_STAT))
380 return 0;
381
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300382 allow_user_set = perf_header__has_feat(&session->header,
383 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600384
David Ahern1424dc92011-03-09 22:23:28 -0700385 if (PRINT_FIELD(TRACE) &&
386 !perf_session__has_traces(session, "record -R"))
387 return -EINVAL;
388
David Ahern787bef12011-05-27 14:28:43 -0600389 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300390 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
391 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700392 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700393 }
David Ahern7cec0922011-05-30 13:08:23 -0600394
395 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300396 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
397 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600398 return -EINVAL;
399
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100400 if (PRINT_FIELD(DATA_SRC) &&
401 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
402 PERF_OUTPUT_DATA_SRC))
403 return -EINVAL;
404
405 if (PRINT_FIELD(WEIGHT) &&
406 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
407 PERF_OUTPUT_WEIGHT))
408 return -EINVAL;
409
Andi Kleen37fed3d2018-09-18 05:32:09 -0700410 if (PRINT_FIELD(SYM) &&
411 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600412 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700413 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600414 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600415 return -EINVAL;
416 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900417 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
418 pr_err("Display of offsets requested but symbol is not"
419 "selected.\n");
420 return -EINVAL;
421 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700422 if (PRINT_FIELD(DSO) &&
423 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
424 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600425 return -EINVAL;
426 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200427 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
428 pr_err("Display of source line number requested but sample IP is not\n"
429 "selected. Hence, no address to lookup the source line number.\n");
430 return -EINVAL;
431 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800432 if (PRINT_FIELD(BRSTACKINSN) &&
433 !(perf_evlist__combined_branch_type(session->evlist) &
434 PERF_SAMPLE_BRANCH_ANY)) {
435 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
436 "Hint: run 'perf record -b ...'\n");
437 return -EINVAL;
438 }
David Ahern1424dc92011-03-09 22:23:28 -0700439 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300440 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
441 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700442 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700443
444 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300445 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
446 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700447 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700448
449 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300450 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
451 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700452 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600453
Stephane Eranianfc36f942015-08-31 18:41:10 +0200454 if (PRINT_FIELD(IREGS) &&
455 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
456 PERF_OUTPUT_IREGS))
457 return -EINVAL;
458
Andi Kleenb1491ac2017-09-05 11:40:57 -0700459 if (PRINT_FIELD(UREGS) &&
460 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
461 PERF_OUTPUT_UREGS))
462 return -EINVAL;
463
Kan Liang49d58f02017-08-29 13:11:11 -0400464 if (PRINT_FIELD(PHYS_ADDR) &&
465 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
466 PERF_OUTPUT_PHYS_ADDR))
467 return -EINVAL;
468
David Ahern9cbdb702011-04-06 21:54:20 -0600469 return 0;
470}
471
Adrian Hunter7ea95722013-11-01 15:51:30 +0200472static void set_print_ip_opts(struct perf_event_attr *attr)
473{
Adrian Hunter14057202017-06-21 13:17:19 +0300474 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200475
476 output[type].print_ip_opts = 0;
477 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300478 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200479
480 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300481 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200482
483 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300484 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200485
486 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300487 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200488
489 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300490 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200491}
492
David Ahern9cbdb702011-04-06 21:54:20 -0600493/*
494 * verify all user requested events exist and the samples
495 * have the expected data
496 */
497static int perf_session__check_output_opt(struct perf_session *session)
498{
He Kuang40f20e52016-05-16 04:51:19 +0000499 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600500 struct perf_evsel *evsel;
501
Adrian Hunter14057202017-06-21 13:17:19 +0300502 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
503 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600504
505 /*
506 * even if fields is set to 0 (ie., show nothing) event must
507 * exist if user explicitly includes it on the command line
508 */
Adrian Hunter14057202017-06-21 13:17:19 +0300509 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
510 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600511 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300512 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600513 event_type(j));
514 return -1;
515 }
516
517 if (evsel && output[j].fields &&
518 perf_evsel__check_attr(evsel, session))
519 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400520
521 if (evsel == NULL)
522 continue;
523
Adrian Hunter7ea95722013-11-01 15:51:30 +0200524 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700525 }
526
Adrian Hunter98526ee2014-07-31 09:00:59 +0300527 if (!no_callchain) {
528 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000529 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300530
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300531 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000532 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300533 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300534 use_callchain = true;
535 break;
536 }
537 }
He Kuang71ac8992016-08-04 11:25:43 +0000538 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300539 symbol_conf.use_callchain = false;
540 }
541
David Ahern80b8b492013-11-19 21:07:37 -0700542 /*
543 * set default for tracepoints to print symbols only
544 * if callchains are present
545 */
546 if (symbol_conf.use_callchain &&
547 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700548 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700549
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300550 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000551 if (evsel->attr.type != j)
552 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700553
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300554 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000555 output[j].fields |= PERF_OUTPUT_IP;
556 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a7082018-05-17 12:03:26 +0530557 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000558 output[j].fields |= PERF_OUTPUT_DSO;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300559 set_print_ip_opts(&evsel->attr);
He Kuang40f20e52016-05-16 04:51:19 +0000560 goto out;
561 }
David Ahern80b8b492013-11-19 21:07:37 -0700562 }
563 }
564
565out:
David Ahern1424dc92011-03-09 22:23:28 -0700566 return 0;
567}
David Ahern745f43e2011-03-09 22:23:26 -0700568
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300569static int perf_sample__fprintf_iregs(struct perf_sample *sample,
570 struct perf_event_attr *attr, FILE *fp)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200571{
572 struct regs_dump *regs = &sample->intr_regs;
573 uint64_t mask = attr->sample_regs_intr;
574 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300575 int printed = 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200576
577 if (!regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300578 return 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200579
580 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
581 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300582 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Stephane Eranianfc36f942015-08-31 18:41:10 +0200583 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300584
585 return printed;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200586}
587
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300588static int perf_sample__fprintf_uregs(struct perf_sample *sample,
589 struct perf_event_attr *attr, FILE *fp)
Andi Kleenb1491ac2017-09-05 11:40:57 -0700590{
591 struct regs_dump *regs = &sample->user_regs;
592 uint64_t mask = attr->sample_regs_user;
593 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300594 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700595
596 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300597 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700598
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300599 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700600
601 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
602 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300603 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700604 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300605
606 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700607}
608
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300609static int perf_sample__fprintf_start(struct perf_sample *sample,
610 struct thread *thread,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100611 struct perf_evsel *evsel,
612 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700613{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300614 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700615 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700616 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300617 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700618
David Ahern745f43e2011-03-09 22:23:26 -0700619 if (PRINT_FIELD(COMM)) {
620 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300621 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300622 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300623 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700624 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300625 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700626 }
David Ahernc70c94b2011-03-09 22:23:25 -0700627
David Ahern745f43e2011-03-09 22:23:26 -0700628 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300629 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700630 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300631 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700632 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300633 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700634
David Ahern745f43e2011-03-09 22:23:26 -0700635 if (PRINT_FIELD(CPU)) {
636 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300637 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700638 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300639 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700640 }
David Ahernc70c94b2011-03-09 22:23:25 -0700641
Jiri Olsa28a0b392018-01-07 17:03:52 +0100642 if (PRINT_FIELD(MISC)) {
643 int ret = 0;
644
645 #define has(m) \
646 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
647
648 if (has(KERNEL))
649 ret += fprintf(fp, "K");
650 if (has(USER))
651 ret += fprintf(fp, "U");
652 if (has(HYPERVISOR))
653 ret += fprintf(fp, "H");
654 if (has(GUEST_KERNEL))
655 ret += fprintf(fp, "G");
656 if (has(GUEST_USER))
657 ret += fprintf(fp, "g");
658
659 switch (type) {
660 case PERF_RECORD_MMAP:
661 case PERF_RECORD_MMAP2:
662 if (has(MMAP_DATA))
663 ret += fprintf(fp, "M");
664 break;
665 case PERF_RECORD_COMM:
666 if (has(COMM_EXEC))
667 ret += fprintf(fp, "E");
668 break;
669 case PERF_RECORD_SWITCH:
670 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300671 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100672 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300673 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
674 ret += fprintf(fp, "p");
675 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100676 default:
677 break;
678 }
679
680 #undef has
681
682 ret += fprintf(fp, "%*s", 6 - ret, " ");
683 printed += ret;
684 }
685
David Ahern745f43e2011-03-09 22:23:26 -0700686 if (PRINT_FIELD(TIME)) {
687 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300688 secs = nsecs / NSEC_PER_SEC;
689 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900690
Adrian Hunter83e19862015-09-25 16:15:36 +0300691 if (nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300692 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900693 else {
694 char sample_time[32];
695 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300696 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900697 }
David Ahern745f43e2011-03-09 22:23:26 -0700698 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300699
700 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700701}
702
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200703static inline char
704mispred_str(struct branch_entry *br)
705{
706 if (!(br->flags.mispred || br->flags.predicted))
707 return '-';
708
709 return br->flags.predicted ? 'P' : 'M';
710}
711
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300712static int perf_sample__fprintf_brstack(struct perf_sample *sample,
713 struct thread *thread,
714 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200715{
716 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700717 struct addr_location alf, alt;
718 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300719 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200720
721 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300722 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200723
724 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700725 from = br->entries[i].from;
726 to = br->entries[i].to;
727
728 if (PRINT_FIELD(DSO)) {
729 memset(&alf, 0, sizeof(alf));
730 memset(&alt, 0, sizeof(alt));
Arnaldo Carvalho de Melof07a2d32018-04-24 10:49:50 -0300731 thread__find_map(thread, sample->cpumode, from, &alf);
732 thread__find_map(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700733 }
734
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300735 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700736 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300737 printed += fprintf(fp, "(");
738 printed += map__fprintf_dsoname(alf.map, fp);
739 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700740 }
741
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300742 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700743 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300744 printed += fprintf(fp, "(");
745 printed += map__fprintf_dsoname(alt.map, fp);
746 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700747 }
748
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300749 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200750 mispred_str( br->entries + i),
751 br->entries[i].flags.in_tx? 'X' : '-',
752 br->entries[i].flags.abort? 'A' : '-',
753 br->entries[i].flags.cycles);
754 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300755
756 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200757}
758
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300759static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
760 struct thread *thread,
761 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200762{
763 struct branch_stack *br = sample->branch_stack;
764 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200765 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300766 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200767
768 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300769 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200770
771 for (i = 0; i < br->nr; i++) {
772
773 memset(&alf, 0, sizeof(alf));
774 memset(&alt, 0, sizeof(alt));
775 from = br->entries[i].from;
776 to = br->entries[i].to;
777
Arnaldo Carvalho de Melocc5f02f2018-04-24 11:32:30 -0300778 thread__find_symbol(thread, sample->cpumode, from, &alf);
779 thread__find_symbol(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200780
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300781 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700782 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300783 printed += fprintf(fp, "(");
784 printed += map__fprintf_dsoname(alf.map, fp);
785 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700786 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300787 printed += fprintf(fp, "%c", '/');
788 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700789 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300790 printed += fprintf(fp, "(");
791 printed += map__fprintf_dsoname(alt.map, fp);
792 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700793 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300794 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200795 mispred_str( br->entries + i),
796 br->entries[i].flags.in_tx? 'X' : '-',
797 br->entries[i].flags.abort? 'A' : '-',
798 br->entries[i].flags.cycles);
799 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300800
801 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200802}
803
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300804static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
805 struct thread *thread,
806 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700807{
808 struct branch_stack *br = sample->branch_stack;
809 struct addr_location alf, alt;
810 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300811 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700812
813 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300814 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700815
816 for (i = 0; i < br->nr; i++) {
817
818 memset(&alf, 0, sizeof(alf));
819 memset(&alt, 0, sizeof(alt));
820 from = br->entries[i].from;
821 to = br->entries[i].to;
822
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300823 if (thread__find_map(thread, sample->cpumode, from, &alf) &&
824 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700825 from = map__map_ip(alf.map, from);
826
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300827 if (thread__find_map(thread, sample->cpumode, to, &alt) &&
828 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700829 to = map__map_ip(alt.map, to);
830
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300831 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700832 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300833 printed += fprintf(fp, "(");
834 printed += map__fprintf_dsoname(alf.map, fp);
835 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700836 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300837 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700838 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300839 printed += fprintf(fp, "(");
840 printed += map__fprintf_dsoname(alt.map, fp);
841 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700842 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300843 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700844 mispred_str(br->entries + i),
845 br->entries[i].flags.in_tx ? 'X' : '-',
846 br->entries[i].flags.abort ? 'A' : '-',
847 br->entries[i].flags.cycles);
848 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300849
850 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700851}
Andi Kleen48d02a12017-02-23 15:46:34 -0800852#define MAXBB 16384UL
853
854static int grab_bb(u8 *buffer, u64 start, u64 end,
855 struct machine *machine, struct thread *thread,
856 bool *is64bit, u8 *cpumode, bool last)
857{
858 long offset, len;
859 struct addr_location al;
860 bool kernel;
861
862 if (!start || !end)
863 return 0;
864
865 kernel = machine__kernel_ip(machine, start);
866 if (kernel)
867 *cpumode = PERF_RECORD_MISC_KERNEL;
868 else
869 *cpumode = PERF_RECORD_MISC_USER;
870
871 /*
872 * Block overlaps between kernel and user.
873 * This can happen due to ring filtering
874 * On Intel CPUs the entry into the kernel is filtered,
875 * but the exit is not. Let the caller patch it up.
876 */
877 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300878 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800879 return -ENXIO;
880 }
881
882 memset(&al, 0, sizeof(al));
883 if (end - start > MAXBB - MAXINSN) {
884 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300885 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800886 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300887 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800888 return 0;
889 }
890
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300891 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300892 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800893 return 0;
894 }
895 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300896 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800897 return 0;
898 }
899
900 /* Load maps to ensure dso->is_64_bit has been updated */
901 map__load(al.map);
902
903 offset = al.map->map_ip(al.map, start);
904 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
905 end - start + MAXINSN);
906
907 *is64bit = al.map->dso->is_64_bit;
908 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300909 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800910 start, end);
911 return len;
912}
913
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300914static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
915 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -0700916 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -0800917{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300918 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
919 dump_insn(x, ip, inbuf, len, NULL),
920 en->flags.predicted ? " PRED" : "",
921 en->flags.mispred ? " MISPRED" : "",
922 en->flags.in_tx ? " INTX" : "",
923 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800924 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -0700925 *total_cycles += en->flags.cycles;
926 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800927 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300928 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800929 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300930 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800931}
932
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300933static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
934 u8 cpumode, int cpu, struct symbol **lastsym,
935 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800936{
937 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300938 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800939
940 memset(&al, 0, sizeof(al));
941
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300942 thread__find_map(thread, cpumode, addr, &al);
943
Andi Kleen48d02a12017-02-23 15:46:34 -0800944 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300945 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800946
947 al.cpu = cpu;
948 al.sym = NULL;
949 if (al.map)
950 al.sym = map__find_symbol(al.map, al.addr);
951
952 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300953 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800954
955 if (al.addr < al.sym->end)
956 off = al.addr - al.sym->start;
957 else
958 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300959 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800960 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300961 printed += fprintf(fp, "%+d", off);
962 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800963 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300964 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
965 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800966 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300967
968 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800969}
970
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300971static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
972 struct thread *thread,
973 struct perf_event_attr *attr,
974 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800975{
976 struct branch_stack *br = sample->branch_stack;
977 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300978 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800979 struct perf_insn x;
980 u8 buffer[MAXBB];
981 unsigned off;
982 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -0700983 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800984
985 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300986 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800987 nr = br->nr;
988 if (max_blocks && nr > max_blocks + 1)
989 nr = max_blocks + 1;
990
991 x.thread = thread;
992 x.cpu = sample->cpu;
993
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300994 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -0800995
996 /* Handle first from jump, of which we don't know the entry. */
997 len = grab_bb(buffer, br->entries[nr-1].from,
998 br->entries[nr-1].from,
999 machine, thread, &x.is64bit, &x.cpumode, false);
1000 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001001 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1002 x.cpumode, x.cpu, &lastsym, attr, fp);
1003 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001004 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -08001005 }
1006
1007 /* Print all blocks */
1008 for (i = nr - 2; i >= 0; i--) {
1009 if (br->entries[i].from || br->entries[i].to)
1010 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1011 br->entries[i].from,
1012 br->entries[i].to);
1013 start = br->entries[i + 1].to;
1014 end = br->entries[i].from;
1015
1016 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1017 /* Patch up missing kernel transfers due to ring filters */
1018 if (len == -ENXIO && i > 0) {
1019 end = br->entries[--i].from;
1020 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1021 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1022 }
1023 if (len <= 0)
1024 continue;
1025
1026 insn = 0;
1027 for (off = 0;; off += ilen) {
1028 uint64_t ip = start + off;
1029
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001030 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001031 if (ip == end) {
Andi Kleenfe571202018-09-24 10:07:32 -07001032 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp,
1033 &total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -08001034 break;
1035 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001036 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1037 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001038 if (ilen == 0)
1039 break;
1040 insn++;
1041 }
1042 }
1043 }
1044
1045 /*
1046 * Hit the branch? In this case we are already done, and the target
1047 * has not been executed yet.
1048 */
1049 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001050 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001051 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001052 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001053
1054 /*
1055 * Print final block upto sample
1056 */
1057 start = br->entries[0].to;
1058 end = sample->ip;
1059 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001060 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001061 if (len <= 0) {
1062 /* Print at least last IP if basic block did not work */
1063 len = grab_bb(buffer, sample->ip, sample->ip,
1064 machine, thread, &x.is64bit, &x.cpumode, false);
1065 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001066 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001067
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001068 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001069 dump_insn(&x, sample->ip, buffer, len, NULL));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001070 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001071 }
1072 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001073 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1074 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001075 if (ilen == 0)
1076 break;
1077 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001078out:
1079 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001080}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001081
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001082static int perf_sample__fprintf_addr(struct perf_sample *sample,
1083 struct thread *thread,
1084 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001085{
1086 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001087 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001088
1089 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001090 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001091
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001092 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001093
1094 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001095 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001096 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001097 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001098 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001099 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001100 }
1101
1102 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001103 printed += fprintf(fp, " (");
1104 printed += map__fprintf_dsoname(al.map, fp);
1105 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001106 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001107out:
1108 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001109}
1110
Andi Kleen99f753f2018-09-20 11:05:39 -07001111static const char *resolve_branch_sym(struct perf_sample *sample,
1112 struct perf_evsel *evsel,
1113 struct thread *thread,
1114 struct addr_location *al,
1115 u64 *ip)
1116{
1117 struct addr_location addr_al;
1118 struct perf_event_attr *attr = &evsel->attr;
1119 const char *name = NULL;
1120
1121 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1122 if (sample_addr_correlates_sym(attr)) {
1123 thread__resolve(thread, &addr_al, sample);
1124 if (addr_al.sym)
1125 name = addr_al.sym->name;
1126 else
1127 *ip = sample->addr;
1128 } else {
1129 *ip = sample->addr;
1130 }
1131 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1132 if (al->sym)
1133 name = al->sym->name;
1134 else
1135 *ip = sample->ip;
1136 }
1137 return name;
1138}
1139
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001140static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1141 struct perf_evsel *evsel,
1142 struct thread *thread,
1143 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001144{
1145 struct perf_event_attr *attr = &evsel->attr;
1146 size_t depth = thread_stack__depth(thread);
Adrian Huntere2167082016-06-23 16:40:58 +03001147 const char *name = NULL;
1148 static int spacing;
1149 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001150 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001151 u64 ip = 0;
1152
1153 /*
1154 * The 'return' has already been popped off the stack so the depth has
1155 * to be adjusted to match the 'call'.
1156 */
1157 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1158 depth += 1;
1159
Andi Kleen99f753f2018-09-20 11:05:39 -07001160 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001161
Andi Kleena78cdee2018-09-18 05:32:10 -07001162 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1163 dlen += fprintf(fp, "(");
1164 dlen += map__fprintf_dsoname(al->map, fp);
1165 dlen += fprintf(fp, ")\t");
1166 }
1167
Adrian Huntere2167082016-06-23 16:40:58 +03001168 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001169 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001170 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001171 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001172
1173 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001174 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001175
1176 /*
1177 * Try to keep the output length from changing frequently so that the
1178 * output lines up more nicely.
1179 */
1180 if (len > spacing || (len && len < spacing - 52))
1181 spacing = round_up(len + 4, 32);
1182
1183 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001184 len += fprintf(fp, "%*s", spacing - len, "");
1185
Andi Kleena78cdee2018-09-18 05:32:10 -07001186 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001187}
1188
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001189static int perf_sample__fprintf_insn(struct perf_sample *sample,
1190 struct perf_event_attr *attr,
1191 struct thread *thread,
1192 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001193{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001194 int printed = 0;
1195
Andi Kleen224e2c92016-10-07 16:42:27 +03001196 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001197 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen224e2c92016-10-07 16:42:27 +03001198 if (PRINT_FIELD(INSN)) {
1199 int i;
1200
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001201 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001202 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001203 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001204 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001205 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001206 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1207
1208 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001209}
1210
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001211static int perf_sample__fprintf_bts(struct perf_sample *sample,
1212 struct perf_evsel *evsel,
1213 struct thread *thread,
1214 struct addr_location *al,
1215 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001216{
1217 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001218 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001219 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001220 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001221
Adrian Huntere2167082016-06-23 16:40:58 +03001222 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001223 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001224
Akihiro Nagai95582592012-01-30 13:43:09 +09001225 /* print branch_from information */
1226 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001227 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001228 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001229
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001230 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001231 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001232 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001233 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001234
1235 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001236 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001237 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001238 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001239 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001240 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001241 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001242 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001243
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001244 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001245 }
1246
Akihiro Nagai95582592012-01-30 13:43:09 +09001247 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001248 if (PRINT_FIELD(ADDR) ||
1249 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001250 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001251 printed += fprintf(fp, " => ");
1252 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001253 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001254
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001255 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001256 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001257
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001258 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1259 return printed + fprintf(fp, "\n");
Akihiro Nagai95582592012-01-30 13:43:09 +09001260}
1261
Adrian Hunter055cd332016-06-23 16:40:56 +03001262static struct {
1263 u32 flags;
1264 const char *name;
1265} sample_flags[] = {
1266 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1267 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1268 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1269 {PERF_IP_FLAG_BRANCH, "jmp"},
1270 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1271 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1272 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1273 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1274 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1275 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1276 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1277 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1278 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1279 {0, NULL}
1280};
1281
Adrian Hunter62cb1b882018-09-20 16:00:43 +03001282static const char *sample_flags_to_name(u32 flags)
1283{
1284 int i;
1285
1286 for (i = 0; sample_flags[i].name ; i++) {
1287 if (sample_flags[i].flags == flags)
1288 return sample_flags[i].name;
1289 }
1290
1291 return NULL;
1292}
1293
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001294static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001295{
1296 const char *chars = PERF_IP_FLAG_CHARS;
1297 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001298 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1299 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001300 char str[33];
1301 int i, pos = 0;
1302
Adrian Hunter62cb1b882018-09-20 16:00:43 +03001303 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1304 if (name)
1305 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1306
1307 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1308 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1309 if (name)
1310 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1311 }
1312
1313 if (flags & PERF_IP_FLAG_TRACE_END) {
1314 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1315 if (name)
1316 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001317 }
1318
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001319 for (i = 0; i < n; i++, flags >>= 1) {
1320 if (flags & 1)
1321 str[pos++] = chars[i];
1322 }
1323 for (; i < 32; i++, flags >>= 1) {
1324 if (flags & 1)
1325 str[pos++] = '?';
1326 }
1327 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001328
Adrian Hunter62cb1b882018-09-20 16:00:43 +03001329 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001330}
1331
Wang Nan30372f02016-02-24 11:20:45 +00001332struct printer_data {
1333 int line_no;
1334 bool hit_nul;
1335 bool is_printable;
1336};
1337
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001338static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1339 unsigned int val,
1340 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001341{
1342 unsigned char ch = (unsigned char)val;
1343 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001344 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001345
1346 switch (op) {
1347 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001348 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001349 break;
1350 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001351 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001352 " ");
1353 break;
1354 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001355 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001356 break;
1357 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001358 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001359 break;
1360 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001361 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001362 break;
1363 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001364 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001365 break;
1366 case BINARY_PRINT_CHAR_DATA:
1367 if (printer_data->hit_nul && ch)
1368 printer_data->is_printable = false;
1369
1370 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001371 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001372
1373 if (!printer_data->is_printable)
1374 break;
1375
1376 if (ch == '\0')
1377 printer_data->hit_nul = true;
1378 else
1379 printer_data->is_printable = false;
1380 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001381 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001382 }
1383 break;
1384 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001385 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001386 break;
1387 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001388 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001389 printer_data->line_no++;
1390 break;
1391 case BINARY_PRINT_DATA_END:
1392 default:
1393 break;
1394 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001395
1396 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001397}
1398
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001399static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001400{
1401 unsigned int nr_bytes = sample->raw_size;
1402 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001403 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1404 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001405
1406 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001407 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1408
1409 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001410}
1411
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001412static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001413{
1414 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001415 return fprintf(fp, "%*s", spacing - len, "");
1416
1417 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001418}
1419
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001420static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001421{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001422 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001423}
1424
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001425static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001426{
1427 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1428 int len;
1429
1430 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001431 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001432
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001433 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001434 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001435 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001436}
1437
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001438static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001439{
1440 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1441 int len;
1442
1443 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001444 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001445
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001446 len = fprintf(fp, " hints: %#x extensions: %#x ",
1447 data->hints, data->extensions);
1448 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001449}
1450
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001451static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001452{
1453 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1454 int len;
1455
1456 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001457 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001458
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001459 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1460 data->hw, data->cstate, data->subcstate);
1461 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001462}
1463
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001464static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001465{
1466 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1467 int len;
1468
1469 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001470 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001471
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001472 len = fprintf(fp, " IP: %u ", data->ip);
1473 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001474}
1475
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001476static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001477{
1478 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1479 int len;
1480
1481 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001482 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001483
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001484 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001485 data->deepest_cstate, data->last_cstate,
1486 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001487 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001488}
1489
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001490static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001491{
1492 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1493 unsigned int percent, freq;
1494 int len;
1495
1496 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001497 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001498
1499 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001500 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001501 if (data->max_nonturbo) {
1502 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001503 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001504 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001505 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001506}
1507
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001508static int perf_sample__fprintf_synth(struct perf_sample *sample,
1509 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001510{
1511 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001512 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001513 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001514 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001515 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001516 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001517 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001518 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001519 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001520 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001521 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001522 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001523 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001524 default:
1525 break;
1526 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001527
1528 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001529}
1530
Jiri Olsa809e9422015-11-26 18:55:21 +01001531struct perf_script {
1532 struct perf_tool tool;
1533 struct perf_session *session;
1534 bool show_task_events;
1535 bool show_mmap_events;
1536 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301537 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001538 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001539 bool show_round_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001540 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001541 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001542 struct cpu_map *cpus;
1543 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001544 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001545 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001546 struct perf_time_interval *ptime_range;
1547 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001548 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001549};
1550
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001551static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1552{
1553 struct perf_evsel *evsel;
1554 int max = 0;
1555
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001556 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001557 int len = strlen(perf_evsel__name(evsel));
1558
1559 max = MAX(len, max);
1560 }
1561
1562 return max;
1563}
1564
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001565static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001566{
1567 struct mem_info mi = { .data_src.val = data_src };
1568 char decode[100];
1569 char out[100];
1570 static int maxlen;
1571 int len;
1572
1573 perf_script__meminfo_scnprintf(decode, 100, &mi);
1574
1575 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1576 if (maxlen < len)
1577 maxlen = len;
1578
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001579 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001580}
1581
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001582struct metric_ctx {
1583 struct perf_sample *sample;
1584 struct thread *thread;
1585 struct perf_evsel *evsel;
1586 FILE *fp;
1587};
1588
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001589static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1590 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001591 const char *fmt,
1592 const char *unit, double val)
1593{
1594 struct metric_ctx *mctx = ctx;
1595
1596 if (!fmt)
1597 return;
1598 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001599 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001600 fputs("\tmetric: ", mctx->fp);
1601 if (color)
1602 color_fprintf(mctx->fp, color, fmt, val);
1603 else
1604 printf(fmt, val);
1605 fprintf(mctx->fp, " %s\n", unit);
1606}
1607
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001608static void script_new_line(struct perf_stat_config *config __maybe_unused,
1609 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001610{
1611 struct metric_ctx *mctx = ctx;
1612
1613 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001614 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001615 fputs("\tmetric: ", mctx->fp);
1616}
1617
1618static void perf_sample__fprint_metric(struct perf_script *script,
1619 struct thread *thread,
1620 struct perf_evsel *evsel,
1621 struct perf_sample *sample,
1622 FILE *fp)
1623{
1624 struct perf_stat_output_ctx ctx = {
1625 .print_metric = script_print_metric,
1626 .new_line = script_new_line,
1627 .ctx = &(struct metric_ctx) {
1628 .sample = sample,
1629 .thread = thread,
1630 .evsel = evsel,
1631 .fp = fp,
1632 },
1633 .force_header = false,
1634 };
1635 struct perf_evsel *ev2;
1636 static bool init;
1637 u64 val;
1638
1639 if (!init) {
1640 perf_stat__init_shadow_stats();
1641 init = true;
1642 }
1643 if (!evsel->stats)
1644 perf_evlist__alloc_stats(script->session->evlist, false);
1645 if (evsel_script(evsel->leader)->gnum++ == 0)
1646 perf_stat__reset_shadow_stats();
1647 val = sample->period * evsel->scale;
1648 perf_stat__update_shadow_stats(evsel,
1649 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001650 sample->cpu,
1651 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001652 evsel_script(evsel)->val = val;
1653 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1654 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001655 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001656 evsel_script(ev2)->val,
1657 sample->cpu,
1658 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001659 NULL,
1660 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001661 }
1662 evsel_script(evsel->leader)->gnum = 0;
1663 }
1664}
1665
Andi Kleen99f753f2018-09-20 11:05:39 -07001666static bool show_event(struct perf_sample *sample,
1667 struct perf_evsel *evsel,
1668 struct thread *thread,
1669 struct addr_location *al)
1670{
1671 int depth = thread_stack__depth(thread);
1672
1673 if (!symbol_conf.graph_function)
1674 return true;
1675
1676 if (thread->filter) {
1677 if (depth <= thread->filter_entry_depth) {
1678 thread->filter = false;
1679 return false;
1680 }
1681 return true;
1682 } else {
1683 const char *s = symbol_conf.graph_function;
1684 u64 ip;
1685 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1686 &ip);
1687 unsigned nlen;
1688
1689 if (!name)
1690 return false;
1691 nlen = strlen(name);
1692 while (*s) {
1693 unsigned len = strcspn(s, ",");
1694 if (nlen == len && !strncmp(name, s, len)) {
1695 thread->filter = true;
1696 thread->filter_entry_depth = depth;
1697 return true;
1698 }
1699 s += len;
1700 if (*s == ',')
1701 s++;
1702 }
1703 return false;
1704 }
1705}
1706
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001707static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001708 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001709 struct addr_location *al,
1710 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001711{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001712 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001713 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001714 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001715 struct perf_evsel_script *es = evsel->priv;
1716 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001717
Adrian Hunter14057202017-06-21 13:17:19 +03001718 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001719 return;
1720
Andi Kleen99f753f2018-09-20 11:05:39 -07001721 if (!show_event(sample, evsel, thread, al))
1722 return;
1723
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001724 ++es->samples;
1725
Jiri Olsa28a0b392018-01-07 17:03:52 +01001726 perf_sample__fprintf_start(sample, thread, evsel,
1727 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001728
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001729 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001730 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001731
Namhyung Kime944d3d2013-11-18 14:34:52 +09001732 if (PRINT_FIELD(EVNAME)) {
1733 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001734
1735 if (!script->name_width)
1736 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1737
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001738 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001739 }
1740
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001741 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001742 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001743
Akihiro Nagai95582592012-01-30 13:43:09 +09001744 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001745 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001746 return;
1747 }
1748
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001749 if (PRINT_FIELD(TRACE)) {
1750 event_format__fprintf(evsel->tp_format, sample->cpu,
1751 sample->raw_data, sample->raw_size, fp);
1752 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001753
1754 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001755 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001756
David Ahern7cec0922011-05-30 13:08:23 -06001757 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001758 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001759
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001760 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001761 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001762
1763 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001764 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001765
David Ahern787bef12011-05-27 14:28:43 -06001766 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001767 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001768
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001769 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001770 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001771 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001772 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001773
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001774 fputc(cursor ? '\n' : ' ', fp);
1775 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001776 }
1777
Stephane Eranianfc36f942015-08-31 18:41:10 +02001778 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001779 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001780
Andi Kleenb1491ac2017-09-05 11:40:57 -07001781 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001782 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001783
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001784 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001785 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001786 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001787 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001788 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001789 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001790
Wang Nan30372f02016-02-24 11:20:45 +00001791 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001792 perf_sample__fprintf_bpf_output(sample, fp);
1793 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001794
1795 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001796 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1797 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001798
1799 if (PRINT_FIELD(METRIC))
1800 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee406782018-10-21 21:14:24 +02001801
1802 if (verbose)
1803 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001804}
1805
Tom Zanussi956ffd02009-11-25 01:15:46 -06001806static struct scripting_ops *scripting_ops;
1807
Jiri Olsa36e33c52016-01-06 11:49:56 +01001808static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1809{
1810 int nthreads = thread_map__nr(counter->threads);
1811 int ncpus = perf_evsel__nr_cpus(counter);
1812 int cpu, thread;
1813 static int header_printed;
1814
1815 if (counter->system_wide)
1816 nthreads = 1;
1817
1818 if (!header_printed) {
1819 printf("%3s %8s %15s %15s %15s %15s %s\n",
1820 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1821 header_printed = 1;
1822 }
1823
1824 for (thread = 0; thread < nthreads; thread++) {
1825 for (cpu = 0; cpu < ncpus; cpu++) {
1826 struct perf_counts_values *counts;
1827
1828 counts = perf_counts(counter->counts, cpu, thread);
1829
1830 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1831 counter->cpus->map[cpu],
1832 thread_map__pid(counter->threads, thread),
1833 counts->val,
1834 counts->ena,
1835 counts->run,
1836 tstamp,
1837 perf_evsel__name(counter));
1838 }
1839 }
1840}
1841
Jiri Olsae099eba2016-01-05 22:09:09 +01001842static void process_stat(struct perf_evsel *counter, u64 tstamp)
1843{
1844 if (scripting_ops && scripting_ops->process_stat)
1845 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001846 else
1847 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001848}
1849
1850static void process_stat_interval(u64 tstamp)
1851{
1852 if (scripting_ops && scripting_ops->process_stat_interval)
1853 scripting_ops->process_stat_interval(tstamp);
1854}
1855
Tom Zanussi956ffd02009-11-25 01:15:46 -06001856static void setup_scripting(void)
1857{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001858 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001859 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001860}
1861
Adrian Hunterd445dd22014-08-15 22:08:37 +03001862static int flush_scripting(void)
1863{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001864 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001865}
1866
Tom Zanussi956ffd02009-11-25 01:15:46 -06001867static int cleanup_scripting(void)
1868{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001869 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001870
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001871 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001872}
1873
Jiri Olsa809e9422015-11-26 18:55:21 +01001874static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001875 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001876 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001877 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb8682011-11-28 07:56:39 -02001878 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001879{
Jiri Olsa809e9422015-11-26 18:55:21 +01001880 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001881 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001882
Jin Yao2ab046c2017-12-08 21:13:46 +08001883 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1884 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001885 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001886 }
David Aherna91f4c42016-11-29 10:15:43 -07001887
David Ahern1424dc92011-03-09 22:23:28 -07001888 if (debug_mode) {
1889 if (sample->time < last_timestamp) {
1890 pr_err("Samples misordered, previous: %" PRIu64
1891 " this: %" PRIu64 "\n", last_timestamp,
1892 sample->time);
1893 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001894 }
David Ahern1424dc92011-03-09 22:23:28 -07001895 last_timestamp = sample->time;
1896 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001897 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001898
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001899 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001900 pr_err("problem processing %d event, skipping it.\n",
1901 event->header.type);
1902 return -1;
1903 }
1904
1905 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001906 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001907
Anton Blanchard5d67be92011-07-04 21:57:50 +10001908 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001909 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001910
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001911 if (scripting_ops)
1912 scripting_ops->process_event(event, sample, evsel, &al);
1913 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001914 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001915
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001916out_put:
1917 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001918 return 0;
1919}
1920
Adrian Hunter7ea95722013-11-01 15:51:30 +02001921static int process_attr(struct perf_tool *tool, union perf_event *event,
1922 struct perf_evlist **pevlist)
1923{
1924 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1925 struct perf_evlist *evlist;
1926 struct perf_evsel *evsel, *pos;
1927 int err;
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301928 static struct perf_evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001929
1930 err = perf_event__process_attr(tool, event, pevlist);
1931 if (err)
1932 return err;
1933
1934 evlist = *pevlist;
1935 evsel = perf_evlist__last(*pevlist);
1936
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301937 if (!evsel->priv) {
1938 if (scr->per_event_dump) {
1939 evsel->priv = perf_evsel_script__new(evsel,
1940 scr->session->data);
1941 } else {
1942 es = zalloc(sizeof(*es));
1943 if (!es)
1944 return -ENOMEM;
1945 es->fp = stdout;
1946 evsel->priv = es;
1947 }
1948 }
1949
Adrian Hunter14057202017-06-21 13:17:19 +03001950 if (evsel->attr.type >= PERF_TYPE_MAX &&
1951 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001952 return 0;
1953
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001954 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001955 if (pos->attr.type == evsel->attr.type && pos != evsel)
1956 return 0;
1957 }
1958
1959 set_print_ip_opts(&evsel->attr);
1960
Jiri Olsad2b5a312015-10-16 12:41:25 +02001961 if (evsel->attr.sample_type)
1962 err = perf_evsel__check_attr(evsel, scr->session);
1963
1964 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001965}
1966
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001967static int process_comm_event(struct perf_tool *tool,
1968 union perf_event *event,
1969 struct perf_sample *sample,
1970 struct machine *machine)
1971{
1972 struct thread *thread;
1973 struct perf_script *script = container_of(tool, struct perf_script, tool);
1974 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001975 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001976 int ret = -1;
1977
1978 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1979 if (thread == NULL) {
1980 pr_debug("problem processing COMM event, skipping it.\n");
1981 return -1;
1982 }
1983
1984 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1985 goto out;
1986
1987 if (!evsel->attr.sample_id_all) {
1988 sample->cpu = 0;
1989 sample->time = 0;
1990 sample->tid = event->comm.tid;
1991 sample->pid = event->comm.pid;
1992 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01001993 perf_sample__fprintf_start(sample, thread, evsel,
1994 PERF_RECORD_COMM, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001995 perf_event__fprintf(event, stdout);
1996 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001997out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001998 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001999 return ret;
2000}
2001
Hari Bathini96a44bb2017-03-08 02:12:06 +05302002static int process_namespaces_event(struct perf_tool *tool,
2003 union perf_event *event,
2004 struct perf_sample *sample,
2005 struct machine *machine)
2006{
2007 struct thread *thread;
2008 struct perf_script *script = container_of(tool, struct perf_script, tool);
2009 struct perf_session *session = script->session;
2010 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2011 int ret = -1;
2012
2013 thread = machine__findnew_thread(machine, event->namespaces.pid,
2014 event->namespaces.tid);
2015 if (thread == NULL) {
2016 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2017 return -1;
2018 }
2019
2020 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2021 goto out;
2022
2023 if (!evsel->attr.sample_id_all) {
2024 sample->cpu = 0;
2025 sample->time = 0;
2026 sample->tid = event->namespaces.tid;
2027 sample->pid = event->namespaces.pid;
2028 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002029 perf_sample__fprintf_start(sample, thread, evsel,
2030 PERF_RECORD_NAMESPACES, stdout);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302031 perf_event__fprintf(event, stdout);
2032 ret = 0;
2033out:
2034 thread__put(thread);
2035 return ret;
2036}
2037
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002038static int process_fork_event(struct perf_tool *tool,
2039 union perf_event *event,
2040 struct perf_sample *sample,
2041 struct machine *machine)
2042{
2043 struct thread *thread;
2044 struct perf_script *script = container_of(tool, struct perf_script, tool);
2045 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002046 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002047
2048 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2049 return -1;
2050
2051 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2052 if (thread == NULL) {
2053 pr_debug("problem processing FORK event, skipping it.\n");
2054 return -1;
2055 }
2056
2057 if (!evsel->attr.sample_id_all) {
2058 sample->cpu = 0;
2059 sample->time = event->fork.time;
2060 sample->tid = event->fork.tid;
2061 sample->pid = event->fork.pid;
2062 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002063 perf_sample__fprintf_start(sample, thread, evsel,
2064 PERF_RECORD_FORK, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002065 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002066 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002067
2068 return 0;
2069}
2070static int process_exit_event(struct perf_tool *tool,
2071 union perf_event *event,
2072 struct perf_sample *sample,
2073 struct machine *machine)
2074{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002075 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002076 struct thread *thread;
2077 struct perf_script *script = container_of(tool, struct perf_script, tool);
2078 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002079 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002080
2081 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2082 if (thread == NULL) {
2083 pr_debug("problem processing EXIT event, skipping it.\n");
2084 return -1;
2085 }
2086
2087 if (!evsel->attr.sample_id_all) {
2088 sample->cpu = 0;
2089 sample->time = 0;
Adrian Hunter53ff6bc32015-08-18 12:07:05 +03002090 sample->tid = event->fork.tid;
2091 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002092 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002093 perf_sample__fprintf_start(sample, thread, evsel,
2094 PERF_RECORD_EXIT, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002095 perf_event__fprintf(event, stdout);
2096
2097 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002098 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002099
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002100 thread__put(thread);
2101 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002102}
2103
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002104static int process_mmap_event(struct perf_tool *tool,
2105 union perf_event *event,
2106 struct perf_sample *sample,
2107 struct machine *machine)
2108{
2109 struct thread *thread;
2110 struct perf_script *script = container_of(tool, struct perf_script, tool);
2111 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002112 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002113
2114 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2115 return -1;
2116
2117 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2118 if (thread == NULL) {
2119 pr_debug("problem processing MMAP event, skipping it.\n");
2120 return -1;
2121 }
2122
2123 if (!evsel->attr.sample_id_all) {
2124 sample->cpu = 0;
2125 sample->time = 0;
2126 sample->tid = event->mmap.tid;
2127 sample->pid = event->mmap.pid;
2128 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002129 perf_sample__fprintf_start(sample, thread, evsel,
2130 PERF_RECORD_MMAP, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002131 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002132 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002133 return 0;
2134}
2135
2136static int process_mmap2_event(struct perf_tool *tool,
2137 union perf_event *event,
2138 struct perf_sample *sample,
2139 struct machine *machine)
2140{
2141 struct thread *thread;
2142 struct perf_script *script = container_of(tool, struct perf_script, tool);
2143 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002144 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002145
2146 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2147 return -1;
2148
2149 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2150 if (thread == NULL) {
2151 pr_debug("problem processing MMAP2 event, skipping it.\n");
2152 return -1;
2153 }
2154
2155 if (!evsel->attr.sample_id_all) {
2156 sample->cpu = 0;
2157 sample->time = 0;
2158 sample->tid = event->mmap2.tid;
2159 sample->pid = event->mmap2.pid;
2160 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002161 perf_sample__fprintf_start(sample, thread, evsel,
2162 PERF_RECORD_MMAP2, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002163 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002164 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002165 return 0;
2166}
2167
Adrian Hunter7c148982015-07-21 12:44:06 +03002168static int process_switch_event(struct perf_tool *tool,
2169 union perf_event *event,
2170 struct perf_sample *sample,
2171 struct machine *machine)
2172{
2173 struct thread *thread;
2174 struct perf_script *script = container_of(tool, struct perf_script, tool);
2175 struct perf_session *session = script->session;
2176 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2177
2178 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2179 return -1;
2180
2181 thread = machine__findnew_thread(machine, sample->pid,
2182 sample->tid);
2183 if (thread == NULL) {
2184 pr_debug("problem processing SWITCH event, skipping it.\n");
2185 return -1;
2186 }
2187
Jiri Olsa28a0b392018-01-07 17:03:52 +01002188 perf_sample__fprintf_start(sample, thread, evsel,
2189 PERF_RECORD_SWITCH, stdout);
Adrian Hunter7c148982015-07-21 12:44:06 +03002190 perf_event__fprintf(event, stdout);
2191 thread__put(thread);
2192 return 0;
2193}
2194
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002195static int
2196process_lost_event(struct perf_tool *tool,
2197 union perf_event *event,
2198 struct perf_sample *sample,
2199 struct machine *machine)
2200{
2201 struct perf_script *script = container_of(tool, struct perf_script, tool);
2202 struct perf_session *session = script->session;
2203 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2204 struct thread *thread;
2205
2206 thread = machine__findnew_thread(machine, sample->pid,
2207 sample->tid);
2208 if (thread == NULL)
2209 return -1;
2210
2211 perf_sample__fprintf_start(sample, thread, evsel,
2212 PERF_RECORD_LOST, stdout);
2213 perf_event__fprintf(event, stdout);
2214 thread__put(thread);
2215 return 0;
2216}
2217
Jiri Olsa3233b372018-02-06 19:17:59 +01002218static int
2219process_finished_round_event(struct perf_tool *tool __maybe_unused,
2220 union perf_event *event,
2221 struct ordered_events *oe __maybe_unused)
2222
2223{
2224 perf_event__fprintf(event, stdout);
2225 return 0;
2226}
2227
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002228static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002229{
2230 session_done = 1;
2231}
2232
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002233static void perf_script__fclose_per_event_dump(struct perf_script *script)
2234{
2235 struct perf_evlist *evlist = script->session->evlist;
2236 struct perf_evsel *evsel;
2237
2238 evlist__for_each_entry(evlist, evsel) {
2239 if (!evsel->priv)
2240 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002241 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002242 evsel->priv = NULL;
2243 }
2244}
2245
2246static int perf_script__fopen_per_event_dump(struct perf_script *script)
2247{
2248 struct perf_evsel *evsel;
2249
2250 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002251 /*
2252 * Already setup? I.e. we may be called twice in cases like
2253 * Intel PT, one for the intel_pt// and dummy events, then
2254 * for the evsels syntheized from the auxtrace info.
2255 *
2256 * Ses perf_script__process_auxtrace_info.
2257 */
2258 if (evsel->priv != NULL)
2259 continue;
2260
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002261 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002262 if (evsel->priv == NULL)
2263 goto out_err_fclose;
2264 }
2265
2266 return 0;
2267
2268out_err_fclose:
2269 perf_script__fclose_per_event_dump(script);
2270 return -1;
2271}
2272
2273static int perf_script__setup_per_event_dump(struct perf_script *script)
2274{
2275 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002276 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002277
2278 if (script->per_event_dump)
2279 return perf_script__fopen_per_event_dump(script);
2280
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002281 es_stdout.fp = stdout;
2282
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002283 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002284 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002285
2286 return 0;
2287}
2288
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002289static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2290{
2291 struct perf_evsel *evsel;
2292
2293 evlist__for_each_entry(script->session->evlist, evsel) {
2294 struct perf_evsel_script *es = evsel->priv;
2295
2296 perf_evsel_script__fprintf(es, stdout);
2297 perf_evsel_script__delete(es);
2298 evsel->priv = NULL;
2299 }
2300}
2301
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002302static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002303{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002304 int ret;
2305
Tom Zanussic239da32010-04-01 23:59:18 -05002306 signal(SIGINT, sig_handler);
2307
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002308 /* override event processing functions */
2309 if (script->show_task_events) {
2310 script->tool.comm = process_comm_event;
2311 script->tool.fork = process_fork_event;
2312 script->tool.exit = process_exit_event;
2313 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002314 if (script->show_mmap_events) {
2315 script->tool.mmap = process_mmap_event;
2316 script->tool.mmap2 = process_mmap2_event;
2317 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002318 if (script->show_switch_events)
2319 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302320 if (script->show_namespace_events)
2321 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002322 if (script->show_lost_events)
2323 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002324 if (script->show_round_events) {
2325 script->tool.ordered_events = false;
2326 script->tool.finished_round = process_finished_round_event;
2327 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002328
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002329 if (perf_script__setup_per_event_dump(script)) {
2330 pr_err("Couldn't create the per event dump files\n");
2331 return -1;
2332 }
2333
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002334 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002335
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002336 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002337 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002338
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002339 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002340 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002341
2342 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002343}
2344
Tom Zanussi956ffd02009-11-25 01:15:46 -06002345struct script_spec {
2346 struct list_head node;
2347 struct scripting_ops *ops;
2348 char spec[0];
2349};
2350
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002351static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002352
2353static struct script_spec *script_spec__new(const char *spec,
2354 struct scripting_ops *ops)
2355{
2356 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2357
2358 if (s != NULL) {
2359 strcpy(s->spec, spec);
2360 s->ops = ops;
2361 }
2362
2363 return s;
2364}
2365
Tom Zanussi956ffd02009-11-25 01:15:46 -06002366static void script_spec__add(struct script_spec *s)
2367{
2368 list_add_tail(&s->node, &script_specs);
2369}
2370
2371static struct script_spec *script_spec__find(const char *spec)
2372{
2373 struct script_spec *s;
2374
2375 list_for_each_entry(s, &script_specs, node)
2376 if (strcasecmp(s->spec, spec) == 0)
2377 return s;
2378 return NULL;
2379}
2380
Tom Zanussi956ffd02009-11-25 01:15:46 -06002381int script_spec_register(const char *spec, struct scripting_ops *ops)
2382{
2383 struct script_spec *s;
2384
2385 s = script_spec__find(spec);
2386 if (s)
2387 return -1;
2388
Taeung Song8560bae2016-02-26 00:13:10 +09002389 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002390 if (!s)
2391 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002392 else
2393 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002394
2395 return 0;
2396}
2397
2398static struct scripting_ops *script_spec__lookup(const char *spec)
2399{
2400 struct script_spec *s = script_spec__find(spec);
2401 if (!s)
2402 return NULL;
2403
2404 return s->ops;
2405}
2406
2407static void list_available_languages(void)
2408{
2409 struct script_spec *s;
2410
2411 fprintf(stderr, "\n");
2412 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002413 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002414
2415 list_for_each_entry(s, &script_specs, node)
2416 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2417
2418 fprintf(stderr, "\n");
2419}
2420
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002421static int parse_scriptname(const struct option *opt __maybe_unused,
2422 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002423{
2424 char spec[PATH_MAX];
2425 const char *script, *ext;
2426 int len;
2427
Tom Zanussif526d682010-01-27 02:27:52 -06002428 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002429 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002430 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002431 }
2432
2433 script = strchr(str, ':');
2434 if (script) {
2435 len = script - str;
2436 if (len >= PATH_MAX) {
2437 fprintf(stderr, "invalid language specifier");
2438 return -1;
2439 }
2440 strncpy(spec, str, len);
2441 spec[len] = '\0';
2442 scripting_ops = script_spec__lookup(spec);
2443 if (!scripting_ops) {
2444 fprintf(stderr, "invalid language specifier");
2445 return -1;
2446 }
2447 script++;
2448 } else {
2449 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002450 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002451 if (!ext) {
2452 fprintf(stderr, "invalid script extension");
2453 return -1;
2454 }
2455 scripting_ops = script_spec__lookup(++ext);
2456 if (!scripting_ops) {
2457 fprintf(stderr, "invalid script extension");
2458 return -1;
2459 }
2460 }
2461
2462 script_name = strdup(script);
2463
2464 return 0;
2465}
2466
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002467static int parse_output_fields(const struct option *opt __maybe_unused,
2468 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002469{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002470 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002471 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002472 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002473 int rc = 0;
2474 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002475 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002476 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002477
2478 if (!str)
2479 return -ENOMEM;
2480
David Ahern2c9e45f72011-03-17 10:03:21 -06002481 /* first word can state for which event type the user is specifying
2482 * the fields. If no type exists, the specified fields apply to all
2483 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002484 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002485 tok = strchr(str, ':');
2486 if (tok) {
2487 *tok = '\0';
2488 tok++;
2489 if (!strcmp(str, "hw"))
2490 type = PERF_TYPE_HARDWARE;
2491 else if (!strcmp(str, "sw"))
2492 type = PERF_TYPE_SOFTWARE;
2493 else if (!strcmp(str, "trace"))
2494 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002495 else if (!strcmp(str, "raw"))
2496 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002497 else if (!strcmp(str, "break"))
2498 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002499 else if (!strcmp(str, "synth"))
2500 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002501 else {
2502 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002503 rc = -EINVAL;
2504 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002505 }
2506
2507 if (output[type].user_set)
2508 pr_warning("Overriding previous field request for %s events.\n",
2509 event_type(type));
2510
2511 output[type].fields = 0;
2512 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002513 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002514
2515 } else {
2516 tok = str;
2517 if (strlen(str) == 0) {
2518 fprintf(stderr,
2519 "Cannot set fields to 'none' for all event types.\n");
2520 rc = -EINVAL;
2521 goto out;
2522 }
2523
Andi Kleen36ce5652017-06-02 08:48:10 -07002524 /* Don't override defaults for +- */
2525 if (strchr(str, '+') || strchr(str, '-'))
2526 goto parse;
2527
David Ahern2c9e45f72011-03-17 10:03:21 -06002528 if (output_set_by_user())
2529 pr_warning("Overriding previous field request for all events.\n");
2530
Adrian Hunter14057202017-06-21 13:17:19 +03002531 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002532 output[j].fields = 0;
2533 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002534 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002535 }
David Ahern1424dc92011-03-09 22:23:28 -07002536 }
2537
Andi Kleen36ce5652017-06-02 08:48:10 -07002538parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002539 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002540 if (*tok == '+') {
2541 if (change == SET)
2542 goto out_badmix;
2543 change = ADD;
2544 tok++;
2545 } else if (*tok == '-') {
2546 if (change == SET)
2547 goto out_badmix;
2548 change = REMOVE;
2549 tok++;
2550 } else {
2551 if (change != SET && change != DEFAULT)
2552 goto out_badmix;
2553 change = SET;
2554 }
2555
David Ahern745f43e2011-03-09 22:23:26 -07002556 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002557 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002558 break;
David Ahern745f43e2011-03-09 22:23:26 -07002559 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002560 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002561 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002562 continue;
2563 }
David Ahern745f43e2011-03-09 22:23:26 -07002564 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002565 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002566 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002567 goto out;
2568 }
2569
2570 if (type == -1) {
2571 /* add user option to all events types for
2572 * which it is valid
2573 */
Adrian Hunter14057202017-06-21 13:17:19 +03002574 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002575 if (output[j].invalid_fields & all_output_options[i].field) {
2576 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2577 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002578 } else {
2579 if (change == REMOVE)
2580 output[j].fields &= ~all_output_options[i].field;
2581 else
2582 output[j].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002583 output[j].user_set = true;
2584 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002585 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002586 }
2587 } else {
2588 if (output[type].invalid_fields & all_output_options[i].field) {
2589 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2590 all_output_options[i].str, event_type(type));
2591
2592 rc = -EINVAL;
2593 goto out;
2594 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002595 output[type].user_set = true;
2596 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002597 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002598 }
2599
2600 if (type >= 0) {
2601 if (output[type].fields == 0) {
2602 pr_debug("No fields requested for %s type. "
2603 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002604 }
David Ahern1424dc92011-03-09 22:23:28 -07002605 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002606 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002607
Andi Kleen36ce5652017-06-02 08:48:10 -07002608out_badmix:
2609 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2610 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002611out:
David Ahern745f43e2011-03-09 22:23:26 -07002612 free(str);
2613 return rc;
2614}
2615
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002616#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2617 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2618 if ((lang_dirent->d_type == DT_DIR || \
2619 (lang_dirent->d_type == DT_UNKNOWN && \
2620 is_directory(scripts_path, lang_dirent))) && \
2621 (strcmp(lang_dirent->d_name, ".")) && \
2622 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002623
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002624#define for_each_script(lang_path, lang_dir, script_dirent) \
2625 while ((script_dirent = readdir(lang_dir)) != NULL) \
2626 if (script_dirent->d_type != DT_DIR && \
2627 (script_dirent->d_type != DT_UNKNOWN || \
2628 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002629
2630
2631#define RECORD_SUFFIX "-record"
2632#define REPORT_SUFFIX "-report"
2633
2634struct script_desc {
2635 struct list_head node;
2636 char *name;
2637 char *half_liner;
2638 char *args;
2639};
2640
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002641static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002642
2643static struct script_desc *script_desc__new(const char *name)
2644{
2645 struct script_desc *s = zalloc(sizeof(*s));
2646
Tom Zanussib5b87312010-11-10 08:16:51 -06002647 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002648 s->name = strdup(name);
2649
2650 return s;
2651}
2652
2653static void script_desc__delete(struct script_desc *s)
2654{
Arnaldo Carvalho de Melo74cf249d2013-12-27 16:55:14 -03002655 zfree(&s->name);
2656 zfree(&s->half_liner);
2657 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002658 free(s);
2659}
2660
2661static void script_desc__add(struct script_desc *s)
2662{
2663 list_add_tail(&s->node, &script_descs);
2664}
2665
2666static struct script_desc *script_desc__find(const char *name)
2667{
2668 struct script_desc *s;
2669
2670 list_for_each_entry(s, &script_descs, node)
2671 if (strcasecmp(s->name, name) == 0)
2672 return s;
2673 return NULL;
2674}
2675
2676static struct script_desc *script_desc__findnew(const char *name)
2677{
2678 struct script_desc *s = script_desc__find(name);
2679
2680 if (s)
2681 return s;
2682
2683 s = script_desc__new(name);
2684 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002685 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002686
2687 script_desc__add(s);
2688
2689 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002690}
2691
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002692static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002693{
2694 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002695 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002696
2697 if (strlen(str) > suffix_len) {
2698 p = str + strlen(str) - suffix_len;
2699 if (!strncmp(p, suffix, suffix_len))
2700 return p;
2701 }
2702
2703 return NULL;
2704}
2705
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002706static int read_script_info(struct script_desc *desc, const char *filename)
2707{
2708 char line[BUFSIZ], *p;
2709 FILE *fp;
2710
2711 fp = fopen(filename, "r");
2712 if (!fp)
2713 return -1;
2714
2715 while (fgets(line, sizeof(line), fp)) {
2716 p = ltrim(line);
2717 if (strlen(p) == 0)
2718 continue;
2719 if (*p != '#')
2720 continue;
2721 p++;
2722 if (strlen(p) && *p == '!')
2723 continue;
2724
2725 p = ltrim(p);
2726 if (strlen(p) && p[strlen(p) - 1] == '\n')
2727 p[strlen(p) - 1] = '\0';
2728
2729 if (!strncmp(p, "description:", strlen("description:"))) {
2730 p += strlen("description:");
2731 desc->half_liner = strdup(ltrim(p));
2732 continue;
2733 }
2734
2735 if (!strncmp(p, "args:", strlen("args:"))) {
2736 p += strlen("args:");
2737 desc->args = strdup(ltrim(p));
2738 continue;
2739 }
2740 }
2741
2742 fclose(fp);
2743
2744 return 0;
2745}
2746
Robert Richter38efb532011-11-25 11:38:40 +01002747static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2748{
2749 char *script_root, *str;
2750
2751 script_root = strdup(script_dirent->d_name);
2752 if (!script_root)
2753 return NULL;
2754
2755 str = (char *)ends_with(script_root, suffix);
2756 if (!str) {
2757 free(script_root);
2758 return NULL;
2759 }
2760
2761 *str = '\0';
2762 return script_root;
2763}
2764
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002765static int list_available_scripts(const struct option *opt __maybe_unused,
2766 const char *s __maybe_unused,
2767 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002768{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002769 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002770 char scripts_path[MAXPATHLEN];
2771 DIR *scripts_dir, *lang_dir;
2772 char script_path[MAXPATHLEN];
2773 char lang_path[MAXPATHLEN];
2774 struct script_desc *desc;
2775 char first_half[BUFSIZ];
2776 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002777
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002778 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002779
2780 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002781 if (!scripts_dir) {
2782 fprintf(stdout,
2783 "open(%s) failed.\n"
2784 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2785 scripts_path);
2786 exit(-1);
2787 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002788
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002789 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002790 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2791 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002792 lang_dir = opendir(lang_path);
2793 if (!lang_dir)
2794 continue;
2795
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002796 for_each_script(lang_path, lang_dir, script_dirent) {
2797 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002798 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002799 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002800 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2801 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002802 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002803 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002804 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002805 }
2806 }
2807
2808 fprintf(stdout, "List of available trace scripts:\n");
2809 list_for_each_entry(desc, &script_descs, node) {
2810 sprintf(first_half, "%s %s", desc->name,
2811 desc->args ? desc->args : "");
2812 fprintf(stdout, " %-36s %s\n", first_half,
2813 desc->half_liner ? desc->half_liner : "");
2814 }
2815
2816 exit(0);
2817}
2818
Feng Tange5f37052012-09-07 16:42:26 +08002819/*
Feng Tang49e639e2012-10-30 11:56:03 +08002820 * Some scripts specify the required events in their "xxx-record" file,
2821 * this function will check if the events in perf.data match those
2822 * mentioned in the "xxx-record".
2823 *
2824 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2825 * which is covered well now. And new parsing code should be added to
2826 * cover the future complexing formats like event groups etc.
2827 */
2828static int check_ev_match(char *dir_name, char *scriptname,
2829 struct perf_session *session)
2830{
2831 char filename[MAXPATHLEN], evname[128];
2832 char line[BUFSIZ], *p;
2833 struct perf_evsel *pos;
2834 int match, len;
2835 FILE *fp;
2836
Jiri Olsa77f18152018-03-19 09:29:01 +01002837 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08002838
2839 fp = fopen(filename, "r");
2840 if (!fp)
2841 return -1;
2842
2843 while (fgets(line, sizeof(line), fp)) {
2844 p = ltrim(line);
2845 if (*p == '#')
2846 continue;
2847
2848 while (strlen(p)) {
2849 p = strstr(p, "-e");
2850 if (!p)
2851 break;
2852
2853 p += 2;
2854 p = ltrim(p);
2855 len = strcspn(p, " \t");
2856 if (!len)
2857 break;
2858
2859 snprintf(evname, len + 1, "%s", p);
2860
2861 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002862 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002863 if (!strcmp(perf_evsel__name(pos), evname)) {
2864 match = 1;
2865 break;
2866 }
2867 }
2868
2869 if (!match) {
2870 fclose(fp);
2871 return -1;
2872 }
2873 }
2874 }
2875
2876 fclose(fp);
2877 return 0;
2878}
2879
2880/*
Feng Tange5f37052012-09-07 16:42:26 +08002881 * Return -1 if none is found, otherwise the actual scripts number.
2882 *
2883 * Currently the only user of this function is the script browser, which
2884 * will list all statically runnable scripts, select one, execute it and
2885 * show the output in a perf browser.
2886 */
2887int find_scripts(char **scripts_array, char **scripts_path_array)
2888{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002889 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002890 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002891 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002892 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002893 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002894 .file = {
2895 .path = input_name,
2896 },
2897 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002898 };
Feng Tange5f37052012-09-07 16:42:26 +08002899 char *temp;
2900 int i = 0;
2901
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002902 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002903 if (!session)
2904 return -1;
2905
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002906 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002907
2908 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002909 if (!scripts_dir) {
2910 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002911 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002912 }
Feng Tange5f37052012-09-07 16:42:26 +08002913
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002914 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002915 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
2916 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08002917#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08002918 if (strstr(lang_path, "perl"))
2919 continue;
2920#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08002921#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08002922 if (strstr(lang_path, "python"))
2923 continue;
2924#endif
2925
2926 lang_dir = opendir(lang_path);
2927 if (!lang_dir)
2928 continue;
2929
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002930 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002931 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002932 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002933 continue;
2934 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002935 script_dirent->d_name);
2936 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002937 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002938 (temp - script_dirent->d_name) + 1,
2939 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002940
2941 if (check_ev_match(lang_path,
2942 scripts_array[i], session))
2943 continue;
2944
Feng Tange5f37052012-09-07 16:42:26 +08002945 i++;
2946 }
Feng Tang49e639e2012-10-30 11:56:03 +08002947 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002948 }
2949
Feng Tang49e639e2012-10-30 11:56:03 +08002950 closedir(scripts_dir);
2951 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002952 return i;
2953}
2954
Tom Zanussi38752942009-12-15 02:53:39 -06002955static char *get_script_path(const char *script_root, const char *suffix)
2956{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002957 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002958 char scripts_path[MAXPATHLEN];
2959 char script_path[MAXPATHLEN];
2960 DIR *scripts_dir, *lang_dir;
2961 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002962 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002963
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002964 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002965
2966 scripts_dir = opendir(scripts_path);
2967 if (!scripts_dir)
2968 return NULL;
2969
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002970 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002971 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2972 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002973 lang_dir = opendir(lang_path);
2974 if (!lang_dir)
2975 continue;
2976
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002977 for_each_script(lang_path, lang_dir, script_dirent) {
2978 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002979 if (__script_root && !strcmp(script_root, __script_root)) {
2980 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002981 closedir(lang_dir);
2982 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01002983 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2984 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002985 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002986 }
2987 free(__script_root);
2988 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002989 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002990 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002991 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002992
Robert Richter38efb532011-11-25 11:38:40 +01002993 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002994}
2995
Tom Zanussib5b87312010-11-10 08:16:51 -06002996static bool is_top_script(const char *script_path)
2997{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002998 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002999}
3000
3001static int has_required_arg(char *script_path)
3002{
3003 struct script_desc *desc;
3004 int n_args = 0;
3005 char *p;
3006
3007 desc = script_desc__new(NULL);
3008
3009 if (read_script_info(desc, script_path))
3010 goto out;
3011
3012 if (!desc->args)
3013 goto out;
3014
3015 for (p = desc->args; *p; p++)
3016 if (*p == '<')
3017 n_args++;
3018out:
3019 script_desc__delete(desc);
3020
3021 return n_args;
3022}
3023
David Ahernd54b1a92012-08-26 12:24:46 -06003024static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003025{
3026 char **__argv = malloc(sizeof(const char *) * argc);
3027
David Ahernd54b1a92012-08-26 12:24:46 -06003028 if (!__argv) {
3029 pr_err("malloc failed\n");
3030 return -1;
3031 }
3032
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003033 memcpy(__argv, argv, sizeof(const char *) * argc);
3034 argc = parse_options(argc, (const char **)__argv, record_options,
3035 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3036 free(__argv);
3037
David Ahernd54b1a92012-08-26 12:24:46 -06003038 system_wide = (argc == 0);
3039
3040 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003041}
3042
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003043static void script__setup_sample_type(struct perf_script *script)
3044{
3045 struct perf_session *session = script->session;
3046 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3047
3048 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3049 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003050 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003051 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003052 dwarf_callchain_users = true;
3053 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003054 callchain_param.record_mode = CALLCHAIN_LBR;
3055 else
3056 callchain_param.record_mode = CALLCHAIN_FP;
3057 }
3058}
3059
Jiri Olsa89f16882018-09-13 14:54:03 +02003060static int process_stat_round_event(struct perf_session *session,
3061 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003062{
3063 struct stat_round_event *round = &event->stat_round;
3064 struct perf_evsel *counter;
3065
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003066 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003067 perf_stat_process_counter(&stat_config, counter);
3068 process_stat(counter, round->time);
3069 }
3070
3071 process_stat_interval(round->time);
3072 return 0;
3073}
3074
Jiri Olsa89f16882018-09-13 14:54:03 +02003075static int process_stat_config_event(struct perf_session *session __maybe_unused,
3076 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003077{
3078 perf_event__read_stat_config(&stat_config, &event->stat_config);
3079 return 0;
3080}
3081
Jiri Olsacfc88742016-01-05 22:09:06 +01003082static int set_maps(struct perf_script *script)
3083{
3084 struct perf_evlist *evlist = script->session->evlist;
3085
3086 if (!script->cpus || !script->threads)
3087 return 0;
3088
3089 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3090 return -EINVAL;
3091
3092 perf_evlist__set_maps(evlist, script->cpus, script->threads);
3093
3094 if (perf_evlist__alloc_stats(evlist, true))
3095 return -ENOMEM;
3096
3097 script->allocated = true;
3098 return 0;
3099}
3100
3101static
Jiri Olsa89f16882018-09-13 14:54:03 +02003102int process_thread_map_event(struct perf_session *session,
3103 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003104{
Jiri Olsa89f16882018-09-13 14:54:03 +02003105 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003106 struct perf_script *script = container_of(tool, struct perf_script, tool);
3107
3108 if (script->threads) {
3109 pr_warning("Extra thread map event, ignoring.\n");
3110 return 0;
3111 }
3112
3113 script->threads = thread_map__new_event(&event->thread_map);
3114 if (!script->threads)
3115 return -ENOMEM;
3116
3117 return set_maps(script);
3118}
3119
3120static
Jiri Olsa89f16882018-09-13 14:54:03 +02003121int process_cpu_map_event(struct perf_session *session,
3122 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003123{
Jiri Olsa89f16882018-09-13 14:54:03 +02003124 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003125 struct perf_script *script = container_of(tool, struct perf_script, tool);
3126
3127 if (script->cpus) {
3128 pr_warning("Extra cpu map event, ignoring.\n");
3129 return 0;
3130 }
3131
3132 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3133 if (!script->cpus)
3134 return -ENOMEM;
3135
3136 return set_maps(script);
3137}
3138
Jiri Olsa89f16882018-09-13 14:54:03 +02003139static int process_feature_event(struct perf_session *session,
3140 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303141{
3142 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003143 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303144 return 0;
3145}
3146
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003147#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003148static int perf_script__process_auxtrace_info(struct perf_session *session,
3149 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003150{
Jiri Olsa89f16882018-09-13 14:54:03 +02003151 struct perf_tool *tool = session->tool;
3152
3153 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003154
3155 if (ret == 0) {
3156 struct perf_script *script = container_of(tool, struct perf_script, tool);
3157
3158 ret = perf_script__setup_per_event_dump(script);
3159 }
3160
3161 return ret;
3162}
3163#else
3164#define perf_script__process_auxtrace_info 0
3165#endif
3166
Andi Kleenb585ebd2018-09-20 11:05:36 -07003167static int parse_insn_trace(const struct option *opt __maybe_unused,
3168 const char *str __maybe_unused,
3169 int unset __maybe_unused)
3170{
3171 parse_output_fields(NULL, "+insn,-event,-period", 0);
3172 itrace_parse_synth_opts(opt, "i0ns", 0);
3173 nanosecs = true;
3174 return 0;
3175}
3176
3177static int parse_xed(const struct option *opt __maybe_unused,
3178 const char *str __maybe_unused,
3179 int unset __maybe_unused)
3180{
3181 force_pager("xed -F insn: -A -64 | less");
3182 return 0;
3183}
3184
Andi Kleend1b15522018-09-20 11:05:38 -07003185static int parse_call_trace(const struct option *opt __maybe_unused,
3186 const char *str __maybe_unused,
3187 int unset __maybe_unused)
3188{
3189 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3190 itrace_parse_synth_opts(opt, "cewp", 0);
3191 nanosecs = true;
3192 return 0;
3193}
3194
3195static int parse_callret_trace(const struct option *opt __maybe_unused,
3196 const char *str __maybe_unused,
3197 int unset __maybe_unused)
3198{
3199 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3200 itrace_parse_synth_opts(opt, "crewp", 0);
3201 nanosecs = true;
3202 return 0;
3203}
3204
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003205int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003206{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003207 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003208 bool header = false;
3209 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003210 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003211 char *rec_script_path = NULL;
3212 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003213 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003214 struct itrace_synth_opts itrace_synth_opts = {
3215 .set = false,
3216 .default_no_sample = true,
3217 };
Tom Zanussib5b87312010-11-10 08:16:51 -06003218 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003219 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003220 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003221 struct perf_script script = {
3222 .tool = {
3223 .sample = process_sample_event,
3224 .mmap = perf_event__process_mmap,
3225 .mmap2 = perf_event__process_mmap2,
3226 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303227 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003228 .exit = perf_event__process_exit,
3229 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003230 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003231 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003232 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303233 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003234 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003235 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003236 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003237 .auxtrace = perf_event__process_auxtrace,
3238 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003239 .stat = perf_event__process_stat_event,
3240 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003241 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003242 .thread_map = process_thread_map_event,
3243 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003244 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003245 .ordering_requires_timestamps = true,
3246 },
3247 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003248 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003249 .mode = PERF_DATA_MODE_READ,
3250 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003251 const struct option options[] = {
3252 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3253 "dump raw trace in ASCII"),
3254 OPT_INCR('v', "verbose", &verbose,
3255 "be more verbose (show symbol address, etc)"),
3256 OPT_BOOLEAN('L', "Latency", &latency_format,
3257 "show latency attributes (irqs/preemption disabled, etc)"),
3258 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3259 list_available_scripts),
3260 OPT_CALLBACK('s', "script", NULL, "name",
3261 "script file name (lang:script name, script name, or *)",
3262 parse_scriptname),
3263 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3264 "generate perf-script.xx script in specified language"),
3265 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3266 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3267 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003268 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3269 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003270 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3271 "file", "vmlinux pathname"),
3272 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3273 "file", "kallsyms pathname"),
3274 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3275 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003276 OPT_CALLBACK(0, "symfs", NULL, "directory",
3277 "Look for files with symbols relative to this directory",
3278 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003279 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003280 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003281 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003282 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003283 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303284 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3285 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
3286 "callindent,insn,insnlen,synth,phys_addr,metric,misc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003287 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003288 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3289 "system-wide collection from all CPUs"),
3290 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3291 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003292 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3293 "Decode instructions from itrace", parse_insn_trace),
3294 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3295 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003296 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3297 "Decode calls from from itrace", parse_call_trace),
3298 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3299 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003300 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3301 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003302 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3303 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003304 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3305 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3306 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003307 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3308 "only consider symbols in these pids"),
3309 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3310 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003311 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3312 "Set the maximum stack depth when parsing the callchain, "
3313 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003314 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003315 OPT_BOOLEAN('I', "show-info", &show_full_info,
3316 "display extended information from perf.data file"),
3317 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3318 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003319 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3320 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003321 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3322 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003323 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3324 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303325 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3326 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003327 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3328 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003329 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3330 "Show round events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003331 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3332 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003333 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003334 OPT_INTEGER(0, "max-blocks", &max_blocks,
3335 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03003336 OPT_BOOLEAN(0, "ns", &nanosecs,
3337 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003338 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003339 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003340 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003341 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3342 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003343 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3344 "Enable symbol demangling"),
3345 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3346 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003347 OPT_STRING(0, "time", &script.time_str, "str",
3348 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003349 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3350 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003351 OPT_END()
3352 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003353 const char * const script_subcommands[] = { "record", "report", NULL };
3354 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003355 "perf script [<options>]",
3356 "perf script [<options>] record <script> [<record-options>] <command>",
3357 "perf script [<options>] report <script> [script-args]",
3358 "perf script [<options>] <script> [<record-options>] <command>",
3359 "perf script [<options>] <top-script> [script-args]",
3360 NULL
3361 };
Tom Zanussi38752942009-12-15 02:53:39 -06003362
Arnaldo Carvalho de Melo0a7c74ea2017-04-04 13:15:04 -03003363 perf_set_singlethreaded();
3364
Tom Zanussib5b87312010-11-10 08:16:51 -06003365 setup_scripting();
3366
Yunlong Song40cae2b2015-03-18 21:35:54 +08003367 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003368 PARSE_OPT_STOP_AT_NON_OPTION);
3369
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003370 data.file.path = input_name;
3371 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003372
Tom Zanussib5b87312010-11-10 08:16:51 -06003373 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3374 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3375 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003376 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003377 }
3378
Tom Zanussib5b87312010-11-10 08:16:51 -06003379 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3380 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3381 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003382 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003383 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003384 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003385 return -1;
3386 }
Tom Zanussi38752942009-12-15 02:53:39 -06003387 }
3388
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003389 if (itrace_synth_opts.callchain &&
3390 itrace_synth_opts.callchain_sz > scripting_max_stack)
3391 scripting_max_stack = itrace_synth_opts.callchain_sz;
3392
Ben Hutchings44e668c2010-10-10 16:10:03 +01003393 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003394 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003395
Tom Zanussib5b87312010-11-10 08:16:51 -06003396 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003397 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003398 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003399 pid_t pid;
3400
Tom Zanussib5b87312010-11-10 08:16:51 -06003401 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3402 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3403
3404 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003405 usage_with_options_msg(script_usage, options,
3406 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003407 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003408 }
3409
Tom Zanussib5b87312010-11-10 08:16:51 -06003410 if (is_top_script(argv[0])) {
3411 rep_args = argc - 1;
3412 } else {
3413 int rec_args;
3414
3415 rep_args = has_required_arg(rep_script_path);
3416 rec_args = (argc - 1) - rep_args;
3417 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003418 usage_with_options_msg(script_usage, options,
3419 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003420 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003421 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003422 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003423 }
3424
3425 if (pipe(live_pipe) < 0) {
3426 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003427 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003428 }
3429
3430 pid = fork();
3431 if (pid < 0) {
3432 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003433 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003434 }
3435
3436 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003437 j = 0;
3438
Tom Zanussia0cccc22010-04-01 23:59:25 -05003439 dup2(live_pipe[1], 1);
3440 close(live_pipe[0]);
3441
Robert Richter317df652011-11-25 15:05:25 +01003442 if (is_top_script(argv[0])) {
3443 system_wide = true;
3444 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003445 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3446 err = -1;
3447 goto out;
3448 }
Robert Richter317df652011-11-25 15:05:25 +01003449 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003450
3451 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003452 if (!__argv) {
3453 pr_err("malloc failed\n");
3454 err = -ENOMEM;
3455 goto out;
3456 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003457
Tom Zanussib5b87312010-11-10 08:16:51 -06003458 __argv[j++] = "/bin/sh";
3459 __argv[j++] = rec_script_path;
3460 if (system_wide)
3461 __argv[j++] = "-a";
3462 __argv[j++] = "-q";
3463 __argv[j++] = "-o";
3464 __argv[j++] = "-";
3465 for (i = rep_args + 1; i < argc; i++)
3466 __argv[j++] = argv[i];
3467 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003468
3469 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003470 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003471 exit(-1);
3472 }
3473
3474 dup2(live_pipe[0], 0);
3475 close(live_pipe[1]);
3476
Tom Zanussib5b87312010-11-10 08:16:51 -06003477 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003478 if (!__argv) {
3479 pr_err("malloc failed\n");
3480 err = -ENOMEM;
3481 goto out;
3482 }
3483
Tom Zanussib5b87312010-11-10 08:16:51 -06003484 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003485 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003486 __argv[j++] = rep_script_path;
3487 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003488 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003489 __argv[j++] = "-i";
3490 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003491 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003492
3493 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003494 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003495 exit(-1);
3496 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003497
Tom Zanussib5b87312010-11-10 08:16:51 -06003498 if (rec_script_path)
3499 script_path = rec_script_path;
3500 if (rep_script_path)
3501 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003502
Tom Zanussib5b87312010-11-10 08:16:51 -06003503 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003504 j = 0;
3505
Robert Richter317df652011-11-25 15:05:25 +01003506 if (!rec_script_path)
3507 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003508 else if (!system_wide) {
3509 if (have_cmd(argc - 1, &argv[1]) != 0) {
3510 err = -1;
3511 goto out;
3512 }
3513 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003514
3515 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003516 if (!__argv) {
3517 pr_err("malloc failed\n");
3518 err = -ENOMEM;
3519 goto out;
3520 }
3521
Tom Zanussib5b87312010-11-10 08:16:51 -06003522 __argv[j++] = "/bin/sh";
3523 __argv[j++] = script_path;
3524 if (system_wide)
3525 __argv[j++] = "-a";
3526 for (i = 2; i < argc; i++)
3527 __argv[j++] = argv[i];
3528 __argv[j++] = NULL;
3529
3530 execvp("/bin/sh", (char **)__argv);
3531 free(__argv);
3532 exit(-1);
3533 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003534
Milian Wolffc1c9b962018-10-21 21:14:23 +02003535 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003536 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003537 use_browser = 0;
3538 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003539
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003540 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003541 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003542 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003543
Jiri Olsae90debd2013-12-09 11:02:50 +01003544 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003545 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003546 perf_session__fprintf_info(session, stdout, show_full_info);
3547 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003548 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003549 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003550 if (show_full_info)
3551 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003552
Namhyung Kim0a7e6d1b2014-08-12 15:40:45 +09003553 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003554 goto out_delete;
3555
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003556 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003557 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003558
Andi Kleen99f753f2018-09-20 11:05:39 -07003559 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3560 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003561 itrace_synth_opts.thread_stack = true;
3562
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003563 session->itrace_synth_opts = &itrace_synth_opts;
3564
Anton Blanchard5d67be92011-07-04 21:57:50 +10003565 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003566 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3567 if (err < 0)
3568 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003569 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003570 }
3571
David Ahern1424dc92011-03-09 22:23:28 -07003572 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003573 symbol_conf.use_callchain = true;
3574 else
3575 symbol_conf.use_callchain = false;
3576
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003577 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003578 tep_set_function_resolver(session->tevent.pevent,
3579 machine__resolve_kernel_addr,
3580 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003581 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003582 err = -1;
3583 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003584 }
3585
Tom Zanussi956ffd02009-11-25 01:15:46 -06003586 if (generate_script_lang) {
3587 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003588 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003589
David Ahern2c9e45f72011-03-17 10:03:21 -06003590 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003591 fprintf(stderr,
3592 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003593 err = -EINVAL;
3594 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003595 }
3596
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003597 input = open(data.file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003598 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003599 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003600 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003601 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003602 }
3603
3604 err = fstat(input, &perf_stat);
3605 if (err < 0) {
3606 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003607 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003608 }
3609
3610 if (!perf_stat.st_size) {
3611 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003612 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003613 }
3614
3615 scripting_ops = script_spec__lookup(generate_script_lang);
3616 if (!scripting_ops) {
3617 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003618 err = -ENOENT;
3619 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003620 }
3621
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003622 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003623 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003624 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003625 }
3626
3627 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003628 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003629 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003630 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003631 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003632 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003633 }
3634
David Ahern9cbdb702011-04-06 21:54:20 -06003635
3636 err = perf_session__check_output_opt(session);
3637 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003638 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003639
Jin Yaocc2ef582018-01-10 23:00:33 +08003640 script.ptime_range = perf_time__range_alloc(script.time_str,
3641 &script.range_size);
3642 if (!script.ptime_range) {
3643 err = -ENOMEM;
3644 goto out_delete;
3645 }
3646
David Aherna91f4c42016-11-29 10:15:43 -07003647 /* needs to be parsed after looking up reference time */
Jin Yao2ab046c2017-12-08 21:13:46 +08003648 if (perf_time__parse_str(script.ptime_range, script.time_str) != 0) {
3649 if (session->evlist->first_sample_time == 0 &&
3650 session->evlist->last_sample_time == 0) {
Jin Yao1e2778e2018-01-10 23:00:27 +08003651 pr_err("HINT: no first/last sample time found in perf data.\n"
3652 "Please use latest perf binary to execute 'perf record'\n"
3653 "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
Jin Yao2ab046c2017-12-08 21:13:46 +08003654 err = -EINVAL;
3655 goto out_delete;
3656 }
3657
3658 script.range_num = perf_time__percent_parse_str(
Jin Yaocc2ef582018-01-10 23:00:33 +08003659 script.ptime_range, script.range_size,
Jin Yao2ab046c2017-12-08 21:13:46 +08003660 script.time_str,
3661 session->evlist->first_sample_time,
3662 session->evlist->last_sample_time);
3663
3664 if (script.range_num < 0) {
3665 pr_err("Invalid time string\n");
3666 err = -EINVAL;
3667 goto out_delete;
3668 }
3669 } else {
3670 script.range_num = 1;
David Aherna91f4c42016-11-29 10:15:43 -07003671 }
3672
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003673 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003674
Adrian Hunterd445dd22014-08-15 22:08:37 +03003675 flush_scripting();
3676
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003677out_delete:
Jin Yaocc2ef582018-01-10 23:00:33 +08003678 zfree(&script.ptime_range);
3679
Jiri Olsacfc88742016-01-05 22:09:06 +01003680 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003681 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003682
3683 if (script_started)
3684 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003685out:
3686 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003687}