blob: 8f8fa952b50635c6f8e0f2449d256fb0366ce502 [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#include "builtin.h"
2
Andrea Gelminib7eead82010-08-05 15:51:38 +02003#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02004#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02005#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06006#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02007#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06008#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +02009#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020010#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020011#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020012#include "util/symbol.h"
13#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010014#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020015#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070016#include "util/evlist.h"
17#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080018#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030020#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010021#include "util/cpumap.h"
22#include "util/thread_map.h"
23#include "util/stat.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030024#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030025#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070026#include "util/time-utils.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030027#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100028#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030029#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030030#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030031#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010032#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010033#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080034#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030035#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030036#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030037#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030038#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030039#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030040#include <sys/types.h>
41#include <sys/stat.h>
42#include <unistd.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020043
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030044#include "sane_ctype.h"
45
Tom Zanussi956ffd02009-11-25 01:15:46 -060046static char const *script_name;
47static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020048static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020049static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020050static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070051static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090052static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010053static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030054static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030055static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100056static const char *cpu_list;
57static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010058static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080059static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060060
Adrian Hunter44cbe722015-09-25 16:15:50 +030061unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030062
David Ahern745f43e2011-03-09 22:23:26 -070063enum perf_output_field {
64 PERF_OUTPUT_COMM = 1U << 0,
65 PERF_OUTPUT_TID = 1U << 1,
66 PERF_OUTPUT_PID = 1U << 2,
67 PERF_OUTPUT_TIME = 1U << 3,
68 PERF_OUTPUT_CPU = 1U << 4,
69 PERF_OUTPUT_EVNAME = 1U << 5,
70 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060071 PERF_OUTPUT_IP = 1U << 7,
72 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060073 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060074 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090075 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020076 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020077 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020078 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020079 PERF_OUTPUT_BRSTACK = 1U << 15,
80 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010081 PERF_OUTPUT_DATA_SRC = 1U << 17,
82 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000083 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030084 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030085 PERF_OUTPUT_INSN = 1U << 21,
86 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080087 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070088 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030089 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -040090 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -070091 PERF_OUTPUT_UREGS = 1U << 27,
David Ahern745f43e2011-03-09 22:23:26 -070092};
93
94struct output_option {
95 const char *str;
96 enum perf_output_field field;
97} all_output_options[] = {
98 {.str = "comm", .field = PERF_OUTPUT_COMM},
99 {.str = "tid", .field = PERF_OUTPUT_TID},
100 {.str = "pid", .field = PERF_OUTPUT_PID},
101 {.str = "time", .field = PERF_OUTPUT_TIME},
102 {.str = "cpu", .field = PERF_OUTPUT_CPU},
103 {.str = "event", .field = PERF_OUTPUT_EVNAME},
104 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600105 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700106 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600107 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600108 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900109 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200110 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200111 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200112 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700113 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200114 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
115 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100116 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
117 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000118 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300119 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300120 {.str = "insn", .field = PERF_OUTPUT_INSN},
121 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800122 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700123 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300124 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400125 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
David Ahern745f43e2011-03-09 22:23:26 -0700126};
127
Adrian Hunter14057202017-06-21 13:17:19 +0300128enum {
129 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
130 OUTPUT_TYPE_MAX
131};
132
David Ahern745f43e2011-03-09 22:23:26 -0700133/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600134static struct {
135 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600136 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400137 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600138 u64 fields;
139 u64 invalid_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300140} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700141
David Ahern2c9e45f72011-03-17 10:03:21 -0600142 [PERF_TYPE_HARDWARE] = {
143 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700144
David Ahern2c9e45f72011-03-17 10:03:21 -0600145 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
146 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600147 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200148 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
149 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600150
Wang Nan30372f02016-02-24 11:20:45 +0000151 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600152 },
153
154 [PERF_TYPE_SOFTWARE] = {
155 .user_set = false,
156
157 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
158 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600159 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200160 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000161 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600162
163 .invalid_fields = PERF_OUTPUT_TRACE,
164 },
165
166 [PERF_TYPE_TRACEPOINT] = {
167 .user_set = false,
168
169 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
170 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000171 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600172 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700173
174 [PERF_TYPE_RAW] = {
175 .user_set = false,
176
177 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
178 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600179 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200180 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100181 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
Kan Liang49d58f02017-08-29 13:11:11 -0400182 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT |
183 PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700184
Wang Nan30372f02016-02-24 11:20:45 +0000185 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700186 },
Wang Nan27cfef02015-12-08 02:25:43 +0000187
188 [PERF_TYPE_BREAKPOINT] = {
189 .user_set = false,
190
191 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
192 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
193 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
194 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
195 PERF_OUTPUT_PERIOD,
196
Wang Nan30372f02016-02-24 11:20:45 +0000197 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000198 },
Adrian Hunter14057202017-06-21 13:17:19 +0300199
200 [OUTPUT_TYPE_SYNTH] = {
201 .user_set = false,
202
203 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
204 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
205 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Adrian Hunter47e78082017-05-26 11:17:22 +0300206 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
207 PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300208
209 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
210 },
David Ahern1424dc92011-03-09 22:23:28 -0700211};
David Ahern745f43e2011-03-09 22:23:26 -0700212
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300213struct perf_evsel_script {
214 char *filename;
215 FILE *fp;
216 u64 samples;
217};
218
219static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100220 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300221{
222 struct perf_evsel_script *es = malloc(sizeof(*es));
223
224 if (es != NULL) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100225 if (asprintf(&es->filename, "%s.%s.dump", data->path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300226 goto out_free;
227 es->fp = fopen(es->filename, "w");
228 if (es->fp == NULL)
229 goto out_free_filename;
230 es->samples = 0;
231 }
232
233 return es;
234out_free_filename:
235 zfree(&es->filename);
236out_free:
237 free(es);
238 return NULL;
239}
240
241static void perf_evsel_script__delete(struct perf_evsel_script *es)
242{
243 zfree(&es->filename);
244 fclose(es->fp);
245 es->fp = NULL;
246 free(es);
247}
248
249static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
250{
251 struct stat st;
252
253 fstat(fileno(es->fp), &st);
254 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
255 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
256}
257
Adrian Hunter14057202017-06-21 13:17:19 +0300258static inline int output_type(unsigned int type)
259{
260 switch (type) {
261 case PERF_TYPE_SYNTH:
262 return OUTPUT_TYPE_SYNTH;
263 default:
264 return type;
265 }
266}
267
268static inline unsigned int attr_type(unsigned int type)
269{
270 switch (type) {
271 case OUTPUT_TYPE_SYNTH:
272 return PERF_TYPE_SYNTH;
273 default:
274 return type;
275 }
276}
277
David Ahern2c9e45f72011-03-17 10:03:21 -0600278static bool output_set_by_user(void)
279{
280 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300281 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600282 if (output[j].user_set)
283 return true;
284 }
285 return false;
286}
David Ahern745f43e2011-03-09 22:23:26 -0700287
David Ahern9cbdb702011-04-06 21:54:20 -0600288static const char *output_field2str(enum perf_output_field field)
289{
290 int i, imax = ARRAY_SIZE(all_output_options);
291 const char *str = "";
292
293 for (i = 0; i < imax; ++i) {
294 if (all_output_options[i].field == field) {
295 str = all_output_options[i].str;
296 break;
297 }
298 }
299 return str;
300}
301
Adrian Hunter14057202017-06-21 13:17:19 +0300302#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700303
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300304static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
305 u64 sample_type, const char *sample_msg,
306 enum perf_output_field field,
307 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700308{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300309 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300310 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600311 const char *evname;
312
313 if (attr->sample_type & sample_type)
314 return 0;
315
316 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300317 if (allow_user_set)
318 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300319 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600320 pr_err("Samples for '%s' event do not have %s attribute set. "
321 "Cannot print '%s' field.\n",
322 evname, sample_msg, output_field2str(field));
323 return -1;
324 }
325
326 /* user did not ask for it explicitly so remove from the default list */
327 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300328 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600329 pr_debug("Samples for '%s' event do not have %s attribute set. "
330 "Skipping '%s' field.\n",
331 evname, sample_msg, output_field2str(field));
332
333 return 0;
334}
335
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300336static int perf_evsel__check_stype(struct perf_evsel *evsel,
337 u64 sample_type, const char *sample_msg,
338 enum perf_output_field field)
339{
340 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
341 false);
342}
343
David Ahern9cbdb702011-04-06 21:54:20 -0600344static int perf_evsel__check_attr(struct perf_evsel *evsel,
345 struct perf_session *session)
346{
347 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300348 bool allow_user_set;
349
Jiri Olsae099eba2016-01-05 22:09:09 +0100350 if (perf_header__has_feat(&session->header, HEADER_STAT))
351 return 0;
352
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300353 allow_user_set = perf_header__has_feat(&session->header,
354 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600355
David Ahern1424dc92011-03-09 22:23:28 -0700356 if (PRINT_FIELD(TRACE) &&
357 !perf_session__has_traces(session, "record -R"))
358 return -EINVAL;
359
David Ahern787bef12011-05-27 14:28:43 -0600360 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300361 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
362 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700363 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700364 }
David Ahern7cec0922011-05-30 13:08:23 -0600365
366 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300367 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
368 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600369 return -EINVAL;
370
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100371 if (PRINT_FIELD(DATA_SRC) &&
372 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
373 PERF_OUTPUT_DATA_SRC))
374 return -EINVAL;
375
376 if (PRINT_FIELD(WEIGHT) &&
377 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
378 PERF_OUTPUT_WEIGHT))
379 return -EINVAL;
380
David Ahern7cec0922011-05-30 13:08:23 -0600381 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
382 pr_err("Display of symbols requested but neither sample IP nor "
383 "sample address\nis selected. Hence, no addresses to convert "
384 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600385 return -EINVAL;
386 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900387 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
388 pr_err("Display of offsets requested but symbol is not"
389 "selected.\n");
390 return -EINVAL;
391 }
Mark Santaniello55b9b502017-06-19 09:38:24 -0700392 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR) &&
Mark Santaniello106dacd2017-06-19 09:38:25 -0700393 !PRINT_FIELD(BRSTACK) && !PRINT_FIELD(BRSTACKSYM) && !PRINT_FIELD(BRSTACKOFF)) {
394 pr_err("Display of DSO requested but no address to convert. Select\n"
395 "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
David Ahern610723f2011-05-27 14:28:44 -0600396 return -EINVAL;
397 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200398 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
399 pr_err("Display of source line number requested but sample IP is not\n"
400 "selected. Hence, no address to lookup the source line number.\n");
401 return -EINVAL;
402 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800403 if (PRINT_FIELD(BRSTACKINSN) &&
404 !(perf_evlist__combined_branch_type(session->evlist) &
405 PERF_SAMPLE_BRANCH_ANY)) {
406 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
407 "Hint: run 'perf record -b ...'\n");
408 return -EINVAL;
409 }
David Ahern1424dc92011-03-09 22:23:28 -0700410 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300411 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
412 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700413 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700414
415 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300416 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
417 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700418 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700419
420 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300421 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
422 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700423 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600424
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200425 if (PRINT_FIELD(PERIOD) &&
426 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
427 PERF_OUTPUT_PERIOD))
428 return -EINVAL;
429
Stephane Eranianfc36f942015-08-31 18:41:10 +0200430 if (PRINT_FIELD(IREGS) &&
431 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
432 PERF_OUTPUT_IREGS))
433 return -EINVAL;
434
Andi Kleenb1491ac2017-09-05 11:40:57 -0700435 if (PRINT_FIELD(UREGS) &&
436 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
437 PERF_OUTPUT_UREGS))
438 return -EINVAL;
439
Kan Liang49d58f02017-08-29 13:11:11 -0400440 if (PRINT_FIELD(PHYS_ADDR) &&
441 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
442 PERF_OUTPUT_PHYS_ADDR))
443 return -EINVAL;
444
David Ahern9cbdb702011-04-06 21:54:20 -0600445 return 0;
446}
447
Adrian Hunter7ea95722013-11-01 15:51:30 +0200448static void set_print_ip_opts(struct perf_event_attr *attr)
449{
Adrian Hunter14057202017-06-21 13:17:19 +0300450 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200451
452 output[type].print_ip_opts = 0;
453 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300454 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200455
456 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300457 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200458
459 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300460 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200461
462 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300463 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200464
465 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300466 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200467}
468
David Ahern9cbdb702011-04-06 21:54:20 -0600469/*
470 * verify all user requested events exist and the samples
471 * have the expected data
472 */
473static int perf_session__check_output_opt(struct perf_session *session)
474{
He Kuang40f20e52016-05-16 04:51:19 +0000475 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600476 struct perf_evsel *evsel;
477
Adrian Hunter14057202017-06-21 13:17:19 +0300478 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
479 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600480
481 /*
482 * even if fields is set to 0 (ie., show nothing) event must
483 * exist if user explicitly includes it on the command line
484 */
Adrian Hunter14057202017-06-21 13:17:19 +0300485 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
486 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600487 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300488 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600489 event_type(j));
490 return -1;
491 }
492
493 if (evsel && output[j].fields &&
494 perf_evsel__check_attr(evsel, session))
495 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400496
497 if (evsel == NULL)
498 continue;
499
Adrian Hunter7ea95722013-11-01 15:51:30 +0200500 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700501 }
502
Adrian Hunter98526ee2014-07-31 09:00:59 +0300503 if (!no_callchain) {
504 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000505 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300506
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300507 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000508 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300509 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
510 use_callchain = true;
511 break;
512 }
513 }
He Kuang71ac8992016-08-04 11:25:43 +0000514 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300515 symbol_conf.use_callchain = false;
516 }
517
David Ahern80b8b492013-11-19 21:07:37 -0700518 /*
519 * set default for tracepoints to print symbols only
520 * if callchains are present
521 */
522 if (symbol_conf.use_callchain &&
523 !output[PERF_TYPE_TRACEPOINT].user_set) {
524 struct perf_event_attr *attr;
525
526 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700527
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300528 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000529 if (evsel->attr.type != j)
530 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700531
He Kuang40f20e52016-05-16 04:51:19 +0000532 attr = &evsel->attr;
533
534 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
535 output[j].fields |= PERF_OUTPUT_IP;
536 output[j].fields |= PERF_OUTPUT_SYM;
537 output[j].fields |= PERF_OUTPUT_DSO;
538 set_print_ip_opts(attr);
539 goto out;
540 }
David Ahern80b8b492013-11-19 21:07:37 -0700541 }
542 }
543
544out:
David Ahern1424dc92011-03-09 22:23:28 -0700545 return 0;
546}
David Ahern745f43e2011-03-09 22:23:26 -0700547
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300548static int perf_sample__fprintf_iregs(struct perf_sample *sample,
549 struct perf_event_attr *attr, FILE *fp)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200550{
551 struct regs_dump *regs = &sample->intr_regs;
552 uint64_t mask = attr->sample_regs_intr;
553 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300554 int printed = 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200555
556 if (!regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300557 return 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200558
559 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
560 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300561 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Stephane Eranianfc36f942015-08-31 18:41:10 +0200562 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300563
564 return printed;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200565}
566
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300567static int perf_sample__fprintf_uregs(struct perf_sample *sample,
568 struct perf_event_attr *attr, FILE *fp)
Andi Kleenb1491ac2017-09-05 11:40:57 -0700569{
570 struct regs_dump *regs = &sample->user_regs;
571 uint64_t mask = attr->sample_regs_user;
572 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300573 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700574
575 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300576 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700577
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300578 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700579
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);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700583 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300584
585 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700586}
587
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300588static int perf_sample__fprintf_start(struct perf_sample *sample,
589 struct thread *thread,
590 struct perf_evsel *evsel, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700591{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300592 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700593 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700594 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300595 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700596
David Ahern745f43e2011-03-09 22:23:26 -0700597 if (PRINT_FIELD(COMM)) {
598 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300599 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600600 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300601 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700602 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300603 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700604 }
David Ahernc70c94b2011-03-09 22:23:25 -0700605
David Ahern745f43e2011-03-09 22:23:26 -0700606 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300607 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700608 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300609 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700610 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300611 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700612
David Ahern745f43e2011-03-09 22:23:26 -0700613 if (PRINT_FIELD(CPU)) {
614 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300615 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700616 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300617 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700618 }
David Ahernc70c94b2011-03-09 22:23:25 -0700619
David Ahern745f43e2011-03-09 22:23:26 -0700620 if (PRINT_FIELD(TIME)) {
621 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300622 secs = nsecs / NSEC_PER_SEC;
623 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900624
Adrian Hunter83e19862015-09-25 16:15:36 +0300625 if (nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300626 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900627 else {
628 char sample_time[32];
629 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300630 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900631 }
David Ahern745f43e2011-03-09 22:23:26 -0700632 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300633
634 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700635}
636
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200637static inline char
638mispred_str(struct branch_entry *br)
639{
640 if (!(br->flags.mispred || br->flags.predicted))
641 return '-';
642
643 return br->flags.predicted ? 'P' : 'M';
644}
645
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300646static int perf_sample__fprintf_brstack(struct perf_sample *sample,
647 struct thread *thread,
648 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200649{
650 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700651 struct addr_location alf, alt;
652 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300653 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200654
655 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300656 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200657
658 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700659 from = br->entries[i].from;
660 to = br->entries[i].to;
661
662 if (PRINT_FIELD(DSO)) {
663 memset(&alf, 0, sizeof(alf));
664 memset(&alt, 0, sizeof(alt));
665 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
666 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
667 }
668
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300669 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700670 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300671 printed += fprintf(fp, "(");
672 printed += map__fprintf_dsoname(alf.map, fp);
673 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700674 }
675
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300676 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700677 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300678 printed += fprintf(fp, "(");
679 printed += map__fprintf_dsoname(alt.map, fp);
680 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700681 }
682
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300683 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200684 mispred_str( br->entries + i),
685 br->entries[i].flags.in_tx? 'X' : '-',
686 br->entries[i].flags.abort? 'A' : '-',
687 br->entries[i].flags.cycles);
688 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300689
690 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200691}
692
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300693static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
694 struct thread *thread,
695 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200696{
697 struct branch_stack *br = sample->branch_stack;
698 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200699 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300700 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200701
702 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300703 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200704
705 for (i = 0; i < br->nr; i++) {
706
707 memset(&alf, 0, sizeof(alf));
708 memset(&alt, 0, sizeof(alt));
709 from = br->entries[i].from;
710 to = br->entries[i].to;
711
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300712 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200713 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300714 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200715
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300716 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200717 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300718 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200719
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300720 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700721 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300722 printed += fprintf(fp, "(");
723 printed += map__fprintf_dsoname(alf.map, fp);
724 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700725 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300726 printed += fprintf(fp, "%c", '/');
727 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700728 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300729 printed += fprintf(fp, "(");
730 printed += map__fprintf_dsoname(alt.map, fp);
731 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700732 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300733 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200734 mispred_str( br->entries + i),
735 br->entries[i].flags.in_tx? 'X' : '-',
736 br->entries[i].flags.abort? 'A' : '-',
737 br->entries[i].flags.cycles);
738 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300739
740 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200741}
742
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300743static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
744 struct thread *thread,
745 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700746{
747 struct branch_stack *br = sample->branch_stack;
748 struct addr_location alf, alt;
749 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300750 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700751
752 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300753 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700754
755 for (i = 0; i < br->nr; i++) {
756
757 memset(&alf, 0, sizeof(alf));
758 memset(&alt, 0, sizeof(alt));
759 from = br->entries[i].from;
760 to = br->entries[i].to;
761
762 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
763 if (alf.map && !alf.map->dso->adjust_symbols)
764 from = map__map_ip(alf.map, from);
765
766 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
767 if (alt.map && !alt.map->dso->adjust_symbols)
768 to = map__map_ip(alt.map, to);
769
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300770 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700771 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300772 printed += fprintf(fp, "(");
773 printed += map__fprintf_dsoname(alf.map, fp);
774 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700775 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300776 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700777 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300778 printed += fprintf(fp, "(");
779 printed += map__fprintf_dsoname(alt.map, fp);
780 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700781 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300782 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700783 mispred_str(br->entries + i),
784 br->entries[i].flags.in_tx ? 'X' : '-',
785 br->entries[i].flags.abort ? 'A' : '-',
786 br->entries[i].flags.cycles);
787 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300788
789 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700790}
Andi Kleen48d02a12017-02-23 15:46:34 -0800791#define MAXBB 16384UL
792
793static int grab_bb(u8 *buffer, u64 start, u64 end,
794 struct machine *machine, struct thread *thread,
795 bool *is64bit, u8 *cpumode, bool last)
796{
797 long offset, len;
798 struct addr_location al;
799 bool kernel;
800
801 if (!start || !end)
802 return 0;
803
804 kernel = machine__kernel_ip(machine, start);
805 if (kernel)
806 *cpumode = PERF_RECORD_MISC_KERNEL;
807 else
808 *cpumode = PERF_RECORD_MISC_USER;
809
810 /*
811 * Block overlaps between kernel and user.
812 * This can happen due to ring filtering
813 * On Intel CPUs the entry into the kernel is filtered,
814 * but the exit is not. Let the caller patch it up.
815 */
816 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300817 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800818 return -ENXIO;
819 }
820
821 memset(&al, 0, sizeof(al));
822 if (end - start > MAXBB - MAXINSN) {
823 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300824 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800825 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300826 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800827 return 0;
828 }
829
830 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
831 if (!al.map || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300832 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800833 return 0;
834 }
835 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300836 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800837 return 0;
838 }
839
840 /* Load maps to ensure dso->is_64_bit has been updated */
841 map__load(al.map);
842
843 offset = al.map->map_ip(al.map, start);
844 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
845 end - start + MAXINSN);
846
847 *is64bit = al.map->dso->is_64_bit;
848 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300849 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800850 start, end);
851 return len;
852}
853
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300854static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
855 struct perf_insn *x, u8 *inbuf, int len,
856 int insn, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800857{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300858 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
859 dump_insn(x, ip, inbuf, len, NULL),
860 en->flags.predicted ? " PRED" : "",
861 en->flags.mispred ? " MISPRED" : "",
862 en->flags.in_tx ? " INTX" : "",
863 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800864 if (en->flags.cycles) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300865 printed += fprintf(fp, " %d cycles", en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800866 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300867 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800868 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300869 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800870}
871
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300872static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
873 u8 cpumode, int cpu, struct symbol **lastsym,
874 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800875{
876 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300877 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800878
879 memset(&al, 0, sizeof(al));
880
881 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
882 if (!al.map)
883 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
884 addr, &al);
885 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300886 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800887
888 al.cpu = cpu;
889 al.sym = NULL;
890 if (al.map)
891 al.sym = map__find_symbol(al.map, al.addr);
892
893 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300894 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800895
896 if (al.addr < al.sym->end)
897 off = al.addr - al.sym->start;
898 else
899 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300900 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800901 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300902 printed += fprintf(fp, "%+d", off);
903 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800904 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300905 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
906 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800907 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300908
909 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800910}
911
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300912static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
913 struct thread *thread,
914 struct perf_event_attr *attr,
915 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800916{
917 struct branch_stack *br = sample->branch_stack;
918 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300919 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800920 struct perf_insn x;
921 u8 buffer[MAXBB];
922 unsigned off;
923 struct symbol *lastsym = NULL;
924
925 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300926 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800927 nr = br->nr;
928 if (max_blocks && nr > max_blocks + 1)
929 nr = max_blocks + 1;
930
931 x.thread = thread;
932 x.cpu = sample->cpu;
933
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300934 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -0800935
936 /* Handle first from jump, of which we don't know the entry. */
937 len = grab_bb(buffer, br->entries[nr-1].from,
938 br->entries[nr-1].from,
939 machine, thread, &x.is64bit, &x.cpumode, false);
940 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300941 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
942 x.cpumode, x.cpu, &lastsym, attr, fp);
943 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
944 &x, buffer, len, 0, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800945 }
946
947 /* Print all blocks */
948 for (i = nr - 2; i >= 0; i--) {
949 if (br->entries[i].from || br->entries[i].to)
950 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
951 br->entries[i].from,
952 br->entries[i].to);
953 start = br->entries[i + 1].to;
954 end = br->entries[i].from;
955
956 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
957 /* Patch up missing kernel transfers due to ring filters */
958 if (len == -ENXIO && i > 0) {
959 end = br->entries[--i].from;
960 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
961 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
962 }
963 if (len <= 0)
964 continue;
965
966 insn = 0;
967 for (off = 0;; off += ilen) {
968 uint64_t ip = start + off;
969
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300970 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800971 if (ip == end) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300972 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800973 break;
974 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300975 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
976 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -0800977 if (ilen == 0)
978 break;
979 insn++;
980 }
981 }
982 }
983
984 /*
985 * Hit the branch? In this case we are already done, and the target
986 * has not been executed yet.
987 */
988 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300989 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -0800990 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300991 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -0800992
993 /*
994 * Print final block upto sample
995 */
996 start = br->entries[0].to;
997 end = sample->ip;
998 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300999 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001000 if (len <= 0) {
1001 /* Print at least last IP if basic block did not work */
1002 len = grab_bb(buffer, sample->ip, sample->ip,
1003 machine, thread, &x.is64bit, &x.cpumode, false);
1004 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001005 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001006
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001007 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001008 dump_insn(&x, sample->ip, buffer, len, NULL));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001009 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001010 }
1011 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001012 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1013 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001014 if (ilen == 0)
1015 break;
1016 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001017out:
1018 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001019}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001020
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001021static int perf_sample__fprintf_addr(struct perf_sample *sample,
1022 struct thread *thread,
1023 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001024{
1025 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001026 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001027
1028 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001029 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001030
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001031 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001032
1033 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001034 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001035 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001036 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001037 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001038 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001039 }
1040
1041 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001042 printed += fprintf(fp, " (");
1043 printed += map__fprintf_dsoname(al.map, fp);
1044 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001045 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001046out:
1047 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001048}
1049
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001050static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1051 struct perf_evsel *evsel,
1052 struct thread *thread,
1053 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001054{
1055 struct perf_event_attr *attr = &evsel->attr;
1056 size_t depth = thread_stack__depth(thread);
1057 struct addr_location addr_al;
1058 const char *name = NULL;
1059 static int spacing;
1060 int len = 0;
1061 u64 ip = 0;
1062
1063 /*
1064 * The 'return' has already been popped off the stack so the depth has
1065 * to be adjusted to match the 'call'.
1066 */
1067 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1068 depth += 1;
1069
1070 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1071 if (sample_addr_correlates_sym(attr)) {
1072 thread__resolve(thread, &addr_al, sample);
1073 if (addr_al.sym)
1074 name = addr_al.sym->name;
1075 else
1076 ip = sample->addr;
1077 } else {
1078 ip = sample->addr;
1079 }
1080 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1081 if (al->sym)
1082 name = al->sym->name;
1083 else
1084 ip = sample->ip;
1085 }
1086
1087 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001088 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001089 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001090 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001091
1092 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001093 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001094
1095 /*
1096 * Try to keep the output length from changing frequently so that the
1097 * output lines up more nicely.
1098 */
1099 if (len > spacing || (len && len < spacing - 52))
1100 spacing = round_up(len + 4, 32);
1101
1102 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001103 len += fprintf(fp, "%*s", spacing - len, "");
1104
1105 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001106}
1107
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001108static int perf_sample__fprintf_insn(struct perf_sample *sample,
1109 struct perf_event_attr *attr,
1110 struct thread *thread,
1111 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001112{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001113 int printed = 0;
1114
Andi Kleen224e2c92016-10-07 16:42:27 +03001115 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001116 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen224e2c92016-10-07 16:42:27 +03001117 if (PRINT_FIELD(INSN)) {
1118 int i;
1119
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001120 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001121 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001122 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001123 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001124 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001125 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1126
1127 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001128}
1129
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001130static int perf_sample__fprintf_bts(struct perf_sample *sample,
1131 struct perf_evsel *evsel,
1132 struct thread *thread,
1133 struct addr_location *al,
1134 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001135{
1136 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001137 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001138 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001139 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001140
Adrian Huntere2167082016-06-23 16:40:58 +03001141 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001142 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001143
Akihiro Nagai95582592012-01-30 13:43:09 +09001144 /* print branch_from information */
1145 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001146 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001147 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001148
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001149 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001150 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001151 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001152 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001153
1154 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001155 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001156 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001157 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001158 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001159 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001160 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001161 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001162
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001163 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001164 }
1165
Akihiro Nagai95582592012-01-30 13:43:09 +09001166 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001167 if (PRINT_FIELD(ADDR) ||
1168 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001169 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001170 printed += fprintf(fp, " => ");
1171 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001172 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001173
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001174 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001175 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001176
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001177 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1178 return printed + fprintf(fp, "\n");
Akihiro Nagai95582592012-01-30 13:43:09 +09001179}
1180
Adrian Hunter055cd332016-06-23 16:40:56 +03001181static struct {
1182 u32 flags;
1183 const char *name;
1184} sample_flags[] = {
1185 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1186 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1187 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1188 {PERF_IP_FLAG_BRANCH, "jmp"},
1189 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1190 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1191 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1192 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1193 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1194 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1195 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1196 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1197 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1198 {0, NULL}
1199};
1200
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001201static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001202{
1203 const char *chars = PERF_IP_FLAG_CHARS;
1204 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001205 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1206 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001207 char str[33];
1208 int i, pos = 0;
1209
Adrian Hunter055cd332016-06-23 16:40:56 +03001210 for (i = 0; sample_flags[i].name ; i++) {
1211 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1212 name = sample_flags[i].name;
1213 break;
1214 }
1215 }
1216
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001217 for (i = 0; i < n; i++, flags >>= 1) {
1218 if (flags & 1)
1219 str[pos++] = chars[i];
1220 }
1221 for (; i < 32; i++, flags >>= 1) {
1222 if (flags & 1)
1223 str[pos++] = '?';
1224 }
1225 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001226
1227 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001228 return fprintf(fp, " %-7s%4s ", name, in_tx ? "(x)" : "");
1229
1230 return fprintf(fp, " %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001231}
1232
Wang Nan30372f02016-02-24 11:20:45 +00001233struct printer_data {
1234 int line_no;
1235 bool hit_nul;
1236 bool is_printable;
1237};
1238
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001239static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1240 unsigned int val,
1241 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001242{
1243 unsigned char ch = (unsigned char)val;
1244 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001245 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001246
1247 switch (op) {
1248 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001249 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001250 break;
1251 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001252 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001253 " ");
1254 break;
1255 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001256 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001257 break;
1258 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001259 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001260 break;
1261 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001262 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001263 break;
1264 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001265 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001266 break;
1267 case BINARY_PRINT_CHAR_DATA:
1268 if (printer_data->hit_nul && ch)
1269 printer_data->is_printable = false;
1270
1271 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001272 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001273
1274 if (!printer_data->is_printable)
1275 break;
1276
1277 if (ch == '\0')
1278 printer_data->hit_nul = true;
1279 else
1280 printer_data->is_printable = false;
1281 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001282 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001283 }
1284 break;
1285 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001286 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001287 break;
1288 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001289 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001290 printer_data->line_no++;
1291 break;
1292 case BINARY_PRINT_DATA_END:
1293 default:
1294 break;
1295 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001296
1297 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001298}
1299
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001300static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001301{
1302 unsigned int nr_bytes = sample->raw_size;
1303 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001304 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1305 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001306
1307 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001308 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1309
1310 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001311}
1312
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001313static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001314{
1315 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001316 return fprintf(fp, "%*s", spacing - len, "");
1317
1318 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001319}
1320
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001321static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001322{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001323 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001324}
1325
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001326static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001327{
1328 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1329 int len;
1330
1331 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001332 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001333
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001334 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001335 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001336 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001337}
1338
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001339static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001340{
1341 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1342 int len;
1343
1344 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001345 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001346
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001347 len = fprintf(fp, " hints: %#x extensions: %#x ",
1348 data->hints, data->extensions);
1349 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001350}
1351
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001352static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001353{
1354 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1355 int len;
1356
1357 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001358 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001359
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001360 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1361 data->hw, data->cstate, data->subcstate);
1362 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001363}
1364
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001365static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001366{
1367 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1368 int len;
1369
1370 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001371 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001372
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001373 len = fprintf(fp, " IP: %u ", data->ip);
1374 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001375}
1376
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001377static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001378{
1379 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1380 int len;
1381
1382 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001383 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001384
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001385 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001386 data->deepest_cstate, data->last_cstate,
1387 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001388 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001389}
1390
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001391static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001392{
1393 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1394 unsigned int percent, freq;
1395 int len;
1396
1397 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001398 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001399
1400 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001401 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001402 if (data->max_nonturbo) {
1403 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001404 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001405 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001406 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001407}
1408
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001409static int perf_sample__fprintf_synth(struct perf_sample *sample,
1410 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001411{
1412 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001413 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001414 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001415 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001416 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001417 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001418 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001419 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001420 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001421 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001422 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001423 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001424 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001425 default:
1426 break;
1427 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001428
1429 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001430}
1431
Jiri Olsa809e9422015-11-26 18:55:21 +01001432struct perf_script {
1433 struct perf_tool tool;
1434 struct perf_session *session;
1435 bool show_task_events;
1436 bool show_mmap_events;
1437 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301438 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001439 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001440 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001441 struct cpu_map *cpus;
1442 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001443 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001444 const char *time_str;
1445 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001446};
1447
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001448static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1449{
1450 struct perf_evsel *evsel;
1451 int max = 0;
1452
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001453 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001454 int len = strlen(perf_evsel__name(evsel));
1455
1456 max = MAX(len, max);
1457 }
1458
1459 return max;
1460}
1461
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001462static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001463{
1464 struct mem_info mi = { .data_src.val = data_src };
1465 char decode[100];
1466 char out[100];
1467 static int maxlen;
1468 int len;
1469
1470 perf_script__meminfo_scnprintf(decode, 100, &mi);
1471
1472 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1473 if (maxlen < len)
1474 maxlen = len;
1475
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001476 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001477}
1478
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001479static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001480 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001481 struct addr_location *al,
1482 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001483{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001484 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001485 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001486 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001487 struct perf_evsel_script *es = evsel->priv;
1488 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001489
Adrian Hunter14057202017-06-21 13:17:19 +03001490 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001491 return;
1492
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001493 ++es->samples;
1494
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001495 perf_sample__fprintf_start(sample, thread, evsel, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001496
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001497 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001498 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001499
Namhyung Kime944d3d2013-11-18 14:34:52 +09001500 if (PRINT_FIELD(EVNAME)) {
1501 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001502
1503 if (!script->name_width)
1504 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1505
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001506 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001507 }
1508
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001509 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001510 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001511
Akihiro Nagai95582592012-01-30 13:43:09 +09001512 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001513 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001514 return;
1515 }
1516
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001517 if (PRINT_FIELD(TRACE)) {
1518 event_format__fprintf(evsel->tp_format, sample->cpu,
1519 sample->raw_data, sample->raw_size, fp);
1520 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001521
1522 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001523 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001524
David Ahern7cec0922011-05-30 13:08:23 -06001525 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001526 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001527
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001528 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001529 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001530
1531 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001532 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001533
David Ahern787bef12011-05-27 14:28:43 -06001534 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001535 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001536
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001537 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001538 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001539 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001540 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001541
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001542 fputc(cursor ? '\n' : ' ', fp);
1543 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001544 }
1545
Stephane Eranianfc36f942015-08-31 18:41:10 +02001546 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001547 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001548
Andi Kleenb1491ac2017-09-05 11:40:57 -07001549 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001550 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001551
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001552 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001553 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001554 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001555 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001556 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001557 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001558
Wang Nan30372f02016-02-24 11:20:45 +00001559 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001560 perf_sample__fprintf_bpf_output(sample, fp);
1561 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001562
1563 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001564 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1565 fprintf(fp, "\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001566}
1567
Tom Zanussi956ffd02009-11-25 01:15:46 -06001568static struct scripting_ops *scripting_ops;
1569
Jiri Olsa36e33c52016-01-06 11:49:56 +01001570static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1571{
1572 int nthreads = thread_map__nr(counter->threads);
1573 int ncpus = perf_evsel__nr_cpus(counter);
1574 int cpu, thread;
1575 static int header_printed;
1576
1577 if (counter->system_wide)
1578 nthreads = 1;
1579
1580 if (!header_printed) {
1581 printf("%3s %8s %15s %15s %15s %15s %s\n",
1582 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1583 header_printed = 1;
1584 }
1585
1586 for (thread = 0; thread < nthreads; thread++) {
1587 for (cpu = 0; cpu < ncpus; cpu++) {
1588 struct perf_counts_values *counts;
1589
1590 counts = perf_counts(counter->counts, cpu, thread);
1591
1592 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1593 counter->cpus->map[cpu],
1594 thread_map__pid(counter->threads, thread),
1595 counts->val,
1596 counts->ena,
1597 counts->run,
1598 tstamp,
1599 perf_evsel__name(counter));
1600 }
1601 }
1602}
1603
Jiri Olsae099eba2016-01-05 22:09:09 +01001604static void process_stat(struct perf_evsel *counter, u64 tstamp)
1605{
1606 if (scripting_ops && scripting_ops->process_stat)
1607 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001608 else
1609 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001610}
1611
1612static void process_stat_interval(u64 tstamp)
1613{
1614 if (scripting_ops && scripting_ops->process_stat_interval)
1615 scripting_ops->process_stat_interval(tstamp);
1616}
1617
Tom Zanussi956ffd02009-11-25 01:15:46 -06001618static void setup_scripting(void)
1619{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001620 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001621 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001622}
1623
Adrian Hunterd445dd22014-08-15 22:08:37 +03001624static int flush_scripting(void)
1625{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001626 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001627}
1628
Tom Zanussi956ffd02009-11-25 01:15:46 -06001629static int cleanup_scripting(void)
1630{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001631 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001632
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001633 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001634}
1635
Jiri Olsa809e9422015-11-26 18:55:21 +01001636static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001637 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001638 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001639 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001640 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001641{
Jiri Olsa809e9422015-11-26 18:55:21 +01001642 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001643 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001644
David Aherna91f4c42016-11-29 10:15:43 -07001645 if (perf_time__skip_sample(&scr->ptime, sample->time))
1646 return 0;
1647
David Ahern1424dc92011-03-09 22:23:28 -07001648 if (debug_mode) {
1649 if (sample->time < last_timestamp) {
1650 pr_err("Samples misordered, previous: %" PRIu64
1651 " this: %" PRIu64 "\n", last_timestamp,
1652 sample->time);
1653 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001654 }
David Ahern1424dc92011-03-09 22:23:28 -07001655 last_timestamp = sample->time;
1656 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001657 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001658
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001659 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001660 pr_err("problem processing %d event, skipping it.\n",
1661 event->header.type);
1662 return -1;
1663 }
1664
1665 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001666 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001667
Anton Blanchard5d67be92011-07-04 21:57:50 +10001668 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001669 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001670
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001671 if (scripting_ops)
1672 scripting_ops->process_event(event, sample, evsel, &al);
1673 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001674 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001675
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001676out_put:
1677 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001678 return 0;
1679}
1680
Adrian Hunter7ea95722013-11-01 15:51:30 +02001681static int process_attr(struct perf_tool *tool, union perf_event *event,
1682 struct perf_evlist **pevlist)
1683{
1684 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1685 struct perf_evlist *evlist;
1686 struct perf_evsel *evsel, *pos;
1687 int err;
1688
1689 err = perf_event__process_attr(tool, event, pevlist);
1690 if (err)
1691 return err;
1692
1693 evlist = *pevlist;
1694 evsel = perf_evlist__last(*pevlist);
1695
Adrian Hunter14057202017-06-21 13:17:19 +03001696 if (evsel->attr.type >= PERF_TYPE_MAX &&
1697 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001698 return 0;
1699
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001700 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001701 if (pos->attr.type == evsel->attr.type && pos != evsel)
1702 return 0;
1703 }
1704
1705 set_print_ip_opts(&evsel->attr);
1706
Jiri Olsad2b5a312015-10-16 12:41:25 +02001707 if (evsel->attr.sample_type)
1708 err = perf_evsel__check_attr(evsel, scr->session);
1709
1710 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001711}
1712
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001713static int process_comm_event(struct perf_tool *tool,
1714 union perf_event *event,
1715 struct perf_sample *sample,
1716 struct machine *machine)
1717{
1718 struct thread *thread;
1719 struct perf_script *script = container_of(tool, struct perf_script, tool);
1720 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001721 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001722 int ret = -1;
1723
1724 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1725 if (thread == NULL) {
1726 pr_debug("problem processing COMM event, skipping it.\n");
1727 return -1;
1728 }
1729
1730 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1731 goto out;
1732
1733 if (!evsel->attr.sample_id_all) {
1734 sample->cpu = 0;
1735 sample->time = 0;
1736 sample->tid = event->comm.tid;
1737 sample->pid = event->comm.pid;
1738 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001739 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001740 perf_event__fprintf(event, stdout);
1741 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001742out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001743 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001744 return ret;
1745}
1746
Hari Bathini96a44bb2017-03-08 02:12:06 +05301747static int process_namespaces_event(struct perf_tool *tool,
1748 union perf_event *event,
1749 struct perf_sample *sample,
1750 struct machine *machine)
1751{
1752 struct thread *thread;
1753 struct perf_script *script = container_of(tool, struct perf_script, tool);
1754 struct perf_session *session = script->session;
1755 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1756 int ret = -1;
1757
1758 thread = machine__findnew_thread(machine, event->namespaces.pid,
1759 event->namespaces.tid);
1760 if (thread == NULL) {
1761 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1762 return -1;
1763 }
1764
1765 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1766 goto out;
1767
1768 if (!evsel->attr.sample_id_all) {
1769 sample->cpu = 0;
1770 sample->time = 0;
1771 sample->tid = event->namespaces.tid;
1772 sample->pid = event->namespaces.pid;
1773 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001774 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Hari Bathini96a44bb2017-03-08 02:12:06 +05301775 perf_event__fprintf(event, stdout);
1776 ret = 0;
1777out:
1778 thread__put(thread);
1779 return ret;
1780}
1781
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001782static int process_fork_event(struct perf_tool *tool,
1783 union perf_event *event,
1784 struct perf_sample *sample,
1785 struct machine *machine)
1786{
1787 struct thread *thread;
1788 struct perf_script *script = container_of(tool, struct perf_script, tool);
1789 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001790 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001791
1792 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1793 return -1;
1794
1795 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1796 if (thread == NULL) {
1797 pr_debug("problem processing FORK event, skipping it.\n");
1798 return -1;
1799 }
1800
1801 if (!evsel->attr.sample_id_all) {
1802 sample->cpu = 0;
1803 sample->time = event->fork.time;
1804 sample->tid = event->fork.tid;
1805 sample->pid = event->fork.pid;
1806 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001807 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001808 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001809 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001810
1811 return 0;
1812}
1813static int process_exit_event(struct perf_tool *tool,
1814 union perf_event *event,
1815 struct perf_sample *sample,
1816 struct machine *machine)
1817{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001818 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001819 struct thread *thread;
1820 struct perf_script *script = container_of(tool, struct perf_script, tool);
1821 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001822 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001823
1824 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1825 if (thread == NULL) {
1826 pr_debug("problem processing EXIT event, skipping it.\n");
1827 return -1;
1828 }
1829
1830 if (!evsel->attr.sample_id_all) {
1831 sample->cpu = 0;
1832 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001833 sample->tid = event->fork.tid;
1834 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001835 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001836 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001837 perf_event__fprintf(event, stdout);
1838
1839 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001840 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001841
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001842 thread__put(thread);
1843 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001844}
1845
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001846static int process_mmap_event(struct perf_tool *tool,
1847 union perf_event *event,
1848 struct perf_sample *sample,
1849 struct machine *machine)
1850{
1851 struct thread *thread;
1852 struct perf_script *script = container_of(tool, struct perf_script, tool);
1853 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001854 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001855
1856 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1857 return -1;
1858
1859 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1860 if (thread == NULL) {
1861 pr_debug("problem processing MMAP event, skipping it.\n");
1862 return -1;
1863 }
1864
1865 if (!evsel->attr.sample_id_all) {
1866 sample->cpu = 0;
1867 sample->time = 0;
1868 sample->tid = event->mmap.tid;
1869 sample->pid = event->mmap.pid;
1870 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001871 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001872 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001873 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001874 return 0;
1875}
1876
1877static int process_mmap2_event(struct perf_tool *tool,
1878 union perf_event *event,
1879 struct perf_sample *sample,
1880 struct machine *machine)
1881{
1882 struct thread *thread;
1883 struct perf_script *script = container_of(tool, struct perf_script, tool);
1884 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001885 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001886
1887 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1888 return -1;
1889
1890 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1891 if (thread == NULL) {
1892 pr_debug("problem processing MMAP2 event, skipping it.\n");
1893 return -1;
1894 }
1895
1896 if (!evsel->attr.sample_id_all) {
1897 sample->cpu = 0;
1898 sample->time = 0;
1899 sample->tid = event->mmap2.tid;
1900 sample->pid = event->mmap2.pid;
1901 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001902 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001903 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001904 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001905 return 0;
1906}
1907
Adrian Hunter7c148982015-07-21 12:44:06 +03001908static int process_switch_event(struct perf_tool *tool,
1909 union perf_event *event,
1910 struct perf_sample *sample,
1911 struct machine *machine)
1912{
1913 struct thread *thread;
1914 struct perf_script *script = container_of(tool, struct perf_script, tool);
1915 struct perf_session *session = script->session;
1916 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1917
1918 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1919 return -1;
1920
1921 thread = machine__findnew_thread(machine, sample->pid,
1922 sample->tid);
1923 if (thread == NULL) {
1924 pr_debug("problem processing SWITCH event, skipping it.\n");
1925 return -1;
1926 }
1927
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001928 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Adrian Hunter7c148982015-07-21 12:44:06 +03001929 perf_event__fprintf(event, stdout);
1930 thread__put(thread);
1931 return 0;
1932}
1933
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001934static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001935{
1936 session_done = 1;
1937}
1938
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001939static void perf_script__fclose_per_event_dump(struct perf_script *script)
1940{
1941 struct perf_evlist *evlist = script->session->evlist;
1942 struct perf_evsel *evsel;
1943
1944 evlist__for_each_entry(evlist, evsel) {
1945 if (!evsel->priv)
1946 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001947 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001948 evsel->priv = NULL;
1949 }
1950}
1951
1952static int perf_script__fopen_per_event_dump(struct perf_script *script)
1953{
1954 struct perf_evsel *evsel;
1955
1956 evlist__for_each_entry(script->session->evlist, evsel) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001957 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001958 if (evsel->priv == NULL)
1959 goto out_err_fclose;
1960 }
1961
1962 return 0;
1963
1964out_err_fclose:
1965 perf_script__fclose_per_event_dump(script);
1966 return -1;
1967}
1968
1969static int perf_script__setup_per_event_dump(struct perf_script *script)
1970{
1971 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001972 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001973
1974 if (script->per_event_dump)
1975 return perf_script__fopen_per_event_dump(script);
1976
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001977 es_stdout.fp = stdout;
1978
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001979 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001980 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001981
1982 return 0;
1983}
1984
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001985static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
1986{
1987 struct perf_evsel *evsel;
1988
1989 evlist__for_each_entry(script->session->evlist, evsel) {
1990 struct perf_evsel_script *es = evsel->priv;
1991
1992 perf_evsel_script__fprintf(es, stdout);
1993 perf_evsel_script__delete(es);
1994 evsel->priv = NULL;
1995 }
1996}
1997
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001998static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001999{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002000 int ret;
2001
Tom Zanussic239da32010-04-01 23:59:18 -05002002 signal(SIGINT, sig_handler);
2003
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002004 /* override event processing functions */
2005 if (script->show_task_events) {
2006 script->tool.comm = process_comm_event;
2007 script->tool.fork = process_fork_event;
2008 script->tool.exit = process_exit_event;
2009 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002010 if (script->show_mmap_events) {
2011 script->tool.mmap = process_mmap_event;
2012 script->tool.mmap2 = process_mmap2_event;
2013 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002014 if (script->show_switch_events)
2015 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302016 if (script->show_namespace_events)
2017 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002018
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002019 if (perf_script__setup_per_event_dump(script)) {
2020 pr_err("Couldn't create the per event dump files\n");
2021 return -1;
2022 }
2023
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002024 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002025
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002026 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002027 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002028
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002029 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002030 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002031
2032 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002033}
2034
Tom Zanussi956ffd02009-11-25 01:15:46 -06002035struct script_spec {
2036 struct list_head node;
2037 struct scripting_ops *ops;
2038 char spec[0];
2039};
2040
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002041static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002042
2043static struct script_spec *script_spec__new(const char *spec,
2044 struct scripting_ops *ops)
2045{
2046 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2047
2048 if (s != NULL) {
2049 strcpy(s->spec, spec);
2050 s->ops = ops;
2051 }
2052
2053 return s;
2054}
2055
Tom Zanussi956ffd02009-11-25 01:15:46 -06002056static void script_spec__add(struct script_spec *s)
2057{
2058 list_add_tail(&s->node, &script_specs);
2059}
2060
2061static struct script_spec *script_spec__find(const char *spec)
2062{
2063 struct script_spec *s;
2064
2065 list_for_each_entry(s, &script_specs, node)
2066 if (strcasecmp(s->spec, spec) == 0)
2067 return s;
2068 return NULL;
2069}
2070
Tom Zanussi956ffd02009-11-25 01:15:46 -06002071int script_spec_register(const char *spec, struct scripting_ops *ops)
2072{
2073 struct script_spec *s;
2074
2075 s = script_spec__find(spec);
2076 if (s)
2077 return -1;
2078
Taeung Song8560bae2016-02-26 00:13:10 +09002079 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002080 if (!s)
2081 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002082 else
2083 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002084
2085 return 0;
2086}
2087
2088static struct scripting_ops *script_spec__lookup(const char *spec)
2089{
2090 struct script_spec *s = script_spec__find(spec);
2091 if (!s)
2092 return NULL;
2093
2094 return s->ops;
2095}
2096
2097static void list_available_languages(void)
2098{
2099 struct script_spec *s;
2100
2101 fprintf(stderr, "\n");
2102 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002103 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002104
2105 list_for_each_entry(s, &script_specs, node)
2106 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2107
2108 fprintf(stderr, "\n");
2109}
2110
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002111static int parse_scriptname(const struct option *opt __maybe_unused,
2112 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002113{
2114 char spec[PATH_MAX];
2115 const char *script, *ext;
2116 int len;
2117
Tom Zanussif526d682010-01-27 02:27:52 -06002118 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002119 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002120 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002121 }
2122
2123 script = strchr(str, ':');
2124 if (script) {
2125 len = script - str;
2126 if (len >= PATH_MAX) {
2127 fprintf(stderr, "invalid language specifier");
2128 return -1;
2129 }
2130 strncpy(spec, str, len);
2131 spec[len] = '\0';
2132 scripting_ops = script_spec__lookup(spec);
2133 if (!scripting_ops) {
2134 fprintf(stderr, "invalid language specifier");
2135 return -1;
2136 }
2137 script++;
2138 } else {
2139 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002140 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002141 if (!ext) {
2142 fprintf(stderr, "invalid script extension");
2143 return -1;
2144 }
2145 scripting_ops = script_spec__lookup(++ext);
2146 if (!scripting_ops) {
2147 fprintf(stderr, "invalid script extension");
2148 return -1;
2149 }
2150 }
2151
2152 script_name = strdup(script);
2153
2154 return 0;
2155}
2156
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002157static int parse_output_fields(const struct option *opt __maybe_unused,
2158 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002159{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002160 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002161 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002162 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002163 int rc = 0;
2164 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002165 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002166 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002167
2168 if (!str)
2169 return -ENOMEM;
2170
David Ahern2c9e45f72011-03-17 10:03:21 -06002171 /* first word can state for which event type the user is specifying
2172 * the fields. If no type exists, the specified fields apply to all
2173 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002174 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002175 tok = strchr(str, ':');
2176 if (tok) {
2177 *tok = '\0';
2178 tok++;
2179 if (!strcmp(str, "hw"))
2180 type = PERF_TYPE_HARDWARE;
2181 else if (!strcmp(str, "sw"))
2182 type = PERF_TYPE_SOFTWARE;
2183 else if (!strcmp(str, "trace"))
2184 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002185 else if (!strcmp(str, "raw"))
2186 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002187 else if (!strcmp(str, "break"))
2188 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002189 else if (!strcmp(str, "synth"))
2190 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002191 else {
2192 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002193 rc = -EINVAL;
2194 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002195 }
2196
2197 if (output[type].user_set)
2198 pr_warning("Overriding previous field request for %s events.\n",
2199 event_type(type));
2200
2201 output[type].fields = 0;
2202 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002203 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002204
2205 } else {
2206 tok = str;
2207 if (strlen(str) == 0) {
2208 fprintf(stderr,
2209 "Cannot set fields to 'none' for all event types.\n");
2210 rc = -EINVAL;
2211 goto out;
2212 }
2213
Andi Kleen36ce5652017-06-02 08:48:10 -07002214 /* Don't override defaults for +- */
2215 if (strchr(str, '+') || strchr(str, '-'))
2216 goto parse;
2217
David Ahern2c9e45f72011-03-17 10:03:21 -06002218 if (output_set_by_user())
2219 pr_warning("Overriding previous field request for all events.\n");
2220
Adrian Hunter14057202017-06-21 13:17:19 +03002221 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002222 output[j].fields = 0;
2223 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002224 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002225 }
David Ahern1424dc92011-03-09 22:23:28 -07002226 }
2227
Andi Kleen36ce5652017-06-02 08:48:10 -07002228parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002229 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002230 if (*tok == '+') {
2231 if (change == SET)
2232 goto out_badmix;
2233 change = ADD;
2234 tok++;
2235 } else if (*tok == '-') {
2236 if (change == SET)
2237 goto out_badmix;
2238 change = REMOVE;
2239 tok++;
2240 } else {
2241 if (change != SET && change != DEFAULT)
2242 goto out_badmix;
2243 change = SET;
2244 }
2245
David Ahern745f43e2011-03-09 22:23:26 -07002246 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002247 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002248 break;
David Ahern745f43e2011-03-09 22:23:26 -07002249 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002250 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002251 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002252 continue;
2253 }
David Ahern745f43e2011-03-09 22:23:26 -07002254 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002255 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002256 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002257 goto out;
2258 }
2259
2260 if (type == -1) {
2261 /* add user option to all events types for
2262 * which it is valid
2263 */
Adrian Hunter14057202017-06-21 13:17:19 +03002264 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002265 if (output[j].invalid_fields & all_output_options[i].field) {
2266 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2267 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002268 } else {
2269 if (change == REMOVE)
2270 output[j].fields &= ~all_output_options[i].field;
2271 else
2272 output[j].fields |= all_output_options[i].field;
2273 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002274 }
2275 } else {
2276 if (output[type].invalid_fields & all_output_options[i].field) {
2277 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2278 all_output_options[i].str, event_type(type));
2279
2280 rc = -EINVAL;
2281 goto out;
2282 }
2283 output[type].fields |= all_output_options[i].field;
2284 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002285 }
2286
2287 if (type >= 0) {
2288 if (output[type].fields == 0) {
2289 pr_debug("No fields requested for %s type. "
2290 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002291 }
David Ahern1424dc92011-03-09 22:23:28 -07002292 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002293 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002294
Andi Kleen36ce5652017-06-02 08:48:10 -07002295out_badmix:
2296 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2297 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002298out:
David Ahern745f43e2011-03-09 22:23:26 -07002299 free(str);
2300 return rc;
2301}
2302
Shawn Bohrer008f29d2010-11-21 10:09:39 -06002303/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
2304static int is_directory(const char *base_path, const struct dirent *dent)
2305{
2306 char path[PATH_MAX];
2307 struct stat st;
2308
2309 sprintf(path, "%s/%s", base_path, dent->d_name);
2310 if (stat(path, &st))
2311 return 0;
2312
2313 return S_ISDIR(st.st_mode);
2314}
2315
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002316#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2317 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2318 if ((lang_dirent->d_type == DT_DIR || \
2319 (lang_dirent->d_type == DT_UNKNOWN && \
2320 is_directory(scripts_path, lang_dirent))) && \
2321 (strcmp(lang_dirent->d_name, ".")) && \
2322 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002323
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002324#define for_each_script(lang_path, lang_dir, script_dirent) \
2325 while ((script_dirent = readdir(lang_dir)) != NULL) \
2326 if (script_dirent->d_type != DT_DIR && \
2327 (script_dirent->d_type != DT_UNKNOWN || \
2328 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002329
2330
2331#define RECORD_SUFFIX "-record"
2332#define REPORT_SUFFIX "-report"
2333
2334struct script_desc {
2335 struct list_head node;
2336 char *name;
2337 char *half_liner;
2338 char *args;
2339};
2340
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002341static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002342
2343static struct script_desc *script_desc__new(const char *name)
2344{
2345 struct script_desc *s = zalloc(sizeof(*s));
2346
Tom Zanussib5b87312010-11-10 08:16:51 -06002347 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002348 s->name = strdup(name);
2349
2350 return s;
2351}
2352
2353static void script_desc__delete(struct script_desc *s)
2354{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002355 zfree(&s->name);
2356 zfree(&s->half_liner);
2357 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002358 free(s);
2359}
2360
2361static void script_desc__add(struct script_desc *s)
2362{
2363 list_add_tail(&s->node, &script_descs);
2364}
2365
2366static struct script_desc *script_desc__find(const char *name)
2367{
2368 struct script_desc *s;
2369
2370 list_for_each_entry(s, &script_descs, node)
2371 if (strcasecmp(s->name, name) == 0)
2372 return s;
2373 return NULL;
2374}
2375
2376static struct script_desc *script_desc__findnew(const char *name)
2377{
2378 struct script_desc *s = script_desc__find(name);
2379
2380 if (s)
2381 return s;
2382
2383 s = script_desc__new(name);
2384 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002385 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002386
2387 script_desc__add(s);
2388
2389 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002390}
2391
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002392static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002393{
2394 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002395 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002396
2397 if (strlen(str) > suffix_len) {
2398 p = str + strlen(str) - suffix_len;
2399 if (!strncmp(p, suffix, suffix_len))
2400 return p;
2401 }
2402
2403 return NULL;
2404}
2405
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002406static int read_script_info(struct script_desc *desc, const char *filename)
2407{
2408 char line[BUFSIZ], *p;
2409 FILE *fp;
2410
2411 fp = fopen(filename, "r");
2412 if (!fp)
2413 return -1;
2414
2415 while (fgets(line, sizeof(line), fp)) {
2416 p = ltrim(line);
2417 if (strlen(p) == 0)
2418 continue;
2419 if (*p != '#')
2420 continue;
2421 p++;
2422 if (strlen(p) && *p == '!')
2423 continue;
2424
2425 p = ltrim(p);
2426 if (strlen(p) && p[strlen(p) - 1] == '\n')
2427 p[strlen(p) - 1] = '\0';
2428
2429 if (!strncmp(p, "description:", strlen("description:"))) {
2430 p += strlen("description:");
2431 desc->half_liner = strdup(ltrim(p));
2432 continue;
2433 }
2434
2435 if (!strncmp(p, "args:", strlen("args:"))) {
2436 p += strlen("args:");
2437 desc->args = strdup(ltrim(p));
2438 continue;
2439 }
2440 }
2441
2442 fclose(fp);
2443
2444 return 0;
2445}
2446
Robert Richter38efb532011-11-25 11:38:40 +01002447static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2448{
2449 char *script_root, *str;
2450
2451 script_root = strdup(script_dirent->d_name);
2452 if (!script_root)
2453 return NULL;
2454
2455 str = (char *)ends_with(script_root, suffix);
2456 if (!str) {
2457 free(script_root);
2458 return NULL;
2459 }
2460
2461 *str = '\0';
2462 return script_root;
2463}
2464
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002465static int list_available_scripts(const struct option *opt __maybe_unused,
2466 const char *s __maybe_unused,
2467 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002468{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002469 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002470 char scripts_path[MAXPATHLEN];
2471 DIR *scripts_dir, *lang_dir;
2472 char script_path[MAXPATHLEN];
2473 char lang_path[MAXPATHLEN];
2474 struct script_desc *desc;
2475 char first_half[BUFSIZ];
2476 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002477
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002478 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002479
2480 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002481 if (!scripts_dir) {
2482 fprintf(stdout,
2483 "open(%s) failed.\n"
2484 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2485 scripts_path);
2486 exit(-1);
2487 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002488
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002489 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002490 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002491 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002492 lang_dir = opendir(lang_path);
2493 if (!lang_dir)
2494 continue;
2495
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002496 for_each_script(lang_path, lang_dir, script_dirent) {
2497 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002498 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002499 desc = script_desc__findnew(script_root);
2500 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002501 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002502 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002503 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002504 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002505 }
2506 }
2507
2508 fprintf(stdout, "List of available trace scripts:\n");
2509 list_for_each_entry(desc, &script_descs, node) {
2510 sprintf(first_half, "%s %s", desc->name,
2511 desc->args ? desc->args : "");
2512 fprintf(stdout, " %-36s %s\n", first_half,
2513 desc->half_liner ? desc->half_liner : "");
2514 }
2515
2516 exit(0);
2517}
2518
Feng Tange5f37052012-09-07 16:42:26 +08002519/*
Feng Tang49e639e2012-10-30 11:56:03 +08002520 * Some scripts specify the required events in their "xxx-record" file,
2521 * this function will check if the events in perf.data match those
2522 * mentioned in the "xxx-record".
2523 *
2524 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2525 * which is covered well now. And new parsing code should be added to
2526 * cover the future complexing formats like event groups etc.
2527 */
2528static int check_ev_match(char *dir_name, char *scriptname,
2529 struct perf_session *session)
2530{
2531 char filename[MAXPATHLEN], evname[128];
2532 char line[BUFSIZ], *p;
2533 struct perf_evsel *pos;
2534 int match, len;
2535 FILE *fp;
2536
2537 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2538
2539 fp = fopen(filename, "r");
2540 if (!fp)
2541 return -1;
2542
2543 while (fgets(line, sizeof(line), fp)) {
2544 p = ltrim(line);
2545 if (*p == '#')
2546 continue;
2547
2548 while (strlen(p)) {
2549 p = strstr(p, "-e");
2550 if (!p)
2551 break;
2552
2553 p += 2;
2554 p = ltrim(p);
2555 len = strcspn(p, " \t");
2556 if (!len)
2557 break;
2558
2559 snprintf(evname, len + 1, "%s", p);
2560
2561 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002562 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002563 if (!strcmp(perf_evsel__name(pos), evname)) {
2564 match = 1;
2565 break;
2566 }
2567 }
2568
2569 if (!match) {
2570 fclose(fp);
2571 return -1;
2572 }
2573 }
2574 }
2575
2576 fclose(fp);
2577 return 0;
2578}
2579
2580/*
Feng Tange5f37052012-09-07 16:42:26 +08002581 * Return -1 if none is found, otherwise the actual scripts number.
2582 *
2583 * Currently the only user of this function is the script browser, which
2584 * will list all statically runnable scripts, select one, execute it and
2585 * show the output in a perf browser.
2586 */
2587int find_scripts(char **scripts_array, char **scripts_path_array)
2588{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002589 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002590 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002591 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002592 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002593 struct perf_data data = {
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002594 .path = input_name,
2595 .mode = PERF_DATA_MODE_READ,
2596 };
Feng Tange5f37052012-09-07 16:42:26 +08002597 char *temp;
2598 int i = 0;
2599
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002600 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002601 if (!session)
2602 return -1;
2603
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002604 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002605
2606 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002607 if (!scripts_dir) {
2608 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002609 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002610 }
Feng Tange5f37052012-09-07 16:42:26 +08002611
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002612 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002613 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002614 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002615#ifdef NO_LIBPERL
2616 if (strstr(lang_path, "perl"))
2617 continue;
2618#endif
2619#ifdef NO_LIBPYTHON
2620 if (strstr(lang_path, "python"))
2621 continue;
2622#endif
2623
2624 lang_dir = opendir(lang_path);
2625 if (!lang_dir)
2626 continue;
2627
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002628 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002629 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002630 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002631 continue;
2632 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002633 script_dirent->d_name);
2634 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002635 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002636 (temp - script_dirent->d_name) + 1,
2637 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002638
2639 if (check_ev_match(lang_path,
2640 scripts_array[i], session))
2641 continue;
2642
Feng Tange5f37052012-09-07 16:42:26 +08002643 i++;
2644 }
Feng Tang49e639e2012-10-30 11:56:03 +08002645 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002646 }
2647
Feng Tang49e639e2012-10-30 11:56:03 +08002648 closedir(scripts_dir);
2649 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002650 return i;
2651}
2652
Tom Zanussi38752942009-12-15 02:53:39 -06002653static char *get_script_path(const char *script_root, const char *suffix)
2654{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002655 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002656 char scripts_path[MAXPATHLEN];
2657 char script_path[MAXPATHLEN];
2658 DIR *scripts_dir, *lang_dir;
2659 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002660 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002661
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002662 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002663
2664 scripts_dir = opendir(scripts_path);
2665 if (!scripts_dir)
2666 return NULL;
2667
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002668 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002669 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002670 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002671 lang_dir = opendir(lang_path);
2672 if (!lang_dir)
2673 continue;
2674
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002675 for_each_script(lang_path, lang_dir, script_dirent) {
2676 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002677 if (__script_root && !strcmp(script_root, __script_root)) {
2678 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002679 closedir(lang_dir);
2680 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002681 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002682 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002683 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002684 }
2685 free(__script_root);
2686 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002687 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002688 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002689 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002690
Robert Richter38efb532011-11-25 11:38:40 +01002691 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002692}
2693
Tom Zanussib5b87312010-11-10 08:16:51 -06002694static bool is_top_script(const char *script_path)
2695{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002696 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002697}
2698
2699static int has_required_arg(char *script_path)
2700{
2701 struct script_desc *desc;
2702 int n_args = 0;
2703 char *p;
2704
2705 desc = script_desc__new(NULL);
2706
2707 if (read_script_info(desc, script_path))
2708 goto out;
2709
2710 if (!desc->args)
2711 goto out;
2712
2713 for (p = desc->args; *p; p++)
2714 if (*p == '<')
2715 n_args++;
2716out:
2717 script_desc__delete(desc);
2718
2719 return n_args;
2720}
2721
David Ahernd54b1a92012-08-26 12:24:46 -06002722static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002723{
2724 char **__argv = malloc(sizeof(const char *) * argc);
2725
David Ahernd54b1a92012-08-26 12:24:46 -06002726 if (!__argv) {
2727 pr_err("malloc failed\n");
2728 return -1;
2729 }
2730
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002731 memcpy(__argv, argv, sizeof(const char *) * argc);
2732 argc = parse_options(argc, (const char **)__argv, record_options,
2733 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2734 free(__argv);
2735
David Ahernd54b1a92012-08-26 12:24:46 -06002736 system_wide = (argc == 0);
2737
2738 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002739}
2740
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002741static void script__setup_sample_type(struct perf_script *script)
2742{
2743 struct perf_session *session = script->session;
2744 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2745
2746 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2747 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2748 (sample_type & PERF_SAMPLE_STACK_USER))
2749 callchain_param.record_mode = CALLCHAIN_DWARF;
2750 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2751 callchain_param.record_mode = CALLCHAIN_LBR;
2752 else
2753 callchain_param.record_mode = CALLCHAIN_FP;
2754 }
2755}
2756
Jiri Olsae099eba2016-01-05 22:09:09 +01002757static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2758 union perf_event *event,
2759 struct perf_session *session)
2760{
2761 struct stat_round_event *round = &event->stat_round;
2762 struct perf_evsel *counter;
2763
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002764 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002765 perf_stat_process_counter(&stat_config, counter);
2766 process_stat(counter, round->time);
2767 }
2768
2769 process_stat_interval(round->time);
2770 return 0;
2771}
2772
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002773static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2774 union perf_event *event,
2775 struct perf_session *session __maybe_unused)
2776{
2777 perf_event__read_stat_config(&stat_config, &event->stat_config);
2778 return 0;
2779}
2780
Jiri Olsacfc88742016-01-05 22:09:06 +01002781static int set_maps(struct perf_script *script)
2782{
2783 struct perf_evlist *evlist = script->session->evlist;
2784
2785 if (!script->cpus || !script->threads)
2786 return 0;
2787
2788 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2789 return -EINVAL;
2790
2791 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2792
2793 if (perf_evlist__alloc_stats(evlist, true))
2794 return -ENOMEM;
2795
2796 script->allocated = true;
2797 return 0;
2798}
2799
2800static
2801int process_thread_map_event(struct perf_tool *tool,
2802 union perf_event *event,
2803 struct perf_session *session __maybe_unused)
2804{
2805 struct perf_script *script = container_of(tool, struct perf_script, tool);
2806
2807 if (script->threads) {
2808 pr_warning("Extra thread map event, ignoring.\n");
2809 return 0;
2810 }
2811
2812 script->threads = thread_map__new_event(&event->thread_map);
2813 if (!script->threads)
2814 return -ENOMEM;
2815
2816 return set_maps(script);
2817}
2818
2819static
2820int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2821 union perf_event *event,
2822 struct perf_session *session __maybe_unused)
2823{
2824 struct perf_script *script = container_of(tool, struct perf_script, tool);
2825
2826 if (script->cpus) {
2827 pr_warning("Extra cpu map event, ignoring.\n");
2828 return 0;
2829 }
2830
2831 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2832 if (!script->cpus)
2833 return -ENOMEM;
2834
2835 return set_maps(script);
2836}
2837
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002838int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002839{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002840 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002841 bool header = false;
2842 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002843 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002844 char *rec_script_path = NULL;
2845 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002846 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002847 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002848 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002849 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002850 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002851 struct perf_script script = {
2852 .tool = {
2853 .sample = process_sample_event,
2854 .mmap = perf_event__process_mmap,
2855 .mmap2 = perf_event__process_mmap2,
2856 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302857 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002858 .exit = perf_event__process_exit,
2859 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002860 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002861 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002862 .tracing_data = perf_event__process_tracing_data,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07002863 .feature = perf_event__process_feature,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002864 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002865 .id_index = perf_event__process_id_index,
2866 .auxtrace_info = perf_event__process_auxtrace_info,
2867 .auxtrace = perf_event__process_auxtrace,
2868 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002869 .stat = perf_event__process_stat_event,
2870 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002871 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002872 .thread_map = process_thread_map_event,
2873 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002874 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002875 .ordering_requires_timestamps = true,
2876 },
2877 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002878 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08002879 .mode = PERF_DATA_MODE_READ,
2880 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002881 const struct option options[] = {
2882 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2883 "dump raw trace in ASCII"),
2884 OPT_INCR('v', "verbose", &verbose,
2885 "be more verbose (show symbol address, etc)"),
2886 OPT_BOOLEAN('L', "Latency", &latency_format,
2887 "show latency attributes (irqs/preemption disabled, etc)"),
2888 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2889 list_available_scripts),
2890 OPT_CALLBACK('s', "script", NULL, "name",
2891 "script file name (lang:script name, script name, or *)",
2892 parse_scriptname),
2893 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2894 "generate perf-script.xx script in specified language"),
2895 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2896 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2897 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002898 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2899 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002900 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2901 "file", "vmlinux pathname"),
2902 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2903 "file", "kallsyms pathname"),
2904 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2905 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002906 OPT_CALLBACK(0, "symfs", NULL, "directory",
2907 "Look for files with symbols relative to this directory",
2908 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002909 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002910 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07002911 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03002912 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002913 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Andi Kleenb1491ac2017-09-05 11:40:57 -07002914 "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
Kan Liang49d58f02017-08-29 13:11:11 -04002915 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
Andi Kleen48d02a12017-02-23 15:46:34 -08002916 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002917 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2918 "system-wide collection from all CPUs"),
2919 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2920 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002921 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2922 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002923 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2924 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2925 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002926 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2927 "only consider symbols in these pids"),
2928 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2929 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002930 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2931 "Set the maximum stack depth when parsing the callchain, "
2932 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002933 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002934 OPT_BOOLEAN('I', "show-info", &show_full_info,
2935 "display extended information from perf.data file"),
2936 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2937 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002938 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2939 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002940 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2941 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002942 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2943 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302944 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2945 "Show namespace events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002946 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
2947 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002948 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002949 OPT_INTEGER(0, "max-blocks", &max_blocks,
2950 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002951 OPT_BOOLEAN(0, "ns", &nanosecs,
2952 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002953 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2954 "Instruction Tracing options",
2955 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002956 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2957 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002958 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2959 "Enable symbol demangling"),
2960 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2961 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002962 OPT_STRING(0, "time", &script.time_str, "str",
2963 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09002964 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2965 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002966 OPT_END()
2967 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002968 const char * const script_subcommands[] = { "record", "report", NULL };
2969 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002970 "perf script [<options>]",
2971 "perf script [<options>] record <script> [<record-options>] <command>",
2972 "perf script [<options>] report <script> [script-args]",
2973 "perf script [<options>] <script> [<record-options>] <command>",
2974 "perf script [<options>] <top-script> [script-args]",
2975 NULL
2976 };
Tom Zanussi38752942009-12-15 02:53:39 -06002977
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03002978 perf_set_singlethreaded();
2979
Tom Zanussib5b87312010-11-10 08:16:51 -06002980 setup_scripting();
2981
Yunlong Song40cae2b2015-03-18 21:35:54 +08002982 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002983 PARSE_OPT_STOP_AT_NON_OPTION);
2984
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002985 data.path = input_name;
2986 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002987
Tom Zanussib5b87312010-11-10 08:16:51 -06002988 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2989 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2990 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002991 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002992 }
2993
Tom Zanussib5b87312010-11-10 08:16:51 -06002994 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2995 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2996 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002997 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002998 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002999 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003000 return -1;
3001 }
Tom Zanussi38752942009-12-15 02:53:39 -06003002 }
3003
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003004 if (itrace_synth_opts.callchain &&
3005 itrace_synth_opts.callchain_sz > scripting_max_stack)
3006 scripting_max_stack = itrace_synth_opts.callchain_sz;
3007
Ben Hutchings44e668c2010-10-10 16:10:03 +01003008 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003009 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003010
Tom Zanussib5b87312010-11-10 08:16:51 -06003011 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003012 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003013 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003014 pid_t pid;
3015
Tom Zanussib5b87312010-11-10 08:16:51 -06003016 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3017 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3018
3019 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003020 usage_with_options_msg(script_usage, options,
3021 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003022 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003023 }
3024
Tom Zanussib5b87312010-11-10 08:16:51 -06003025 if (is_top_script(argv[0])) {
3026 rep_args = argc - 1;
3027 } else {
3028 int rec_args;
3029
3030 rep_args = has_required_arg(rep_script_path);
3031 rec_args = (argc - 1) - rep_args;
3032 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003033 usage_with_options_msg(script_usage, options,
3034 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003035 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003036 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003037 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003038 }
3039
3040 if (pipe(live_pipe) < 0) {
3041 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003042 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003043 }
3044
3045 pid = fork();
3046 if (pid < 0) {
3047 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003048 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003049 }
3050
3051 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003052 j = 0;
3053
Tom Zanussia0cccc22010-04-01 23:59:25 -05003054 dup2(live_pipe[1], 1);
3055 close(live_pipe[0]);
3056
Robert Richter317df652011-11-25 15:05:25 +01003057 if (is_top_script(argv[0])) {
3058 system_wide = true;
3059 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003060 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3061 err = -1;
3062 goto out;
3063 }
Robert Richter317df652011-11-25 15:05:25 +01003064 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003065
3066 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003067 if (!__argv) {
3068 pr_err("malloc failed\n");
3069 err = -ENOMEM;
3070 goto out;
3071 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003072
Tom Zanussib5b87312010-11-10 08:16:51 -06003073 __argv[j++] = "/bin/sh";
3074 __argv[j++] = rec_script_path;
3075 if (system_wide)
3076 __argv[j++] = "-a";
3077 __argv[j++] = "-q";
3078 __argv[j++] = "-o";
3079 __argv[j++] = "-";
3080 for (i = rep_args + 1; i < argc; i++)
3081 __argv[j++] = argv[i];
3082 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003083
3084 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003085 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003086 exit(-1);
3087 }
3088
3089 dup2(live_pipe[0], 0);
3090 close(live_pipe[1]);
3091
Tom Zanussib5b87312010-11-10 08:16:51 -06003092 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003093 if (!__argv) {
3094 pr_err("malloc failed\n");
3095 err = -ENOMEM;
3096 goto out;
3097 }
3098
Tom Zanussib5b87312010-11-10 08:16:51 -06003099 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003100 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003101 __argv[j++] = rep_script_path;
3102 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003103 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003104 __argv[j++] = "-i";
3105 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003106 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003107
3108 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003109 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003110 exit(-1);
3111 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003112
Tom Zanussib5b87312010-11-10 08:16:51 -06003113 if (rec_script_path)
3114 script_path = rec_script_path;
3115 if (rep_script_path)
3116 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003117
Tom Zanussib5b87312010-11-10 08:16:51 -06003118 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003119 j = 0;
3120
Robert Richter317df652011-11-25 15:05:25 +01003121 if (!rec_script_path)
3122 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003123 else if (!system_wide) {
3124 if (have_cmd(argc - 1, &argv[1]) != 0) {
3125 err = -1;
3126 goto out;
3127 }
3128 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003129
3130 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003131 if (!__argv) {
3132 pr_err("malloc failed\n");
3133 err = -ENOMEM;
3134 goto out;
3135 }
3136
Tom Zanussib5b87312010-11-10 08:16:51 -06003137 __argv[j++] = "/bin/sh";
3138 __argv[j++] = script_path;
3139 if (system_wide)
3140 __argv[j++] = "-a";
3141 for (i = 2; i < argc; i++)
3142 __argv[j++] = argv[i];
3143 __argv[j++] = NULL;
3144
3145 execvp("/bin/sh", (char **)__argv);
3146 free(__argv);
3147 exit(-1);
3148 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003149
Tom Zanussicf4fee52010-03-03 01:04:33 -06003150 if (!script_name)
3151 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003152
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003153 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003154 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003155 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003156
Jiri Olsae90debd2013-12-09 11:02:50 +01003157 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003158 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003159 perf_session__fprintf_info(session, stdout, show_full_info);
3160 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003161 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003162 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003163 if (show_full_info)
3164 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003165
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003166 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003167 goto out_delete;
3168
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003169 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003170 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003171
Adrian Huntere2167082016-06-23 16:40:58 +03003172 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3173 itrace_synth_opts.thread_stack = true;
3174
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003175 session->itrace_synth_opts = &itrace_synth_opts;
3176
Anton Blanchard5d67be92011-07-04 21:57:50 +10003177 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003178 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3179 if (err < 0)
3180 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003181 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003182 }
3183
David Ahern1424dc92011-03-09 22:23:28 -07003184 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003185 symbol_conf.use_callchain = true;
3186 else
3187 symbol_conf.use_callchain = false;
3188
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003189 if (session->tevent.pevent &&
3190 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003191 machine__resolve_kernel_addr,
3192 &session->machines.host) < 0) {
3193 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003194 err = -1;
3195 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003196 }
3197
Tom Zanussi956ffd02009-11-25 01:15:46 -06003198 if (generate_script_lang) {
3199 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003200 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003201
David Ahern2c9e45f72011-03-17 10:03:21 -06003202 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003203 fprintf(stderr,
3204 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003205 err = -EINVAL;
3206 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003207 }
3208
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003209 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003210 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003211 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003212 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003213 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003214 }
3215
3216 err = fstat(input, &perf_stat);
3217 if (err < 0) {
3218 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003219 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003220 }
3221
3222 if (!perf_stat.st_size) {
3223 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003224 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003225 }
3226
3227 scripting_ops = script_spec__lookup(generate_script_lang);
3228 if (!scripting_ops) {
3229 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003230 err = -ENOENT;
3231 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003232 }
3233
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003234 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003235 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003236 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003237 }
3238
3239 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003240 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003241 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003242 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003243 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003244 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003245 }
3246
David Ahern9cbdb702011-04-06 21:54:20 -06003247
3248 err = perf_session__check_output_opt(session);
3249 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003250 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003251
David Aherna91f4c42016-11-29 10:15:43 -07003252 /* needs to be parsed after looking up reference time */
3253 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
3254 pr_err("Invalid time string\n");
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003255 err = -EINVAL;
3256 goto out_delete;
David Aherna91f4c42016-11-29 10:15:43 -07003257 }
3258
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003259 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003260
Adrian Hunterd445dd22014-08-15 22:08:37 +03003261 flush_scripting();
3262
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003263out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01003264 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003265 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003266
3267 if (script_started)
3268 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003269out:
3270 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003271}