blob: 9b43bda45a415649a06c7b9743796bff7b760dbe [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) {
Arnaldo Carvalho de Melo501e5bb2017-11-09 16:04:26 -03001958 /*
1959 * Already setup? I.e. we may be called twice in cases like
1960 * Intel PT, one for the intel_pt// and dummy events, then
1961 * for the evsels syntheized from the auxtrace info.
1962 *
1963 * Ses perf_script__process_auxtrace_info.
1964 */
1965 if (evsel->priv != NULL)
1966 continue;
1967
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001968 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001969 if (evsel->priv == NULL)
1970 goto out_err_fclose;
1971 }
1972
1973 return 0;
1974
1975out_err_fclose:
1976 perf_script__fclose_per_event_dump(script);
1977 return -1;
1978}
1979
1980static int perf_script__setup_per_event_dump(struct perf_script *script)
1981{
1982 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001983 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001984
1985 if (script->per_event_dump)
1986 return perf_script__fopen_per_event_dump(script);
1987
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001988 es_stdout.fp = stdout;
1989
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001990 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001991 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001992
1993 return 0;
1994}
1995
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001996static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
1997{
1998 struct perf_evsel *evsel;
1999
2000 evlist__for_each_entry(script->session->evlist, evsel) {
2001 struct perf_evsel_script *es = evsel->priv;
2002
2003 perf_evsel_script__fprintf(es, stdout);
2004 perf_evsel_script__delete(es);
2005 evsel->priv = NULL;
2006 }
2007}
2008
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002009static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002010{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002011 int ret;
2012
Tom Zanussic239da32010-04-01 23:59:18 -05002013 signal(SIGINT, sig_handler);
2014
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002015 /* override event processing functions */
2016 if (script->show_task_events) {
2017 script->tool.comm = process_comm_event;
2018 script->tool.fork = process_fork_event;
2019 script->tool.exit = process_exit_event;
2020 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002021 if (script->show_mmap_events) {
2022 script->tool.mmap = process_mmap_event;
2023 script->tool.mmap2 = process_mmap2_event;
2024 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002025 if (script->show_switch_events)
2026 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302027 if (script->show_namespace_events)
2028 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002029
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002030 if (perf_script__setup_per_event_dump(script)) {
2031 pr_err("Couldn't create the per event dump files\n");
2032 return -1;
2033 }
2034
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002035 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002036
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002037 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002038 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002039
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002040 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002041 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002042
2043 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002044}
2045
Tom Zanussi956ffd02009-11-25 01:15:46 -06002046struct script_spec {
2047 struct list_head node;
2048 struct scripting_ops *ops;
2049 char spec[0];
2050};
2051
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002052static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002053
2054static struct script_spec *script_spec__new(const char *spec,
2055 struct scripting_ops *ops)
2056{
2057 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2058
2059 if (s != NULL) {
2060 strcpy(s->spec, spec);
2061 s->ops = ops;
2062 }
2063
2064 return s;
2065}
2066
Tom Zanussi956ffd02009-11-25 01:15:46 -06002067static void script_spec__add(struct script_spec *s)
2068{
2069 list_add_tail(&s->node, &script_specs);
2070}
2071
2072static struct script_spec *script_spec__find(const char *spec)
2073{
2074 struct script_spec *s;
2075
2076 list_for_each_entry(s, &script_specs, node)
2077 if (strcasecmp(s->spec, spec) == 0)
2078 return s;
2079 return NULL;
2080}
2081
Tom Zanussi956ffd02009-11-25 01:15:46 -06002082int script_spec_register(const char *spec, struct scripting_ops *ops)
2083{
2084 struct script_spec *s;
2085
2086 s = script_spec__find(spec);
2087 if (s)
2088 return -1;
2089
Taeung Song8560bae2016-02-26 00:13:10 +09002090 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002091 if (!s)
2092 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002093 else
2094 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002095
2096 return 0;
2097}
2098
2099static struct scripting_ops *script_spec__lookup(const char *spec)
2100{
2101 struct script_spec *s = script_spec__find(spec);
2102 if (!s)
2103 return NULL;
2104
2105 return s->ops;
2106}
2107
2108static void list_available_languages(void)
2109{
2110 struct script_spec *s;
2111
2112 fprintf(stderr, "\n");
2113 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002114 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002115
2116 list_for_each_entry(s, &script_specs, node)
2117 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2118
2119 fprintf(stderr, "\n");
2120}
2121
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002122static int parse_scriptname(const struct option *opt __maybe_unused,
2123 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002124{
2125 char spec[PATH_MAX];
2126 const char *script, *ext;
2127 int len;
2128
Tom Zanussif526d682010-01-27 02:27:52 -06002129 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002130 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002131 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002132 }
2133
2134 script = strchr(str, ':');
2135 if (script) {
2136 len = script - str;
2137 if (len >= PATH_MAX) {
2138 fprintf(stderr, "invalid language specifier");
2139 return -1;
2140 }
2141 strncpy(spec, str, len);
2142 spec[len] = '\0';
2143 scripting_ops = script_spec__lookup(spec);
2144 if (!scripting_ops) {
2145 fprintf(stderr, "invalid language specifier");
2146 return -1;
2147 }
2148 script++;
2149 } else {
2150 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002151 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002152 if (!ext) {
2153 fprintf(stderr, "invalid script extension");
2154 return -1;
2155 }
2156 scripting_ops = script_spec__lookup(++ext);
2157 if (!scripting_ops) {
2158 fprintf(stderr, "invalid script extension");
2159 return -1;
2160 }
2161 }
2162
2163 script_name = strdup(script);
2164
2165 return 0;
2166}
2167
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002168static int parse_output_fields(const struct option *opt __maybe_unused,
2169 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002170{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002171 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002172 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002173 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002174 int rc = 0;
2175 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002176 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002177 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002178
2179 if (!str)
2180 return -ENOMEM;
2181
David Ahern2c9e45f72011-03-17 10:03:21 -06002182 /* first word can state for which event type the user is specifying
2183 * the fields. If no type exists, the specified fields apply to all
2184 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002185 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002186 tok = strchr(str, ':');
2187 if (tok) {
2188 *tok = '\0';
2189 tok++;
2190 if (!strcmp(str, "hw"))
2191 type = PERF_TYPE_HARDWARE;
2192 else if (!strcmp(str, "sw"))
2193 type = PERF_TYPE_SOFTWARE;
2194 else if (!strcmp(str, "trace"))
2195 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002196 else if (!strcmp(str, "raw"))
2197 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002198 else if (!strcmp(str, "break"))
2199 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002200 else if (!strcmp(str, "synth"))
2201 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002202 else {
2203 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002204 rc = -EINVAL;
2205 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002206 }
2207
2208 if (output[type].user_set)
2209 pr_warning("Overriding previous field request for %s events.\n",
2210 event_type(type));
2211
2212 output[type].fields = 0;
2213 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002214 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002215
2216 } else {
2217 tok = str;
2218 if (strlen(str) == 0) {
2219 fprintf(stderr,
2220 "Cannot set fields to 'none' for all event types.\n");
2221 rc = -EINVAL;
2222 goto out;
2223 }
2224
Andi Kleen36ce5652017-06-02 08:48:10 -07002225 /* Don't override defaults for +- */
2226 if (strchr(str, '+') || strchr(str, '-'))
2227 goto parse;
2228
David Ahern2c9e45f72011-03-17 10:03:21 -06002229 if (output_set_by_user())
2230 pr_warning("Overriding previous field request for all events.\n");
2231
Adrian Hunter14057202017-06-21 13:17:19 +03002232 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002233 output[j].fields = 0;
2234 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002235 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002236 }
David Ahern1424dc92011-03-09 22:23:28 -07002237 }
2238
Andi Kleen36ce5652017-06-02 08:48:10 -07002239parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002240 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002241 if (*tok == '+') {
2242 if (change == SET)
2243 goto out_badmix;
2244 change = ADD;
2245 tok++;
2246 } else if (*tok == '-') {
2247 if (change == SET)
2248 goto out_badmix;
2249 change = REMOVE;
2250 tok++;
2251 } else {
2252 if (change != SET && change != DEFAULT)
2253 goto out_badmix;
2254 change = SET;
2255 }
2256
David Ahern745f43e2011-03-09 22:23:26 -07002257 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002258 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002259 break;
David Ahern745f43e2011-03-09 22:23:26 -07002260 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002261 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002262 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002263 continue;
2264 }
David Ahern745f43e2011-03-09 22:23:26 -07002265 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002266 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002267 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002268 goto out;
2269 }
2270
2271 if (type == -1) {
2272 /* add user option to all events types for
2273 * which it is valid
2274 */
Adrian Hunter14057202017-06-21 13:17:19 +03002275 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002276 if (output[j].invalid_fields & all_output_options[i].field) {
2277 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2278 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002279 } else {
2280 if (change == REMOVE)
2281 output[j].fields &= ~all_output_options[i].field;
2282 else
2283 output[j].fields |= all_output_options[i].field;
2284 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002285 }
2286 } else {
2287 if (output[type].invalid_fields & all_output_options[i].field) {
2288 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2289 all_output_options[i].str, event_type(type));
2290
2291 rc = -EINVAL;
2292 goto out;
2293 }
2294 output[type].fields |= all_output_options[i].field;
2295 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002296 }
2297
2298 if (type >= 0) {
2299 if (output[type].fields == 0) {
2300 pr_debug("No fields requested for %s type. "
2301 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002302 }
David Ahern1424dc92011-03-09 22:23:28 -07002303 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002304 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002305
Andi Kleen36ce5652017-06-02 08:48:10 -07002306out_badmix:
2307 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2308 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002309out:
David Ahern745f43e2011-03-09 22:23:26 -07002310 free(str);
2311 return rc;
2312}
2313
Shawn Bohrer008f29d2010-11-21 10:09:39 -06002314/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
2315static int is_directory(const char *base_path, const struct dirent *dent)
2316{
2317 char path[PATH_MAX];
2318 struct stat st;
2319
2320 sprintf(path, "%s/%s", base_path, dent->d_name);
2321 if (stat(path, &st))
2322 return 0;
2323
2324 return S_ISDIR(st.st_mode);
2325}
2326
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002327#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2328 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2329 if ((lang_dirent->d_type == DT_DIR || \
2330 (lang_dirent->d_type == DT_UNKNOWN && \
2331 is_directory(scripts_path, lang_dirent))) && \
2332 (strcmp(lang_dirent->d_name, ".")) && \
2333 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002334
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002335#define for_each_script(lang_path, lang_dir, script_dirent) \
2336 while ((script_dirent = readdir(lang_dir)) != NULL) \
2337 if (script_dirent->d_type != DT_DIR && \
2338 (script_dirent->d_type != DT_UNKNOWN || \
2339 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002340
2341
2342#define RECORD_SUFFIX "-record"
2343#define REPORT_SUFFIX "-report"
2344
2345struct script_desc {
2346 struct list_head node;
2347 char *name;
2348 char *half_liner;
2349 char *args;
2350};
2351
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002352static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002353
2354static struct script_desc *script_desc__new(const char *name)
2355{
2356 struct script_desc *s = zalloc(sizeof(*s));
2357
Tom Zanussib5b87312010-11-10 08:16:51 -06002358 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002359 s->name = strdup(name);
2360
2361 return s;
2362}
2363
2364static void script_desc__delete(struct script_desc *s)
2365{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002366 zfree(&s->name);
2367 zfree(&s->half_liner);
2368 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002369 free(s);
2370}
2371
2372static void script_desc__add(struct script_desc *s)
2373{
2374 list_add_tail(&s->node, &script_descs);
2375}
2376
2377static struct script_desc *script_desc__find(const char *name)
2378{
2379 struct script_desc *s;
2380
2381 list_for_each_entry(s, &script_descs, node)
2382 if (strcasecmp(s->name, name) == 0)
2383 return s;
2384 return NULL;
2385}
2386
2387static struct script_desc *script_desc__findnew(const char *name)
2388{
2389 struct script_desc *s = script_desc__find(name);
2390
2391 if (s)
2392 return s;
2393
2394 s = script_desc__new(name);
2395 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002396 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002397
2398 script_desc__add(s);
2399
2400 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002401}
2402
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002403static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002404{
2405 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002406 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002407
2408 if (strlen(str) > suffix_len) {
2409 p = str + strlen(str) - suffix_len;
2410 if (!strncmp(p, suffix, suffix_len))
2411 return p;
2412 }
2413
2414 return NULL;
2415}
2416
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002417static int read_script_info(struct script_desc *desc, const char *filename)
2418{
2419 char line[BUFSIZ], *p;
2420 FILE *fp;
2421
2422 fp = fopen(filename, "r");
2423 if (!fp)
2424 return -1;
2425
2426 while (fgets(line, sizeof(line), fp)) {
2427 p = ltrim(line);
2428 if (strlen(p) == 0)
2429 continue;
2430 if (*p != '#')
2431 continue;
2432 p++;
2433 if (strlen(p) && *p == '!')
2434 continue;
2435
2436 p = ltrim(p);
2437 if (strlen(p) && p[strlen(p) - 1] == '\n')
2438 p[strlen(p) - 1] = '\0';
2439
2440 if (!strncmp(p, "description:", strlen("description:"))) {
2441 p += strlen("description:");
2442 desc->half_liner = strdup(ltrim(p));
2443 continue;
2444 }
2445
2446 if (!strncmp(p, "args:", strlen("args:"))) {
2447 p += strlen("args:");
2448 desc->args = strdup(ltrim(p));
2449 continue;
2450 }
2451 }
2452
2453 fclose(fp);
2454
2455 return 0;
2456}
2457
Robert Richter38efb532011-11-25 11:38:40 +01002458static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2459{
2460 char *script_root, *str;
2461
2462 script_root = strdup(script_dirent->d_name);
2463 if (!script_root)
2464 return NULL;
2465
2466 str = (char *)ends_with(script_root, suffix);
2467 if (!str) {
2468 free(script_root);
2469 return NULL;
2470 }
2471
2472 *str = '\0';
2473 return script_root;
2474}
2475
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002476static int list_available_scripts(const struct option *opt __maybe_unused,
2477 const char *s __maybe_unused,
2478 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002479{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002480 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002481 char scripts_path[MAXPATHLEN];
2482 DIR *scripts_dir, *lang_dir;
2483 char script_path[MAXPATHLEN];
2484 char lang_path[MAXPATHLEN];
2485 struct script_desc *desc;
2486 char first_half[BUFSIZ];
2487 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002488
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002489 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002490
2491 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002492 if (!scripts_dir) {
2493 fprintf(stdout,
2494 "open(%s) failed.\n"
2495 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2496 scripts_path);
2497 exit(-1);
2498 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002499
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002500 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002501 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002502 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002503 lang_dir = opendir(lang_path);
2504 if (!lang_dir)
2505 continue;
2506
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002507 for_each_script(lang_path, lang_dir, script_dirent) {
2508 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002509 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002510 desc = script_desc__findnew(script_root);
2511 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002512 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002513 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002514 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002515 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002516 }
2517 }
2518
2519 fprintf(stdout, "List of available trace scripts:\n");
2520 list_for_each_entry(desc, &script_descs, node) {
2521 sprintf(first_half, "%s %s", desc->name,
2522 desc->args ? desc->args : "");
2523 fprintf(stdout, " %-36s %s\n", first_half,
2524 desc->half_liner ? desc->half_liner : "");
2525 }
2526
2527 exit(0);
2528}
2529
Feng Tange5f37052012-09-07 16:42:26 +08002530/*
Feng Tang49e639e2012-10-30 11:56:03 +08002531 * Some scripts specify the required events in their "xxx-record" file,
2532 * this function will check if the events in perf.data match those
2533 * mentioned in the "xxx-record".
2534 *
2535 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2536 * which is covered well now. And new parsing code should be added to
2537 * cover the future complexing formats like event groups etc.
2538 */
2539static int check_ev_match(char *dir_name, char *scriptname,
2540 struct perf_session *session)
2541{
2542 char filename[MAXPATHLEN], evname[128];
2543 char line[BUFSIZ], *p;
2544 struct perf_evsel *pos;
2545 int match, len;
2546 FILE *fp;
2547
2548 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2549
2550 fp = fopen(filename, "r");
2551 if (!fp)
2552 return -1;
2553
2554 while (fgets(line, sizeof(line), fp)) {
2555 p = ltrim(line);
2556 if (*p == '#')
2557 continue;
2558
2559 while (strlen(p)) {
2560 p = strstr(p, "-e");
2561 if (!p)
2562 break;
2563
2564 p += 2;
2565 p = ltrim(p);
2566 len = strcspn(p, " \t");
2567 if (!len)
2568 break;
2569
2570 snprintf(evname, len + 1, "%s", p);
2571
2572 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002573 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002574 if (!strcmp(perf_evsel__name(pos), evname)) {
2575 match = 1;
2576 break;
2577 }
2578 }
2579
2580 if (!match) {
2581 fclose(fp);
2582 return -1;
2583 }
2584 }
2585 }
2586
2587 fclose(fp);
2588 return 0;
2589}
2590
2591/*
Feng Tange5f37052012-09-07 16:42:26 +08002592 * Return -1 if none is found, otherwise the actual scripts number.
2593 *
2594 * Currently the only user of this function is the script browser, which
2595 * will list all statically runnable scripts, select one, execute it and
2596 * show the output in a perf browser.
2597 */
2598int find_scripts(char **scripts_array, char **scripts_path_array)
2599{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002600 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002601 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002602 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002603 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002604 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002605 .file = {
2606 .path = input_name,
2607 },
2608 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002609 };
Feng Tange5f37052012-09-07 16:42:26 +08002610 char *temp;
2611 int i = 0;
2612
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002613 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002614 if (!session)
2615 return -1;
2616
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002617 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002618
2619 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002620 if (!scripts_dir) {
2621 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002622 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002623 }
Feng Tange5f37052012-09-07 16:42:26 +08002624
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002625 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002626 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002627 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002628#ifdef NO_LIBPERL
2629 if (strstr(lang_path, "perl"))
2630 continue;
2631#endif
2632#ifdef NO_LIBPYTHON
2633 if (strstr(lang_path, "python"))
2634 continue;
2635#endif
2636
2637 lang_dir = opendir(lang_path);
2638 if (!lang_dir)
2639 continue;
2640
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002641 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002642 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002643 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002644 continue;
2645 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002646 script_dirent->d_name);
2647 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002648 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002649 (temp - script_dirent->d_name) + 1,
2650 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002651
2652 if (check_ev_match(lang_path,
2653 scripts_array[i], session))
2654 continue;
2655
Feng Tange5f37052012-09-07 16:42:26 +08002656 i++;
2657 }
Feng Tang49e639e2012-10-30 11:56:03 +08002658 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002659 }
2660
Feng Tang49e639e2012-10-30 11:56:03 +08002661 closedir(scripts_dir);
2662 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002663 return i;
2664}
2665
Tom Zanussi38752942009-12-15 02:53:39 -06002666static char *get_script_path(const char *script_root, const char *suffix)
2667{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002668 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002669 char scripts_path[MAXPATHLEN];
2670 char script_path[MAXPATHLEN];
2671 DIR *scripts_dir, *lang_dir;
2672 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002673 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002674
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002675 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002676
2677 scripts_dir = opendir(scripts_path);
2678 if (!scripts_dir)
2679 return NULL;
2680
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002681 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002682 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002683 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002684 lang_dir = opendir(lang_path);
2685 if (!lang_dir)
2686 continue;
2687
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002688 for_each_script(lang_path, lang_dir, script_dirent) {
2689 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002690 if (__script_root && !strcmp(script_root, __script_root)) {
2691 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002692 closedir(lang_dir);
2693 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002694 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002695 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002696 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002697 }
2698 free(__script_root);
2699 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002700 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002701 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002702 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002703
Robert Richter38efb532011-11-25 11:38:40 +01002704 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002705}
2706
Tom Zanussib5b87312010-11-10 08:16:51 -06002707static bool is_top_script(const char *script_path)
2708{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002709 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002710}
2711
2712static int has_required_arg(char *script_path)
2713{
2714 struct script_desc *desc;
2715 int n_args = 0;
2716 char *p;
2717
2718 desc = script_desc__new(NULL);
2719
2720 if (read_script_info(desc, script_path))
2721 goto out;
2722
2723 if (!desc->args)
2724 goto out;
2725
2726 for (p = desc->args; *p; p++)
2727 if (*p == '<')
2728 n_args++;
2729out:
2730 script_desc__delete(desc);
2731
2732 return n_args;
2733}
2734
David Ahernd54b1a92012-08-26 12:24:46 -06002735static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002736{
2737 char **__argv = malloc(sizeof(const char *) * argc);
2738
David Ahernd54b1a92012-08-26 12:24:46 -06002739 if (!__argv) {
2740 pr_err("malloc failed\n");
2741 return -1;
2742 }
2743
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002744 memcpy(__argv, argv, sizeof(const char *) * argc);
2745 argc = parse_options(argc, (const char **)__argv, record_options,
2746 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2747 free(__argv);
2748
David Ahernd54b1a92012-08-26 12:24:46 -06002749 system_wide = (argc == 0);
2750
2751 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002752}
2753
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002754static void script__setup_sample_type(struct perf_script *script)
2755{
2756 struct perf_session *session = script->session;
2757 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2758
2759 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2760 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2761 (sample_type & PERF_SAMPLE_STACK_USER))
2762 callchain_param.record_mode = CALLCHAIN_DWARF;
2763 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2764 callchain_param.record_mode = CALLCHAIN_LBR;
2765 else
2766 callchain_param.record_mode = CALLCHAIN_FP;
2767 }
2768}
2769
Jiri Olsae099eba2016-01-05 22:09:09 +01002770static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2771 union perf_event *event,
2772 struct perf_session *session)
2773{
2774 struct stat_round_event *round = &event->stat_round;
2775 struct perf_evsel *counter;
2776
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002777 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002778 perf_stat_process_counter(&stat_config, counter);
2779 process_stat(counter, round->time);
2780 }
2781
2782 process_stat_interval(round->time);
2783 return 0;
2784}
2785
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002786static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2787 union perf_event *event,
2788 struct perf_session *session __maybe_unused)
2789{
2790 perf_event__read_stat_config(&stat_config, &event->stat_config);
2791 return 0;
2792}
2793
Jiri Olsacfc88742016-01-05 22:09:06 +01002794static int set_maps(struct perf_script *script)
2795{
2796 struct perf_evlist *evlist = script->session->evlist;
2797
2798 if (!script->cpus || !script->threads)
2799 return 0;
2800
2801 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2802 return -EINVAL;
2803
2804 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2805
2806 if (perf_evlist__alloc_stats(evlist, true))
2807 return -ENOMEM;
2808
2809 script->allocated = true;
2810 return 0;
2811}
2812
2813static
2814int process_thread_map_event(struct perf_tool *tool,
2815 union perf_event *event,
2816 struct perf_session *session __maybe_unused)
2817{
2818 struct perf_script *script = container_of(tool, struct perf_script, tool);
2819
2820 if (script->threads) {
2821 pr_warning("Extra thread map event, ignoring.\n");
2822 return 0;
2823 }
2824
2825 script->threads = thread_map__new_event(&event->thread_map);
2826 if (!script->threads)
2827 return -ENOMEM;
2828
2829 return set_maps(script);
2830}
2831
2832static
2833int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2834 union perf_event *event,
2835 struct perf_session *session __maybe_unused)
2836{
2837 struct perf_script *script = container_of(tool, struct perf_script, tool);
2838
2839 if (script->cpus) {
2840 pr_warning("Extra cpu map event, ignoring.\n");
2841 return 0;
2842 }
2843
2844 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2845 if (!script->cpus)
2846 return -ENOMEM;
2847
2848 return set_maps(script);
2849}
2850
Arnaldo Carvalho de Melo501e5bb2017-11-09 16:04:26 -03002851#ifdef HAVE_AUXTRACE_SUPPORT
2852static int perf_script__process_auxtrace_info(struct perf_tool *tool,
2853 union perf_event *event,
2854 struct perf_session *session)
2855{
2856 int ret = perf_event__process_auxtrace_info(tool, event, session);
2857
2858 if (ret == 0) {
2859 struct perf_script *script = container_of(tool, struct perf_script, tool);
2860
2861 ret = perf_script__setup_per_event_dump(script);
2862 }
2863
2864 return ret;
2865}
2866#else
2867#define perf_script__process_auxtrace_info 0
2868#endif
2869
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002870int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002871{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002872 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002873 bool header = false;
2874 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002875 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002876 char *rec_script_path = NULL;
2877 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002878 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002879 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002880 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002881 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002882 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002883 struct perf_script script = {
2884 .tool = {
2885 .sample = process_sample_event,
2886 .mmap = perf_event__process_mmap,
2887 .mmap2 = perf_event__process_mmap2,
2888 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302889 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002890 .exit = perf_event__process_exit,
2891 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002892 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002893 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002894 .tracing_data = perf_event__process_tracing_data,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07002895 .feature = perf_event__process_feature,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002896 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002897 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melo501e5bb2017-11-09 16:04:26 -03002898 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002899 .auxtrace = perf_event__process_auxtrace,
2900 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002901 .stat = perf_event__process_stat_event,
2902 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002903 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002904 .thread_map = process_thread_map_event,
2905 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002906 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002907 .ordering_requires_timestamps = true,
2908 },
2909 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002910 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08002911 .mode = PERF_DATA_MODE_READ,
2912 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002913 const struct option options[] = {
2914 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2915 "dump raw trace in ASCII"),
2916 OPT_INCR('v', "verbose", &verbose,
2917 "be more verbose (show symbol address, etc)"),
2918 OPT_BOOLEAN('L', "Latency", &latency_format,
2919 "show latency attributes (irqs/preemption disabled, etc)"),
2920 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2921 list_available_scripts),
2922 OPT_CALLBACK('s', "script", NULL, "name",
2923 "script file name (lang:script name, script name, or *)",
2924 parse_scriptname),
2925 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2926 "generate perf-script.xx script in specified language"),
2927 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2928 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2929 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002930 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2931 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002932 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2933 "file", "vmlinux pathname"),
2934 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2935 "file", "kallsyms pathname"),
2936 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2937 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002938 OPT_CALLBACK(0, "symfs", NULL, "directory",
2939 "Look for files with symbols relative to this directory",
2940 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002941 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002942 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07002943 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03002944 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002945 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Andi Kleenb1491ac2017-09-05 11:40:57 -07002946 "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
Kan Liang49d58f02017-08-29 13:11:11 -04002947 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
Andi Kleen48d02a12017-02-23 15:46:34 -08002948 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002949 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2950 "system-wide collection from all CPUs"),
2951 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2952 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002953 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2954 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002955 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2956 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2957 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002958 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2959 "only consider symbols in these pids"),
2960 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2961 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002962 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2963 "Set the maximum stack depth when parsing the callchain, "
2964 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002965 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002966 OPT_BOOLEAN('I', "show-info", &show_full_info,
2967 "display extended information from perf.data file"),
2968 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2969 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002970 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2971 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002972 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2973 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002974 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2975 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302976 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2977 "Show namespace events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002978 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
2979 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002980 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002981 OPT_INTEGER(0, "max-blocks", &max_blocks,
2982 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002983 OPT_BOOLEAN(0, "ns", &nanosecs,
2984 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002985 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2986 "Instruction Tracing options",
2987 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002988 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2989 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002990 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2991 "Enable symbol demangling"),
2992 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2993 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002994 OPT_STRING(0, "time", &script.time_str, "str",
2995 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09002996 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2997 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002998 OPT_END()
2999 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003000 const char * const script_subcommands[] = { "record", "report", NULL };
3001 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003002 "perf script [<options>]",
3003 "perf script [<options>] record <script> [<record-options>] <command>",
3004 "perf script [<options>] report <script> [script-args]",
3005 "perf script [<options>] <script> [<record-options>] <command>",
3006 "perf script [<options>] <top-script> [script-args]",
3007 NULL
3008 };
Tom Zanussi38752942009-12-15 02:53:39 -06003009
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003010 perf_set_singlethreaded();
3011
Tom Zanussib5b87312010-11-10 08:16:51 -06003012 setup_scripting();
3013
Yunlong Song40cae2b2015-03-18 21:35:54 +08003014 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003015 PARSE_OPT_STOP_AT_NON_OPTION);
3016
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003017 data.file.path = input_name;
3018 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003019
Tom Zanussib5b87312010-11-10 08:16:51 -06003020 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3021 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3022 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003023 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003024 }
3025
Tom Zanussib5b87312010-11-10 08:16:51 -06003026 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3027 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3028 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003029 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003030 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003031 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003032 return -1;
3033 }
Tom Zanussi38752942009-12-15 02:53:39 -06003034 }
3035
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003036 if (itrace_synth_opts.callchain &&
3037 itrace_synth_opts.callchain_sz > scripting_max_stack)
3038 scripting_max_stack = itrace_synth_opts.callchain_sz;
3039
Ben Hutchings44e668c2010-10-10 16:10:03 +01003040 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003041 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003042
Tom Zanussib5b87312010-11-10 08:16:51 -06003043 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003044 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003045 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003046 pid_t pid;
3047
Tom Zanussib5b87312010-11-10 08:16:51 -06003048 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3049 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3050
3051 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003052 usage_with_options_msg(script_usage, options,
3053 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003054 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003055 }
3056
Tom Zanussib5b87312010-11-10 08:16:51 -06003057 if (is_top_script(argv[0])) {
3058 rep_args = argc - 1;
3059 } else {
3060 int rec_args;
3061
3062 rep_args = has_required_arg(rep_script_path);
3063 rec_args = (argc - 1) - rep_args;
3064 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003065 usage_with_options_msg(script_usage, options,
3066 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003067 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003068 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003069 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003070 }
3071
3072 if (pipe(live_pipe) < 0) {
3073 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003074 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003075 }
3076
3077 pid = fork();
3078 if (pid < 0) {
3079 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003080 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003081 }
3082
3083 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003084 j = 0;
3085
Tom Zanussia0cccc22010-04-01 23:59:25 -05003086 dup2(live_pipe[1], 1);
3087 close(live_pipe[0]);
3088
Robert Richter317df652011-11-25 15:05:25 +01003089 if (is_top_script(argv[0])) {
3090 system_wide = true;
3091 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003092 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3093 err = -1;
3094 goto out;
3095 }
Robert Richter317df652011-11-25 15:05:25 +01003096 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003097
3098 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003099 if (!__argv) {
3100 pr_err("malloc failed\n");
3101 err = -ENOMEM;
3102 goto out;
3103 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003104
Tom Zanussib5b87312010-11-10 08:16:51 -06003105 __argv[j++] = "/bin/sh";
3106 __argv[j++] = rec_script_path;
3107 if (system_wide)
3108 __argv[j++] = "-a";
3109 __argv[j++] = "-q";
3110 __argv[j++] = "-o";
3111 __argv[j++] = "-";
3112 for (i = rep_args + 1; i < argc; i++)
3113 __argv[j++] = argv[i];
3114 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003115
3116 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003117 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003118 exit(-1);
3119 }
3120
3121 dup2(live_pipe[0], 0);
3122 close(live_pipe[1]);
3123
Tom Zanussib5b87312010-11-10 08:16:51 -06003124 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003125 if (!__argv) {
3126 pr_err("malloc failed\n");
3127 err = -ENOMEM;
3128 goto out;
3129 }
3130
Tom Zanussib5b87312010-11-10 08:16:51 -06003131 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003132 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003133 __argv[j++] = rep_script_path;
3134 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003135 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003136 __argv[j++] = "-i";
3137 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003138 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003139
3140 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003141 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003142 exit(-1);
3143 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003144
Tom Zanussib5b87312010-11-10 08:16:51 -06003145 if (rec_script_path)
3146 script_path = rec_script_path;
3147 if (rep_script_path)
3148 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003149
Tom Zanussib5b87312010-11-10 08:16:51 -06003150 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003151 j = 0;
3152
Robert Richter317df652011-11-25 15:05:25 +01003153 if (!rec_script_path)
3154 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003155 else if (!system_wide) {
3156 if (have_cmd(argc - 1, &argv[1]) != 0) {
3157 err = -1;
3158 goto out;
3159 }
3160 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003161
3162 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003163 if (!__argv) {
3164 pr_err("malloc failed\n");
3165 err = -ENOMEM;
3166 goto out;
3167 }
3168
Tom Zanussib5b87312010-11-10 08:16:51 -06003169 __argv[j++] = "/bin/sh";
3170 __argv[j++] = script_path;
3171 if (system_wide)
3172 __argv[j++] = "-a";
3173 for (i = 2; i < argc; i++)
3174 __argv[j++] = argv[i];
3175 __argv[j++] = NULL;
3176
3177 execvp("/bin/sh", (char **)__argv);
3178 free(__argv);
3179 exit(-1);
3180 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003181
Tom Zanussicf4fee52010-03-03 01:04:33 -06003182 if (!script_name)
3183 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003184
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003185 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003186 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003187 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003188
Jiri Olsae90debd2013-12-09 11:02:50 +01003189 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003190 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003191 perf_session__fprintf_info(session, stdout, show_full_info);
3192 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003193 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003194 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003195 if (show_full_info)
3196 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003197
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003198 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003199 goto out_delete;
3200
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003201 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003202 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003203
Adrian Huntere2167082016-06-23 16:40:58 +03003204 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3205 itrace_synth_opts.thread_stack = true;
3206
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003207 session->itrace_synth_opts = &itrace_synth_opts;
3208
Anton Blanchard5d67be92011-07-04 21:57:50 +10003209 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003210 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3211 if (err < 0)
3212 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003213 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003214 }
3215
David Ahern1424dc92011-03-09 22:23:28 -07003216 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003217 symbol_conf.use_callchain = true;
3218 else
3219 symbol_conf.use_callchain = false;
3220
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003221 if (session->tevent.pevent &&
3222 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003223 machine__resolve_kernel_addr,
3224 &session->machines.host) < 0) {
3225 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003226 err = -1;
3227 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003228 }
3229
Tom Zanussi956ffd02009-11-25 01:15:46 -06003230 if (generate_script_lang) {
3231 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003232 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003233
David Ahern2c9e45f72011-03-17 10:03:21 -06003234 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003235 fprintf(stderr,
3236 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003237 err = -EINVAL;
3238 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003239 }
3240
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003241 input = open(data.file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003242 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003243 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003244 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003245 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003246 }
3247
3248 err = fstat(input, &perf_stat);
3249 if (err < 0) {
3250 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003251 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003252 }
3253
3254 if (!perf_stat.st_size) {
3255 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003256 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003257 }
3258
3259 scripting_ops = script_spec__lookup(generate_script_lang);
3260 if (!scripting_ops) {
3261 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003262 err = -ENOENT;
3263 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003264 }
3265
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003266 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003267 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003268 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003269 }
3270
3271 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003272 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003273 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003274 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003275 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003276 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003277 }
3278
David Ahern9cbdb702011-04-06 21:54:20 -06003279
3280 err = perf_session__check_output_opt(session);
3281 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003282 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003283
David Aherna91f4c42016-11-29 10:15:43 -07003284 /* needs to be parsed after looking up reference time */
3285 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
3286 pr_err("Invalid time string\n");
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003287 err = -EINVAL;
3288 goto out_delete;
David Aherna91f4c42016-11-29 10:15:43 -07003289 }
3290
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003291 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003292
Adrian Hunterd445dd22014-08-15 22:08:37 +03003293 flush_scripting();
3294
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003295out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01003296 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003297 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003298
3299 if (script_started)
3300 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003301out:
3302 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003303}