blob: 9d2249ea75e3c6e64ed4aed0ae99e0b7009a4631 [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 Das7903a702018-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 Das7903a702018-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 Thoufeekfad76d42018-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 Das7903a702018-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 Das7903a702018-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 Das7903a702018-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 Das7903a702018-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,
916 int insn, FILE *fp)
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) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300925 printed += fprintf(fp, " %d cycles", en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800926 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300927 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800928 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300929 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800930}
931
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300932static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
933 u8 cpumode, int cpu, struct symbol **lastsym,
934 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800935{
936 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300937 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800938
939 memset(&al, 0, sizeof(al));
940
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300941 thread__find_map(thread, cpumode, addr, &al);
942
Andi Kleen48d02a12017-02-23 15:46:34 -0800943 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300944 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800945
946 al.cpu = cpu;
947 al.sym = NULL;
948 if (al.map)
949 al.sym = map__find_symbol(al.map, al.addr);
950
951 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300952 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800953
954 if (al.addr < al.sym->end)
955 off = al.addr - al.sym->start;
956 else
957 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300958 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800959 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300960 printed += fprintf(fp, "%+d", off);
961 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800962 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300963 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
964 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800965 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300966
967 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800968}
969
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300970static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
971 struct thread *thread,
972 struct perf_event_attr *attr,
973 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800974{
975 struct branch_stack *br = sample->branch_stack;
976 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300977 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800978 struct perf_insn x;
979 u8 buffer[MAXBB];
980 unsigned off;
981 struct symbol *lastsym = NULL;
982
983 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300984 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800985 nr = br->nr;
986 if (max_blocks && nr > max_blocks + 1)
987 nr = max_blocks + 1;
988
989 x.thread = thread;
990 x.cpu = sample->cpu;
991
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300992 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -0800993
994 /* Handle first from jump, of which we don't know the entry. */
995 len = grab_bb(buffer, br->entries[nr-1].from,
996 br->entries[nr-1].from,
997 machine, thread, &x.is64bit, &x.cpumode, false);
998 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300999 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1000 x.cpumode, x.cpu, &lastsym, attr, fp);
1001 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
1002 &x, buffer, len, 0, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001003 }
1004
1005 /* Print all blocks */
1006 for (i = nr - 2; i >= 0; i--) {
1007 if (br->entries[i].from || br->entries[i].to)
1008 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1009 br->entries[i].from,
1010 br->entries[i].to);
1011 start = br->entries[i + 1].to;
1012 end = br->entries[i].from;
1013
1014 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1015 /* Patch up missing kernel transfers due to ring filters */
1016 if (len == -ENXIO && i > 0) {
1017 end = br->entries[--i].from;
1018 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1019 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1020 }
1021 if (len <= 0)
1022 continue;
1023
1024 insn = 0;
1025 for (off = 0;; off += ilen) {
1026 uint64_t ip = start + off;
1027
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001028 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001029 if (ip == end) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001030 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001031 break;
1032 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001033 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1034 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001035 if (ilen == 0)
1036 break;
1037 insn++;
1038 }
1039 }
1040 }
1041
1042 /*
1043 * Hit the branch? In this case we are already done, and the target
1044 * has not been executed yet.
1045 */
1046 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001047 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001048 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001049 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001050
1051 /*
1052 * Print final block upto sample
1053 */
1054 start = br->entries[0].to;
1055 end = sample->ip;
1056 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001057 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001058 if (len <= 0) {
1059 /* Print at least last IP if basic block did not work */
1060 len = grab_bb(buffer, sample->ip, sample->ip,
1061 machine, thread, &x.is64bit, &x.cpumode, false);
1062 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001063 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001064
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001065 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001066 dump_insn(&x, sample->ip, buffer, len, NULL));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001067 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001068 }
1069 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001070 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1071 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001072 if (ilen == 0)
1073 break;
1074 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001075out:
1076 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001077}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001078
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001079static int perf_sample__fprintf_addr(struct perf_sample *sample,
1080 struct thread *thread,
1081 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001082{
1083 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001084 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001085
1086 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001087 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001088
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001089 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001090
1091 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001092 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001093 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001094 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001095 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001096 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001097 }
1098
1099 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001100 printed += fprintf(fp, " (");
1101 printed += map__fprintf_dsoname(al.map, fp);
1102 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001103 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001104out:
1105 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001106}
1107
Andi Kleen99f753f2018-09-20 11:05:39 -07001108static const char *resolve_branch_sym(struct perf_sample *sample,
1109 struct perf_evsel *evsel,
1110 struct thread *thread,
1111 struct addr_location *al,
1112 u64 *ip)
1113{
1114 struct addr_location addr_al;
1115 struct perf_event_attr *attr = &evsel->attr;
1116 const char *name = NULL;
1117
1118 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1119 if (sample_addr_correlates_sym(attr)) {
1120 thread__resolve(thread, &addr_al, sample);
1121 if (addr_al.sym)
1122 name = addr_al.sym->name;
1123 else
1124 *ip = sample->addr;
1125 } else {
1126 *ip = sample->addr;
1127 }
1128 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1129 if (al->sym)
1130 name = al->sym->name;
1131 else
1132 *ip = sample->ip;
1133 }
1134 return name;
1135}
1136
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001137static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1138 struct perf_evsel *evsel,
1139 struct thread *thread,
1140 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001141{
1142 struct perf_event_attr *attr = &evsel->attr;
1143 size_t depth = thread_stack__depth(thread);
Adrian Huntere2167082016-06-23 16:40:58 +03001144 const char *name = NULL;
1145 static int spacing;
1146 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001147 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001148 u64 ip = 0;
1149
1150 /*
1151 * The 'return' has already been popped off the stack so the depth has
1152 * to be adjusted to match the 'call'.
1153 */
1154 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1155 depth += 1;
1156
Andi Kleen99f753f2018-09-20 11:05:39 -07001157 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001158
Andi Kleena78cdee2018-09-18 05:32:10 -07001159 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1160 dlen += fprintf(fp, "(");
1161 dlen += map__fprintf_dsoname(al->map, fp);
1162 dlen += fprintf(fp, ")\t");
1163 }
1164
Adrian Huntere2167082016-06-23 16:40:58 +03001165 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001166 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001167 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001168 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001169
1170 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001171 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001172
1173 /*
1174 * Try to keep the output length from changing frequently so that the
1175 * output lines up more nicely.
1176 */
1177 if (len > spacing || (len && len < spacing - 52))
1178 spacing = round_up(len + 4, 32);
1179
1180 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001181 len += fprintf(fp, "%*s", spacing - len, "");
1182
Andi Kleena78cdee2018-09-18 05:32:10 -07001183 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001184}
1185
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001186static int perf_sample__fprintf_insn(struct perf_sample *sample,
1187 struct perf_event_attr *attr,
1188 struct thread *thread,
1189 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001190{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001191 int printed = 0;
1192
Andi Kleen224e2c92016-10-07 16:42:27 +03001193 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001194 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen224e2c92016-10-07 16:42:27 +03001195 if (PRINT_FIELD(INSN)) {
1196 int i;
1197
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001198 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001199 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001200 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001201 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001202 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001203 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1204
1205 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001206}
1207
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001208static int perf_sample__fprintf_bts(struct perf_sample *sample,
1209 struct perf_evsel *evsel,
1210 struct thread *thread,
1211 struct addr_location *al,
1212 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001213{
1214 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001215 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001216 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001217 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001218
Adrian Huntere2167082016-06-23 16:40:58 +03001219 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001220 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001221
Akihiro Nagai95582592012-01-30 13:43:09 +09001222 /* print branch_from information */
1223 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001224 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001225 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001226
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001227 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001228 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001229 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001230 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001231
1232 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001233 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001234 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001235 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001236 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001237 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001238 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001239 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001240
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001241 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001242 }
1243
Akihiro Nagai95582592012-01-30 13:43:09 +09001244 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001245 if (PRINT_FIELD(ADDR) ||
1246 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001247 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001248 printed += fprintf(fp, " => ");
1249 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001250 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001251
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001252 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001253 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001254
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001255 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1256 return printed + fprintf(fp, "\n");
Akihiro Nagai95582592012-01-30 13:43:09 +09001257}
1258
Adrian Hunter055cd332016-06-23 16:40:56 +03001259static struct {
1260 u32 flags;
1261 const char *name;
1262} sample_flags[] = {
1263 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1264 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1265 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1266 {PERF_IP_FLAG_BRANCH, "jmp"},
1267 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1268 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1269 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1270 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1271 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1272 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1273 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1274 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1275 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1276 {0, NULL}
1277};
1278
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001279static const char *sample_flags_to_name(u32 flags)
1280{
1281 int i;
1282
1283 for (i = 0; sample_flags[i].name ; i++) {
1284 if (sample_flags[i].flags == flags)
1285 return sample_flags[i].name;
1286 }
1287
1288 return NULL;
1289}
1290
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001291static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001292{
1293 const char *chars = PERF_IP_FLAG_CHARS;
1294 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001295 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1296 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001297 char str[33];
1298 int i, pos = 0;
1299
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001300 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1301 if (name)
1302 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1303
1304 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1305 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1306 if (name)
1307 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1308 }
1309
1310 if (flags & PERF_IP_FLAG_TRACE_END) {
1311 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1312 if (name)
1313 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001314 }
1315
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001316 for (i = 0; i < n; i++, flags >>= 1) {
1317 if (flags & 1)
1318 str[pos++] = chars[i];
1319 }
1320 for (; i < 32; i++, flags >>= 1) {
1321 if (flags & 1)
1322 str[pos++] = '?';
1323 }
1324 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001325
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001326 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001327}
1328
Wang Nan30372f02016-02-24 11:20:45 +00001329struct printer_data {
1330 int line_no;
1331 bool hit_nul;
1332 bool is_printable;
1333};
1334
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001335static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1336 unsigned int val,
1337 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001338{
1339 unsigned char ch = (unsigned char)val;
1340 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001341 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001342
1343 switch (op) {
1344 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001345 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001346 break;
1347 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001348 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001349 " ");
1350 break;
1351 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001352 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001353 break;
1354 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001355 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001356 break;
1357 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001358 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001359 break;
1360 case BINARY_PRINT_SEP:
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_CHAR_DATA:
1364 if (printer_data->hit_nul && ch)
1365 printer_data->is_printable = false;
1366
1367 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001368 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001369
1370 if (!printer_data->is_printable)
1371 break;
1372
1373 if (ch == '\0')
1374 printer_data->hit_nul = true;
1375 else
1376 printer_data->is_printable = false;
1377 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001378 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001379 }
1380 break;
1381 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001382 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001383 break;
1384 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001385 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001386 printer_data->line_no++;
1387 break;
1388 case BINARY_PRINT_DATA_END:
1389 default:
1390 break;
1391 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001392
1393 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001394}
1395
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001396static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001397{
1398 unsigned int nr_bytes = sample->raw_size;
1399 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001400 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1401 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001402
1403 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001404 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1405
1406 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001407}
1408
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001409static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001410{
1411 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001412 return fprintf(fp, "%*s", spacing - len, "");
1413
1414 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001415}
1416
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001417static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001418{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001419 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001420}
1421
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001422static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001423{
1424 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1425 int len;
1426
1427 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001428 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001429
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001430 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001431 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001432 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001433}
1434
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001435static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001436{
1437 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1438 int len;
1439
1440 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001441 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001442
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001443 len = fprintf(fp, " hints: %#x extensions: %#x ",
1444 data->hints, data->extensions);
1445 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001446}
1447
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001448static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001449{
1450 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1451 int len;
1452
1453 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001454 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001455
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001456 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1457 data->hw, data->cstate, data->subcstate);
1458 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001459}
1460
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001461static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001462{
1463 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1464 int len;
1465
1466 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001467 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001468
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001469 len = fprintf(fp, " IP: %u ", data->ip);
1470 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001471}
1472
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001473static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001474{
1475 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1476 int len;
1477
1478 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001479 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001480
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001481 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001482 data->deepest_cstate, data->last_cstate,
1483 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001484 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001485}
1486
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001487static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001488{
1489 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1490 unsigned int percent, freq;
1491 int len;
1492
1493 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001494 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001495
1496 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001497 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001498 if (data->max_nonturbo) {
1499 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001500 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001501 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001502 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001503}
1504
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001505static int perf_sample__fprintf_synth(struct perf_sample *sample,
1506 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001507{
1508 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001509 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001510 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001511 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001512 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001513 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001514 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001515 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001516 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001517 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001518 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001519 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001520 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001521 default:
1522 break;
1523 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001524
1525 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001526}
1527
Jiri Olsa809e9422015-11-26 18:55:21 +01001528struct perf_script {
1529 struct perf_tool tool;
1530 struct perf_session *session;
1531 bool show_task_events;
1532 bool show_mmap_events;
1533 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301534 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001535 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001536 bool show_round_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001537 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001538 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001539 struct cpu_map *cpus;
1540 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001541 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001542 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001543 struct perf_time_interval *ptime_range;
1544 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001545 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001546};
1547
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001548static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1549{
1550 struct perf_evsel *evsel;
1551 int max = 0;
1552
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001553 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001554 int len = strlen(perf_evsel__name(evsel));
1555
1556 max = MAX(len, max);
1557 }
1558
1559 return max;
1560}
1561
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001562static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001563{
1564 struct mem_info mi = { .data_src.val = data_src };
1565 char decode[100];
1566 char out[100];
1567 static int maxlen;
1568 int len;
1569
1570 perf_script__meminfo_scnprintf(decode, 100, &mi);
1571
1572 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1573 if (maxlen < len)
1574 maxlen = len;
1575
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001576 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001577}
1578
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001579struct metric_ctx {
1580 struct perf_sample *sample;
1581 struct thread *thread;
1582 struct perf_evsel *evsel;
1583 FILE *fp;
1584};
1585
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001586static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1587 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001588 const char *fmt,
1589 const char *unit, double val)
1590{
1591 struct metric_ctx *mctx = ctx;
1592
1593 if (!fmt)
1594 return;
1595 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001596 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001597 fputs("\tmetric: ", mctx->fp);
1598 if (color)
1599 color_fprintf(mctx->fp, color, fmt, val);
1600 else
1601 printf(fmt, val);
1602 fprintf(mctx->fp, " %s\n", unit);
1603}
1604
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001605static void script_new_line(struct perf_stat_config *config __maybe_unused,
1606 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001607{
1608 struct metric_ctx *mctx = ctx;
1609
1610 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001611 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001612 fputs("\tmetric: ", mctx->fp);
1613}
1614
1615static void perf_sample__fprint_metric(struct perf_script *script,
1616 struct thread *thread,
1617 struct perf_evsel *evsel,
1618 struct perf_sample *sample,
1619 FILE *fp)
1620{
1621 struct perf_stat_output_ctx ctx = {
1622 .print_metric = script_print_metric,
1623 .new_line = script_new_line,
1624 .ctx = &(struct metric_ctx) {
1625 .sample = sample,
1626 .thread = thread,
1627 .evsel = evsel,
1628 .fp = fp,
1629 },
1630 .force_header = false,
1631 };
1632 struct perf_evsel *ev2;
1633 static bool init;
1634 u64 val;
1635
1636 if (!init) {
1637 perf_stat__init_shadow_stats();
1638 init = true;
1639 }
1640 if (!evsel->stats)
1641 perf_evlist__alloc_stats(script->session->evlist, false);
1642 if (evsel_script(evsel->leader)->gnum++ == 0)
1643 perf_stat__reset_shadow_stats();
1644 val = sample->period * evsel->scale;
1645 perf_stat__update_shadow_stats(evsel,
1646 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001647 sample->cpu,
1648 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001649 evsel_script(evsel)->val = val;
1650 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1651 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001652 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001653 evsel_script(ev2)->val,
1654 sample->cpu,
1655 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001656 NULL,
1657 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001658 }
1659 evsel_script(evsel->leader)->gnum = 0;
1660 }
1661}
1662
Andi Kleen99f753f2018-09-20 11:05:39 -07001663static bool show_event(struct perf_sample *sample,
1664 struct perf_evsel *evsel,
1665 struct thread *thread,
1666 struct addr_location *al)
1667{
1668 int depth = thread_stack__depth(thread);
1669
1670 if (!symbol_conf.graph_function)
1671 return true;
1672
1673 if (thread->filter) {
1674 if (depth <= thread->filter_entry_depth) {
1675 thread->filter = false;
1676 return false;
1677 }
1678 return true;
1679 } else {
1680 const char *s = symbol_conf.graph_function;
1681 u64 ip;
1682 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1683 &ip);
1684 unsigned nlen;
1685
1686 if (!name)
1687 return false;
1688 nlen = strlen(name);
1689 while (*s) {
1690 unsigned len = strcspn(s, ",");
1691 if (nlen == len && !strncmp(name, s, len)) {
1692 thread->filter = true;
1693 thread->filter_entry_depth = depth;
1694 return true;
1695 }
1696 s += len;
1697 if (*s == ',')
1698 s++;
1699 }
1700 return false;
1701 }
1702}
1703
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001704static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001705 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001706 struct addr_location *al,
1707 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001708{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001709 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001710 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001711 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001712 struct perf_evsel_script *es = evsel->priv;
1713 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001714
Adrian Hunter14057202017-06-21 13:17:19 +03001715 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001716 return;
1717
Andi Kleen99f753f2018-09-20 11:05:39 -07001718 if (!show_event(sample, evsel, thread, al))
1719 return;
1720
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001721 ++es->samples;
1722
Jiri Olsa28a0b392018-01-07 17:03:52 +01001723 perf_sample__fprintf_start(sample, thread, evsel,
1724 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001725
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001726 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001727 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001728
Namhyung Kime944d3d2013-11-18 14:34:52 +09001729 if (PRINT_FIELD(EVNAME)) {
1730 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001731
1732 if (!script->name_width)
1733 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1734
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001735 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001736 }
1737
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001738 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001739 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001740
Akihiro Nagai95582592012-01-30 13:43:09 +09001741 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001742 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001743 return;
1744 }
1745
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001746 if (PRINT_FIELD(TRACE)) {
1747 event_format__fprintf(evsel->tp_format, sample->cpu,
1748 sample->raw_data, sample->raw_size, fp);
1749 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001750
1751 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001752 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001753
David Ahern7cec0922011-05-30 13:08:23 -06001754 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001755 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001756
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001757 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001758 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001759
1760 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001761 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001762
David Ahern787bef12011-05-27 14:28:43 -06001763 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001764 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001765
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001766 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001767 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001768 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001769 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001770
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001771 fputc(cursor ? '\n' : ' ', fp);
1772 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001773 }
1774
Stephane Eranianfc36f942015-08-31 18:41:10 +02001775 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001776 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001777
Andi Kleenb1491ac2017-09-05 11:40:57 -07001778 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001779 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001780
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001781 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001782 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001783 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001784 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001785 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001786 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001787
Wang Nan30372f02016-02-24 11:20:45 +00001788 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001789 perf_sample__fprintf_bpf_output(sample, fp);
1790 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001791
1792 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001793 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1794 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001795
1796 if (PRINT_FIELD(METRIC))
1797 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001798
1799 if (verbose)
1800 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001801}
1802
Tom Zanussi956ffd02009-11-25 01:15:46 -06001803static struct scripting_ops *scripting_ops;
1804
Jiri Olsa36e33c52016-01-06 11:49:56 +01001805static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1806{
1807 int nthreads = thread_map__nr(counter->threads);
1808 int ncpus = perf_evsel__nr_cpus(counter);
1809 int cpu, thread;
1810 static int header_printed;
1811
1812 if (counter->system_wide)
1813 nthreads = 1;
1814
1815 if (!header_printed) {
1816 printf("%3s %8s %15s %15s %15s %15s %s\n",
1817 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1818 header_printed = 1;
1819 }
1820
1821 for (thread = 0; thread < nthreads; thread++) {
1822 for (cpu = 0; cpu < ncpus; cpu++) {
1823 struct perf_counts_values *counts;
1824
1825 counts = perf_counts(counter->counts, cpu, thread);
1826
1827 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1828 counter->cpus->map[cpu],
1829 thread_map__pid(counter->threads, thread),
1830 counts->val,
1831 counts->ena,
1832 counts->run,
1833 tstamp,
1834 perf_evsel__name(counter));
1835 }
1836 }
1837}
1838
Jiri Olsae099eba2016-01-05 22:09:09 +01001839static void process_stat(struct perf_evsel *counter, u64 tstamp)
1840{
1841 if (scripting_ops && scripting_ops->process_stat)
1842 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001843 else
1844 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001845}
1846
1847static void process_stat_interval(u64 tstamp)
1848{
1849 if (scripting_ops && scripting_ops->process_stat_interval)
1850 scripting_ops->process_stat_interval(tstamp);
1851}
1852
Tom Zanussi956ffd02009-11-25 01:15:46 -06001853static void setup_scripting(void)
1854{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001855 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001856 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001857}
1858
Adrian Hunterd445dd22014-08-15 22:08:37 +03001859static int flush_scripting(void)
1860{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001861 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001862}
1863
Tom Zanussi956ffd02009-11-25 01:15:46 -06001864static int cleanup_scripting(void)
1865{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001866 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001867
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001868 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001869}
1870
Jiri Olsa809e9422015-11-26 18:55:21 +01001871static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001872 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001873 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001874 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001875 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001876{
Jiri Olsa809e9422015-11-26 18:55:21 +01001877 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001878 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001879
Jin Yao2ab046c2017-12-08 21:13:46 +08001880 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1881 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001882 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001883 }
David Aherna91f4c42016-11-29 10:15:43 -07001884
David Ahern1424dc92011-03-09 22:23:28 -07001885 if (debug_mode) {
1886 if (sample->time < last_timestamp) {
1887 pr_err("Samples misordered, previous: %" PRIu64
1888 " this: %" PRIu64 "\n", last_timestamp,
1889 sample->time);
1890 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001891 }
David Ahern1424dc92011-03-09 22:23:28 -07001892 last_timestamp = sample->time;
1893 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001894 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001895
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001896 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001897 pr_err("problem processing %d event, skipping it.\n",
1898 event->header.type);
1899 return -1;
1900 }
1901
1902 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001903 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001904
Anton Blanchard5d67be92011-07-04 21:57:50 +10001905 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001906 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001907
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001908 if (scripting_ops)
1909 scripting_ops->process_event(event, sample, evsel, &al);
1910 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001911 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001912
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001913out_put:
1914 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001915 return 0;
1916}
1917
Adrian Hunter7ea95722013-11-01 15:51:30 +02001918static int process_attr(struct perf_tool *tool, union perf_event *event,
1919 struct perf_evlist **pevlist)
1920{
1921 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1922 struct perf_evlist *evlist;
1923 struct perf_evsel *evsel, *pos;
1924 int err;
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301925 static struct perf_evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001926
1927 err = perf_event__process_attr(tool, event, pevlist);
1928 if (err)
1929 return err;
1930
1931 evlist = *pevlist;
1932 evsel = perf_evlist__last(*pevlist);
1933
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301934 if (!evsel->priv) {
1935 if (scr->per_event_dump) {
1936 evsel->priv = perf_evsel_script__new(evsel,
1937 scr->session->data);
1938 } else {
1939 es = zalloc(sizeof(*es));
1940 if (!es)
1941 return -ENOMEM;
1942 es->fp = stdout;
1943 evsel->priv = es;
1944 }
1945 }
1946
Adrian Hunter14057202017-06-21 13:17:19 +03001947 if (evsel->attr.type >= PERF_TYPE_MAX &&
1948 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001949 return 0;
1950
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001951 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001952 if (pos->attr.type == evsel->attr.type && pos != evsel)
1953 return 0;
1954 }
1955
1956 set_print_ip_opts(&evsel->attr);
1957
Jiri Olsad2b5a312015-10-16 12:41:25 +02001958 if (evsel->attr.sample_type)
1959 err = perf_evsel__check_attr(evsel, scr->session);
1960
1961 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001962}
1963
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001964static int process_comm_event(struct perf_tool *tool,
1965 union perf_event *event,
1966 struct perf_sample *sample,
1967 struct machine *machine)
1968{
1969 struct thread *thread;
1970 struct perf_script *script = container_of(tool, struct perf_script, tool);
1971 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001972 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001973 int ret = -1;
1974
1975 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1976 if (thread == NULL) {
1977 pr_debug("problem processing COMM event, skipping it.\n");
1978 return -1;
1979 }
1980
1981 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1982 goto out;
1983
1984 if (!evsel->attr.sample_id_all) {
1985 sample->cpu = 0;
1986 sample->time = 0;
1987 sample->tid = event->comm.tid;
1988 sample->pid = event->comm.pid;
1989 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01001990 perf_sample__fprintf_start(sample, thread, evsel,
1991 PERF_RECORD_COMM, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001992 perf_event__fprintf(event, stdout);
1993 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001994out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001995 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001996 return ret;
1997}
1998
Hari Bathini96a44bb2017-03-08 02:12:06 +05301999static int process_namespaces_event(struct perf_tool *tool,
2000 union perf_event *event,
2001 struct perf_sample *sample,
2002 struct machine *machine)
2003{
2004 struct thread *thread;
2005 struct perf_script *script = container_of(tool, struct perf_script, tool);
2006 struct perf_session *session = script->session;
2007 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2008 int ret = -1;
2009
2010 thread = machine__findnew_thread(machine, event->namespaces.pid,
2011 event->namespaces.tid);
2012 if (thread == NULL) {
2013 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2014 return -1;
2015 }
2016
2017 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2018 goto out;
2019
2020 if (!evsel->attr.sample_id_all) {
2021 sample->cpu = 0;
2022 sample->time = 0;
2023 sample->tid = event->namespaces.tid;
2024 sample->pid = event->namespaces.pid;
2025 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002026 perf_sample__fprintf_start(sample, thread, evsel,
2027 PERF_RECORD_NAMESPACES, stdout);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302028 perf_event__fprintf(event, stdout);
2029 ret = 0;
2030out:
2031 thread__put(thread);
2032 return ret;
2033}
2034
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002035static int process_fork_event(struct perf_tool *tool,
2036 union perf_event *event,
2037 struct perf_sample *sample,
2038 struct machine *machine)
2039{
2040 struct thread *thread;
2041 struct perf_script *script = container_of(tool, struct perf_script, tool);
2042 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002043 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002044
2045 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2046 return -1;
2047
2048 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2049 if (thread == NULL) {
2050 pr_debug("problem processing FORK event, skipping it.\n");
2051 return -1;
2052 }
2053
2054 if (!evsel->attr.sample_id_all) {
2055 sample->cpu = 0;
2056 sample->time = event->fork.time;
2057 sample->tid = event->fork.tid;
2058 sample->pid = event->fork.pid;
2059 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002060 perf_sample__fprintf_start(sample, thread, evsel,
2061 PERF_RECORD_FORK, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002062 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002063 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002064
2065 return 0;
2066}
2067static int process_exit_event(struct perf_tool *tool,
2068 union perf_event *event,
2069 struct perf_sample *sample,
2070 struct machine *machine)
2071{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002072 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002073 struct thread *thread;
2074 struct perf_script *script = container_of(tool, struct perf_script, tool);
2075 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002076 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002077
2078 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2079 if (thread == NULL) {
2080 pr_debug("problem processing EXIT event, skipping it.\n");
2081 return -1;
2082 }
2083
2084 if (!evsel->attr.sample_id_all) {
2085 sample->cpu = 0;
2086 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002087 sample->tid = event->fork.tid;
2088 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002089 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002090 perf_sample__fprintf_start(sample, thread, evsel,
2091 PERF_RECORD_EXIT, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002092 perf_event__fprintf(event, stdout);
2093
2094 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002095 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002096
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002097 thread__put(thread);
2098 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002099}
2100
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002101static int process_mmap_event(struct perf_tool *tool,
2102 union perf_event *event,
2103 struct perf_sample *sample,
2104 struct machine *machine)
2105{
2106 struct thread *thread;
2107 struct perf_script *script = container_of(tool, struct perf_script, tool);
2108 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002109 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002110
2111 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2112 return -1;
2113
2114 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2115 if (thread == NULL) {
2116 pr_debug("problem processing MMAP event, skipping it.\n");
2117 return -1;
2118 }
2119
2120 if (!evsel->attr.sample_id_all) {
2121 sample->cpu = 0;
2122 sample->time = 0;
2123 sample->tid = event->mmap.tid;
2124 sample->pid = event->mmap.pid;
2125 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002126 perf_sample__fprintf_start(sample, thread, evsel,
2127 PERF_RECORD_MMAP, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002128 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002129 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002130 return 0;
2131}
2132
2133static int process_mmap2_event(struct perf_tool *tool,
2134 union perf_event *event,
2135 struct perf_sample *sample,
2136 struct machine *machine)
2137{
2138 struct thread *thread;
2139 struct perf_script *script = container_of(tool, struct perf_script, tool);
2140 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002141 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002142
2143 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2144 return -1;
2145
2146 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2147 if (thread == NULL) {
2148 pr_debug("problem processing MMAP2 event, skipping it.\n");
2149 return -1;
2150 }
2151
2152 if (!evsel->attr.sample_id_all) {
2153 sample->cpu = 0;
2154 sample->time = 0;
2155 sample->tid = event->mmap2.tid;
2156 sample->pid = event->mmap2.pid;
2157 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002158 perf_sample__fprintf_start(sample, thread, evsel,
2159 PERF_RECORD_MMAP2, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002160 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002161 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002162 return 0;
2163}
2164
Adrian Hunter7c148982015-07-21 12:44:06 +03002165static int process_switch_event(struct perf_tool *tool,
2166 union perf_event *event,
2167 struct perf_sample *sample,
2168 struct machine *machine)
2169{
2170 struct thread *thread;
2171 struct perf_script *script = container_of(tool, struct perf_script, tool);
2172 struct perf_session *session = script->session;
2173 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2174
2175 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2176 return -1;
2177
2178 thread = machine__findnew_thread(machine, sample->pid,
2179 sample->tid);
2180 if (thread == NULL) {
2181 pr_debug("problem processing SWITCH event, skipping it.\n");
2182 return -1;
2183 }
2184
Jiri Olsa28a0b392018-01-07 17:03:52 +01002185 perf_sample__fprintf_start(sample, thread, evsel,
2186 PERF_RECORD_SWITCH, stdout);
Adrian Hunter7c148982015-07-21 12:44:06 +03002187 perf_event__fprintf(event, stdout);
2188 thread__put(thread);
2189 return 0;
2190}
2191
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002192static int
2193process_lost_event(struct perf_tool *tool,
2194 union perf_event *event,
2195 struct perf_sample *sample,
2196 struct machine *machine)
2197{
2198 struct perf_script *script = container_of(tool, struct perf_script, tool);
2199 struct perf_session *session = script->session;
2200 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2201 struct thread *thread;
2202
2203 thread = machine__findnew_thread(machine, sample->pid,
2204 sample->tid);
2205 if (thread == NULL)
2206 return -1;
2207
2208 perf_sample__fprintf_start(sample, thread, evsel,
2209 PERF_RECORD_LOST, stdout);
2210 perf_event__fprintf(event, stdout);
2211 thread__put(thread);
2212 return 0;
2213}
2214
Jiri Olsa3233b372018-02-06 19:17:59 +01002215static int
2216process_finished_round_event(struct perf_tool *tool __maybe_unused,
2217 union perf_event *event,
2218 struct ordered_events *oe __maybe_unused)
2219
2220{
2221 perf_event__fprintf(event, stdout);
2222 return 0;
2223}
2224
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002225static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002226{
2227 session_done = 1;
2228}
2229
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002230static void perf_script__fclose_per_event_dump(struct perf_script *script)
2231{
2232 struct perf_evlist *evlist = script->session->evlist;
2233 struct perf_evsel *evsel;
2234
2235 evlist__for_each_entry(evlist, evsel) {
2236 if (!evsel->priv)
2237 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002238 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002239 evsel->priv = NULL;
2240 }
2241}
2242
2243static int perf_script__fopen_per_event_dump(struct perf_script *script)
2244{
2245 struct perf_evsel *evsel;
2246
2247 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002248 /*
2249 * Already setup? I.e. we may be called twice in cases like
2250 * Intel PT, one for the intel_pt// and dummy events, then
2251 * for the evsels syntheized from the auxtrace info.
2252 *
2253 * Ses perf_script__process_auxtrace_info.
2254 */
2255 if (evsel->priv != NULL)
2256 continue;
2257
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002258 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002259 if (evsel->priv == NULL)
2260 goto out_err_fclose;
2261 }
2262
2263 return 0;
2264
2265out_err_fclose:
2266 perf_script__fclose_per_event_dump(script);
2267 return -1;
2268}
2269
2270static int perf_script__setup_per_event_dump(struct perf_script *script)
2271{
2272 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002273 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002274
2275 if (script->per_event_dump)
2276 return perf_script__fopen_per_event_dump(script);
2277
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002278 es_stdout.fp = stdout;
2279
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002280 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002281 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002282
2283 return 0;
2284}
2285
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002286static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2287{
2288 struct perf_evsel *evsel;
2289
2290 evlist__for_each_entry(script->session->evlist, evsel) {
2291 struct perf_evsel_script *es = evsel->priv;
2292
2293 perf_evsel_script__fprintf(es, stdout);
2294 perf_evsel_script__delete(es);
2295 evsel->priv = NULL;
2296 }
2297}
2298
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002299static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002300{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002301 int ret;
2302
Tom Zanussic239da32010-04-01 23:59:18 -05002303 signal(SIGINT, sig_handler);
2304
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002305 /* override event processing functions */
2306 if (script->show_task_events) {
2307 script->tool.comm = process_comm_event;
2308 script->tool.fork = process_fork_event;
2309 script->tool.exit = process_exit_event;
2310 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002311 if (script->show_mmap_events) {
2312 script->tool.mmap = process_mmap_event;
2313 script->tool.mmap2 = process_mmap2_event;
2314 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002315 if (script->show_switch_events)
2316 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302317 if (script->show_namespace_events)
2318 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002319 if (script->show_lost_events)
2320 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002321 if (script->show_round_events) {
2322 script->tool.ordered_events = false;
2323 script->tool.finished_round = process_finished_round_event;
2324 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002325
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002326 if (perf_script__setup_per_event_dump(script)) {
2327 pr_err("Couldn't create the per event dump files\n");
2328 return -1;
2329 }
2330
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002331 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002332
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002333 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002334 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002335
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002336 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002337 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002338
2339 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002340}
2341
Tom Zanussi956ffd02009-11-25 01:15:46 -06002342struct script_spec {
2343 struct list_head node;
2344 struct scripting_ops *ops;
2345 char spec[0];
2346};
2347
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002348static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002349
2350static struct script_spec *script_spec__new(const char *spec,
2351 struct scripting_ops *ops)
2352{
2353 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2354
2355 if (s != NULL) {
2356 strcpy(s->spec, spec);
2357 s->ops = ops;
2358 }
2359
2360 return s;
2361}
2362
Tom Zanussi956ffd02009-11-25 01:15:46 -06002363static void script_spec__add(struct script_spec *s)
2364{
2365 list_add_tail(&s->node, &script_specs);
2366}
2367
2368static struct script_spec *script_spec__find(const char *spec)
2369{
2370 struct script_spec *s;
2371
2372 list_for_each_entry(s, &script_specs, node)
2373 if (strcasecmp(s->spec, spec) == 0)
2374 return s;
2375 return NULL;
2376}
2377
Tom Zanussi956ffd02009-11-25 01:15:46 -06002378int script_spec_register(const char *spec, struct scripting_ops *ops)
2379{
2380 struct script_spec *s;
2381
2382 s = script_spec__find(spec);
2383 if (s)
2384 return -1;
2385
Taeung Song8560bae2016-02-26 00:13:10 +09002386 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002387 if (!s)
2388 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002389 else
2390 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002391
2392 return 0;
2393}
2394
2395static struct scripting_ops *script_spec__lookup(const char *spec)
2396{
2397 struct script_spec *s = script_spec__find(spec);
2398 if (!s)
2399 return NULL;
2400
2401 return s->ops;
2402}
2403
2404static void list_available_languages(void)
2405{
2406 struct script_spec *s;
2407
2408 fprintf(stderr, "\n");
2409 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002410 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002411
2412 list_for_each_entry(s, &script_specs, node)
2413 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2414
2415 fprintf(stderr, "\n");
2416}
2417
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002418static int parse_scriptname(const struct option *opt __maybe_unused,
2419 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002420{
2421 char spec[PATH_MAX];
2422 const char *script, *ext;
2423 int len;
2424
Tom Zanussif526d682010-01-27 02:27:52 -06002425 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002426 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002427 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002428 }
2429
2430 script = strchr(str, ':');
2431 if (script) {
2432 len = script - str;
2433 if (len >= PATH_MAX) {
2434 fprintf(stderr, "invalid language specifier");
2435 return -1;
2436 }
2437 strncpy(spec, str, len);
2438 spec[len] = '\0';
2439 scripting_ops = script_spec__lookup(spec);
2440 if (!scripting_ops) {
2441 fprintf(stderr, "invalid language specifier");
2442 return -1;
2443 }
2444 script++;
2445 } else {
2446 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002447 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002448 if (!ext) {
2449 fprintf(stderr, "invalid script extension");
2450 return -1;
2451 }
2452 scripting_ops = script_spec__lookup(++ext);
2453 if (!scripting_ops) {
2454 fprintf(stderr, "invalid script extension");
2455 return -1;
2456 }
2457 }
2458
2459 script_name = strdup(script);
2460
2461 return 0;
2462}
2463
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002464static int parse_output_fields(const struct option *opt __maybe_unused,
2465 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002466{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002467 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002468 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002469 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002470 int rc = 0;
2471 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002472 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002473 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002474
2475 if (!str)
2476 return -ENOMEM;
2477
David Ahern2c9e45f72011-03-17 10:03:21 -06002478 /* first word can state for which event type the user is specifying
2479 * the fields. If no type exists, the specified fields apply to all
2480 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002481 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002482 tok = strchr(str, ':');
2483 if (tok) {
2484 *tok = '\0';
2485 tok++;
2486 if (!strcmp(str, "hw"))
2487 type = PERF_TYPE_HARDWARE;
2488 else if (!strcmp(str, "sw"))
2489 type = PERF_TYPE_SOFTWARE;
2490 else if (!strcmp(str, "trace"))
2491 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002492 else if (!strcmp(str, "raw"))
2493 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002494 else if (!strcmp(str, "break"))
2495 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002496 else if (!strcmp(str, "synth"))
2497 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002498 else {
2499 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002500 rc = -EINVAL;
2501 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002502 }
2503
2504 if (output[type].user_set)
2505 pr_warning("Overriding previous field request for %s events.\n",
2506 event_type(type));
2507
2508 output[type].fields = 0;
2509 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002510 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002511
2512 } else {
2513 tok = str;
2514 if (strlen(str) == 0) {
2515 fprintf(stderr,
2516 "Cannot set fields to 'none' for all event types.\n");
2517 rc = -EINVAL;
2518 goto out;
2519 }
2520
Andi Kleen36ce5652017-06-02 08:48:10 -07002521 /* Don't override defaults for +- */
2522 if (strchr(str, '+') || strchr(str, '-'))
2523 goto parse;
2524
David Ahern2c9e45f72011-03-17 10:03:21 -06002525 if (output_set_by_user())
2526 pr_warning("Overriding previous field request for all events.\n");
2527
Adrian Hunter14057202017-06-21 13:17:19 +03002528 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002529 output[j].fields = 0;
2530 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002531 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002532 }
David Ahern1424dc92011-03-09 22:23:28 -07002533 }
2534
Andi Kleen36ce5652017-06-02 08:48:10 -07002535parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002536 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002537 if (*tok == '+') {
2538 if (change == SET)
2539 goto out_badmix;
2540 change = ADD;
2541 tok++;
2542 } else if (*tok == '-') {
2543 if (change == SET)
2544 goto out_badmix;
2545 change = REMOVE;
2546 tok++;
2547 } else {
2548 if (change != SET && change != DEFAULT)
2549 goto out_badmix;
2550 change = SET;
2551 }
2552
David Ahern745f43e2011-03-09 22:23:26 -07002553 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002554 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002555 break;
David Ahern745f43e2011-03-09 22:23:26 -07002556 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002557 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002558 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002559 continue;
2560 }
David Ahern745f43e2011-03-09 22:23:26 -07002561 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002562 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002563 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002564 goto out;
2565 }
2566
2567 if (type == -1) {
2568 /* add user option to all events types for
2569 * which it is valid
2570 */
Adrian Hunter14057202017-06-21 13:17:19 +03002571 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002572 if (output[j].invalid_fields & all_output_options[i].field) {
2573 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2574 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002575 } else {
2576 if (change == REMOVE)
2577 output[j].fields &= ~all_output_options[i].field;
2578 else
2579 output[j].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002580 output[j].user_set = true;
2581 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002582 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002583 }
2584 } else {
2585 if (output[type].invalid_fields & all_output_options[i].field) {
2586 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2587 all_output_options[i].str, event_type(type));
2588
2589 rc = -EINVAL;
2590 goto out;
2591 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002592 output[type].user_set = true;
2593 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002594 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002595 }
2596
2597 if (type >= 0) {
2598 if (output[type].fields == 0) {
2599 pr_debug("No fields requested for %s type. "
2600 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002601 }
David Ahern1424dc92011-03-09 22:23:28 -07002602 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002603 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002604
Andi Kleen36ce5652017-06-02 08:48:10 -07002605out_badmix:
2606 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2607 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002608out:
David Ahern745f43e2011-03-09 22:23:26 -07002609 free(str);
2610 return rc;
2611}
2612
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002613#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2614 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2615 if ((lang_dirent->d_type == DT_DIR || \
2616 (lang_dirent->d_type == DT_UNKNOWN && \
2617 is_directory(scripts_path, lang_dirent))) && \
2618 (strcmp(lang_dirent->d_name, ".")) && \
2619 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002620
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002621#define for_each_script(lang_path, lang_dir, script_dirent) \
2622 while ((script_dirent = readdir(lang_dir)) != NULL) \
2623 if (script_dirent->d_type != DT_DIR && \
2624 (script_dirent->d_type != DT_UNKNOWN || \
2625 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002626
2627
2628#define RECORD_SUFFIX "-record"
2629#define REPORT_SUFFIX "-report"
2630
2631struct script_desc {
2632 struct list_head node;
2633 char *name;
2634 char *half_liner;
2635 char *args;
2636};
2637
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002638static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002639
2640static struct script_desc *script_desc__new(const char *name)
2641{
2642 struct script_desc *s = zalloc(sizeof(*s));
2643
Tom Zanussib5b87312010-11-10 08:16:51 -06002644 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002645 s->name = strdup(name);
2646
2647 return s;
2648}
2649
2650static void script_desc__delete(struct script_desc *s)
2651{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002652 zfree(&s->name);
2653 zfree(&s->half_liner);
2654 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002655 free(s);
2656}
2657
2658static void script_desc__add(struct script_desc *s)
2659{
2660 list_add_tail(&s->node, &script_descs);
2661}
2662
2663static struct script_desc *script_desc__find(const char *name)
2664{
2665 struct script_desc *s;
2666
2667 list_for_each_entry(s, &script_descs, node)
2668 if (strcasecmp(s->name, name) == 0)
2669 return s;
2670 return NULL;
2671}
2672
2673static struct script_desc *script_desc__findnew(const char *name)
2674{
2675 struct script_desc *s = script_desc__find(name);
2676
2677 if (s)
2678 return s;
2679
2680 s = script_desc__new(name);
2681 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002682 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002683
2684 script_desc__add(s);
2685
2686 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002687}
2688
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002689static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002690{
2691 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002692 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002693
2694 if (strlen(str) > suffix_len) {
2695 p = str + strlen(str) - suffix_len;
2696 if (!strncmp(p, suffix, suffix_len))
2697 return p;
2698 }
2699
2700 return NULL;
2701}
2702
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002703static int read_script_info(struct script_desc *desc, const char *filename)
2704{
2705 char line[BUFSIZ], *p;
2706 FILE *fp;
2707
2708 fp = fopen(filename, "r");
2709 if (!fp)
2710 return -1;
2711
2712 while (fgets(line, sizeof(line), fp)) {
2713 p = ltrim(line);
2714 if (strlen(p) == 0)
2715 continue;
2716 if (*p != '#')
2717 continue;
2718 p++;
2719 if (strlen(p) && *p == '!')
2720 continue;
2721
2722 p = ltrim(p);
2723 if (strlen(p) && p[strlen(p) - 1] == '\n')
2724 p[strlen(p) - 1] = '\0';
2725
2726 if (!strncmp(p, "description:", strlen("description:"))) {
2727 p += strlen("description:");
2728 desc->half_liner = strdup(ltrim(p));
2729 continue;
2730 }
2731
2732 if (!strncmp(p, "args:", strlen("args:"))) {
2733 p += strlen("args:");
2734 desc->args = strdup(ltrim(p));
2735 continue;
2736 }
2737 }
2738
2739 fclose(fp);
2740
2741 return 0;
2742}
2743
Robert Richter38efb532011-11-25 11:38:40 +01002744static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2745{
2746 char *script_root, *str;
2747
2748 script_root = strdup(script_dirent->d_name);
2749 if (!script_root)
2750 return NULL;
2751
2752 str = (char *)ends_with(script_root, suffix);
2753 if (!str) {
2754 free(script_root);
2755 return NULL;
2756 }
2757
2758 *str = '\0';
2759 return script_root;
2760}
2761
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002762static int list_available_scripts(const struct option *opt __maybe_unused,
2763 const char *s __maybe_unused,
2764 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002765{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002766 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002767 char scripts_path[MAXPATHLEN];
2768 DIR *scripts_dir, *lang_dir;
2769 char script_path[MAXPATHLEN];
2770 char lang_path[MAXPATHLEN];
2771 struct script_desc *desc;
2772 char first_half[BUFSIZ];
2773 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002774
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002775 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002776
2777 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002778 if (!scripts_dir) {
2779 fprintf(stdout,
2780 "open(%s) failed.\n"
2781 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2782 scripts_path);
2783 exit(-1);
2784 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002785
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002786 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002787 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2788 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002789 lang_dir = opendir(lang_path);
2790 if (!lang_dir)
2791 continue;
2792
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002793 for_each_script(lang_path, lang_dir, script_dirent) {
2794 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002795 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002796 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002797 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2798 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002799 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002800 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002801 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002802 }
2803 }
2804
2805 fprintf(stdout, "List of available trace scripts:\n");
2806 list_for_each_entry(desc, &script_descs, node) {
2807 sprintf(first_half, "%s %s", desc->name,
2808 desc->args ? desc->args : "");
2809 fprintf(stdout, " %-36s %s\n", first_half,
2810 desc->half_liner ? desc->half_liner : "");
2811 }
2812
2813 exit(0);
2814}
2815
Feng Tange5f37052012-09-07 16:42:26 +08002816/*
Feng Tang49e639e2012-10-30 11:56:03 +08002817 * Some scripts specify the required events in their "xxx-record" file,
2818 * this function will check if the events in perf.data match those
2819 * mentioned in the "xxx-record".
2820 *
2821 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2822 * which is covered well now. And new parsing code should be added to
2823 * cover the future complexing formats like event groups etc.
2824 */
2825static int check_ev_match(char *dir_name, char *scriptname,
2826 struct perf_session *session)
2827{
2828 char filename[MAXPATHLEN], evname[128];
2829 char line[BUFSIZ], *p;
2830 struct perf_evsel *pos;
2831 int match, len;
2832 FILE *fp;
2833
Jiri Olsa77f18152018-03-19 09:29:01 +01002834 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08002835
2836 fp = fopen(filename, "r");
2837 if (!fp)
2838 return -1;
2839
2840 while (fgets(line, sizeof(line), fp)) {
2841 p = ltrim(line);
2842 if (*p == '#')
2843 continue;
2844
2845 while (strlen(p)) {
2846 p = strstr(p, "-e");
2847 if (!p)
2848 break;
2849
2850 p += 2;
2851 p = ltrim(p);
2852 len = strcspn(p, " \t");
2853 if (!len)
2854 break;
2855
2856 snprintf(evname, len + 1, "%s", p);
2857
2858 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002859 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002860 if (!strcmp(perf_evsel__name(pos), evname)) {
2861 match = 1;
2862 break;
2863 }
2864 }
2865
2866 if (!match) {
2867 fclose(fp);
2868 return -1;
2869 }
2870 }
2871 }
2872
2873 fclose(fp);
2874 return 0;
2875}
2876
2877/*
Feng Tange5f37052012-09-07 16:42:26 +08002878 * Return -1 if none is found, otherwise the actual scripts number.
2879 *
2880 * Currently the only user of this function is the script browser, which
2881 * will list all statically runnable scripts, select one, execute it and
2882 * show the output in a perf browser.
2883 */
2884int find_scripts(char **scripts_array, char **scripts_path_array)
2885{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002886 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002887 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002888 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002889 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002890 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002891 .file = {
2892 .path = input_name,
2893 },
2894 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002895 };
Feng Tange5f37052012-09-07 16:42:26 +08002896 char *temp;
2897 int i = 0;
2898
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002899 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002900 if (!session)
2901 return -1;
2902
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002903 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002904
2905 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002906 if (!scripts_dir) {
2907 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002908 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002909 }
Feng Tange5f37052012-09-07 16:42:26 +08002910
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002911 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002912 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
2913 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08002914#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08002915 if (strstr(lang_path, "perl"))
2916 continue;
2917#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08002918#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08002919 if (strstr(lang_path, "python"))
2920 continue;
2921#endif
2922
2923 lang_dir = opendir(lang_path);
2924 if (!lang_dir)
2925 continue;
2926
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002927 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002928 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002929 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002930 continue;
2931 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002932 script_dirent->d_name);
2933 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002934 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002935 (temp - script_dirent->d_name) + 1,
2936 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002937
2938 if (check_ev_match(lang_path,
2939 scripts_array[i], session))
2940 continue;
2941
Feng Tange5f37052012-09-07 16:42:26 +08002942 i++;
2943 }
Feng Tang49e639e2012-10-30 11:56:03 +08002944 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002945 }
2946
Feng Tang49e639e2012-10-30 11:56:03 +08002947 closedir(scripts_dir);
2948 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002949 return i;
2950}
2951
Tom Zanussi38752942009-12-15 02:53:39 -06002952static char *get_script_path(const char *script_root, const char *suffix)
2953{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002954 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002955 char scripts_path[MAXPATHLEN];
2956 char script_path[MAXPATHLEN];
2957 DIR *scripts_dir, *lang_dir;
2958 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002959 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002960
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002961 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002962
2963 scripts_dir = opendir(scripts_path);
2964 if (!scripts_dir)
2965 return NULL;
2966
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002967 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002968 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2969 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002970 lang_dir = opendir(lang_path);
2971 if (!lang_dir)
2972 continue;
2973
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002974 for_each_script(lang_path, lang_dir, script_dirent) {
2975 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002976 if (__script_root && !strcmp(script_root, __script_root)) {
2977 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002978 closedir(lang_dir);
2979 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01002980 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2981 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002982 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002983 }
2984 free(__script_root);
2985 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002986 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002987 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002988 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002989
Robert Richter38efb532011-11-25 11:38:40 +01002990 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002991}
2992
Tom Zanussib5b87312010-11-10 08:16:51 -06002993static bool is_top_script(const char *script_path)
2994{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002995 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002996}
2997
2998static int has_required_arg(char *script_path)
2999{
3000 struct script_desc *desc;
3001 int n_args = 0;
3002 char *p;
3003
3004 desc = script_desc__new(NULL);
3005
3006 if (read_script_info(desc, script_path))
3007 goto out;
3008
3009 if (!desc->args)
3010 goto out;
3011
3012 for (p = desc->args; *p; p++)
3013 if (*p == '<')
3014 n_args++;
3015out:
3016 script_desc__delete(desc);
3017
3018 return n_args;
3019}
3020
David Ahernd54b1a92012-08-26 12:24:46 -06003021static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003022{
3023 char **__argv = malloc(sizeof(const char *) * argc);
3024
David Ahernd54b1a92012-08-26 12:24:46 -06003025 if (!__argv) {
3026 pr_err("malloc failed\n");
3027 return -1;
3028 }
3029
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003030 memcpy(__argv, argv, sizeof(const char *) * argc);
3031 argc = parse_options(argc, (const char **)__argv, record_options,
3032 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3033 free(__argv);
3034
David Ahernd54b1a92012-08-26 12:24:46 -06003035 system_wide = (argc == 0);
3036
3037 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003038}
3039
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003040static void script__setup_sample_type(struct perf_script *script)
3041{
3042 struct perf_session *session = script->session;
3043 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3044
3045 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3046 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003047 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003048 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003049 dwarf_callchain_users = true;
3050 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003051 callchain_param.record_mode = CALLCHAIN_LBR;
3052 else
3053 callchain_param.record_mode = CALLCHAIN_FP;
3054 }
3055}
3056
Jiri Olsa89f16882018-09-13 14:54:03 +02003057static int process_stat_round_event(struct perf_session *session,
3058 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003059{
3060 struct stat_round_event *round = &event->stat_round;
3061 struct perf_evsel *counter;
3062
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003063 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003064 perf_stat_process_counter(&stat_config, counter);
3065 process_stat(counter, round->time);
3066 }
3067
3068 process_stat_interval(round->time);
3069 return 0;
3070}
3071
Jiri Olsa89f16882018-09-13 14:54:03 +02003072static int process_stat_config_event(struct perf_session *session __maybe_unused,
3073 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003074{
3075 perf_event__read_stat_config(&stat_config, &event->stat_config);
3076 return 0;
3077}
3078
Jiri Olsacfc88742016-01-05 22:09:06 +01003079static int set_maps(struct perf_script *script)
3080{
3081 struct perf_evlist *evlist = script->session->evlist;
3082
3083 if (!script->cpus || !script->threads)
3084 return 0;
3085
3086 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3087 return -EINVAL;
3088
3089 perf_evlist__set_maps(evlist, script->cpus, script->threads);
3090
3091 if (perf_evlist__alloc_stats(evlist, true))
3092 return -ENOMEM;
3093
3094 script->allocated = true;
3095 return 0;
3096}
3097
3098static
Jiri Olsa89f16882018-09-13 14:54:03 +02003099int process_thread_map_event(struct perf_session *session,
3100 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003101{
Jiri Olsa89f16882018-09-13 14:54:03 +02003102 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003103 struct perf_script *script = container_of(tool, struct perf_script, tool);
3104
3105 if (script->threads) {
3106 pr_warning("Extra thread map event, ignoring.\n");
3107 return 0;
3108 }
3109
3110 script->threads = thread_map__new_event(&event->thread_map);
3111 if (!script->threads)
3112 return -ENOMEM;
3113
3114 return set_maps(script);
3115}
3116
3117static
Jiri Olsa89f16882018-09-13 14:54:03 +02003118int process_cpu_map_event(struct perf_session *session,
3119 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003120{
Jiri Olsa89f16882018-09-13 14:54:03 +02003121 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003122 struct perf_script *script = container_of(tool, struct perf_script, tool);
3123
3124 if (script->cpus) {
3125 pr_warning("Extra cpu map event, ignoring.\n");
3126 return 0;
3127 }
3128
3129 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3130 if (!script->cpus)
3131 return -ENOMEM;
3132
3133 return set_maps(script);
3134}
3135
Jiri Olsa89f16882018-09-13 14:54:03 +02003136static int process_feature_event(struct perf_session *session,
3137 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303138{
3139 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003140 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303141 return 0;
3142}
3143
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003144#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003145static int perf_script__process_auxtrace_info(struct perf_session *session,
3146 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003147{
Jiri Olsa89f16882018-09-13 14:54:03 +02003148 struct perf_tool *tool = session->tool;
3149
3150 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003151
3152 if (ret == 0) {
3153 struct perf_script *script = container_of(tool, struct perf_script, tool);
3154
3155 ret = perf_script__setup_per_event_dump(script);
3156 }
3157
3158 return ret;
3159}
3160#else
3161#define perf_script__process_auxtrace_info 0
3162#endif
3163
Andi Kleenb585ebd2018-09-20 11:05:36 -07003164static int parse_insn_trace(const struct option *opt __maybe_unused,
3165 const char *str __maybe_unused,
3166 int unset __maybe_unused)
3167{
3168 parse_output_fields(NULL, "+insn,-event,-period", 0);
3169 itrace_parse_synth_opts(opt, "i0ns", 0);
3170 nanosecs = true;
3171 return 0;
3172}
3173
3174static int parse_xed(const struct option *opt __maybe_unused,
3175 const char *str __maybe_unused,
3176 int unset __maybe_unused)
3177{
3178 force_pager("xed -F insn: -A -64 | less");
3179 return 0;
3180}
3181
Andi Kleend1b15522018-09-20 11:05:38 -07003182static int parse_call_trace(const struct option *opt __maybe_unused,
3183 const char *str __maybe_unused,
3184 int unset __maybe_unused)
3185{
3186 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3187 itrace_parse_synth_opts(opt, "cewp", 0);
3188 nanosecs = true;
3189 return 0;
3190}
3191
3192static int parse_callret_trace(const struct option *opt __maybe_unused,
3193 const char *str __maybe_unused,
3194 int unset __maybe_unused)
3195{
3196 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3197 itrace_parse_synth_opts(opt, "crewp", 0);
3198 nanosecs = true;
3199 return 0;
3200}
3201
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003202int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003203{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003204 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003205 bool header = false;
3206 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003207 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003208 char *rec_script_path = NULL;
3209 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003210 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003211 struct itrace_synth_opts itrace_synth_opts = {
3212 .set = false,
3213 .default_no_sample = true,
3214 };
Tom Zanussib5b87312010-11-10 08:16:51 -06003215 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003216 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003217 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003218 struct perf_script script = {
3219 .tool = {
3220 .sample = process_sample_event,
3221 .mmap = perf_event__process_mmap,
3222 .mmap2 = perf_event__process_mmap2,
3223 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303224 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003225 .exit = perf_event__process_exit,
3226 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003227 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003228 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003229 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303230 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003231 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003232 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003233 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003234 .auxtrace = perf_event__process_auxtrace,
3235 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003236 .stat = perf_event__process_stat_event,
3237 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003238 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003239 .thread_map = process_thread_map_event,
3240 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003241 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003242 .ordering_requires_timestamps = true,
3243 },
3244 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003245 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003246 .mode = PERF_DATA_MODE_READ,
3247 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003248 const struct option options[] = {
3249 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3250 "dump raw trace in ASCII"),
3251 OPT_INCR('v', "verbose", &verbose,
3252 "be more verbose (show symbol address, etc)"),
3253 OPT_BOOLEAN('L', "Latency", &latency_format,
3254 "show latency attributes (irqs/preemption disabled, etc)"),
3255 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3256 list_available_scripts),
3257 OPT_CALLBACK('s', "script", NULL, "name",
3258 "script file name (lang:script name, script name, or *)",
3259 parse_scriptname),
3260 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3261 "generate perf-script.xx script in specified language"),
3262 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3263 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3264 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003265 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3266 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003267 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3268 "file", "vmlinux pathname"),
3269 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3270 "file", "kallsyms pathname"),
3271 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3272 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003273 OPT_CALLBACK(0, "symfs", NULL, "directory",
3274 "Look for files with symbols relative to this directory",
3275 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003276 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003277 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003278 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003279 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003280 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303281 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3282 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
3283 "callindent,insn,insnlen,synth,phys_addr,metric,misc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003284 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003285 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3286 "system-wide collection from all CPUs"),
3287 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3288 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003289 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3290 "Decode instructions from itrace", parse_insn_trace),
3291 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3292 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003293 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3294 "Decode calls from from itrace", parse_call_trace),
3295 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3296 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003297 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3298 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003299 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3300 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003301 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3302 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3303 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003304 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3305 "only consider symbols in these pids"),
3306 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3307 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003308 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3309 "Set the maximum stack depth when parsing the callchain, "
3310 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003311 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003312 OPT_BOOLEAN('I', "show-info", &show_full_info,
3313 "display extended information from perf.data file"),
3314 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3315 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003316 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3317 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003318 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3319 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003320 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3321 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303322 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3323 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003324 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3325 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003326 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3327 "Show round events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003328 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3329 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003330 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003331 OPT_INTEGER(0, "max-blocks", &max_blocks,
3332 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03003333 OPT_BOOLEAN(0, "ns", &nanosecs,
3334 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003335 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003336 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003337 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003338 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3339 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003340 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3341 "Enable symbol demangling"),
3342 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3343 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003344 OPT_STRING(0, "time", &script.time_str, "str",
3345 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003346 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3347 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003348 OPT_END()
3349 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003350 const char * const script_subcommands[] = { "record", "report", NULL };
3351 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003352 "perf script [<options>]",
3353 "perf script [<options>] record <script> [<record-options>] <command>",
3354 "perf script [<options>] report <script> [script-args]",
3355 "perf script [<options>] <script> [<record-options>] <command>",
3356 "perf script [<options>] <top-script> [script-args]",
3357 NULL
3358 };
Tom Zanussi38752942009-12-15 02:53:39 -06003359
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003360 perf_set_singlethreaded();
3361
Tom Zanussib5b87312010-11-10 08:16:51 -06003362 setup_scripting();
3363
Yunlong Song40cae2b2015-03-18 21:35:54 +08003364 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003365 PARSE_OPT_STOP_AT_NON_OPTION);
3366
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003367 data.file.path = input_name;
3368 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003369
Tom Zanussib5b87312010-11-10 08:16:51 -06003370 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3371 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3372 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003373 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003374 }
3375
Tom Zanussib5b87312010-11-10 08:16:51 -06003376 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3377 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3378 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003379 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003380 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003381 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003382 return -1;
3383 }
Tom Zanussi38752942009-12-15 02:53:39 -06003384 }
3385
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003386 if (itrace_synth_opts.callchain &&
3387 itrace_synth_opts.callchain_sz > scripting_max_stack)
3388 scripting_max_stack = itrace_synth_opts.callchain_sz;
3389
Ben Hutchings44e668c2010-10-10 16:10:03 +01003390 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003391 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003392
Tom Zanussib5b87312010-11-10 08:16:51 -06003393 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003394 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003395 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003396 pid_t pid;
3397
Tom Zanussib5b87312010-11-10 08:16:51 -06003398 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3399 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3400
3401 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003402 usage_with_options_msg(script_usage, options,
3403 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003404 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003405 }
3406
Tom Zanussib5b87312010-11-10 08:16:51 -06003407 if (is_top_script(argv[0])) {
3408 rep_args = argc - 1;
3409 } else {
3410 int rec_args;
3411
3412 rep_args = has_required_arg(rep_script_path);
3413 rec_args = (argc - 1) - rep_args;
3414 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003415 usage_with_options_msg(script_usage, options,
3416 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003417 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003418 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003419 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003420 }
3421
3422 if (pipe(live_pipe) < 0) {
3423 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003424 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003425 }
3426
3427 pid = fork();
3428 if (pid < 0) {
3429 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003430 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003431 }
3432
3433 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003434 j = 0;
3435
Tom Zanussia0cccc22010-04-01 23:59:25 -05003436 dup2(live_pipe[1], 1);
3437 close(live_pipe[0]);
3438
Robert Richter317df652011-11-25 15:05:25 +01003439 if (is_top_script(argv[0])) {
3440 system_wide = true;
3441 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003442 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3443 err = -1;
3444 goto out;
3445 }
Robert Richter317df652011-11-25 15:05:25 +01003446 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003447
3448 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003449 if (!__argv) {
3450 pr_err("malloc failed\n");
3451 err = -ENOMEM;
3452 goto out;
3453 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003454
Tom Zanussib5b87312010-11-10 08:16:51 -06003455 __argv[j++] = "/bin/sh";
3456 __argv[j++] = rec_script_path;
3457 if (system_wide)
3458 __argv[j++] = "-a";
3459 __argv[j++] = "-q";
3460 __argv[j++] = "-o";
3461 __argv[j++] = "-";
3462 for (i = rep_args + 1; i < argc; i++)
3463 __argv[j++] = argv[i];
3464 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003465
3466 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003467 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003468 exit(-1);
3469 }
3470
3471 dup2(live_pipe[0], 0);
3472 close(live_pipe[1]);
3473
Tom Zanussib5b87312010-11-10 08:16:51 -06003474 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003475 if (!__argv) {
3476 pr_err("malloc failed\n");
3477 err = -ENOMEM;
3478 goto out;
3479 }
3480
Tom Zanussib5b87312010-11-10 08:16:51 -06003481 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003482 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003483 __argv[j++] = rep_script_path;
3484 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003485 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003486 __argv[j++] = "-i";
3487 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003488 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003489
3490 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003491 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003492 exit(-1);
3493 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003494
Tom Zanussib5b87312010-11-10 08:16:51 -06003495 if (rec_script_path)
3496 script_path = rec_script_path;
3497 if (rep_script_path)
3498 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003499
Tom Zanussib5b87312010-11-10 08:16:51 -06003500 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003501 j = 0;
3502
Robert Richter317df652011-11-25 15:05:25 +01003503 if (!rec_script_path)
3504 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003505 else if (!system_wide) {
3506 if (have_cmd(argc - 1, &argv[1]) != 0) {
3507 err = -1;
3508 goto out;
3509 }
3510 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003511
3512 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003513 if (!__argv) {
3514 pr_err("malloc failed\n");
3515 err = -ENOMEM;
3516 goto out;
3517 }
3518
Tom Zanussib5b87312010-11-10 08:16:51 -06003519 __argv[j++] = "/bin/sh";
3520 __argv[j++] = script_path;
3521 if (system_wide)
3522 __argv[j++] = "-a";
3523 for (i = 2; i < argc; i++)
3524 __argv[j++] = argv[i];
3525 __argv[j++] = NULL;
3526
3527 execvp("/bin/sh", (char **)__argv);
3528 free(__argv);
3529 exit(-1);
3530 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003531
Milian Wolffc1c9b962018-10-21 21:14:23 +02003532 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003533 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003534 use_browser = 0;
3535 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003536
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003537 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003538 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003539 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003540
Jiri Olsae90debd2013-12-09 11:02:50 +01003541 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003542 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003543 perf_session__fprintf_info(session, stdout, show_full_info);
3544 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003545 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003546 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003547 if (show_full_info)
3548 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003549
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003550 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003551 goto out_delete;
3552
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003553 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003554 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003555
Andi Kleen99f753f2018-09-20 11:05:39 -07003556 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3557 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003558 itrace_synth_opts.thread_stack = true;
3559
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003560 session->itrace_synth_opts = &itrace_synth_opts;
3561
Anton Blanchard5d67be92011-07-04 21:57:50 +10003562 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003563 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3564 if (err < 0)
3565 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003566 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003567 }
3568
David Ahern1424dc92011-03-09 22:23:28 -07003569 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003570 symbol_conf.use_callchain = true;
3571 else
3572 symbol_conf.use_callchain = false;
3573
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003574 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003575 tep_set_function_resolver(session->tevent.pevent,
3576 machine__resolve_kernel_addr,
3577 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003578 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003579 err = -1;
3580 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003581 }
3582
Tom Zanussi956ffd02009-11-25 01:15:46 -06003583 if (generate_script_lang) {
3584 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003585 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003586
David Ahern2c9e45f72011-03-17 10:03:21 -06003587 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003588 fprintf(stderr,
3589 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003590 err = -EINVAL;
3591 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003592 }
3593
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003594 input = open(data.file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003595 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003596 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003597 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003598 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003599 }
3600
3601 err = fstat(input, &perf_stat);
3602 if (err < 0) {
3603 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003604 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003605 }
3606
3607 if (!perf_stat.st_size) {
3608 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003609 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003610 }
3611
3612 scripting_ops = script_spec__lookup(generate_script_lang);
3613 if (!scripting_ops) {
3614 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003615 err = -ENOENT;
3616 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003617 }
3618
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003619 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003620 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003621 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003622 }
3623
3624 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003625 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003626 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003627 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003628 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003629 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003630 }
3631
David Ahern9cbdb702011-04-06 21:54:20 -06003632
3633 err = perf_session__check_output_opt(session);
3634 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003635 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003636
Jin Yaocc2ef582018-01-10 23:00:33 +08003637 script.ptime_range = perf_time__range_alloc(script.time_str,
3638 &script.range_size);
3639 if (!script.ptime_range) {
3640 err = -ENOMEM;
3641 goto out_delete;
3642 }
3643
David Aherna91f4c42016-11-29 10:15:43 -07003644 /* needs to be parsed after looking up reference time */
Jin Yao2ab046c2017-12-08 21:13:46 +08003645 if (perf_time__parse_str(script.ptime_range, script.time_str) != 0) {
3646 if (session->evlist->first_sample_time == 0 &&
3647 session->evlist->last_sample_time == 0) {
Jin Yao1e2778e2018-01-10 23:00:27 +08003648 pr_err("HINT: no first/last sample time found in perf data.\n"
3649 "Please use latest perf binary to execute 'perf record'\n"
3650 "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
Jin Yao2ab046c2017-12-08 21:13:46 +08003651 err = -EINVAL;
3652 goto out_delete;
3653 }
3654
3655 script.range_num = perf_time__percent_parse_str(
Jin Yaocc2ef582018-01-10 23:00:33 +08003656 script.ptime_range, script.range_size,
Jin Yao2ab046c2017-12-08 21:13:46 +08003657 script.time_str,
3658 session->evlist->first_sample_time,
3659 session->evlist->last_sample_time);
3660
3661 if (script.range_num < 0) {
3662 pr_err("Invalid time string\n");
3663 err = -EINVAL;
3664 goto out_delete;
3665 }
3666 } else {
3667 script.range_num = 1;
David Aherna91f4c42016-11-29 10:15:43 -07003668 }
3669
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003670 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003671
Adrian Hunterd445dd22014-08-15 22:08:37 +03003672 flush_scripting();
3673
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003674out_delete:
Jin Yaocc2ef582018-01-10 23:00:33 +08003675 zfree(&script.ptime_range);
3676
Jiri Olsacfc88742016-01-05 22:09:06 +01003677 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003678 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003679
3680 if (script_started)
3681 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003682out:
3683 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003684}