blob: 68f36dc0344f2bb7d07a1bcdb05f8af4419d1d88 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002#include "builtin.h"
3
Andrea Gelminib7eead82010-08-05 15:51:38 +02004#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02005#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02006#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06007#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02008#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06009#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +020010#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020013#include "util/symbol.h"
14#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010015#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020016#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070017#include "util/evlist.h"
18#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080019#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020020#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030021#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010022#include "util/cpumap.h"
23#include "util/thread_map.h"
24#include "util/stat.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030025#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030026#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070027#include "util/time-utils.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030028#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100029#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030030#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030031#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030032#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010033#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010034#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080035#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030036#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030037#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030038#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030039#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030040#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030041#include <sys/types.h>
42#include <sys/stat.h>
43#include <unistd.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020044
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030045#include "sane_ctype.h"
46
Tom Zanussi956ffd02009-11-25 01:15:46 -060047static char const *script_name;
48static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020049static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020050static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020051static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070052static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090053static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010054static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030055static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030056static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100057static const char *cpu_list;
58static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010059static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080060static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060061
Adrian Hunter44cbe722015-09-25 16:15:50 +030062unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030063
David Ahern745f43e2011-03-09 22:23:26 -070064enum perf_output_field {
65 PERF_OUTPUT_COMM = 1U << 0,
66 PERF_OUTPUT_TID = 1U << 1,
67 PERF_OUTPUT_PID = 1U << 2,
68 PERF_OUTPUT_TIME = 1U << 3,
69 PERF_OUTPUT_CPU = 1U << 4,
70 PERF_OUTPUT_EVNAME = 1U << 5,
71 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060072 PERF_OUTPUT_IP = 1U << 7,
73 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060074 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060075 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090076 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020077 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020078 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020079 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020080 PERF_OUTPUT_BRSTACK = 1U << 15,
81 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010082 PERF_OUTPUT_DATA_SRC = 1U << 17,
83 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000084 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030085 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030086 PERF_OUTPUT_INSN = 1U << 21,
87 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080088 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070089 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030090 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -040091 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -070092 PERF_OUTPUT_UREGS = 1U << 27,
David Ahern745f43e2011-03-09 22:23:26 -070093};
94
95struct output_option {
96 const char *str;
97 enum perf_output_field field;
98} all_output_options[] = {
99 {.str = "comm", .field = PERF_OUTPUT_COMM},
100 {.str = "tid", .field = PERF_OUTPUT_TID},
101 {.str = "pid", .field = PERF_OUTPUT_PID},
102 {.str = "time", .field = PERF_OUTPUT_TIME},
103 {.str = "cpu", .field = PERF_OUTPUT_CPU},
104 {.str = "event", .field = PERF_OUTPUT_EVNAME},
105 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600106 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700107 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600108 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600109 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900110 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200111 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200112 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200113 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700114 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200115 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
116 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100117 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
118 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000119 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300120 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300121 {.str = "insn", .field = PERF_OUTPUT_INSN},
122 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800123 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700124 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300125 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400126 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
David Ahern745f43e2011-03-09 22:23:26 -0700127};
128
Adrian Hunter14057202017-06-21 13:17:19 +0300129enum {
130 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
131 OUTPUT_TYPE_MAX
132};
133
David Ahern745f43e2011-03-09 22:23:26 -0700134/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600135static struct {
136 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600137 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400138 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600139 u64 fields;
140 u64 invalid_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300141} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700142
David Ahern2c9e45f72011-03-17 10:03:21 -0600143 [PERF_TYPE_HARDWARE] = {
144 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700145
David Ahern2c9e45f72011-03-17 10:03:21 -0600146 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
147 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600148 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200149 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
150 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600151
Wang Nan30372f02016-02-24 11:20:45 +0000152 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600153 },
154
155 [PERF_TYPE_SOFTWARE] = {
156 .user_set = false,
157
158 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
159 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600160 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200161 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000162 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600163
164 .invalid_fields = PERF_OUTPUT_TRACE,
165 },
166
167 [PERF_TYPE_TRACEPOINT] = {
168 .user_set = false,
169
170 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
171 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000172 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600173 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700174
175 [PERF_TYPE_RAW] = {
176 .user_set = false,
177
178 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
179 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600180 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200181 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100182 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
Kan Liang49d58f02017-08-29 13:11:11 -0400183 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT |
184 PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700185
Wang Nan30372f02016-02-24 11:20:45 +0000186 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700187 },
Wang Nan27cfef02015-12-08 02:25:43 +0000188
189 [PERF_TYPE_BREAKPOINT] = {
190 .user_set = false,
191
192 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
193 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
194 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
195 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
196 PERF_OUTPUT_PERIOD,
197
Wang Nan30372f02016-02-24 11:20:45 +0000198 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000199 },
Adrian Hunter14057202017-06-21 13:17:19 +0300200
201 [OUTPUT_TYPE_SYNTH] = {
202 .user_set = false,
203
204 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
205 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
206 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Adrian Hunter47e78082017-05-26 11:17:22 +0300207 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
208 PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300209
210 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
211 },
David Ahern1424dc92011-03-09 22:23:28 -0700212};
David Ahern745f43e2011-03-09 22:23:26 -0700213
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300214struct perf_evsel_script {
215 char *filename;
216 FILE *fp;
217 u64 samples;
218};
219
220static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100221 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300222{
223 struct perf_evsel_script *es = malloc(sizeof(*es));
224
225 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100226 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300227 goto out_free;
228 es->fp = fopen(es->filename, "w");
229 if (es->fp == NULL)
230 goto out_free_filename;
231 es->samples = 0;
232 }
233
234 return es;
235out_free_filename:
236 zfree(&es->filename);
237out_free:
238 free(es);
239 return NULL;
240}
241
242static void perf_evsel_script__delete(struct perf_evsel_script *es)
243{
244 zfree(&es->filename);
245 fclose(es->fp);
246 es->fp = NULL;
247 free(es);
248}
249
250static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
251{
252 struct stat st;
253
254 fstat(fileno(es->fp), &st);
255 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
256 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
257}
258
Adrian Hunter14057202017-06-21 13:17:19 +0300259static inline int output_type(unsigned int type)
260{
261 switch (type) {
262 case PERF_TYPE_SYNTH:
263 return OUTPUT_TYPE_SYNTH;
264 default:
265 return type;
266 }
267}
268
269static inline unsigned int attr_type(unsigned int type)
270{
271 switch (type) {
272 case OUTPUT_TYPE_SYNTH:
273 return PERF_TYPE_SYNTH;
274 default:
275 return type;
276 }
277}
278
David Ahern2c9e45f72011-03-17 10:03:21 -0600279static bool output_set_by_user(void)
280{
281 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300282 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600283 if (output[j].user_set)
284 return true;
285 }
286 return false;
287}
David Ahern745f43e2011-03-09 22:23:26 -0700288
David Ahern9cbdb702011-04-06 21:54:20 -0600289static const char *output_field2str(enum perf_output_field field)
290{
291 int i, imax = ARRAY_SIZE(all_output_options);
292 const char *str = "";
293
294 for (i = 0; i < imax; ++i) {
295 if (all_output_options[i].field == field) {
296 str = all_output_options[i].str;
297 break;
298 }
299 }
300 return str;
301}
302
Adrian Hunter14057202017-06-21 13:17:19 +0300303#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700304
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300305static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
306 u64 sample_type, const char *sample_msg,
307 enum perf_output_field field,
308 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700309{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300310 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300311 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600312 const char *evname;
313
314 if (attr->sample_type & sample_type)
315 return 0;
316
317 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300318 if (allow_user_set)
319 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300320 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600321 pr_err("Samples for '%s' event do not have %s attribute set. "
322 "Cannot print '%s' field.\n",
323 evname, sample_msg, output_field2str(field));
324 return -1;
325 }
326
327 /* user did not ask for it explicitly so remove from the default list */
328 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300329 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600330 pr_debug("Samples for '%s' event do not have %s attribute set. "
331 "Skipping '%s' field.\n",
332 evname, sample_msg, output_field2str(field));
333
334 return 0;
335}
336
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300337static int perf_evsel__check_stype(struct perf_evsel *evsel,
338 u64 sample_type, const char *sample_msg,
339 enum perf_output_field field)
340{
341 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
342 false);
343}
344
David Ahern9cbdb702011-04-06 21:54:20 -0600345static int perf_evsel__check_attr(struct perf_evsel *evsel,
346 struct perf_session *session)
347{
348 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300349 bool allow_user_set;
350
Jiri Olsae099eba2016-01-05 22:09:09 +0100351 if (perf_header__has_feat(&session->header, HEADER_STAT))
352 return 0;
353
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300354 allow_user_set = perf_header__has_feat(&session->header,
355 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600356
David Ahern1424dc92011-03-09 22:23:28 -0700357 if (PRINT_FIELD(TRACE) &&
358 !perf_session__has_traces(session, "record -R"))
359 return -EINVAL;
360
David Ahern787bef12011-05-27 14:28:43 -0600361 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300362 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
363 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700364 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700365 }
David Ahern7cec0922011-05-30 13:08:23 -0600366
367 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300368 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
369 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600370 return -EINVAL;
371
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100372 if (PRINT_FIELD(DATA_SRC) &&
373 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
374 PERF_OUTPUT_DATA_SRC))
375 return -EINVAL;
376
377 if (PRINT_FIELD(WEIGHT) &&
378 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
379 PERF_OUTPUT_WEIGHT))
380 return -EINVAL;
381
David Ahern7cec0922011-05-30 13:08:23 -0600382 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
383 pr_err("Display of symbols requested but neither sample IP nor "
384 "sample address\nis selected. Hence, no addresses to convert "
385 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600386 return -EINVAL;
387 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900388 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
389 pr_err("Display of offsets requested but symbol is not"
390 "selected.\n");
391 return -EINVAL;
392 }
Mark Santaniello55b9b502017-06-19 09:38:24 -0700393 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR) &&
Mark Santaniello106dacd2017-06-19 09:38:25 -0700394 !PRINT_FIELD(BRSTACK) && !PRINT_FIELD(BRSTACKSYM) && !PRINT_FIELD(BRSTACKOFF)) {
395 pr_err("Display of DSO requested but no address to convert. Select\n"
396 "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
David Ahern610723f2011-05-27 14:28:44 -0600397 return -EINVAL;
398 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200399 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
400 pr_err("Display of source line number requested but sample IP is not\n"
401 "selected. Hence, no address to lookup the source line number.\n");
402 return -EINVAL;
403 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800404 if (PRINT_FIELD(BRSTACKINSN) &&
405 !(perf_evlist__combined_branch_type(session->evlist) &
406 PERF_SAMPLE_BRANCH_ANY)) {
407 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
408 "Hint: run 'perf record -b ...'\n");
409 return -EINVAL;
410 }
David Ahern1424dc92011-03-09 22:23:28 -0700411 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300412 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
413 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700414 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700415
416 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300417 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
418 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700419 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700420
421 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300422 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
423 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700424 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600425
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200426 if (PRINT_FIELD(PERIOD) &&
427 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
428 PERF_OUTPUT_PERIOD))
429 return -EINVAL;
430
Stephane Eranianfc36f942015-08-31 18:41:10 +0200431 if (PRINT_FIELD(IREGS) &&
432 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
433 PERF_OUTPUT_IREGS))
434 return -EINVAL;
435
Andi Kleenb1491ac2017-09-05 11:40:57 -0700436 if (PRINT_FIELD(UREGS) &&
437 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
438 PERF_OUTPUT_UREGS))
439 return -EINVAL;
440
Kan Liang49d58f02017-08-29 13:11:11 -0400441 if (PRINT_FIELD(PHYS_ADDR) &&
442 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
443 PERF_OUTPUT_PHYS_ADDR))
444 return -EINVAL;
445
David Ahern9cbdb702011-04-06 21:54:20 -0600446 return 0;
447}
448
Adrian Hunter7ea95722013-11-01 15:51:30 +0200449static void set_print_ip_opts(struct perf_event_attr *attr)
450{
Adrian Hunter14057202017-06-21 13:17:19 +0300451 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200452
453 output[type].print_ip_opts = 0;
454 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300455 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200456
457 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300458 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200459
460 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300461 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200462
463 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300464 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200465
466 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300467 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200468}
469
David Ahern9cbdb702011-04-06 21:54:20 -0600470/*
471 * verify all user requested events exist and the samples
472 * have the expected data
473 */
474static int perf_session__check_output_opt(struct perf_session *session)
475{
He Kuang40f20e52016-05-16 04:51:19 +0000476 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600477 struct perf_evsel *evsel;
478
Adrian Hunter14057202017-06-21 13:17:19 +0300479 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
480 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600481
482 /*
483 * even if fields is set to 0 (ie., show nothing) event must
484 * exist if user explicitly includes it on the command line
485 */
Adrian Hunter14057202017-06-21 13:17:19 +0300486 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
487 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600488 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300489 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600490 event_type(j));
491 return -1;
492 }
493
494 if (evsel && output[j].fields &&
495 perf_evsel__check_attr(evsel, session))
496 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400497
498 if (evsel == NULL)
499 continue;
500
Adrian Hunter7ea95722013-11-01 15:51:30 +0200501 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700502 }
503
Adrian Hunter98526ee2014-07-31 09:00:59 +0300504 if (!no_callchain) {
505 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000506 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300507
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300508 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000509 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300510 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
511 use_callchain = true;
512 break;
513 }
514 }
He Kuang71ac8992016-08-04 11:25:43 +0000515 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300516 symbol_conf.use_callchain = false;
517 }
518
David Ahern80b8b492013-11-19 21:07:37 -0700519 /*
520 * set default for tracepoints to print symbols only
521 * if callchains are present
522 */
523 if (symbol_conf.use_callchain &&
524 !output[PERF_TYPE_TRACEPOINT].user_set) {
525 struct perf_event_attr *attr;
526
527 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700528
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300529 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000530 if (evsel->attr.type != j)
531 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700532
He Kuang40f20e52016-05-16 04:51:19 +0000533 attr = &evsel->attr;
534
535 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
536 output[j].fields |= PERF_OUTPUT_IP;
537 output[j].fields |= PERF_OUTPUT_SYM;
538 output[j].fields |= PERF_OUTPUT_DSO;
539 set_print_ip_opts(attr);
540 goto out;
541 }
David Ahern80b8b492013-11-19 21:07:37 -0700542 }
543 }
544
545out:
David Ahern1424dc92011-03-09 22:23:28 -0700546 return 0;
547}
David Ahern745f43e2011-03-09 22:23:26 -0700548
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300549static int perf_sample__fprintf_iregs(struct perf_sample *sample,
550 struct perf_event_attr *attr, FILE *fp)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200551{
552 struct regs_dump *regs = &sample->intr_regs;
553 uint64_t mask = attr->sample_regs_intr;
554 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300555 int printed = 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200556
557 if (!regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300558 return 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200559
560 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
561 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300562 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Stephane Eranianfc36f942015-08-31 18:41:10 +0200563 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300564
565 return printed;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200566}
567
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300568static int perf_sample__fprintf_uregs(struct perf_sample *sample,
569 struct perf_event_attr *attr, FILE *fp)
Andi Kleenb1491ac2017-09-05 11:40:57 -0700570{
571 struct regs_dump *regs = &sample->user_regs;
572 uint64_t mask = attr->sample_regs_user;
573 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300574 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700575
576 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300577 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700578
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300579 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700580
581 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
582 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300583 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700584 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300585
586 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700587}
588
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300589static int perf_sample__fprintf_start(struct perf_sample *sample,
590 struct thread *thread,
591 struct perf_evsel *evsel, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700592{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300593 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700594 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700595 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300596 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700597
David Ahern745f43e2011-03-09 22:23:26 -0700598 if (PRINT_FIELD(COMM)) {
599 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300600 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600601 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300602 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700603 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300604 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700605 }
David Ahernc70c94b2011-03-09 22:23:25 -0700606
David Ahern745f43e2011-03-09 22:23:26 -0700607 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300608 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700609 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300610 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700611 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300612 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700613
David Ahern745f43e2011-03-09 22:23:26 -0700614 if (PRINT_FIELD(CPU)) {
615 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300616 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700617 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300618 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700619 }
David Ahernc70c94b2011-03-09 22:23:25 -0700620
David Ahern745f43e2011-03-09 22:23:26 -0700621 if (PRINT_FIELD(TIME)) {
622 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300623 secs = nsecs / NSEC_PER_SEC;
624 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900625
Adrian Hunter83e19862015-09-25 16:15:36 +0300626 if (nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300627 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900628 else {
629 char sample_time[32];
630 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300631 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900632 }
David Ahern745f43e2011-03-09 22:23:26 -0700633 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300634
635 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700636}
637
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200638static inline char
639mispred_str(struct branch_entry *br)
640{
641 if (!(br->flags.mispred || br->flags.predicted))
642 return '-';
643
644 return br->flags.predicted ? 'P' : 'M';
645}
646
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300647static int perf_sample__fprintf_brstack(struct perf_sample *sample,
648 struct thread *thread,
649 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200650{
651 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700652 struct addr_location alf, alt;
653 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300654 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200655
656 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300657 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200658
659 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700660 from = br->entries[i].from;
661 to = br->entries[i].to;
662
663 if (PRINT_FIELD(DSO)) {
664 memset(&alf, 0, sizeof(alf));
665 memset(&alt, 0, sizeof(alt));
666 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
667 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
668 }
669
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300670 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700671 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300672 printed += fprintf(fp, "(");
673 printed += map__fprintf_dsoname(alf.map, fp);
674 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700675 }
676
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300677 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700678 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300679 printed += fprintf(fp, "(");
680 printed += map__fprintf_dsoname(alt.map, fp);
681 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700682 }
683
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300684 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200685 mispred_str( br->entries + i),
686 br->entries[i].flags.in_tx? 'X' : '-',
687 br->entries[i].flags.abort? 'A' : '-',
688 br->entries[i].flags.cycles);
689 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300690
691 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200692}
693
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300694static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
695 struct thread *thread,
696 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200697{
698 struct branch_stack *br = sample->branch_stack;
699 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200700 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300701 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200702
703 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300704 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200705
706 for (i = 0; i < br->nr; i++) {
707
708 memset(&alf, 0, sizeof(alf));
709 memset(&alt, 0, sizeof(alt));
710 from = br->entries[i].from;
711 to = br->entries[i].to;
712
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300713 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200714 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300715 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200716
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300717 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200718 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300719 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200720
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300721 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700722 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300723 printed += fprintf(fp, "(");
724 printed += map__fprintf_dsoname(alf.map, fp);
725 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700726 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300727 printed += fprintf(fp, "%c", '/');
728 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700729 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300730 printed += fprintf(fp, "(");
731 printed += map__fprintf_dsoname(alt.map, fp);
732 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700733 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300734 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200735 mispred_str( br->entries + i),
736 br->entries[i].flags.in_tx? 'X' : '-',
737 br->entries[i].flags.abort? 'A' : '-',
738 br->entries[i].flags.cycles);
739 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300740
741 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200742}
743
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300744static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
745 struct thread *thread,
746 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700747{
748 struct branch_stack *br = sample->branch_stack;
749 struct addr_location alf, alt;
750 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300751 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700752
753 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300754 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700755
756 for (i = 0; i < br->nr; i++) {
757
758 memset(&alf, 0, sizeof(alf));
759 memset(&alt, 0, sizeof(alt));
760 from = br->entries[i].from;
761 to = br->entries[i].to;
762
763 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
764 if (alf.map && !alf.map->dso->adjust_symbols)
765 from = map__map_ip(alf.map, from);
766
767 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
768 if (alt.map && !alt.map->dso->adjust_symbols)
769 to = map__map_ip(alt.map, to);
770
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300771 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700772 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300773 printed += fprintf(fp, "(");
774 printed += map__fprintf_dsoname(alf.map, fp);
775 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700776 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300777 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700778 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300779 printed += fprintf(fp, "(");
780 printed += map__fprintf_dsoname(alt.map, fp);
781 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700782 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300783 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700784 mispred_str(br->entries + i),
785 br->entries[i].flags.in_tx ? 'X' : '-',
786 br->entries[i].flags.abort ? 'A' : '-',
787 br->entries[i].flags.cycles);
788 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300789
790 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700791}
Andi Kleen48d02a12017-02-23 15:46:34 -0800792#define MAXBB 16384UL
793
794static int grab_bb(u8 *buffer, u64 start, u64 end,
795 struct machine *machine, struct thread *thread,
796 bool *is64bit, u8 *cpumode, bool last)
797{
798 long offset, len;
799 struct addr_location al;
800 bool kernel;
801
802 if (!start || !end)
803 return 0;
804
805 kernel = machine__kernel_ip(machine, start);
806 if (kernel)
807 *cpumode = PERF_RECORD_MISC_KERNEL;
808 else
809 *cpumode = PERF_RECORD_MISC_USER;
810
811 /*
812 * Block overlaps between kernel and user.
813 * This can happen due to ring filtering
814 * On Intel CPUs the entry into the kernel is filtered,
815 * but the exit is not. Let the caller patch it up.
816 */
817 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300818 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800819 return -ENXIO;
820 }
821
822 memset(&al, 0, sizeof(al));
823 if (end - start > MAXBB - MAXINSN) {
824 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300825 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800826 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300827 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800828 return 0;
829 }
830
831 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
832 if (!al.map || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300833 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800834 return 0;
835 }
836 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300837 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800838 return 0;
839 }
840
841 /* Load maps to ensure dso->is_64_bit has been updated */
842 map__load(al.map);
843
844 offset = al.map->map_ip(al.map, start);
845 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
846 end - start + MAXINSN);
847
848 *is64bit = al.map->dso->is_64_bit;
849 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300850 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800851 start, end);
852 return len;
853}
854
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300855static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
856 struct perf_insn *x, u8 *inbuf, int len,
857 int insn, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800858{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300859 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
860 dump_insn(x, ip, inbuf, len, NULL),
861 en->flags.predicted ? " PRED" : "",
862 en->flags.mispred ? " MISPRED" : "",
863 en->flags.in_tx ? " INTX" : "",
864 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800865 if (en->flags.cycles) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300866 printed += fprintf(fp, " %d cycles", en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800867 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300868 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800869 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300870 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800871}
872
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300873static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
874 u8 cpumode, int cpu, struct symbol **lastsym,
875 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800876{
877 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300878 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800879
880 memset(&al, 0, sizeof(al));
881
882 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
883 if (!al.map)
884 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
885 addr, &al);
886 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300887 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800888
889 al.cpu = cpu;
890 al.sym = NULL;
891 if (al.map)
892 al.sym = map__find_symbol(al.map, al.addr);
893
894 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300895 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800896
897 if (al.addr < al.sym->end)
898 off = al.addr - al.sym->start;
899 else
900 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300901 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800902 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300903 printed += fprintf(fp, "%+d", off);
904 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800905 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300906 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
907 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800908 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300909
910 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800911}
912
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300913static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
914 struct thread *thread,
915 struct perf_event_attr *attr,
916 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800917{
918 struct branch_stack *br = sample->branch_stack;
919 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300920 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800921 struct perf_insn x;
922 u8 buffer[MAXBB];
923 unsigned off;
924 struct symbol *lastsym = NULL;
925
926 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300927 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800928 nr = br->nr;
929 if (max_blocks && nr > max_blocks + 1)
930 nr = max_blocks + 1;
931
932 x.thread = thread;
933 x.cpu = sample->cpu;
934
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300935 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -0800936
937 /* Handle first from jump, of which we don't know the entry. */
938 len = grab_bb(buffer, br->entries[nr-1].from,
939 br->entries[nr-1].from,
940 machine, thread, &x.is64bit, &x.cpumode, false);
941 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300942 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
943 x.cpumode, x.cpu, &lastsym, attr, fp);
944 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
945 &x, buffer, len, 0, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800946 }
947
948 /* Print all blocks */
949 for (i = nr - 2; i >= 0; i--) {
950 if (br->entries[i].from || br->entries[i].to)
951 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
952 br->entries[i].from,
953 br->entries[i].to);
954 start = br->entries[i + 1].to;
955 end = br->entries[i].from;
956
957 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
958 /* Patch up missing kernel transfers due to ring filters */
959 if (len == -ENXIO && i > 0) {
960 end = br->entries[--i].from;
961 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
962 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
963 }
964 if (len <= 0)
965 continue;
966
967 insn = 0;
968 for (off = 0;; off += ilen) {
969 uint64_t ip = start + off;
970
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300971 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800972 if (ip == end) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300973 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800974 break;
975 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300976 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
977 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -0800978 if (ilen == 0)
979 break;
980 insn++;
981 }
982 }
983 }
984
985 /*
986 * Hit the branch? In this case we are already done, and the target
987 * has not been executed yet.
988 */
989 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300990 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -0800991 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300992 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -0800993
994 /*
995 * Print final block upto sample
996 */
997 start = br->entries[0].to;
998 end = sample->ip;
999 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001000 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001001 if (len <= 0) {
1002 /* Print at least last IP if basic block did not work */
1003 len = grab_bb(buffer, sample->ip, sample->ip,
1004 machine, thread, &x.is64bit, &x.cpumode, false);
1005 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001006 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001007
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001008 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001009 dump_insn(&x, sample->ip, buffer, len, NULL));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001010 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001011 }
1012 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001013 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1014 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001015 if (ilen == 0)
1016 break;
1017 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001018out:
1019 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001020}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001021
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001022static int perf_sample__fprintf_addr(struct perf_sample *sample,
1023 struct thread *thread,
1024 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001025{
1026 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001027 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001028
1029 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001030 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001031
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001032 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001033
1034 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001035 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001036 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001037 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001038 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001039 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001040 }
1041
1042 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001043 printed += fprintf(fp, " (");
1044 printed += map__fprintf_dsoname(al.map, fp);
1045 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001046 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001047out:
1048 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001049}
1050
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001051static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1052 struct perf_evsel *evsel,
1053 struct thread *thread,
1054 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001055{
1056 struct perf_event_attr *attr = &evsel->attr;
1057 size_t depth = thread_stack__depth(thread);
1058 struct addr_location addr_al;
1059 const char *name = NULL;
1060 static int spacing;
1061 int len = 0;
1062 u64 ip = 0;
1063
1064 /*
1065 * The 'return' has already been popped off the stack so the depth has
1066 * to be adjusted to match the 'call'.
1067 */
1068 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1069 depth += 1;
1070
1071 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1072 if (sample_addr_correlates_sym(attr)) {
1073 thread__resolve(thread, &addr_al, sample);
1074 if (addr_al.sym)
1075 name = addr_al.sym->name;
1076 else
1077 ip = sample->addr;
1078 } else {
1079 ip = sample->addr;
1080 }
1081 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1082 if (al->sym)
1083 name = al->sym->name;
1084 else
1085 ip = sample->ip;
1086 }
1087
1088 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001089 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001090 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001091 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001092
1093 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001094 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001095
1096 /*
1097 * Try to keep the output length from changing frequently so that the
1098 * output lines up more nicely.
1099 */
1100 if (len > spacing || (len && len < spacing - 52))
1101 spacing = round_up(len + 4, 32);
1102
1103 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001104 len += fprintf(fp, "%*s", spacing - len, "");
1105
1106 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001107}
1108
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001109static int perf_sample__fprintf_insn(struct perf_sample *sample,
1110 struct perf_event_attr *attr,
1111 struct thread *thread,
1112 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001113{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001114 int printed = 0;
1115
Andi Kleen224e2c92016-10-07 16:42:27 +03001116 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001117 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen224e2c92016-10-07 16:42:27 +03001118 if (PRINT_FIELD(INSN)) {
1119 int i;
1120
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001121 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001122 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001123 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001124 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001125 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001126 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1127
1128 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001129}
1130
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001131static int perf_sample__fprintf_bts(struct perf_sample *sample,
1132 struct perf_evsel *evsel,
1133 struct thread *thread,
1134 struct addr_location *al,
1135 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001136{
1137 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001138 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001139 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001140 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001141
Adrian Huntere2167082016-06-23 16:40:58 +03001142 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001143 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001144
Akihiro Nagai95582592012-01-30 13:43:09 +09001145 /* print branch_from information */
1146 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001147 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001148 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001149
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001150 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001151 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001152 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001153 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001154
1155 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001156 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001157 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001158 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001159 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001160 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001161 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001162 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001163
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001164 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001165 }
1166
Akihiro Nagai95582592012-01-30 13:43:09 +09001167 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001168 if (PRINT_FIELD(ADDR) ||
1169 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001170 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001171 printed += fprintf(fp, " => ");
1172 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001173 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001174
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001175 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001176 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001177
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001178 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1179 return printed + fprintf(fp, "\n");
Akihiro Nagai95582592012-01-30 13:43:09 +09001180}
1181
Adrian Hunter055cd332016-06-23 16:40:56 +03001182static struct {
1183 u32 flags;
1184 const char *name;
1185} sample_flags[] = {
1186 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1187 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1188 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1189 {PERF_IP_FLAG_BRANCH, "jmp"},
1190 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1191 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1192 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1193 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1194 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1195 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1196 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1197 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1198 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1199 {0, NULL}
1200};
1201
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001202static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001203{
1204 const char *chars = PERF_IP_FLAG_CHARS;
1205 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001206 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1207 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001208 char str[33];
1209 int i, pos = 0;
1210
Adrian Hunter055cd332016-06-23 16:40:56 +03001211 for (i = 0; sample_flags[i].name ; i++) {
1212 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1213 name = sample_flags[i].name;
1214 break;
1215 }
1216 }
1217
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001218 for (i = 0; i < n; i++, flags >>= 1) {
1219 if (flags & 1)
1220 str[pos++] = chars[i];
1221 }
1222 for (; i < 32; i++, flags >>= 1) {
1223 if (flags & 1)
1224 str[pos++] = '?';
1225 }
1226 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001227
1228 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001229 return fprintf(fp, " %-7s%4s ", name, in_tx ? "(x)" : "");
1230
1231 return fprintf(fp, " %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001232}
1233
Wang Nan30372f02016-02-24 11:20:45 +00001234struct printer_data {
1235 int line_no;
1236 bool hit_nul;
1237 bool is_printable;
1238};
1239
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001240static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1241 unsigned int val,
1242 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001243{
1244 unsigned char ch = (unsigned char)val;
1245 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001246 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001247
1248 switch (op) {
1249 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001250 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001251 break;
1252 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001253 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001254 " ");
1255 break;
1256 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001257 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001258 break;
1259 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001260 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001261 break;
1262 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001263 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001264 break;
1265 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001266 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001267 break;
1268 case BINARY_PRINT_CHAR_DATA:
1269 if (printer_data->hit_nul && ch)
1270 printer_data->is_printable = false;
1271
1272 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001273 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001274
1275 if (!printer_data->is_printable)
1276 break;
1277
1278 if (ch == '\0')
1279 printer_data->hit_nul = true;
1280 else
1281 printer_data->is_printable = false;
1282 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001283 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001284 }
1285 break;
1286 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001287 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001288 break;
1289 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001290 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001291 printer_data->line_no++;
1292 break;
1293 case BINARY_PRINT_DATA_END:
1294 default:
1295 break;
1296 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001297
1298 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001299}
1300
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001301static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001302{
1303 unsigned int nr_bytes = sample->raw_size;
1304 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001305 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1306 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001307
1308 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001309 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1310
1311 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001312}
1313
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001314static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001315{
1316 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001317 return fprintf(fp, "%*s", spacing - len, "");
1318
1319 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001320}
1321
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001322static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001323{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001324 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001325}
1326
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001327static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001328{
1329 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1330 int len;
1331
1332 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001333 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001334
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001335 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001336 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001337 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001338}
1339
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001340static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001341{
1342 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1343 int len;
1344
1345 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001346 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001347
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001348 len = fprintf(fp, " hints: %#x extensions: %#x ",
1349 data->hints, data->extensions);
1350 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001351}
1352
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001353static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001354{
1355 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1356 int len;
1357
1358 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001359 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001360
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001361 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1362 data->hw, data->cstate, data->subcstate);
1363 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001364}
1365
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001366static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001367{
1368 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1369 int len;
1370
1371 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001372 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001373
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001374 len = fprintf(fp, " IP: %u ", data->ip);
1375 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001376}
1377
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001378static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001379{
1380 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1381 int len;
1382
1383 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001384 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001385
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001386 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001387 data->deepest_cstate, data->last_cstate,
1388 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001389 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001390}
1391
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001392static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001393{
1394 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1395 unsigned int percent, freq;
1396 int len;
1397
1398 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001399 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001400
1401 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001402 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001403 if (data->max_nonturbo) {
1404 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001405 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001406 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001407 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001408}
1409
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001410static int perf_sample__fprintf_synth(struct perf_sample *sample,
1411 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001412{
1413 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001414 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001415 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001416 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001417 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001418 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001419 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001420 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001421 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001422 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001423 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001424 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001425 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001426 default:
1427 break;
1428 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001429
1430 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001431}
1432
Jiri Olsa809e9422015-11-26 18:55:21 +01001433struct perf_script {
1434 struct perf_tool tool;
1435 struct perf_session *session;
1436 bool show_task_events;
1437 bool show_mmap_events;
1438 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301439 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001440 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001441 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001442 struct cpu_map *cpus;
1443 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001444 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001445 const char *time_str;
1446 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001447};
1448
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001449static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1450{
1451 struct perf_evsel *evsel;
1452 int max = 0;
1453
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001454 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001455 int len = strlen(perf_evsel__name(evsel));
1456
1457 max = MAX(len, max);
1458 }
1459
1460 return max;
1461}
1462
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001463static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001464{
1465 struct mem_info mi = { .data_src.val = data_src };
1466 char decode[100];
1467 char out[100];
1468 static int maxlen;
1469 int len;
1470
1471 perf_script__meminfo_scnprintf(decode, 100, &mi);
1472
1473 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1474 if (maxlen < len)
1475 maxlen = len;
1476
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001477 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001478}
1479
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001480static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001481 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001482 struct addr_location *al,
1483 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001484{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001485 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001486 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001487 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001488 struct perf_evsel_script *es = evsel->priv;
1489 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001490
Adrian Hunter14057202017-06-21 13:17:19 +03001491 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001492 return;
1493
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001494 ++es->samples;
1495
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001496 perf_sample__fprintf_start(sample, thread, evsel, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001497
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001498 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001499 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001500
Namhyung Kime944d3d2013-11-18 14:34:52 +09001501 if (PRINT_FIELD(EVNAME)) {
1502 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001503
1504 if (!script->name_width)
1505 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1506
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001507 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001508 }
1509
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001510 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001511 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001512
Akihiro Nagai95582592012-01-30 13:43:09 +09001513 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001514 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001515 return;
1516 }
1517
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001518 if (PRINT_FIELD(TRACE)) {
1519 event_format__fprintf(evsel->tp_format, sample->cpu,
1520 sample->raw_data, sample->raw_size, fp);
1521 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001522
1523 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001524 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001525
David Ahern7cec0922011-05-30 13:08:23 -06001526 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001527 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001528
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001529 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001530 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001531
1532 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001533 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001534
David Ahern787bef12011-05-27 14:28:43 -06001535 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001536 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001537
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001538 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001539 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001540 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001541 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001542
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001543 fputc(cursor ? '\n' : ' ', fp);
1544 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001545 }
1546
Stephane Eranianfc36f942015-08-31 18:41:10 +02001547 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001548 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001549
Andi Kleenb1491ac2017-09-05 11:40:57 -07001550 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001551 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001552
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001553 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001554 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001555 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001556 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001557 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001558 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001559
Wang Nan30372f02016-02-24 11:20:45 +00001560 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001561 perf_sample__fprintf_bpf_output(sample, fp);
1562 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001563
1564 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001565 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1566 fprintf(fp, "\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001567}
1568
Tom Zanussi956ffd02009-11-25 01:15:46 -06001569static struct scripting_ops *scripting_ops;
1570
Jiri Olsa36e33c52016-01-06 11:49:56 +01001571static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1572{
1573 int nthreads = thread_map__nr(counter->threads);
1574 int ncpus = perf_evsel__nr_cpus(counter);
1575 int cpu, thread;
1576 static int header_printed;
1577
1578 if (counter->system_wide)
1579 nthreads = 1;
1580
1581 if (!header_printed) {
1582 printf("%3s %8s %15s %15s %15s %15s %s\n",
1583 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1584 header_printed = 1;
1585 }
1586
1587 for (thread = 0; thread < nthreads; thread++) {
1588 for (cpu = 0; cpu < ncpus; cpu++) {
1589 struct perf_counts_values *counts;
1590
1591 counts = perf_counts(counter->counts, cpu, thread);
1592
1593 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1594 counter->cpus->map[cpu],
1595 thread_map__pid(counter->threads, thread),
1596 counts->val,
1597 counts->ena,
1598 counts->run,
1599 tstamp,
1600 perf_evsel__name(counter));
1601 }
1602 }
1603}
1604
Jiri Olsae099eba2016-01-05 22:09:09 +01001605static void process_stat(struct perf_evsel *counter, u64 tstamp)
1606{
1607 if (scripting_ops && scripting_ops->process_stat)
1608 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001609 else
1610 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001611}
1612
1613static void process_stat_interval(u64 tstamp)
1614{
1615 if (scripting_ops && scripting_ops->process_stat_interval)
1616 scripting_ops->process_stat_interval(tstamp);
1617}
1618
Tom Zanussi956ffd02009-11-25 01:15:46 -06001619static void setup_scripting(void)
1620{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001621 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001622 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001623}
1624
Adrian Hunterd445dd22014-08-15 22:08:37 +03001625static int flush_scripting(void)
1626{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001627 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001628}
1629
Tom Zanussi956ffd02009-11-25 01:15:46 -06001630static int cleanup_scripting(void)
1631{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001632 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001633
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001634 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001635}
1636
Jiri Olsa809e9422015-11-26 18:55:21 +01001637static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001638 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001639 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001640 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001641 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001642{
Jiri Olsa809e9422015-11-26 18:55:21 +01001643 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001644 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001645
David Aherna91f4c42016-11-29 10:15:43 -07001646 if (perf_time__skip_sample(&scr->ptime, sample->time))
1647 return 0;
1648
David Ahern1424dc92011-03-09 22:23:28 -07001649 if (debug_mode) {
1650 if (sample->time < last_timestamp) {
1651 pr_err("Samples misordered, previous: %" PRIu64
1652 " this: %" PRIu64 "\n", last_timestamp,
1653 sample->time);
1654 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001655 }
David Ahern1424dc92011-03-09 22:23:28 -07001656 last_timestamp = sample->time;
1657 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001658 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001659
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001660 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001661 pr_err("problem processing %d event, skipping it.\n",
1662 event->header.type);
1663 return -1;
1664 }
1665
1666 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001667 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001668
Anton Blanchard5d67be92011-07-04 21:57:50 +10001669 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001670 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001671
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001672 if (scripting_ops)
1673 scripting_ops->process_event(event, sample, evsel, &al);
1674 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001675 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001676
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001677out_put:
1678 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001679 return 0;
1680}
1681
Adrian Hunter7ea95722013-11-01 15:51:30 +02001682static int process_attr(struct perf_tool *tool, union perf_event *event,
1683 struct perf_evlist **pevlist)
1684{
1685 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1686 struct perf_evlist *evlist;
1687 struct perf_evsel *evsel, *pos;
1688 int err;
1689
1690 err = perf_event__process_attr(tool, event, pevlist);
1691 if (err)
1692 return err;
1693
1694 evlist = *pevlist;
1695 evsel = perf_evlist__last(*pevlist);
1696
Adrian Hunter14057202017-06-21 13:17:19 +03001697 if (evsel->attr.type >= PERF_TYPE_MAX &&
1698 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001699 return 0;
1700
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001701 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001702 if (pos->attr.type == evsel->attr.type && pos != evsel)
1703 return 0;
1704 }
1705
1706 set_print_ip_opts(&evsel->attr);
1707
Jiri Olsad2b5a312015-10-16 12:41:25 +02001708 if (evsel->attr.sample_type)
1709 err = perf_evsel__check_attr(evsel, scr->session);
1710
1711 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001712}
1713
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001714static int process_comm_event(struct perf_tool *tool,
1715 union perf_event *event,
1716 struct perf_sample *sample,
1717 struct machine *machine)
1718{
1719 struct thread *thread;
1720 struct perf_script *script = container_of(tool, struct perf_script, tool);
1721 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001722 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001723 int ret = -1;
1724
1725 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1726 if (thread == NULL) {
1727 pr_debug("problem processing COMM event, skipping it.\n");
1728 return -1;
1729 }
1730
1731 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1732 goto out;
1733
1734 if (!evsel->attr.sample_id_all) {
1735 sample->cpu = 0;
1736 sample->time = 0;
1737 sample->tid = event->comm.tid;
1738 sample->pid = event->comm.pid;
1739 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001740 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001741 perf_event__fprintf(event, stdout);
1742 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001743out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001744 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001745 return ret;
1746}
1747
Hari Bathini96a44bb2017-03-08 02:12:06 +05301748static int process_namespaces_event(struct perf_tool *tool,
1749 union perf_event *event,
1750 struct perf_sample *sample,
1751 struct machine *machine)
1752{
1753 struct thread *thread;
1754 struct perf_script *script = container_of(tool, struct perf_script, tool);
1755 struct perf_session *session = script->session;
1756 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1757 int ret = -1;
1758
1759 thread = machine__findnew_thread(machine, event->namespaces.pid,
1760 event->namespaces.tid);
1761 if (thread == NULL) {
1762 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1763 return -1;
1764 }
1765
1766 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1767 goto out;
1768
1769 if (!evsel->attr.sample_id_all) {
1770 sample->cpu = 0;
1771 sample->time = 0;
1772 sample->tid = event->namespaces.tid;
1773 sample->pid = event->namespaces.pid;
1774 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001775 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Hari Bathini96a44bb2017-03-08 02:12:06 +05301776 perf_event__fprintf(event, stdout);
1777 ret = 0;
1778out:
1779 thread__put(thread);
1780 return ret;
1781}
1782
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001783static int process_fork_event(struct perf_tool *tool,
1784 union perf_event *event,
1785 struct perf_sample *sample,
1786 struct machine *machine)
1787{
1788 struct thread *thread;
1789 struct perf_script *script = container_of(tool, struct perf_script, tool);
1790 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001791 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001792
1793 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1794 return -1;
1795
1796 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1797 if (thread == NULL) {
1798 pr_debug("problem processing FORK event, skipping it.\n");
1799 return -1;
1800 }
1801
1802 if (!evsel->attr.sample_id_all) {
1803 sample->cpu = 0;
1804 sample->time = event->fork.time;
1805 sample->tid = event->fork.tid;
1806 sample->pid = event->fork.pid;
1807 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001808 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001809 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001810 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001811
1812 return 0;
1813}
1814static int process_exit_event(struct perf_tool *tool,
1815 union perf_event *event,
1816 struct perf_sample *sample,
1817 struct machine *machine)
1818{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001819 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001820 struct thread *thread;
1821 struct perf_script *script = container_of(tool, struct perf_script, tool);
1822 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001823 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001824
1825 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1826 if (thread == NULL) {
1827 pr_debug("problem processing EXIT event, skipping it.\n");
1828 return -1;
1829 }
1830
1831 if (!evsel->attr.sample_id_all) {
1832 sample->cpu = 0;
1833 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001834 sample->tid = event->fork.tid;
1835 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001836 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001837 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001838 perf_event__fprintf(event, stdout);
1839
1840 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001841 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001842
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001843 thread__put(thread);
1844 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001845}
1846
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001847static int process_mmap_event(struct perf_tool *tool,
1848 union perf_event *event,
1849 struct perf_sample *sample,
1850 struct machine *machine)
1851{
1852 struct thread *thread;
1853 struct perf_script *script = container_of(tool, struct perf_script, tool);
1854 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001855 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001856
1857 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1858 return -1;
1859
1860 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1861 if (thread == NULL) {
1862 pr_debug("problem processing MMAP event, skipping it.\n");
1863 return -1;
1864 }
1865
1866 if (!evsel->attr.sample_id_all) {
1867 sample->cpu = 0;
1868 sample->time = 0;
1869 sample->tid = event->mmap.tid;
1870 sample->pid = event->mmap.pid;
1871 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001872 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001873 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001874 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001875 return 0;
1876}
1877
1878static int process_mmap2_event(struct perf_tool *tool,
1879 union perf_event *event,
1880 struct perf_sample *sample,
1881 struct machine *machine)
1882{
1883 struct thread *thread;
1884 struct perf_script *script = container_of(tool, struct perf_script, tool);
1885 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001886 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001887
1888 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1889 return -1;
1890
1891 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1892 if (thread == NULL) {
1893 pr_debug("problem processing MMAP2 event, skipping it.\n");
1894 return -1;
1895 }
1896
1897 if (!evsel->attr.sample_id_all) {
1898 sample->cpu = 0;
1899 sample->time = 0;
1900 sample->tid = event->mmap2.tid;
1901 sample->pid = event->mmap2.pid;
1902 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001903 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001904 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001905 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001906 return 0;
1907}
1908
Adrian Hunter7c148982015-07-21 12:44:06 +03001909static int process_switch_event(struct perf_tool *tool,
1910 union perf_event *event,
1911 struct perf_sample *sample,
1912 struct machine *machine)
1913{
1914 struct thread *thread;
1915 struct perf_script *script = container_of(tool, struct perf_script, tool);
1916 struct perf_session *session = script->session;
1917 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1918
1919 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1920 return -1;
1921
1922 thread = machine__findnew_thread(machine, sample->pid,
1923 sample->tid);
1924 if (thread == NULL) {
1925 pr_debug("problem processing SWITCH event, skipping it.\n");
1926 return -1;
1927 }
1928
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001929 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Adrian Hunter7c148982015-07-21 12:44:06 +03001930 perf_event__fprintf(event, stdout);
1931 thread__put(thread);
1932 return 0;
1933}
1934
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001935static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001936{
1937 session_done = 1;
1938}
1939
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001940static void perf_script__fclose_per_event_dump(struct perf_script *script)
1941{
1942 struct perf_evlist *evlist = script->session->evlist;
1943 struct perf_evsel *evsel;
1944
1945 evlist__for_each_entry(evlist, evsel) {
1946 if (!evsel->priv)
1947 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001948 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001949 evsel->priv = NULL;
1950 }
1951}
1952
1953static int perf_script__fopen_per_event_dump(struct perf_script *script)
1954{
1955 struct perf_evsel *evsel;
1956
1957 evlist__for_each_entry(script->session->evlist, evsel) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001958 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001959 if (evsel->priv == NULL)
1960 goto out_err_fclose;
1961 }
1962
1963 return 0;
1964
1965out_err_fclose:
1966 perf_script__fclose_per_event_dump(script);
1967 return -1;
1968}
1969
1970static int perf_script__setup_per_event_dump(struct perf_script *script)
1971{
1972 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001973 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001974
1975 if (script->per_event_dump)
1976 return perf_script__fopen_per_event_dump(script);
1977
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001978 es_stdout.fp = stdout;
1979
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001980 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001981 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001982
1983 return 0;
1984}
1985
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001986static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
1987{
1988 struct perf_evsel *evsel;
1989
1990 evlist__for_each_entry(script->session->evlist, evsel) {
1991 struct perf_evsel_script *es = evsel->priv;
1992
1993 perf_evsel_script__fprintf(es, stdout);
1994 perf_evsel_script__delete(es);
1995 evsel->priv = NULL;
1996 }
1997}
1998
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001999static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002000{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002001 int ret;
2002
Tom Zanussic239da32010-04-01 23:59:18 -05002003 signal(SIGINT, sig_handler);
2004
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002005 /* override event processing functions */
2006 if (script->show_task_events) {
2007 script->tool.comm = process_comm_event;
2008 script->tool.fork = process_fork_event;
2009 script->tool.exit = process_exit_event;
2010 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002011 if (script->show_mmap_events) {
2012 script->tool.mmap = process_mmap_event;
2013 script->tool.mmap2 = process_mmap2_event;
2014 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002015 if (script->show_switch_events)
2016 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302017 if (script->show_namespace_events)
2018 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002019
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002020 if (perf_script__setup_per_event_dump(script)) {
2021 pr_err("Couldn't create the per event dump files\n");
2022 return -1;
2023 }
2024
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002025 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002026
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002027 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002028 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002029
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002030 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002031 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002032
2033 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002034}
2035
Tom Zanussi956ffd02009-11-25 01:15:46 -06002036struct script_spec {
2037 struct list_head node;
2038 struct scripting_ops *ops;
2039 char spec[0];
2040};
2041
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002042static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002043
2044static struct script_spec *script_spec__new(const char *spec,
2045 struct scripting_ops *ops)
2046{
2047 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2048
2049 if (s != NULL) {
2050 strcpy(s->spec, spec);
2051 s->ops = ops;
2052 }
2053
2054 return s;
2055}
2056
Tom Zanussi956ffd02009-11-25 01:15:46 -06002057static void script_spec__add(struct script_spec *s)
2058{
2059 list_add_tail(&s->node, &script_specs);
2060}
2061
2062static struct script_spec *script_spec__find(const char *spec)
2063{
2064 struct script_spec *s;
2065
2066 list_for_each_entry(s, &script_specs, node)
2067 if (strcasecmp(s->spec, spec) == 0)
2068 return s;
2069 return NULL;
2070}
2071
Tom Zanussi956ffd02009-11-25 01:15:46 -06002072int script_spec_register(const char *spec, struct scripting_ops *ops)
2073{
2074 struct script_spec *s;
2075
2076 s = script_spec__find(spec);
2077 if (s)
2078 return -1;
2079
Taeung Song8560bae2016-02-26 00:13:10 +09002080 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002081 if (!s)
2082 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002083 else
2084 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002085
2086 return 0;
2087}
2088
2089static struct scripting_ops *script_spec__lookup(const char *spec)
2090{
2091 struct script_spec *s = script_spec__find(spec);
2092 if (!s)
2093 return NULL;
2094
2095 return s->ops;
2096}
2097
2098static void list_available_languages(void)
2099{
2100 struct script_spec *s;
2101
2102 fprintf(stderr, "\n");
2103 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002104 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002105
2106 list_for_each_entry(s, &script_specs, node)
2107 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2108
2109 fprintf(stderr, "\n");
2110}
2111
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002112static int parse_scriptname(const struct option *opt __maybe_unused,
2113 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002114{
2115 char spec[PATH_MAX];
2116 const char *script, *ext;
2117 int len;
2118
Tom Zanussif526d682010-01-27 02:27:52 -06002119 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002120 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002121 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002122 }
2123
2124 script = strchr(str, ':');
2125 if (script) {
2126 len = script - str;
2127 if (len >= PATH_MAX) {
2128 fprintf(stderr, "invalid language specifier");
2129 return -1;
2130 }
2131 strncpy(spec, str, len);
2132 spec[len] = '\0';
2133 scripting_ops = script_spec__lookup(spec);
2134 if (!scripting_ops) {
2135 fprintf(stderr, "invalid language specifier");
2136 return -1;
2137 }
2138 script++;
2139 } else {
2140 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002141 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002142 if (!ext) {
2143 fprintf(stderr, "invalid script extension");
2144 return -1;
2145 }
2146 scripting_ops = script_spec__lookup(++ext);
2147 if (!scripting_ops) {
2148 fprintf(stderr, "invalid script extension");
2149 return -1;
2150 }
2151 }
2152
2153 script_name = strdup(script);
2154
2155 return 0;
2156}
2157
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002158static int parse_output_fields(const struct option *opt __maybe_unused,
2159 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002160{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002161 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002162 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002163 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002164 int rc = 0;
2165 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002166 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002167 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002168
2169 if (!str)
2170 return -ENOMEM;
2171
David Ahern2c9e45f72011-03-17 10:03:21 -06002172 /* first word can state for which event type the user is specifying
2173 * the fields. If no type exists, the specified fields apply to all
2174 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002175 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002176 tok = strchr(str, ':');
2177 if (tok) {
2178 *tok = '\0';
2179 tok++;
2180 if (!strcmp(str, "hw"))
2181 type = PERF_TYPE_HARDWARE;
2182 else if (!strcmp(str, "sw"))
2183 type = PERF_TYPE_SOFTWARE;
2184 else if (!strcmp(str, "trace"))
2185 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002186 else if (!strcmp(str, "raw"))
2187 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002188 else if (!strcmp(str, "break"))
2189 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002190 else if (!strcmp(str, "synth"))
2191 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002192 else {
2193 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002194 rc = -EINVAL;
2195 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002196 }
2197
2198 if (output[type].user_set)
2199 pr_warning("Overriding previous field request for %s events.\n",
2200 event_type(type));
2201
2202 output[type].fields = 0;
2203 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002204 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002205
2206 } else {
2207 tok = str;
2208 if (strlen(str) == 0) {
2209 fprintf(stderr,
2210 "Cannot set fields to 'none' for all event types.\n");
2211 rc = -EINVAL;
2212 goto out;
2213 }
2214
Andi Kleen36ce5652017-06-02 08:48:10 -07002215 /* Don't override defaults for +- */
2216 if (strchr(str, '+') || strchr(str, '-'))
2217 goto parse;
2218
David Ahern2c9e45f72011-03-17 10:03:21 -06002219 if (output_set_by_user())
2220 pr_warning("Overriding previous field request for all events.\n");
2221
Adrian Hunter14057202017-06-21 13:17:19 +03002222 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002223 output[j].fields = 0;
2224 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002225 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002226 }
David Ahern1424dc92011-03-09 22:23:28 -07002227 }
2228
Andi Kleen36ce5652017-06-02 08:48:10 -07002229parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002230 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002231 if (*tok == '+') {
2232 if (change == SET)
2233 goto out_badmix;
2234 change = ADD;
2235 tok++;
2236 } else if (*tok == '-') {
2237 if (change == SET)
2238 goto out_badmix;
2239 change = REMOVE;
2240 tok++;
2241 } else {
2242 if (change != SET && change != DEFAULT)
2243 goto out_badmix;
2244 change = SET;
2245 }
2246
David Ahern745f43e2011-03-09 22:23:26 -07002247 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002248 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002249 break;
David Ahern745f43e2011-03-09 22:23:26 -07002250 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002251 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002252 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002253 continue;
2254 }
David Ahern745f43e2011-03-09 22:23:26 -07002255 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002256 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002257 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002258 goto out;
2259 }
2260
2261 if (type == -1) {
2262 /* add user option to all events types for
2263 * which it is valid
2264 */
Adrian Hunter14057202017-06-21 13:17:19 +03002265 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002266 if (output[j].invalid_fields & all_output_options[i].field) {
2267 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2268 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002269 } else {
2270 if (change == REMOVE)
2271 output[j].fields &= ~all_output_options[i].field;
2272 else
2273 output[j].fields |= all_output_options[i].field;
2274 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002275 }
2276 } else {
2277 if (output[type].invalid_fields & all_output_options[i].field) {
2278 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2279 all_output_options[i].str, event_type(type));
2280
2281 rc = -EINVAL;
2282 goto out;
2283 }
2284 output[type].fields |= all_output_options[i].field;
2285 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002286 }
2287
2288 if (type >= 0) {
2289 if (output[type].fields == 0) {
2290 pr_debug("No fields requested for %s type. "
2291 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002292 }
David Ahern1424dc92011-03-09 22:23:28 -07002293 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002294 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002295
Andi Kleen36ce5652017-06-02 08:48:10 -07002296out_badmix:
2297 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2298 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002299out:
David Ahern745f43e2011-03-09 22:23:26 -07002300 free(str);
2301 return rc;
2302}
2303
Shawn Bohrer008f29d2010-11-21 10:09:39 -06002304/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
2305static int is_directory(const char *base_path, const struct dirent *dent)
2306{
2307 char path[PATH_MAX];
2308 struct stat st;
2309
2310 sprintf(path, "%s/%s", base_path, dent->d_name);
2311 if (stat(path, &st))
2312 return 0;
2313
2314 return S_ISDIR(st.st_mode);
2315}
2316
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002317#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2318 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2319 if ((lang_dirent->d_type == DT_DIR || \
2320 (lang_dirent->d_type == DT_UNKNOWN && \
2321 is_directory(scripts_path, lang_dirent))) && \
2322 (strcmp(lang_dirent->d_name, ".")) && \
2323 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002324
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002325#define for_each_script(lang_path, lang_dir, script_dirent) \
2326 while ((script_dirent = readdir(lang_dir)) != NULL) \
2327 if (script_dirent->d_type != DT_DIR && \
2328 (script_dirent->d_type != DT_UNKNOWN || \
2329 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002330
2331
2332#define RECORD_SUFFIX "-record"
2333#define REPORT_SUFFIX "-report"
2334
2335struct script_desc {
2336 struct list_head node;
2337 char *name;
2338 char *half_liner;
2339 char *args;
2340};
2341
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002342static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002343
2344static struct script_desc *script_desc__new(const char *name)
2345{
2346 struct script_desc *s = zalloc(sizeof(*s));
2347
Tom Zanussib5b87312010-11-10 08:16:51 -06002348 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002349 s->name = strdup(name);
2350
2351 return s;
2352}
2353
2354static void script_desc__delete(struct script_desc *s)
2355{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002356 zfree(&s->name);
2357 zfree(&s->half_liner);
2358 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002359 free(s);
2360}
2361
2362static void script_desc__add(struct script_desc *s)
2363{
2364 list_add_tail(&s->node, &script_descs);
2365}
2366
2367static struct script_desc *script_desc__find(const char *name)
2368{
2369 struct script_desc *s;
2370
2371 list_for_each_entry(s, &script_descs, node)
2372 if (strcasecmp(s->name, name) == 0)
2373 return s;
2374 return NULL;
2375}
2376
2377static struct script_desc *script_desc__findnew(const char *name)
2378{
2379 struct script_desc *s = script_desc__find(name);
2380
2381 if (s)
2382 return s;
2383
2384 s = script_desc__new(name);
2385 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002386 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002387
2388 script_desc__add(s);
2389
2390 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002391}
2392
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002393static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002394{
2395 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002396 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002397
2398 if (strlen(str) > suffix_len) {
2399 p = str + strlen(str) - suffix_len;
2400 if (!strncmp(p, suffix, suffix_len))
2401 return p;
2402 }
2403
2404 return NULL;
2405}
2406
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002407static int read_script_info(struct script_desc *desc, const char *filename)
2408{
2409 char line[BUFSIZ], *p;
2410 FILE *fp;
2411
2412 fp = fopen(filename, "r");
2413 if (!fp)
2414 return -1;
2415
2416 while (fgets(line, sizeof(line), fp)) {
2417 p = ltrim(line);
2418 if (strlen(p) == 0)
2419 continue;
2420 if (*p != '#')
2421 continue;
2422 p++;
2423 if (strlen(p) && *p == '!')
2424 continue;
2425
2426 p = ltrim(p);
2427 if (strlen(p) && p[strlen(p) - 1] == '\n')
2428 p[strlen(p) - 1] = '\0';
2429
2430 if (!strncmp(p, "description:", strlen("description:"))) {
2431 p += strlen("description:");
2432 desc->half_liner = strdup(ltrim(p));
2433 continue;
2434 }
2435
2436 if (!strncmp(p, "args:", strlen("args:"))) {
2437 p += strlen("args:");
2438 desc->args = strdup(ltrim(p));
2439 continue;
2440 }
2441 }
2442
2443 fclose(fp);
2444
2445 return 0;
2446}
2447
Robert Richter38efb532011-11-25 11:38:40 +01002448static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2449{
2450 char *script_root, *str;
2451
2452 script_root = strdup(script_dirent->d_name);
2453 if (!script_root)
2454 return NULL;
2455
2456 str = (char *)ends_with(script_root, suffix);
2457 if (!str) {
2458 free(script_root);
2459 return NULL;
2460 }
2461
2462 *str = '\0';
2463 return script_root;
2464}
2465
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002466static int list_available_scripts(const struct option *opt __maybe_unused,
2467 const char *s __maybe_unused,
2468 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002469{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002470 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002471 char scripts_path[MAXPATHLEN];
2472 DIR *scripts_dir, *lang_dir;
2473 char script_path[MAXPATHLEN];
2474 char lang_path[MAXPATHLEN];
2475 struct script_desc *desc;
2476 char first_half[BUFSIZ];
2477 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002478
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002479 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002480
2481 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002482 if (!scripts_dir) {
2483 fprintf(stdout,
2484 "open(%s) failed.\n"
2485 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2486 scripts_path);
2487 exit(-1);
2488 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002489
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002490 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002491 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002492 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002493 lang_dir = opendir(lang_path);
2494 if (!lang_dir)
2495 continue;
2496
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002497 for_each_script(lang_path, lang_dir, script_dirent) {
2498 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002499 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002500 desc = script_desc__findnew(script_root);
2501 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002502 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002503 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002504 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002505 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002506 }
2507 }
2508
2509 fprintf(stdout, "List of available trace scripts:\n");
2510 list_for_each_entry(desc, &script_descs, node) {
2511 sprintf(first_half, "%s %s", desc->name,
2512 desc->args ? desc->args : "");
2513 fprintf(stdout, " %-36s %s\n", first_half,
2514 desc->half_liner ? desc->half_liner : "");
2515 }
2516
2517 exit(0);
2518}
2519
Feng Tange5f37052012-09-07 16:42:26 +08002520/*
Feng Tang49e639e2012-10-30 11:56:03 +08002521 * Some scripts specify the required events in their "xxx-record" file,
2522 * this function will check if the events in perf.data match those
2523 * mentioned in the "xxx-record".
2524 *
2525 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2526 * which is covered well now. And new parsing code should be added to
2527 * cover the future complexing formats like event groups etc.
2528 */
2529static int check_ev_match(char *dir_name, char *scriptname,
2530 struct perf_session *session)
2531{
2532 char filename[MAXPATHLEN], evname[128];
2533 char line[BUFSIZ], *p;
2534 struct perf_evsel *pos;
2535 int match, len;
2536 FILE *fp;
2537
2538 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2539
2540 fp = fopen(filename, "r");
2541 if (!fp)
2542 return -1;
2543
2544 while (fgets(line, sizeof(line), fp)) {
2545 p = ltrim(line);
2546 if (*p == '#')
2547 continue;
2548
2549 while (strlen(p)) {
2550 p = strstr(p, "-e");
2551 if (!p)
2552 break;
2553
2554 p += 2;
2555 p = ltrim(p);
2556 len = strcspn(p, " \t");
2557 if (!len)
2558 break;
2559
2560 snprintf(evname, len + 1, "%s", p);
2561
2562 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002563 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002564 if (!strcmp(perf_evsel__name(pos), evname)) {
2565 match = 1;
2566 break;
2567 }
2568 }
2569
2570 if (!match) {
2571 fclose(fp);
2572 return -1;
2573 }
2574 }
2575 }
2576
2577 fclose(fp);
2578 return 0;
2579}
2580
2581/*
Feng Tange5f37052012-09-07 16:42:26 +08002582 * Return -1 if none is found, otherwise the actual scripts number.
2583 *
2584 * Currently the only user of this function is the script browser, which
2585 * will list all statically runnable scripts, select one, execute it and
2586 * show the output in a perf browser.
2587 */
2588int find_scripts(char **scripts_array, char **scripts_path_array)
2589{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002590 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002591 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002592 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002593 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002594 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002595 .file = {
2596 .path = input_name,
2597 },
2598 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002599 };
Feng Tange5f37052012-09-07 16:42:26 +08002600 char *temp;
2601 int i = 0;
2602
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002603 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002604 if (!session)
2605 return -1;
2606
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002607 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002608
2609 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002610 if (!scripts_dir) {
2611 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002612 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002613 }
Feng Tange5f37052012-09-07 16:42:26 +08002614
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002615 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002616 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002617 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002618#ifdef NO_LIBPERL
2619 if (strstr(lang_path, "perl"))
2620 continue;
2621#endif
2622#ifdef NO_LIBPYTHON
2623 if (strstr(lang_path, "python"))
2624 continue;
2625#endif
2626
2627 lang_dir = opendir(lang_path);
2628 if (!lang_dir)
2629 continue;
2630
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002631 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002632 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002633 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002634 continue;
2635 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002636 script_dirent->d_name);
2637 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002638 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002639 (temp - script_dirent->d_name) + 1,
2640 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002641
2642 if (check_ev_match(lang_path,
2643 scripts_array[i], session))
2644 continue;
2645
Feng Tange5f37052012-09-07 16:42:26 +08002646 i++;
2647 }
Feng Tang49e639e2012-10-30 11:56:03 +08002648 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002649 }
2650
Feng Tang49e639e2012-10-30 11:56:03 +08002651 closedir(scripts_dir);
2652 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002653 return i;
2654}
2655
Tom Zanussi38752942009-12-15 02:53:39 -06002656static char *get_script_path(const char *script_root, const char *suffix)
2657{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002658 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002659 char scripts_path[MAXPATHLEN];
2660 char script_path[MAXPATHLEN];
2661 DIR *scripts_dir, *lang_dir;
2662 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002663 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002664
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002665 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002666
2667 scripts_dir = opendir(scripts_path);
2668 if (!scripts_dir)
2669 return NULL;
2670
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002671 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002672 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002673 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002674 lang_dir = opendir(lang_path);
2675 if (!lang_dir)
2676 continue;
2677
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002678 for_each_script(lang_path, lang_dir, script_dirent) {
2679 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002680 if (__script_root && !strcmp(script_root, __script_root)) {
2681 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002682 closedir(lang_dir);
2683 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002684 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002685 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002686 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002687 }
2688 free(__script_root);
2689 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002690 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002691 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002692 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002693
Robert Richter38efb532011-11-25 11:38:40 +01002694 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002695}
2696
Tom Zanussib5b87312010-11-10 08:16:51 -06002697static bool is_top_script(const char *script_path)
2698{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002699 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002700}
2701
2702static int has_required_arg(char *script_path)
2703{
2704 struct script_desc *desc;
2705 int n_args = 0;
2706 char *p;
2707
2708 desc = script_desc__new(NULL);
2709
2710 if (read_script_info(desc, script_path))
2711 goto out;
2712
2713 if (!desc->args)
2714 goto out;
2715
2716 for (p = desc->args; *p; p++)
2717 if (*p == '<')
2718 n_args++;
2719out:
2720 script_desc__delete(desc);
2721
2722 return n_args;
2723}
2724
David Ahernd54b1a92012-08-26 12:24:46 -06002725static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002726{
2727 char **__argv = malloc(sizeof(const char *) * argc);
2728
David Ahernd54b1a92012-08-26 12:24:46 -06002729 if (!__argv) {
2730 pr_err("malloc failed\n");
2731 return -1;
2732 }
2733
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002734 memcpy(__argv, argv, sizeof(const char *) * argc);
2735 argc = parse_options(argc, (const char **)__argv, record_options,
2736 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2737 free(__argv);
2738
David Ahernd54b1a92012-08-26 12:24:46 -06002739 system_wide = (argc == 0);
2740
2741 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002742}
2743
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002744static void script__setup_sample_type(struct perf_script *script)
2745{
2746 struct perf_session *session = script->session;
2747 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2748
2749 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2750 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2751 (sample_type & PERF_SAMPLE_STACK_USER))
2752 callchain_param.record_mode = CALLCHAIN_DWARF;
2753 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2754 callchain_param.record_mode = CALLCHAIN_LBR;
2755 else
2756 callchain_param.record_mode = CALLCHAIN_FP;
2757 }
2758}
2759
Jiri Olsae099eba2016-01-05 22:09:09 +01002760static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2761 union perf_event *event,
2762 struct perf_session *session)
2763{
2764 struct stat_round_event *round = &event->stat_round;
2765 struct perf_evsel *counter;
2766
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002767 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002768 perf_stat_process_counter(&stat_config, counter);
2769 process_stat(counter, round->time);
2770 }
2771
2772 process_stat_interval(round->time);
2773 return 0;
2774}
2775
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002776static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2777 union perf_event *event,
2778 struct perf_session *session __maybe_unused)
2779{
2780 perf_event__read_stat_config(&stat_config, &event->stat_config);
2781 return 0;
2782}
2783
Jiri Olsacfc88742016-01-05 22:09:06 +01002784static int set_maps(struct perf_script *script)
2785{
2786 struct perf_evlist *evlist = script->session->evlist;
2787
2788 if (!script->cpus || !script->threads)
2789 return 0;
2790
2791 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2792 return -EINVAL;
2793
2794 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2795
2796 if (perf_evlist__alloc_stats(evlist, true))
2797 return -ENOMEM;
2798
2799 script->allocated = true;
2800 return 0;
2801}
2802
2803static
2804int process_thread_map_event(struct perf_tool *tool,
2805 union perf_event *event,
2806 struct perf_session *session __maybe_unused)
2807{
2808 struct perf_script *script = container_of(tool, struct perf_script, tool);
2809
2810 if (script->threads) {
2811 pr_warning("Extra thread map event, ignoring.\n");
2812 return 0;
2813 }
2814
2815 script->threads = thread_map__new_event(&event->thread_map);
2816 if (!script->threads)
2817 return -ENOMEM;
2818
2819 return set_maps(script);
2820}
2821
2822static
2823int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2824 union perf_event *event,
2825 struct perf_session *session __maybe_unused)
2826{
2827 struct perf_script *script = container_of(tool, struct perf_script, tool);
2828
2829 if (script->cpus) {
2830 pr_warning("Extra cpu map event, ignoring.\n");
2831 return 0;
2832 }
2833
2834 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2835 if (!script->cpus)
2836 return -ENOMEM;
2837
2838 return set_maps(script);
2839}
2840
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002841int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002842{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002843 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002844 bool header = false;
2845 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002846 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002847 char *rec_script_path = NULL;
2848 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002849 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002850 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002851 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002852 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002853 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002854 struct perf_script script = {
2855 .tool = {
2856 .sample = process_sample_event,
2857 .mmap = perf_event__process_mmap,
2858 .mmap2 = perf_event__process_mmap2,
2859 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302860 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002861 .exit = perf_event__process_exit,
2862 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002863 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002864 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002865 .tracing_data = perf_event__process_tracing_data,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07002866 .feature = perf_event__process_feature,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002867 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002868 .id_index = perf_event__process_id_index,
2869 .auxtrace_info = perf_event__process_auxtrace_info,
2870 .auxtrace = perf_event__process_auxtrace,
2871 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002872 .stat = perf_event__process_stat_event,
2873 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002874 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002875 .thread_map = process_thread_map_event,
2876 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002877 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002878 .ordering_requires_timestamps = true,
2879 },
2880 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002881 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08002882 .mode = PERF_DATA_MODE_READ,
2883 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002884 const struct option options[] = {
2885 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2886 "dump raw trace in ASCII"),
2887 OPT_INCR('v', "verbose", &verbose,
2888 "be more verbose (show symbol address, etc)"),
2889 OPT_BOOLEAN('L', "Latency", &latency_format,
2890 "show latency attributes (irqs/preemption disabled, etc)"),
2891 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2892 list_available_scripts),
2893 OPT_CALLBACK('s', "script", NULL, "name",
2894 "script file name (lang:script name, script name, or *)",
2895 parse_scriptname),
2896 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2897 "generate perf-script.xx script in specified language"),
2898 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2899 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2900 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002901 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2902 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002903 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2904 "file", "vmlinux pathname"),
2905 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2906 "file", "kallsyms pathname"),
2907 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2908 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002909 OPT_CALLBACK(0, "symfs", NULL, "directory",
2910 "Look for files with symbols relative to this directory",
2911 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002912 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002913 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07002914 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03002915 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002916 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Andi Kleenb1491ac2017-09-05 11:40:57 -07002917 "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
Kan Liang49d58f02017-08-29 13:11:11 -04002918 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
Andi Kleen48d02a12017-02-23 15:46:34 -08002919 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002920 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2921 "system-wide collection from all CPUs"),
2922 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2923 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002924 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2925 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002926 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2927 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2928 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002929 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2930 "only consider symbols in these pids"),
2931 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2932 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002933 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2934 "Set the maximum stack depth when parsing the callchain, "
2935 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002936 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002937 OPT_BOOLEAN('I', "show-info", &show_full_info,
2938 "display extended information from perf.data file"),
2939 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2940 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002941 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2942 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002943 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2944 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002945 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2946 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302947 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2948 "Show namespace events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002949 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
2950 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002951 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002952 OPT_INTEGER(0, "max-blocks", &max_blocks,
2953 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002954 OPT_BOOLEAN(0, "ns", &nanosecs,
2955 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002956 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2957 "Instruction Tracing options",
2958 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002959 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2960 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002961 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2962 "Enable symbol demangling"),
2963 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2964 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002965 OPT_STRING(0, "time", &script.time_str, "str",
2966 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09002967 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2968 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002969 OPT_END()
2970 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002971 const char * const script_subcommands[] = { "record", "report", NULL };
2972 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002973 "perf script [<options>]",
2974 "perf script [<options>] record <script> [<record-options>] <command>",
2975 "perf script [<options>] report <script> [script-args]",
2976 "perf script [<options>] <script> [<record-options>] <command>",
2977 "perf script [<options>] <top-script> [script-args]",
2978 NULL
2979 };
Tom Zanussi38752942009-12-15 02:53:39 -06002980
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03002981 perf_set_singlethreaded();
2982
Tom Zanussib5b87312010-11-10 08:16:51 -06002983 setup_scripting();
2984
Yunlong Song40cae2b2015-03-18 21:35:54 +08002985 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002986 PARSE_OPT_STOP_AT_NON_OPTION);
2987
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002988 data.file.path = input_name;
2989 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002990
Tom Zanussib5b87312010-11-10 08:16:51 -06002991 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2992 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2993 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002994 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002995 }
2996
Tom Zanussib5b87312010-11-10 08:16:51 -06002997 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2998 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2999 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003000 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003001 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003002 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003003 return -1;
3004 }
Tom Zanussi38752942009-12-15 02:53:39 -06003005 }
3006
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003007 if (itrace_synth_opts.callchain &&
3008 itrace_synth_opts.callchain_sz > scripting_max_stack)
3009 scripting_max_stack = itrace_synth_opts.callchain_sz;
3010
Ben Hutchings44e668c2010-10-10 16:10:03 +01003011 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003012 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003013
Tom Zanussib5b87312010-11-10 08:16:51 -06003014 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003015 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003016 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003017 pid_t pid;
3018
Tom Zanussib5b87312010-11-10 08:16:51 -06003019 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3020 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3021
3022 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003023 usage_with_options_msg(script_usage, options,
3024 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003025 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003026 }
3027
Tom Zanussib5b87312010-11-10 08:16:51 -06003028 if (is_top_script(argv[0])) {
3029 rep_args = argc - 1;
3030 } else {
3031 int rec_args;
3032
3033 rep_args = has_required_arg(rep_script_path);
3034 rec_args = (argc - 1) - rep_args;
3035 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003036 usage_with_options_msg(script_usage, options,
3037 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003038 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003039 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003040 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003041 }
3042
3043 if (pipe(live_pipe) < 0) {
3044 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003045 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003046 }
3047
3048 pid = fork();
3049 if (pid < 0) {
3050 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003051 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003052 }
3053
3054 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003055 j = 0;
3056
Tom Zanussia0cccc22010-04-01 23:59:25 -05003057 dup2(live_pipe[1], 1);
3058 close(live_pipe[0]);
3059
Robert Richter317df652011-11-25 15:05:25 +01003060 if (is_top_script(argv[0])) {
3061 system_wide = true;
3062 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003063 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3064 err = -1;
3065 goto out;
3066 }
Robert Richter317df652011-11-25 15:05:25 +01003067 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003068
3069 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003070 if (!__argv) {
3071 pr_err("malloc failed\n");
3072 err = -ENOMEM;
3073 goto out;
3074 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003075
Tom Zanussib5b87312010-11-10 08:16:51 -06003076 __argv[j++] = "/bin/sh";
3077 __argv[j++] = rec_script_path;
3078 if (system_wide)
3079 __argv[j++] = "-a";
3080 __argv[j++] = "-q";
3081 __argv[j++] = "-o";
3082 __argv[j++] = "-";
3083 for (i = rep_args + 1; i < argc; i++)
3084 __argv[j++] = argv[i];
3085 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003086
3087 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003088 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003089 exit(-1);
3090 }
3091
3092 dup2(live_pipe[0], 0);
3093 close(live_pipe[1]);
3094
Tom Zanussib5b87312010-11-10 08:16:51 -06003095 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003096 if (!__argv) {
3097 pr_err("malloc failed\n");
3098 err = -ENOMEM;
3099 goto out;
3100 }
3101
Tom Zanussib5b87312010-11-10 08:16:51 -06003102 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003103 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003104 __argv[j++] = rep_script_path;
3105 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003106 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003107 __argv[j++] = "-i";
3108 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003109 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003110
3111 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003112 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003113 exit(-1);
3114 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003115
Tom Zanussib5b87312010-11-10 08:16:51 -06003116 if (rec_script_path)
3117 script_path = rec_script_path;
3118 if (rep_script_path)
3119 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003120
Tom Zanussib5b87312010-11-10 08:16:51 -06003121 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003122 j = 0;
3123
Robert Richter317df652011-11-25 15:05:25 +01003124 if (!rec_script_path)
3125 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003126 else if (!system_wide) {
3127 if (have_cmd(argc - 1, &argv[1]) != 0) {
3128 err = -1;
3129 goto out;
3130 }
3131 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003132
3133 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003134 if (!__argv) {
3135 pr_err("malloc failed\n");
3136 err = -ENOMEM;
3137 goto out;
3138 }
3139
Tom Zanussib5b87312010-11-10 08:16:51 -06003140 __argv[j++] = "/bin/sh";
3141 __argv[j++] = script_path;
3142 if (system_wide)
3143 __argv[j++] = "-a";
3144 for (i = 2; i < argc; i++)
3145 __argv[j++] = argv[i];
3146 __argv[j++] = NULL;
3147
3148 execvp("/bin/sh", (char **)__argv);
3149 free(__argv);
3150 exit(-1);
3151 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003152
Tom Zanussicf4fee52010-03-03 01:04:33 -06003153 if (!script_name)
3154 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003155
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003156 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003157 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003158 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003159
Jiri Olsae90debd2013-12-09 11:02:50 +01003160 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003161 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003162 perf_session__fprintf_info(session, stdout, show_full_info);
3163 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003164 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003165 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003166 if (show_full_info)
3167 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003168
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003169 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003170 goto out_delete;
3171
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003172 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003173 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003174
Adrian Huntere2167082016-06-23 16:40:58 +03003175 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3176 itrace_synth_opts.thread_stack = true;
3177
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003178 session->itrace_synth_opts = &itrace_synth_opts;
3179
Anton Blanchard5d67be92011-07-04 21:57:50 +10003180 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003181 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3182 if (err < 0)
3183 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003184 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003185 }
3186
David Ahern1424dc92011-03-09 22:23:28 -07003187 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003188 symbol_conf.use_callchain = true;
3189 else
3190 symbol_conf.use_callchain = false;
3191
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003192 if (session->tevent.pevent &&
3193 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003194 machine__resolve_kernel_addr,
3195 &session->machines.host) < 0) {
3196 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003197 err = -1;
3198 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003199 }
3200
Tom Zanussi956ffd02009-11-25 01:15:46 -06003201 if (generate_script_lang) {
3202 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003203 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003204
David Ahern2c9e45f72011-03-17 10:03:21 -06003205 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003206 fprintf(stderr,
3207 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003208 err = -EINVAL;
3209 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003210 }
3211
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003212 input = open(data.file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003213 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003214 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003215 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003216 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003217 }
3218
3219 err = fstat(input, &perf_stat);
3220 if (err < 0) {
3221 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003222 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003223 }
3224
3225 if (!perf_stat.st_size) {
3226 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003227 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003228 }
3229
3230 scripting_ops = script_spec__lookup(generate_script_lang);
3231 if (!scripting_ops) {
3232 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003233 err = -ENOENT;
3234 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003235 }
3236
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003237 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003238 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003239 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003240 }
3241
3242 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003243 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003244 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003245 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003246 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003247 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003248 }
3249
David Ahern9cbdb702011-04-06 21:54:20 -06003250
3251 err = perf_session__check_output_opt(session);
3252 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003253 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003254
David Aherna91f4c42016-11-29 10:15:43 -07003255 /* needs to be parsed after looking up reference time */
3256 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
3257 pr_err("Invalid time string\n");
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003258 err = -EINVAL;
3259 goto out_delete;
David Aherna91f4c42016-11-29 10:15:43 -07003260 }
3261
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003262 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003263
Adrian Hunterd445dd22014-08-15 22:08:37 +03003264 flush_scripting();
3265
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003266out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01003267 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003268 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003269
3270 if (script_started)
3271 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003272out:
3273 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003274}