blob: fac6f053e4da9ddb6f9fdcf954dfa29bbb0f2ca4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002#include "builtin.h"
3
Andrea Gelminib7eead82010-08-05 15:51:38 +02004#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02005#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02006#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06007#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02008#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06009#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +020010#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020013#include "util/symbol.h"
14#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010015#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020016#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070017#include "util/evlist.h"
18#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080019#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020020#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030021#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010022#include "util/cpumap.h"
23#include "util/thread_map.h"
24#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080025#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030026#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030027#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070028#include "util/time-utils.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030029#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100030#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030031#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030032#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030033#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010034#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010035#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080036#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030037#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030038#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030039#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030040#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030041#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030042#include <sys/types.h>
43#include <sys/stat.h>
44#include <unistd.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020045
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030046#include "sane_ctype.h"
47
Tom Zanussi956ffd02009-11-25 01:15:46 -060048static char const *script_name;
49static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020050static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020051static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020052static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070053static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090054static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010055static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030056static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030057static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100058static const char *cpu_list;
59static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010060static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080061static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060062
Adrian Hunter44cbe722015-09-25 16:15:50 +030063unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030064
David Ahern745f43e2011-03-09 22:23:26 -070065enum perf_output_field {
66 PERF_OUTPUT_COMM = 1U << 0,
67 PERF_OUTPUT_TID = 1U << 1,
68 PERF_OUTPUT_PID = 1U << 2,
69 PERF_OUTPUT_TIME = 1U << 3,
70 PERF_OUTPUT_CPU = 1U << 4,
71 PERF_OUTPUT_EVNAME = 1U << 5,
72 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060073 PERF_OUTPUT_IP = 1U << 7,
74 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060075 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060076 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090077 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020078 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020079 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020080 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020081 PERF_OUTPUT_BRSTACK = 1U << 15,
82 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010083 PERF_OUTPUT_DATA_SRC = 1U << 17,
84 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000085 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030086 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030087 PERF_OUTPUT_INSN = 1U << 21,
88 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080089 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070090 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030091 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -040092 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -070093 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -080094 PERF_OUTPUT_METRIC = 1U << 28,
David Ahern745f43e2011-03-09 22:23:26 -070095};
96
97struct output_option {
98 const char *str;
99 enum perf_output_field field;
100} all_output_options[] = {
101 {.str = "comm", .field = PERF_OUTPUT_COMM},
102 {.str = "tid", .field = PERF_OUTPUT_TID},
103 {.str = "pid", .field = PERF_OUTPUT_PID},
104 {.str = "time", .field = PERF_OUTPUT_TIME},
105 {.str = "cpu", .field = PERF_OUTPUT_CPU},
106 {.str = "event", .field = PERF_OUTPUT_EVNAME},
107 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600108 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700109 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600110 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600111 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900112 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200113 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200114 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200115 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700116 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200117 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
118 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100119 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
120 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000121 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300122 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300123 {.str = "insn", .field = PERF_OUTPUT_INSN},
124 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800125 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700126 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300127 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400128 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800129 {.str = "metric", .field = PERF_OUTPUT_METRIC},
David Ahern745f43e2011-03-09 22:23:26 -0700130};
131
Adrian Hunter14057202017-06-21 13:17:19 +0300132enum {
133 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
134 OUTPUT_TYPE_MAX
135};
136
David Ahern745f43e2011-03-09 22:23:26 -0700137/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600138static struct {
139 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600140 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400141 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600142 u64 fields;
143 u64 invalid_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300144} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700145
David Ahern2c9e45f72011-03-17 10:03:21 -0600146 [PERF_TYPE_HARDWARE] = {
147 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700148
David Ahern2c9e45f72011-03-17 10:03:21 -0600149 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
150 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600151 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200152 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
153 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600154
Wang Nan30372f02016-02-24 11:20:45 +0000155 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600156 },
157
158 [PERF_TYPE_SOFTWARE] = {
159 .user_set = false,
160
161 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
162 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600163 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200164 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000165 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600166
167 .invalid_fields = PERF_OUTPUT_TRACE,
168 },
169
170 [PERF_TYPE_TRACEPOINT] = {
171 .user_set = false,
172
173 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
174 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000175 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600176 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700177
178 [PERF_TYPE_RAW] = {
179 .user_set = false,
180
181 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
182 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600183 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200184 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100185 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
Kan Liang49d58f02017-08-29 13:11:11 -0400186 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT |
187 PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700188
Wang Nan30372f02016-02-24 11:20:45 +0000189 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700190 },
Wang Nan27cfef02015-12-08 02:25:43 +0000191
192 [PERF_TYPE_BREAKPOINT] = {
193 .user_set = false,
194
195 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
196 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
197 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
198 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
199 PERF_OUTPUT_PERIOD,
200
Wang Nan30372f02016-02-24 11:20:45 +0000201 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000202 },
Adrian Hunter14057202017-06-21 13:17:19 +0300203
204 [OUTPUT_TYPE_SYNTH] = {
205 .user_set = false,
206
207 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
208 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
209 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Adrian Hunter47e78082017-05-26 11:17:22 +0300210 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
211 PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300212
213 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
214 },
David Ahern1424dc92011-03-09 22:23:28 -0700215};
David Ahern745f43e2011-03-09 22:23:26 -0700216
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300217struct perf_evsel_script {
218 char *filename;
219 FILE *fp;
220 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800221 /* For metric output */
222 u64 val;
223 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300224};
225
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800226static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
227{
228 return (struct perf_evsel_script *)evsel->priv;
229}
230
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300231static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100232 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300233{
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800234 struct perf_evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300235
236 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100237 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300238 goto out_free;
239 es->fp = fopen(es->filename, "w");
240 if (es->fp == NULL)
241 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300242 }
243
244 return es;
245out_free_filename:
246 zfree(&es->filename);
247out_free:
248 free(es);
249 return NULL;
250}
251
252static void perf_evsel_script__delete(struct perf_evsel_script *es)
253{
254 zfree(&es->filename);
255 fclose(es->fp);
256 es->fp = NULL;
257 free(es);
258}
259
260static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
261{
262 struct stat st;
263
264 fstat(fileno(es->fp), &st);
265 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
266 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
267}
268
Adrian Hunter14057202017-06-21 13:17:19 +0300269static inline int output_type(unsigned int type)
270{
271 switch (type) {
272 case PERF_TYPE_SYNTH:
273 return OUTPUT_TYPE_SYNTH;
274 default:
275 return type;
276 }
277}
278
279static inline unsigned int attr_type(unsigned int type)
280{
281 switch (type) {
282 case OUTPUT_TYPE_SYNTH:
283 return PERF_TYPE_SYNTH;
284 default:
285 return type;
286 }
287}
288
David Ahern2c9e45f72011-03-17 10:03:21 -0600289static bool output_set_by_user(void)
290{
291 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300292 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600293 if (output[j].user_set)
294 return true;
295 }
296 return false;
297}
David Ahern745f43e2011-03-09 22:23:26 -0700298
David Ahern9cbdb702011-04-06 21:54:20 -0600299static const char *output_field2str(enum perf_output_field field)
300{
301 int i, imax = ARRAY_SIZE(all_output_options);
302 const char *str = "";
303
304 for (i = 0; i < imax; ++i) {
305 if (all_output_options[i].field == field) {
306 str = all_output_options[i].str;
307 break;
308 }
309 }
310 return str;
311}
312
Adrian Hunter14057202017-06-21 13:17:19 +0300313#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700314
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300315static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
316 u64 sample_type, const char *sample_msg,
317 enum perf_output_field field,
318 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700319{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300320 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300321 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600322 const char *evname;
323
324 if (attr->sample_type & sample_type)
325 return 0;
326
327 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300328 if (allow_user_set)
329 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300330 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600331 pr_err("Samples for '%s' event do not have %s attribute set. "
332 "Cannot print '%s' field.\n",
333 evname, sample_msg, output_field2str(field));
334 return -1;
335 }
336
337 /* user did not ask for it explicitly so remove from the default list */
338 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300339 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600340 pr_debug("Samples for '%s' event do not have %s attribute set. "
341 "Skipping '%s' field.\n",
342 evname, sample_msg, output_field2str(field));
343
344 return 0;
345}
346
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300347static int perf_evsel__check_stype(struct perf_evsel *evsel,
348 u64 sample_type, const char *sample_msg,
349 enum perf_output_field field)
350{
351 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
352 false);
353}
354
David Ahern9cbdb702011-04-06 21:54:20 -0600355static int perf_evsel__check_attr(struct perf_evsel *evsel,
356 struct perf_session *session)
357{
358 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300359 bool allow_user_set;
360
Jiri Olsae099eba2016-01-05 22:09:09 +0100361 if (perf_header__has_feat(&session->header, HEADER_STAT))
362 return 0;
363
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300364 allow_user_set = perf_header__has_feat(&session->header,
365 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600366
David Ahern1424dc92011-03-09 22:23:28 -0700367 if (PRINT_FIELD(TRACE) &&
368 !perf_session__has_traces(session, "record -R"))
369 return -EINVAL;
370
David Ahern787bef12011-05-27 14:28:43 -0600371 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300372 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
373 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700374 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700375 }
David Ahern7cec0922011-05-30 13:08:23 -0600376
377 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300378 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
379 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600380 return -EINVAL;
381
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100382 if (PRINT_FIELD(DATA_SRC) &&
383 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
384 PERF_OUTPUT_DATA_SRC))
385 return -EINVAL;
386
387 if (PRINT_FIELD(WEIGHT) &&
388 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
389 PERF_OUTPUT_WEIGHT))
390 return -EINVAL;
391
David Ahern7cec0922011-05-30 13:08:23 -0600392 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
393 pr_err("Display of symbols requested but neither sample IP nor "
394 "sample address\nis selected. Hence, no addresses to convert "
395 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600396 return -EINVAL;
397 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900398 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
399 pr_err("Display of offsets requested but symbol is not"
400 "selected.\n");
401 return -EINVAL;
402 }
Mark Santaniello55b9b502017-06-19 09:38:24 -0700403 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR) &&
Mark Santaniello106dacd2017-06-19 09:38:25 -0700404 !PRINT_FIELD(BRSTACK) && !PRINT_FIELD(BRSTACKSYM) && !PRINT_FIELD(BRSTACKOFF)) {
405 pr_err("Display of DSO requested but no address to convert. Select\n"
406 "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
David Ahern610723f2011-05-27 14:28:44 -0600407 return -EINVAL;
408 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200409 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
410 pr_err("Display of source line number requested but sample IP is not\n"
411 "selected. Hence, no address to lookup the source line number.\n");
412 return -EINVAL;
413 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800414 if (PRINT_FIELD(BRSTACKINSN) &&
415 !(perf_evlist__combined_branch_type(session->evlist) &
416 PERF_SAMPLE_BRANCH_ANY)) {
417 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
418 "Hint: run 'perf record -b ...'\n");
419 return -EINVAL;
420 }
David Ahern1424dc92011-03-09 22:23:28 -0700421 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300422 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
423 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700424 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700425
426 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300427 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
428 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700429 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700430
431 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300432 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
433 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700434 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600435
Stephane Eranianfc36f942015-08-31 18:41:10 +0200436 if (PRINT_FIELD(IREGS) &&
437 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
438 PERF_OUTPUT_IREGS))
439 return -EINVAL;
440
Andi Kleenb1491ac2017-09-05 11:40:57 -0700441 if (PRINT_FIELD(UREGS) &&
442 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
443 PERF_OUTPUT_UREGS))
444 return -EINVAL;
445
Kan Liang49d58f02017-08-29 13:11:11 -0400446 if (PRINT_FIELD(PHYS_ADDR) &&
447 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
448 PERF_OUTPUT_PHYS_ADDR))
449 return -EINVAL;
450
David Ahern9cbdb702011-04-06 21:54:20 -0600451 return 0;
452}
453
Adrian Hunter7ea95722013-11-01 15:51:30 +0200454static void set_print_ip_opts(struct perf_event_attr *attr)
455{
Adrian Hunter14057202017-06-21 13:17:19 +0300456 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200457
458 output[type].print_ip_opts = 0;
459 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300460 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200461
462 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300463 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200464
465 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300466 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200467
468 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300469 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200470
471 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300472 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200473}
474
David Ahern9cbdb702011-04-06 21:54:20 -0600475/*
476 * verify all user requested events exist and the samples
477 * have the expected data
478 */
479static int perf_session__check_output_opt(struct perf_session *session)
480{
He Kuang40f20e52016-05-16 04:51:19 +0000481 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600482 struct perf_evsel *evsel;
483
Adrian Hunter14057202017-06-21 13:17:19 +0300484 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
485 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600486
487 /*
488 * even if fields is set to 0 (ie., show nothing) event must
489 * exist if user explicitly includes it on the command line
490 */
Adrian Hunter14057202017-06-21 13:17:19 +0300491 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
492 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600493 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300494 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600495 event_type(j));
496 return -1;
497 }
498
499 if (evsel && output[j].fields &&
500 perf_evsel__check_attr(evsel, session))
501 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400502
503 if (evsel == NULL)
504 continue;
505
Adrian Hunter7ea95722013-11-01 15:51:30 +0200506 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700507 }
508
Adrian Hunter98526ee2014-07-31 09:00:59 +0300509 if (!no_callchain) {
510 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000511 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300512
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300513 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000514 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300515 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
516 use_callchain = true;
517 break;
518 }
519 }
He Kuang71ac8992016-08-04 11:25:43 +0000520 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300521 symbol_conf.use_callchain = false;
522 }
523
David Ahern80b8b492013-11-19 21:07:37 -0700524 /*
525 * set default for tracepoints to print symbols only
526 * if callchains are present
527 */
528 if (symbol_conf.use_callchain &&
529 !output[PERF_TYPE_TRACEPOINT].user_set) {
530 struct perf_event_attr *attr;
531
532 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700533
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300534 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000535 if (evsel->attr.type != j)
536 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700537
He Kuang40f20e52016-05-16 04:51:19 +0000538 attr = &evsel->attr;
539
540 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
541 output[j].fields |= PERF_OUTPUT_IP;
542 output[j].fields |= PERF_OUTPUT_SYM;
543 output[j].fields |= PERF_OUTPUT_DSO;
544 set_print_ip_opts(attr);
545 goto out;
546 }
David Ahern80b8b492013-11-19 21:07:37 -0700547 }
548 }
549
550out:
David Ahern1424dc92011-03-09 22:23:28 -0700551 return 0;
552}
David Ahern745f43e2011-03-09 22:23:26 -0700553
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300554static int perf_sample__fprintf_iregs(struct perf_sample *sample,
555 struct perf_event_attr *attr, FILE *fp)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200556{
557 struct regs_dump *regs = &sample->intr_regs;
558 uint64_t mask = attr->sample_regs_intr;
559 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300560 int printed = 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200561
562 if (!regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300563 return 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200564
565 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
566 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300567 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Stephane Eranianfc36f942015-08-31 18:41:10 +0200568 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300569
570 return printed;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200571}
572
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300573static int perf_sample__fprintf_uregs(struct perf_sample *sample,
574 struct perf_event_attr *attr, FILE *fp)
Andi Kleenb1491ac2017-09-05 11:40:57 -0700575{
576 struct regs_dump *regs = &sample->user_regs;
577 uint64_t mask = attr->sample_regs_user;
578 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300579 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700580
581 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300582 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700583
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300584 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700585
586 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
587 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300588 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700589 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300590
591 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700592}
593
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300594static int perf_sample__fprintf_start(struct perf_sample *sample,
595 struct thread *thread,
596 struct perf_evsel *evsel, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700597{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300598 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700599 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700600 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300601 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700602
David Ahern745f43e2011-03-09 22:23:26 -0700603 if (PRINT_FIELD(COMM)) {
604 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300605 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600606 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300607 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700608 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300609 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700610 }
David Ahernc70c94b2011-03-09 22:23:25 -0700611
David Ahern745f43e2011-03-09 22:23:26 -0700612 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300613 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700614 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300615 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700616 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300617 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700618
David Ahern745f43e2011-03-09 22:23:26 -0700619 if (PRINT_FIELD(CPU)) {
620 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300621 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700622 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300623 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700624 }
David Ahernc70c94b2011-03-09 22:23:25 -0700625
David Ahern745f43e2011-03-09 22:23:26 -0700626 if (PRINT_FIELD(TIME)) {
627 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300628 secs = nsecs / NSEC_PER_SEC;
629 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900630
Adrian Hunter83e19862015-09-25 16:15:36 +0300631 if (nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300632 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900633 else {
634 char sample_time[32];
635 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300636 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900637 }
David Ahern745f43e2011-03-09 22:23:26 -0700638 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300639
640 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700641}
642
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200643static inline char
644mispred_str(struct branch_entry *br)
645{
646 if (!(br->flags.mispred || br->flags.predicted))
647 return '-';
648
649 return br->flags.predicted ? 'P' : 'M';
650}
651
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300652static int perf_sample__fprintf_brstack(struct perf_sample *sample,
653 struct thread *thread,
654 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200655{
656 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700657 struct addr_location alf, alt;
658 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300659 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200660
661 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300662 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200663
664 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700665 from = br->entries[i].from;
666 to = br->entries[i].to;
667
668 if (PRINT_FIELD(DSO)) {
669 memset(&alf, 0, sizeof(alf));
670 memset(&alt, 0, sizeof(alt));
671 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
672 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
673 }
674
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300675 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700676 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300677 printed += fprintf(fp, "(");
678 printed += map__fprintf_dsoname(alf.map, fp);
679 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700680 }
681
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300682 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700683 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300684 printed += fprintf(fp, "(");
685 printed += map__fprintf_dsoname(alt.map, fp);
686 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700687 }
688
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300689 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200690 mispred_str( br->entries + i),
691 br->entries[i].flags.in_tx? 'X' : '-',
692 br->entries[i].flags.abort? 'A' : '-',
693 br->entries[i].flags.cycles);
694 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300695
696 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200697}
698
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300699static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
700 struct thread *thread,
701 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200702{
703 struct branch_stack *br = sample->branch_stack;
704 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200705 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300706 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200707
708 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300709 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200710
711 for (i = 0; i < br->nr; i++) {
712
713 memset(&alf, 0, sizeof(alf));
714 memset(&alt, 0, sizeof(alt));
715 from = br->entries[i].from;
716 to = br->entries[i].to;
717
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300718 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200719 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300720 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200721
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300722 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200723 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300724 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200725
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300726 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700727 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300728 printed += fprintf(fp, "(");
729 printed += map__fprintf_dsoname(alf.map, fp);
730 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700731 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300732 printed += fprintf(fp, "%c", '/');
733 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700734 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300735 printed += fprintf(fp, "(");
736 printed += map__fprintf_dsoname(alt.map, fp);
737 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700738 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300739 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200740 mispred_str( br->entries + i),
741 br->entries[i].flags.in_tx? 'X' : '-',
742 br->entries[i].flags.abort? 'A' : '-',
743 br->entries[i].flags.cycles);
744 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300745
746 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200747}
748
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300749static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
750 struct thread *thread,
751 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700752{
753 struct branch_stack *br = sample->branch_stack;
754 struct addr_location alf, alt;
755 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300756 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700757
758 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300759 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700760
761 for (i = 0; i < br->nr; i++) {
762
763 memset(&alf, 0, sizeof(alf));
764 memset(&alt, 0, sizeof(alt));
765 from = br->entries[i].from;
766 to = br->entries[i].to;
767
768 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
769 if (alf.map && !alf.map->dso->adjust_symbols)
770 from = map__map_ip(alf.map, from);
771
772 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
773 if (alt.map && !alt.map->dso->adjust_symbols)
774 to = map__map_ip(alt.map, to);
775
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300776 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700777 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300778 printed += fprintf(fp, "(");
779 printed += map__fprintf_dsoname(alf.map, fp);
780 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700781 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300782 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700783 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300784 printed += fprintf(fp, "(");
785 printed += map__fprintf_dsoname(alt.map, fp);
786 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700787 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300788 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700789 mispred_str(br->entries + i),
790 br->entries[i].flags.in_tx ? 'X' : '-',
791 br->entries[i].flags.abort ? 'A' : '-',
792 br->entries[i].flags.cycles);
793 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300794
795 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700796}
Andi Kleen48d02a12017-02-23 15:46:34 -0800797#define MAXBB 16384UL
798
799static int grab_bb(u8 *buffer, u64 start, u64 end,
800 struct machine *machine, struct thread *thread,
801 bool *is64bit, u8 *cpumode, bool last)
802{
803 long offset, len;
804 struct addr_location al;
805 bool kernel;
806
807 if (!start || !end)
808 return 0;
809
810 kernel = machine__kernel_ip(machine, start);
811 if (kernel)
812 *cpumode = PERF_RECORD_MISC_KERNEL;
813 else
814 *cpumode = PERF_RECORD_MISC_USER;
815
816 /*
817 * Block overlaps between kernel and user.
818 * This can happen due to ring filtering
819 * On Intel CPUs the entry into the kernel is filtered,
820 * but the exit is not. Let the caller patch it up.
821 */
822 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300823 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800824 return -ENXIO;
825 }
826
827 memset(&al, 0, sizeof(al));
828 if (end - start > MAXBB - MAXINSN) {
829 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300830 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800831 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300832 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800833 return 0;
834 }
835
836 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
837 if (!al.map || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300838 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800839 return 0;
840 }
841 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300842 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800843 return 0;
844 }
845
846 /* Load maps to ensure dso->is_64_bit has been updated */
847 map__load(al.map);
848
849 offset = al.map->map_ip(al.map, start);
850 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
851 end - start + MAXINSN);
852
853 *is64bit = al.map->dso->is_64_bit;
854 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300855 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800856 start, end);
857 return len;
858}
859
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300860static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
861 struct perf_insn *x, u8 *inbuf, int len,
862 int insn, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800863{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300864 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
865 dump_insn(x, ip, inbuf, len, NULL),
866 en->flags.predicted ? " PRED" : "",
867 en->flags.mispred ? " MISPRED" : "",
868 en->flags.in_tx ? " INTX" : "",
869 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800870 if (en->flags.cycles) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300871 printed += fprintf(fp, " %d cycles", en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800872 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300873 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800874 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300875 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800876}
877
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300878static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
879 u8 cpumode, int cpu, struct symbol **lastsym,
880 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800881{
882 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300883 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800884
885 memset(&al, 0, sizeof(al));
886
887 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
888 if (!al.map)
889 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
890 addr, &al);
891 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300892 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800893
894 al.cpu = cpu;
895 al.sym = NULL;
896 if (al.map)
897 al.sym = map__find_symbol(al.map, al.addr);
898
899 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300900 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800901
902 if (al.addr < al.sym->end)
903 off = al.addr - al.sym->start;
904 else
905 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300906 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800907 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300908 printed += fprintf(fp, "%+d", off);
909 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800910 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300911 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
912 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800913 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300914
915 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800916}
917
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300918static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
919 struct thread *thread,
920 struct perf_event_attr *attr,
921 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800922{
923 struct branch_stack *br = sample->branch_stack;
924 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300925 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800926 struct perf_insn x;
927 u8 buffer[MAXBB];
928 unsigned off;
929 struct symbol *lastsym = NULL;
930
931 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300932 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800933 nr = br->nr;
934 if (max_blocks && nr > max_blocks + 1)
935 nr = max_blocks + 1;
936
937 x.thread = thread;
938 x.cpu = sample->cpu;
939
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300940 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -0800941
942 /* Handle first from jump, of which we don't know the entry. */
943 len = grab_bb(buffer, br->entries[nr-1].from,
944 br->entries[nr-1].from,
945 machine, thread, &x.is64bit, &x.cpumode, false);
946 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300947 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
948 x.cpumode, x.cpu, &lastsym, attr, fp);
949 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
950 &x, buffer, len, 0, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800951 }
952
953 /* Print all blocks */
954 for (i = nr - 2; i >= 0; i--) {
955 if (br->entries[i].from || br->entries[i].to)
956 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
957 br->entries[i].from,
958 br->entries[i].to);
959 start = br->entries[i + 1].to;
960 end = br->entries[i].from;
961
962 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
963 /* Patch up missing kernel transfers due to ring filters */
964 if (len == -ENXIO && i > 0) {
965 end = br->entries[--i].from;
966 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
967 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
968 }
969 if (len <= 0)
970 continue;
971
972 insn = 0;
973 for (off = 0;; off += ilen) {
974 uint64_t ip = start + off;
975
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300976 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800977 if (ip == end) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300978 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800979 break;
980 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300981 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
982 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -0800983 if (ilen == 0)
984 break;
985 insn++;
986 }
987 }
988 }
989
990 /*
991 * Hit the branch? In this case we are already done, and the target
992 * has not been executed yet.
993 */
994 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300995 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -0800996 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300997 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -0800998
999 /*
1000 * Print final block upto sample
1001 */
1002 start = br->entries[0].to;
1003 end = sample->ip;
1004 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001005 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001006 if (len <= 0) {
1007 /* Print at least last IP if basic block did not work */
1008 len = grab_bb(buffer, sample->ip, sample->ip,
1009 machine, thread, &x.is64bit, &x.cpumode, false);
1010 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001011 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001012
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001013 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001014 dump_insn(&x, sample->ip, buffer, len, NULL));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001015 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001016 }
1017 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001018 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1019 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001020 if (ilen == 0)
1021 break;
1022 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001023out:
1024 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001025}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001026
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001027static int perf_sample__fprintf_addr(struct perf_sample *sample,
1028 struct thread *thread,
1029 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001030{
1031 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001032 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001033
1034 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001035 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001036
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001037 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001038
1039 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001040 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001041 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001042 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001043 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001044 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001045 }
1046
1047 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001048 printed += fprintf(fp, " (");
1049 printed += map__fprintf_dsoname(al.map, fp);
1050 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001051 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001052out:
1053 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001054}
1055
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001056static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1057 struct perf_evsel *evsel,
1058 struct thread *thread,
1059 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001060{
1061 struct perf_event_attr *attr = &evsel->attr;
1062 size_t depth = thread_stack__depth(thread);
1063 struct addr_location addr_al;
1064 const char *name = NULL;
1065 static int spacing;
1066 int len = 0;
1067 u64 ip = 0;
1068
1069 /*
1070 * The 'return' has already been popped off the stack so the depth has
1071 * to be adjusted to match the 'call'.
1072 */
1073 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1074 depth += 1;
1075
1076 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1077 if (sample_addr_correlates_sym(attr)) {
1078 thread__resolve(thread, &addr_al, sample);
1079 if (addr_al.sym)
1080 name = addr_al.sym->name;
1081 else
1082 ip = sample->addr;
1083 } else {
1084 ip = sample->addr;
1085 }
1086 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1087 if (al->sym)
1088 name = al->sym->name;
1089 else
1090 ip = sample->ip;
1091 }
1092
1093 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001094 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001095 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001096 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001097
1098 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001099 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001100
1101 /*
1102 * Try to keep the output length from changing frequently so that the
1103 * output lines up more nicely.
1104 */
1105 if (len > spacing || (len && len < spacing - 52))
1106 spacing = round_up(len + 4, 32);
1107
1108 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001109 len += fprintf(fp, "%*s", spacing - len, "");
1110
1111 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001112}
1113
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001114static int perf_sample__fprintf_insn(struct perf_sample *sample,
1115 struct perf_event_attr *attr,
1116 struct thread *thread,
1117 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001118{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001119 int printed = 0;
1120
Andi Kleen224e2c92016-10-07 16:42:27 +03001121 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001122 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen224e2c92016-10-07 16:42:27 +03001123 if (PRINT_FIELD(INSN)) {
1124 int i;
1125
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001126 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001127 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001128 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001129 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001130 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001131 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1132
1133 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001134}
1135
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001136static int perf_sample__fprintf_bts(struct perf_sample *sample,
1137 struct perf_evsel *evsel,
1138 struct thread *thread,
1139 struct addr_location *al,
1140 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001141{
1142 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001143 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001144 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001145 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001146
Adrian Huntere2167082016-06-23 16:40:58 +03001147 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001148 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001149
Akihiro Nagai95582592012-01-30 13:43:09 +09001150 /* print branch_from information */
1151 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001152 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001153 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001154
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001155 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001156 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001157 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001158 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001159
1160 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001161 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001162 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001163 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001164 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001165 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001166 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001167 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001168
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001169 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001170 }
1171
Akihiro Nagai95582592012-01-30 13:43:09 +09001172 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001173 if (PRINT_FIELD(ADDR) ||
1174 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001175 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001176 printed += fprintf(fp, " => ");
1177 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001178 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001179
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001180 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001181 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001182
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001183 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1184 return printed + fprintf(fp, "\n");
Akihiro Nagai95582592012-01-30 13:43:09 +09001185}
1186
Adrian Hunter055cd332016-06-23 16:40:56 +03001187static struct {
1188 u32 flags;
1189 const char *name;
1190} sample_flags[] = {
1191 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1192 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1193 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1194 {PERF_IP_FLAG_BRANCH, "jmp"},
1195 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1196 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1197 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1198 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1199 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1200 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1201 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1202 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1203 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1204 {0, NULL}
1205};
1206
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001207static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001208{
1209 const char *chars = PERF_IP_FLAG_CHARS;
1210 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001211 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1212 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001213 char str[33];
1214 int i, pos = 0;
1215
Adrian Hunter055cd332016-06-23 16:40:56 +03001216 for (i = 0; sample_flags[i].name ; i++) {
1217 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1218 name = sample_flags[i].name;
1219 break;
1220 }
1221 }
1222
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001223 for (i = 0; i < n; i++, flags >>= 1) {
1224 if (flags & 1)
1225 str[pos++] = chars[i];
1226 }
1227 for (; i < 32; i++, flags >>= 1) {
1228 if (flags & 1)
1229 str[pos++] = '?';
1230 }
1231 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001232
1233 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001234 return fprintf(fp, " %-7s%4s ", name, in_tx ? "(x)" : "");
1235
1236 return fprintf(fp, " %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001237}
1238
Wang Nan30372f02016-02-24 11:20:45 +00001239struct printer_data {
1240 int line_no;
1241 bool hit_nul;
1242 bool is_printable;
1243};
1244
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001245static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1246 unsigned int val,
1247 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001248{
1249 unsigned char ch = (unsigned char)val;
1250 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001251 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001252
1253 switch (op) {
1254 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001255 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001256 break;
1257 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001258 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001259 " ");
1260 break;
1261 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001262 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001263 break;
1264 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001265 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001266 break;
1267 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001268 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001269 break;
1270 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001271 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001272 break;
1273 case BINARY_PRINT_CHAR_DATA:
1274 if (printer_data->hit_nul && ch)
1275 printer_data->is_printable = false;
1276
1277 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001278 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001279
1280 if (!printer_data->is_printable)
1281 break;
1282
1283 if (ch == '\0')
1284 printer_data->hit_nul = true;
1285 else
1286 printer_data->is_printable = false;
1287 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001288 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001289 }
1290 break;
1291 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001292 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001293 break;
1294 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001295 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001296 printer_data->line_no++;
1297 break;
1298 case BINARY_PRINT_DATA_END:
1299 default:
1300 break;
1301 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001302
1303 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001304}
1305
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001306static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001307{
1308 unsigned int nr_bytes = sample->raw_size;
1309 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001310 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1311 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001312
1313 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001314 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1315
1316 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001317}
1318
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001319static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001320{
1321 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001322 return fprintf(fp, "%*s", spacing - len, "");
1323
1324 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001325}
1326
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001327static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001328{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001329 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001330}
1331
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001332static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001333{
1334 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1335 int len;
1336
1337 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001338 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001339
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001340 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001341 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001342 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001343}
1344
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001345static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001346{
1347 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1348 int len;
1349
1350 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001351 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001352
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001353 len = fprintf(fp, " hints: %#x extensions: %#x ",
1354 data->hints, data->extensions);
1355 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001356}
1357
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001358static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001359{
1360 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1361 int len;
1362
1363 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001364 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001365
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001366 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1367 data->hw, data->cstate, data->subcstate);
1368 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001369}
1370
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001371static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001372{
1373 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1374 int len;
1375
1376 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001377 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001378
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001379 len = fprintf(fp, " IP: %u ", data->ip);
1380 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001381}
1382
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001383static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001384{
1385 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1386 int len;
1387
1388 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001389 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001390
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001391 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001392 data->deepest_cstate, data->last_cstate,
1393 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001394 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001395}
1396
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001397static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001398{
1399 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1400 unsigned int percent, freq;
1401 int len;
1402
1403 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001404 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001405
1406 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001407 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001408 if (data->max_nonturbo) {
1409 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001410 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001411 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001412 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001413}
1414
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001415static int perf_sample__fprintf_synth(struct perf_sample *sample,
1416 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001417{
1418 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001419 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001420 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001421 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001422 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001423 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001424 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001425 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001426 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001427 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001428 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001429 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001430 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001431 default:
1432 break;
1433 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001434
1435 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001436}
1437
Jiri Olsa809e9422015-11-26 18:55:21 +01001438struct perf_script {
1439 struct perf_tool tool;
1440 struct perf_session *session;
1441 bool show_task_events;
1442 bool show_mmap_events;
1443 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301444 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001445 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001446 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001447 struct cpu_map *cpus;
1448 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001449 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001450 const char *time_str;
1451 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001452};
1453
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001454static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1455{
1456 struct perf_evsel *evsel;
1457 int max = 0;
1458
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001459 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001460 int len = strlen(perf_evsel__name(evsel));
1461
1462 max = MAX(len, max);
1463 }
1464
1465 return max;
1466}
1467
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001468static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001469{
1470 struct mem_info mi = { .data_src.val = data_src };
1471 char decode[100];
1472 char out[100];
1473 static int maxlen;
1474 int len;
1475
1476 perf_script__meminfo_scnprintf(decode, 100, &mi);
1477
1478 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1479 if (maxlen < len)
1480 maxlen = len;
1481
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001482 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001483}
1484
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001485struct metric_ctx {
1486 struct perf_sample *sample;
1487 struct thread *thread;
1488 struct perf_evsel *evsel;
1489 FILE *fp;
1490};
1491
1492static void script_print_metric(void *ctx, const char *color,
1493 const char *fmt,
1494 const char *unit, double val)
1495{
1496 struct metric_ctx *mctx = ctx;
1497
1498 if (!fmt)
1499 return;
1500 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
1501 mctx->fp);
1502 fputs("\tmetric: ", mctx->fp);
1503 if (color)
1504 color_fprintf(mctx->fp, color, fmt, val);
1505 else
1506 printf(fmt, val);
1507 fprintf(mctx->fp, " %s\n", unit);
1508}
1509
1510static void script_new_line(void *ctx)
1511{
1512 struct metric_ctx *mctx = ctx;
1513
1514 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
1515 mctx->fp);
1516 fputs("\tmetric: ", mctx->fp);
1517}
1518
1519static void perf_sample__fprint_metric(struct perf_script *script,
1520 struct thread *thread,
1521 struct perf_evsel *evsel,
1522 struct perf_sample *sample,
1523 FILE *fp)
1524{
1525 struct perf_stat_output_ctx ctx = {
1526 .print_metric = script_print_metric,
1527 .new_line = script_new_line,
1528 .ctx = &(struct metric_ctx) {
1529 .sample = sample,
1530 .thread = thread,
1531 .evsel = evsel,
1532 .fp = fp,
1533 },
1534 .force_header = false,
1535 };
1536 struct perf_evsel *ev2;
1537 static bool init;
1538 u64 val;
1539
1540 if (!init) {
1541 perf_stat__init_shadow_stats();
1542 init = true;
1543 }
1544 if (!evsel->stats)
1545 perf_evlist__alloc_stats(script->session->evlist, false);
1546 if (evsel_script(evsel->leader)->gnum++ == 0)
1547 perf_stat__reset_shadow_stats();
1548 val = sample->period * evsel->scale;
1549 perf_stat__update_shadow_stats(evsel,
1550 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001551 sample->cpu,
1552 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001553 evsel_script(evsel)->val = val;
1554 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1555 for_each_group_member (ev2, evsel->leader) {
1556 perf_stat__print_shadow_stats(ev2,
1557 evsel_script(ev2)->val,
1558 sample->cpu,
1559 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001560 NULL,
1561 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001562 }
1563 evsel_script(evsel->leader)->gnum = 0;
1564 }
1565}
1566
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001567static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001568 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001569 struct addr_location *al,
1570 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001571{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001572 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001573 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001574 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001575 struct perf_evsel_script *es = evsel->priv;
1576 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001577
Adrian Hunter14057202017-06-21 13:17:19 +03001578 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001579 return;
1580
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001581 ++es->samples;
1582
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001583 perf_sample__fprintf_start(sample, thread, evsel, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001584
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001585 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001586 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001587
Namhyung Kime944d3d2013-11-18 14:34:52 +09001588 if (PRINT_FIELD(EVNAME)) {
1589 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001590
1591 if (!script->name_width)
1592 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1593
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001594 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001595 }
1596
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001597 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001598 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001599
Akihiro Nagai95582592012-01-30 13:43:09 +09001600 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001601 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001602 return;
1603 }
1604
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001605 if (PRINT_FIELD(TRACE)) {
1606 event_format__fprintf(evsel->tp_format, sample->cpu,
1607 sample->raw_data, sample->raw_size, fp);
1608 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001609
1610 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001611 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001612
David Ahern7cec0922011-05-30 13:08:23 -06001613 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001614 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001615
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001616 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001617 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001618
1619 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001620 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001621
David Ahern787bef12011-05-27 14:28:43 -06001622 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001623 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001624
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001625 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001626 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001627 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001628 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001629
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001630 fputc(cursor ? '\n' : ' ', fp);
1631 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001632 }
1633
Stephane Eranianfc36f942015-08-31 18:41:10 +02001634 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001635 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001636
Andi Kleenb1491ac2017-09-05 11:40:57 -07001637 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001638 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001639
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001640 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001641 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001642 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001643 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001644 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001645 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001646
Wang Nan30372f02016-02-24 11:20:45 +00001647 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001648 perf_sample__fprintf_bpf_output(sample, fp);
1649 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001650
1651 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001652 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1653 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001654
1655 if (PRINT_FIELD(METRIC))
1656 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001657}
1658
Tom Zanussi956ffd02009-11-25 01:15:46 -06001659static struct scripting_ops *scripting_ops;
1660
Jiri Olsa36e33c52016-01-06 11:49:56 +01001661static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1662{
1663 int nthreads = thread_map__nr(counter->threads);
1664 int ncpus = perf_evsel__nr_cpus(counter);
1665 int cpu, thread;
1666 static int header_printed;
1667
1668 if (counter->system_wide)
1669 nthreads = 1;
1670
1671 if (!header_printed) {
1672 printf("%3s %8s %15s %15s %15s %15s %s\n",
1673 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1674 header_printed = 1;
1675 }
1676
1677 for (thread = 0; thread < nthreads; thread++) {
1678 for (cpu = 0; cpu < ncpus; cpu++) {
1679 struct perf_counts_values *counts;
1680
1681 counts = perf_counts(counter->counts, cpu, thread);
1682
1683 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1684 counter->cpus->map[cpu],
1685 thread_map__pid(counter->threads, thread),
1686 counts->val,
1687 counts->ena,
1688 counts->run,
1689 tstamp,
1690 perf_evsel__name(counter));
1691 }
1692 }
1693}
1694
Jiri Olsae099eba2016-01-05 22:09:09 +01001695static void process_stat(struct perf_evsel *counter, u64 tstamp)
1696{
1697 if (scripting_ops && scripting_ops->process_stat)
1698 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001699 else
1700 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001701}
1702
1703static void process_stat_interval(u64 tstamp)
1704{
1705 if (scripting_ops && scripting_ops->process_stat_interval)
1706 scripting_ops->process_stat_interval(tstamp);
1707}
1708
Tom Zanussi956ffd02009-11-25 01:15:46 -06001709static void setup_scripting(void)
1710{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001711 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001712 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001713}
1714
Adrian Hunterd445dd22014-08-15 22:08:37 +03001715static int flush_scripting(void)
1716{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001717 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001718}
1719
Tom Zanussi956ffd02009-11-25 01:15:46 -06001720static int cleanup_scripting(void)
1721{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001722 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001723
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001724 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001725}
1726
Jiri Olsa809e9422015-11-26 18:55:21 +01001727static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001728 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001729 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001730 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001731 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001732{
Jiri Olsa809e9422015-11-26 18:55:21 +01001733 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001734 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001735
David Aherna91f4c42016-11-29 10:15:43 -07001736 if (perf_time__skip_sample(&scr->ptime, sample->time))
1737 return 0;
1738
David Ahern1424dc92011-03-09 22:23:28 -07001739 if (debug_mode) {
1740 if (sample->time < last_timestamp) {
1741 pr_err("Samples misordered, previous: %" PRIu64
1742 " this: %" PRIu64 "\n", last_timestamp,
1743 sample->time);
1744 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001745 }
David Ahern1424dc92011-03-09 22:23:28 -07001746 last_timestamp = sample->time;
1747 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001748 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001749
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001750 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001751 pr_err("problem processing %d event, skipping it.\n",
1752 event->header.type);
1753 return -1;
1754 }
1755
1756 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001757 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001758
Anton Blanchard5d67be92011-07-04 21:57:50 +10001759 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001760 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001761
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001762 if (scripting_ops)
1763 scripting_ops->process_event(event, sample, evsel, &al);
1764 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001765 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001766
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001767out_put:
1768 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001769 return 0;
1770}
1771
Adrian Hunter7ea95722013-11-01 15:51:30 +02001772static int process_attr(struct perf_tool *tool, union perf_event *event,
1773 struct perf_evlist **pevlist)
1774{
1775 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1776 struct perf_evlist *evlist;
1777 struct perf_evsel *evsel, *pos;
1778 int err;
1779
1780 err = perf_event__process_attr(tool, event, pevlist);
1781 if (err)
1782 return err;
1783
1784 evlist = *pevlist;
1785 evsel = perf_evlist__last(*pevlist);
1786
Adrian Hunter14057202017-06-21 13:17:19 +03001787 if (evsel->attr.type >= PERF_TYPE_MAX &&
1788 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001789 return 0;
1790
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001791 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001792 if (pos->attr.type == evsel->attr.type && pos != evsel)
1793 return 0;
1794 }
1795
1796 set_print_ip_opts(&evsel->attr);
1797
Jiri Olsad2b5a312015-10-16 12:41:25 +02001798 if (evsel->attr.sample_type)
1799 err = perf_evsel__check_attr(evsel, scr->session);
1800
1801 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001802}
1803
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001804static int process_comm_event(struct perf_tool *tool,
1805 union perf_event *event,
1806 struct perf_sample *sample,
1807 struct machine *machine)
1808{
1809 struct thread *thread;
1810 struct perf_script *script = container_of(tool, struct perf_script, tool);
1811 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001812 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001813 int ret = -1;
1814
1815 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1816 if (thread == NULL) {
1817 pr_debug("problem processing COMM event, skipping it.\n");
1818 return -1;
1819 }
1820
1821 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1822 goto out;
1823
1824 if (!evsel->attr.sample_id_all) {
1825 sample->cpu = 0;
1826 sample->time = 0;
1827 sample->tid = event->comm.tid;
1828 sample->pid = event->comm.pid;
1829 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001830 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001831 perf_event__fprintf(event, stdout);
1832 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001833out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001834 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001835 return ret;
1836}
1837
Hari Bathini96a44bb2017-03-08 02:12:06 +05301838static int process_namespaces_event(struct perf_tool *tool,
1839 union perf_event *event,
1840 struct perf_sample *sample,
1841 struct machine *machine)
1842{
1843 struct thread *thread;
1844 struct perf_script *script = container_of(tool, struct perf_script, tool);
1845 struct perf_session *session = script->session;
1846 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1847 int ret = -1;
1848
1849 thread = machine__findnew_thread(machine, event->namespaces.pid,
1850 event->namespaces.tid);
1851 if (thread == NULL) {
1852 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1853 return -1;
1854 }
1855
1856 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1857 goto out;
1858
1859 if (!evsel->attr.sample_id_all) {
1860 sample->cpu = 0;
1861 sample->time = 0;
1862 sample->tid = event->namespaces.tid;
1863 sample->pid = event->namespaces.pid;
1864 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001865 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Hari Bathini96a44bb2017-03-08 02:12:06 +05301866 perf_event__fprintf(event, stdout);
1867 ret = 0;
1868out:
1869 thread__put(thread);
1870 return ret;
1871}
1872
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001873static int process_fork_event(struct perf_tool *tool,
1874 union perf_event *event,
1875 struct perf_sample *sample,
1876 struct machine *machine)
1877{
1878 struct thread *thread;
1879 struct perf_script *script = container_of(tool, struct perf_script, tool);
1880 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001881 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001882
1883 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1884 return -1;
1885
1886 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1887 if (thread == NULL) {
1888 pr_debug("problem processing FORK event, skipping it.\n");
1889 return -1;
1890 }
1891
1892 if (!evsel->attr.sample_id_all) {
1893 sample->cpu = 0;
1894 sample->time = event->fork.time;
1895 sample->tid = event->fork.tid;
1896 sample->pid = event->fork.pid;
1897 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001898 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001899 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001900 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001901
1902 return 0;
1903}
1904static int process_exit_event(struct perf_tool *tool,
1905 union perf_event *event,
1906 struct perf_sample *sample,
1907 struct machine *machine)
1908{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001909 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001910 struct thread *thread;
1911 struct perf_script *script = container_of(tool, struct perf_script, tool);
1912 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001913 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001914
1915 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1916 if (thread == NULL) {
1917 pr_debug("problem processing EXIT event, skipping it.\n");
1918 return -1;
1919 }
1920
1921 if (!evsel->attr.sample_id_all) {
1922 sample->cpu = 0;
1923 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001924 sample->tid = event->fork.tid;
1925 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001926 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001927 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001928 perf_event__fprintf(event, stdout);
1929
1930 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001931 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001932
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001933 thread__put(thread);
1934 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001935}
1936
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001937static int process_mmap_event(struct perf_tool *tool,
1938 union perf_event *event,
1939 struct perf_sample *sample,
1940 struct machine *machine)
1941{
1942 struct thread *thread;
1943 struct perf_script *script = container_of(tool, struct perf_script, tool);
1944 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001945 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001946
1947 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1948 return -1;
1949
1950 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1951 if (thread == NULL) {
1952 pr_debug("problem processing MMAP event, skipping it.\n");
1953 return -1;
1954 }
1955
1956 if (!evsel->attr.sample_id_all) {
1957 sample->cpu = 0;
1958 sample->time = 0;
1959 sample->tid = event->mmap.tid;
1960 sample->pid = event->mmap.pid;
1961 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001962 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001963 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001964 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001965 return 0;
1966}
1967
1968static int process_mmap2_event(struct perf_tool *tool,
1969 union perf_event *event,
1970 struct perf_sample *sample,
1971 struct machine *machine)
1972{
1973 struct thread *thread;
1974 struct perf_script *script = container_of(tool, struct perf_script, tool);
1975 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001976 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001977
1978 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1979 return -1;
1980
1981 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1982 if (thread == NULL) {
1983 pr_debug("problem processing MMAP2 event, skipping it.\n");
1984 return -1;
1985 }
1986
1987 if (!evsel->attr.sample_id_all) {
1988 sample->cpu = 0;
1989 sample->time = 0;
1990 sample->tid = event->mmap2.tid;
1991 sample->pid = event->mmap2.pid;
1992 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001993 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001994 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001995 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001996 return 0;
1997}
1998
Adrian Hunter7c148982015-07-21 12:44:06 +03001999static int process_switch_event(struct perf_tool *tool,
2000 union perf_event *event,
2001 struct perf_sample *sample,
2002 struct machine *machine)
2003{
2004 struct thread *thread;
2005 struct perf_script *script = container_of(tool, struct perf_script, tool);
2006 struct perf_session *session = script->session;
2007 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2008
2009 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2010 return -1;
2011
2012 thread = machine__findnew_thread(machine, sample->pid,
2013 sample->tid);
2014 if (thread == NULL) {
2015 pr_debug("problem processing SWITCH event, skipping it.\n");
2016 return -1;
2017 }
2018
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002019 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Adrian Hunter7c148982015-07-21 12:44:06 +03002020 perf_event__fprintf(event, stdout);
2021 thread__put(thread);
2022 return 0;
2023}
2024
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002025static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002026{
2027 session_done = 1;
2028}
2029
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002030static void perf_script__fclose_per_event_dump(struct perf_script *script)
2031{
2032 struct perf_evlist *evlist = script->session->evlist;
2033 struct perf_evsel *evsel;
2034
2035 evlist__for_each_entry(evlist, evsel) {
2036 if (!evsel->priv)
2037 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002038 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002039 evsel->priv = NULL;
2040 }
2041}
2042
2043static int perf_script__fopen_per_event_dump(struct perf_script *script)
2044{
2045 struct perf_evsel *evsel;
2046
2047 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002048 /*
2049 * Already setup? I.e. we may be called twice in cases like
2050 * Intel PT, one for the intel_pt// and dummy events, then
2051 * for the evsels syntheized from the auxtrace info.
2052 *
2053 * Ses perf_script__process_auxtrace_info.
2054 */
2055 if (evsel->priv != NULL)
2056 continue;
2057
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002058 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002059 if (evsel->priv == NULL)
2060 goto out_err_fclose;
2061 }
2062
2063 return 0;
2064
2065out_err_fclose:
2066 perf_script__fclose_per_event_dump(script);
2067 return -1;
2068}
2069
2070static int perf_script__setup_per_event_dump(struct perf_script *script)
2071{
2072 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002073 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002074
2075 if (script->per_event_dump)
2076 return perf_script__fopen_per_event_dump(script);
2077
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002078 es_stdout.fp = stdout;
2079
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002080 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002081 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002082
2083 return 0;
2084}
2085
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002086static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2087{
2088 struct perf_evsel *evsel;
2089
2090 evlist__for_each_entry(script->session->evlist, evsel) {
2091 struct perf_evsel_script *es = evsel->priv;
2092
2093 perf_evsel_script__fprintf(es, stdout);
2094 perf_evsel_script__delete(es);
2095 evsel->priv = NULL;
2096 }
2097}
2098
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002099static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002100{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002101 int ret;
2102
Tom Zanussic239da32010-04-01 23:59:18 -05002103 signal(SIGINT, sig_handler);
2104
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002105 /* override event processing functions */
2106 if (script->show_task_events) {
2107 script->tool.comm = process_comm_event;
2108 script->tool.fork = process_fork_event;
2109 script->tool.exit = process_exit_event;
2110 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002111 if (script->show_mmap_events) {
2112 script->tool.mmap = process_mmap_event;
2113 script->tool.mmap2 = process_mmap2_event;
2114 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002115 if (script->show_switch_events)
2116 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302117 if (script->show_namespace_events)
2118 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002119
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002120 if (perf_script__setup_per_event_dump(script)) {
2121 pr_err("Couldn't create the per event dump files\n");
2122 return -1;
2123 }
2124
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002125 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002126
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002127 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002128 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002129
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002130 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002131 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002132
2133 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002134}
2135
Tom Zanussi956ffd02009-11-25 01:15:46 -06002136struct script_spec {
2137 struct list_head node;
2138 struct scripting_ops *ops;
2139 char spec[0];
2140};
2141
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002142static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002143
2144static struct script_spec *script_spec__new(const char *spec,
2145 struct scripting_ops *ops)
2146{
2147 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2148
2149 if (s != NULL) {
2150 strcpy(s->spec, spec);
2151 s->ops = ops;
2152 }
2153
2154 return s;
2155}
2156
Tom Zanussi956ffd02009-11-25 01:15:46 -06002157static void script_spec__add(struct script_spec *s)
2158{
2159 list_add_tail(&s->node, &script_specs);
2160}
2161
2162static struct script_spec *script_spec__find(const char *spec)
2163{
2164 struct script_spec *s;
2165
2166 list_for_each_entry(s, &script_specs, node)
2167 if (strcasecmp(s->spec, spec) == 0)
2168 return s;
2169 return NULL;
2170}
2171
Tom Zanussi956ffd02009-11-25 01:15:46 -06002172int script_spec_register(const char *spec, struct scripting_ops *ops)
2173{
2174 struct script_spec *s;
2175
2176 s = script_spec__find(spec);
2177 if (s)
2178 return -1;
2179
Taeung Song8560bae2016-02-26 00:13:10 +09002180 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002181 if (!s)
2182 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002183 else
2184 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002185
2186 return 0;
2187}
2188
2189static struct scripting_ops *script_spec__lookup(const char *spec)
2190{
2191 struct script_spec *s = script_spec__find(spec);
2192 if (!s)
2193 return NULL;
2194
2195 return s->ops;
2196}
2197
2198static void list_available_languages(void)
2199{
2200 struct script_spec *s;
2201
2202 fprintf(stderr, "\n");
2203 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002204 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002205
2206 list_for_each_entry(s, &script_specs, node)
2207 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2208
2209 fprintf(stderr, "\n");
2210}
2211
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002212static int parse_scriptname(const struct option *opt __maybe_unused,
2213 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002214{
2215 char spec[PATH_MAX];
2216 const char *script, *ext;
2217 int len;
2218
Tom Zanussif526d682010-01-27 02:27:52 -06002219 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002220 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002221 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002222 }
2223
2224 script = strchr(str, ':');
2225 if (script) {
2226 len = script - str;
2227 if (len >= PATH_MAX) {
2228 fprintf(stderr, "invalid language specifier");
2229 return -1;
2230 }
2231 strncpy(spec, str, len);
2232 spec[len] = '\0';
2233 scripting_ops = script_spec__lookup(spec);
2234 if (!scripting_ops) {
2235 fprintf(stderr, "invalid language specifier");
2236 return -1;
2237 }
2238 script++;
2239 } else {
2240 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002241 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002242 if (!ext) {
2243 fprintf(stderr, "invalid script extension");
2244 return -1;
2245 }
2246 scripting_ops = script_spec__lookup(++ext);
2247 if (!scripting_ops) {
2248 fprintf(stderr, "invalid script extension");
2249 return -1;
2250 }
2251 }
2252
2253 script_name = strdup(script);
2254
2255 return 0;
2256}
2257
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002258static int parse_output_fields(const struct option *opt __maybe_unused,
2259 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002260{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002261 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002262 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002263 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002264 int rc = 0;
2265 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002266 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002267 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002268
2269 if (!str)
2270 return -ENOMEM;
2271
David Ahern2c9e45f72011-03-17 10:03:21 -06002272 /* first word can state for which event type the user is specifying
2273 * the fields. If no type exists, the specified fields apply to all
2274 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002275 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002276 tok = strchr(str, ':');
2277 if (tok) {
2278 *tok = '\0';
2279 tok++;
2280 if (!strcmp(str, "hw"))
2281 type = PERF_TYPE_HARDWARE;
2282 else if (!strcmp(str, "sw"))
2283 type = PERF_TYPE_SOFTWARE;
2284 else if (!strcmp(str, "trace"))
2285 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002286 else if (!strcmp(str, "raw"))
2287 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002288 else if (!strcmp(str, "break"))
2289 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002290 else if (!strcmp(str, "synth"))
2291 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002292 else {
2293 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002294 rc = -EINVAL;
2295 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002296 }
2297
2298 if (output[type].user_set)
2299 pr_warning("Overriding previous field request for %s events.\n",
2300 event_type(type));
2301
2302 output[type].fields = 0;
2303 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002304 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002305
2306 } else {
2307 tok = str;
2308 if (strlen(str) == 0) {
2309 fprintf(stderr,
2310 "Cannot set fields to 'none' for all event types.\n");
2311 rc = -EINVAL;
2312 goto out;
2313 }
2314
Andi Kleen36ce5652017-06-02 08:48:10 -07002315 /* Don't override defaults for +- */
2316 if (strchr(str, '+') || strchr(str, '-'))
2317 goto parse;
2318
David Ahern2c9e45f72011-03-17 10:03:21 -06002319 if (output_set_by_user())
2320 pr_warning("Overriding previous field request for all events.\n");
2321
Adrian Hunter14057202017-06-21 13:17:19 +03002322 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002323 output[j].fields = 0;
2324 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002325 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002326 }
David Ahern1424dc92011-03-09 22:23:28 -07002327 }
2328
Andi Kleen36ce5652017-06-02 08:48:10 -07002329parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002330 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002331 if (*tok == '+') {
2332 if (change == SET)
2333 goto out_badmix;
2334 change = ADD;
2335 tok++;
2336 } else if (*tok == '-') {
2337 if (change == SET)
2338 goto out_badmix;
2339 change = REMOVE;
2340 tok++;
2341 } else {
2342 if (change != SET && change != DEFAULT)
2343 goto out_badmix;
2344 change = SET;
2345 }
2346
David Ahern745f43e2011-03-09 22:23:26 -07002347 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002348 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002349 break;
David Ahern745f43e2011-03-09 22:23:26 -07002350 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002351 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002352 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002353 continue;
2354 }
David Ahern745f43e2011-03-09 22:23:26 -07002355 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002356 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002357 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002358 goto out;
2359 }
2360
2361 if (type == -1) {
2362 /* add user option to all events types for
2363 * which it is valid
2364 */
Adrian Hunter14057202017-06-21 13:17:19 +03002365 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002366 if (output[j].invalid_fields & all_output_options[i].field) {
2367 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2368 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002369 } else {
2370 if (change == REMOVE)
2371 output[j].fields &= ~all_output_options[i].field;
2372 else
2373 output[j].fields |= all_output_options[i].field;
2374 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002375 }
2376 } else {
2377 if (output[type].invalid_fields & all_output_options[i].field) {
2378 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2379 all_output_options[i].str, event_type(type));
2380
2381 rc = -EINVAL;
2382 goto out;
2383 }
2384 output[type].fields |= all_output_options[i].field;
2385 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002386 }
2387
2388 if (type >= 0) {
2389 if (output[type].fields == 0) {
2390 pr_debug("No fields requested for %s type. "
2391 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002392 }
David Ahern1424dc92011-03-09 22:23:28 -07002393 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002394 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002395
Andi Kleen36ce5652017-06-02 08:48:10 -07002396out_badmix:
2397 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2398 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002399out:
David Ahern745f43e2011-03-09 22:23:26 -07002400 free(str);
2401 return rc;
2402}
2403
Shawn Bohrer008f29d2010-11-21 10:09:39 -06002404/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
2405static int is_directory(const char *base_path, const struct dirent *dent)
2406{
2407 char path[PATH_MAX];
2408 struct stat st;
2409
2410 sprintf(path, "%s/%s", base_path, dent->d_name);
2411 if (stat(path, &st))
2412 return 0;
2413
2414 return S_ISDIR(st.st_mode);
2415}
2416
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002417#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2418 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2419 if ((lang_dirent->d_type == DT_DIR || \
2420 (lang_dirent->d_type == DT_UNKNOWN && \
2421 is_directory(scripts_path, lang_dirent))) && \
2422 (strcmp(lang_dirent->d_name, ".")) && \
2423 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002424
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002425#define for_each_script(lang_path, lang_dir, script_dirent) \
2426 while ((script_dirent = readdir(lang_dir)) != NULL) \
2427 if (script_dirent->d_type != DT_DIR && \
2428 (script_dirent->d_type != DT_UNKNOWN || \
2429 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002430
2431
2432#define RECORD_SUFFIX "-record"
2433#define REPORT_SUFFIX "-report"
2434
2435struct script_desc {
2436 struct list_head node;
2437 char *name;
2438 char *half_liner;
2439 char *args;
2440};
2441
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002442static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002443
2444static struct script_desc *script_desc__new(const char *name)
2445{
2446 struct script_desc *s = zalloc(sizeof(*s));
2447
Tom Zanussib5b87312010-11-10 08:16:51 -06002448 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002449 s->name = strdup(name);
2450
2451 return s;
2452}
2453
2454static void script_desc__delete(struct script_desc *s)
2455{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002456 zfree(&s->name);
2457 zfree(&s->half_liner);
2458 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002459 free(s);
2460}
2461
2462static void script_desc__add(struct script_desc *s)
2463{
2464 list_add_tail(&s->node, &script_descs);
2465}
2466
2467static struct script_desc *script_desc__find(const char *name)
2468{
2469 struct script_desc *s;
2470
2471 list_for_each_entry(s, &script_descs, node)
2472 if (strcasecmp(s->name, name) == 0)
2473 return s;
2474 return NULL;
2475}
2476
2477static struct script_desc *script_desc__findnew(const char *name)
2478{
2479 struct script_desc *s = script_desc__find(name);
2480
2481 if (s)
2482 return s;
2483
2484 s = script_desc__new(name);
2485 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002486 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002487
2488 script_desc__add(s);
2489
2490 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002491}
2492
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002493static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002494{
2495 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002496 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002497
2498 if (strlen(str) > suffix_len) {
2499 p = str + strlen(str) - suffix_len;
2500 if (!strncmp(p, suffix, suffix_len))
2501 return p;
2502 }
2503
2504 return NULL;
2505}
2506
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002507static int read_script_info(struct script_desc *desc, const char *filename)
2508{
2509 char line[BUFSIZ], *p;
2510 FILE *fp;
2511
2512 fp = fopen(filename, "r");
2513 if (!fp)
2514 return -1;
2515
2516 while (fgets(line, sizeof(line), fp)) {
2517 p = ltrim(line);
2518 if (strlen(p) == 0)
2519 continue;
2520 if (*p != '#')
2521 continue;
2522 p++;
2523 if (strlen(p) && *p == '!')
2524 continue;
2525
2526 p = ltrim(p);
2527 if (strlen(p) && p[strlen(p) - 1] == '\n')
2528 p[strlen(p) - 1] = '\0';
2529
2530 if (!strncmp(p, "description:", strlen("description:"))) {
2531 p += strlen("description:");
2532 desc->half_liner = strdup(ltrim(p));
2533 continue;
2534 }
2535
2536 if (!strncmp(p, "args:", strlen("args:"))) {
2537 p += strlen("args:");
2538 desc->args = strdup(ltrim(p));
2539 continue;
2540 }
2541 }
2542
2543 fclose(fp);
2544
2545 return 0;
2546}
2547
Robert Richter38efb532011-11-25 11:38:40 +01002548static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2549{
2550 char *script_root, *str;
2551
2552 script_root = strdup(script_dirent->d_name);
2553 if (!script_root)
2554 return NULL;
2555
2556 str = (char *)ends_with(script_root, suffix);
2557 if (!str) {
2558 free(script_root);
2559 return NULL;
2560 }
2561
2562 *str = '\0';
2563 return script_root;
2564}
2565
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002566static int list_available_scripts(const struct option *opt __maybe_unused,
2567 const char *s __maybe_unused,
2568 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002569{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002570 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002571 char scripts_path[MAXPATHLEN];
2572 DIR *scripts_dir, *lang_dir;
2573 char script_path[MAXPATHLEN];
2574 char lang_path[MAXPATHLEN];
2575 struct script_desc *desc;
2576 char first_half[BUFSIZ];
2577 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002578
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002579 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002580
2581 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002582 if (!scripts_dir) {
2583 fprintf(stdout,
2584 "open(%s) failed.\n"
2585 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2586 scripts_path);
2587 exit(-1);
2588 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002589
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002590 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002591 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002592 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002593 lang_dir = opendir(lang_path);
2594 if (!lang_dir)
2595 continue;
2596
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002597 for_each_script(lang_path, lang_dir, script_dirent) {
2598 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002599 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002600 desc = script_desc__findnew(script_root);
2601 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002602 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002603 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002604 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002605 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002606 }
2607 }
2608
2609 fprintf(stdout, "List of available trace scripts:\n");
2610 list_for_each_entry(desc, &script_descs, node) {
2611 sprintf(first_half, "%s %s", desc->name,
2612 desc->args ? desc->args : "");
2613 fprintf(stdout, " %-36s %s\n", first_half,
2614 desc->half_liner ? desc->half_liner : "");
2615 }
2616
2617 exit(0);
2618}
2619
Feng Tange5f37052012-09-07 16:42:26 +08002620/*
Feng Tang49e639e2012-10-30 11:56:03 +08002621 * Some scripts specify the required events in their "xxx-record" file,
2622 * this function will check if the events in perf.data match those
2623 * mentioned in the "xxx-record".
2624 *
2625 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2626 * which is covered well now. And new parsing code should be added to
2627 * cover the future complexing formats like event groups etc.
2628 */
2629static int check_ev_match(char *dir_name, char *scriptname,
2630 struct perf_session *session)
2631{
2632 char filename[MAXPATHLEN], evname[128];
2633 char line[BUFSIZ], *p;
2634 struct perf_evsel *pos;
2635 int match, len;
2636 FILE *fp;
2637
2638 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2639
2640 fp = fopen(filename, "r");
2641 if (!fp)
2642 return -1;
2643
2644 while (fgets(line, sizeof(line), fp)) {
2645 p = ltrim(line);
2646 if (*p == '#')
2647 continue;
2648
2649 while (strlen(p)) {
2650 p = strstr(p, "-e");
2651 if (!p)
2652 break;
2653
2654 p += 2;
2655 p = ltrim(p);
2656 len = strcspn(p, " \t");
2657 if (!len)
2658 break;
2659
2660 snprintf(evname, len + 1, "%s", p);
2661
2662 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002663 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002664 if (!strcmp(perf_evsel__name(pos), evname)) {
2665 match = 1;
2666 break;
2667 }
2668 }
2669
2670 if (!match) {
2671 fclose(fp);
2672 return -1;
2673 }
2674 }
2675 }
2676
2677 fclose(fp);
2678 return 0;
2679}
2680
2681/*
Feng Tange5f37052012-09-07 16:42:26 +08002682 * Return -1 if none is found, otherwise the actual scripts number.
2683 *
2684 * Currently the only user of this function is the script browser, which
2685 * will list all statically runnable scripts, select one, execute it and
2686 * show the output in a perf browser.
2687 */
2688int find_scripts(char **scripts_array, char **scripts_path_array)
2689{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002690 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002691 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002692 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002693 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002694 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002695 .file = {
2696 .path = input_name,
2697 },
2698 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002699 };
Feng Tange5f37052012-09-07 16:42:26 +08002700 char *temp;
2701 int i = 0;
2702
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002703 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002704 if (!session)
2705 return -1;
2706
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002707 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002708
2709 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002710 if (!scripts_dir) {
2711 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002712 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002713 }
Feng Tange5f37052012-09-07 16:42:26 +08002714
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002715 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002716 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002717 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002718#ifdef NO_LIBPERL
2719 if (strstr(lang_path, "perl"))
2720 continue;
2721#endif
2722#ifdef NO_LIBPYTHON
2723 if (strstr(lang_path, "python"))
2724 continue;
2725#endif
2726
2727 lang_dir = opendir(lang_path);
2728 if (!lang_dir)
2729 continue;
2730
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002731 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002732 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002733 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002734 continue;
2735 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002736 script_dirent->d_name);
2737 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002738 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002739 (temp - script_dirent->d_name) + 1,
2740 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002741
2742 if (check_ev_match(lang_path,
2743 scripts_array[i], session))
2744 continue;
2745
Feng Tange5f37052012-09-07 16:42:26 +08002746 i++;
2747 }
Feng Tang49e639e2012-10-30 11:56:03 +08002748 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002749 }
2750
Feng Tang49e639e2012-10-30 11:56:03 +08002751 closedir(scripts_dir);
2752 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002753 return i;
2754}
2755
Tom Zanussi38752942009-12-15 02:53:39 -06002756static char *get_script_path(const char *script_root, const char *suffix)
2757{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002758 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002759 char scripts_path[MAXPATHLEN];
2760 char script_path[MAXPATHLEN];
2761 DIR *scripts_dir, *lang_dir;
2762 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002763 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002764
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002765 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002766
2767 scripts_dir = opendir(scripts_path);
2768 if (!scripts_dir)
2769 return NULL;
2770
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002771 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002772 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002773 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002774 lang_dir = opendir(lang_path);
2775 if (!lang_dir)
2776 continue;
2777
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002778 for_each_script(lang_path, lang_dir, script_dirent) {
2779 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002780 if (__script_root && !strcmp(script_root, __script_root)) {
2781 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002782 closedir(lang_dir);
2783 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002784 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002785 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002786 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002787 }
2788 free(__script_root);
2789 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002790 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002791 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002792 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002793
Robert Richter38efb532011-11-25 11:38:40 +01002794 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002795}
2796
Tom Zanussib5b87312010-11-10 08:16:51 -06002797static bool is_top_script(const char *script_path)
2798{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002799 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002800}
2801
2802static int has_required_arg(char *script_path)
2803{
2804 struct script_desc *desc;
2805 int n_args = 0;
2806 char *p;
2807
2808 desc = script_desc__new(NULL);
2809
2810 if (read_script_info(desc, script_path))
2811 goto out;
2812
2813 if (!desc->args)
2814 goto out;
2815
2816 for (p = desc->args; *p; p++)
2817 if (*p == '<')
2818 n_args++;
2819out:
2820 script_desc__delete(desc);
2821
2822 return n_args;
2823}
2824
David Ahernd54b1a92012-08-26 12:24:46 -06002825static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002826{
2827 char **__argv = malloc(sizeof(const char *) * argc);
2828
David Ahernd54b1a92012-08-26 12:24:46 -06002829 if (!__argv) {
2830 pr_err("malloc failed\n");
2831 return -1;
2832 }
2833
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002834 memcpy(__argv, argv, sizeof(const char *) * argc);
2835 argc = parse_options(argc, (const char **)__argv, record_options,
2836 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2837 free(__argv);
2838
David Ahernd54b1a92012-08-26 12:24:46 -06002839 system_wide = (argc == 0);
2840
2841 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002842}
2843
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002844static void script__setup_sample_type(struct perf_script *script)
2845{
2846 struct perf_session *session = script->session;
2847 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2848
2849 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2850 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2851 (sample_type & PERF_SAMPLE_STACK_USER))
2852 callchain_param.record_mode = CALLCHAIN_DWARF;
2853 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2854 callchain_param.record_mode = CALLCHAIN_LBR;
2855 else
2856 callchain_param.record_mode = CALLCHAIN_FP;
2857 }
2858}
2859
Jiri Olsae099eba2016-01-05 22:09:09 +01002860static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2861 union perf_event *event,
2862 struct perf_session *session)
2863{
2864 struct stat_round_event *round = &event->stat_round;
2865 struct perf_evsel *counter;
2866
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002867 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002868 perf_stat_process_counter(&stat_config, counter);
2869 process_stat(counter, round->time);
2870 }
2871
2872 process_stat_interval(round->time);
2873 return 0;
2874}
2875
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002876static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2877 union perf_event *event,
2878 struct perf_session *session __maybe_unused)
2879{
2880 perf_event__read_stat_config(&stat_config, &event->stat_config);
2881 return 0;
2882}
2883
Jiri Olsacfc88742016-01-05 22:09:06 +01002884static int set_maps(struct perf_script *script)
2885{
2886 struct perf_evlist *evlist = script->session->evlist;
2887
2888 if (!script->cpus || !script->threads)
2889 return 0;
2890
2891 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2892 return -EINVAL;
2893
2894 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2895
2896 if (perf_evlist__alloc_stats(evlist, true))
2897 return -ENOMEM;
2898
2899 script->allocated = true;
2900 return 0;
2901}
2902
2903static
2904int process_thread_map_event(struct perf_tool *tool,
2905 union perf_event *event,
2906 struct perf_session *session __maybe_unused)
2907{
2908 struct perf_script *script = container_of(tool, struct perf_script, tool);
2909
2910 if (script->threads) {
2911 pr_warning("Extra thread map event, ignoring.\n");
2912 return 0;
2913 }
2914
2915 script->threads = thread_map__new_event(&event->thread_map);
2916 if (!script->threads)
2917 return -ENOMEM;
2918
2919 return set_maps(script);
2920}
2921
2922static
2923int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2924 union perf_event *event,
2925 struct perf_session *session __maybe_unused)
2926{
2927 struct perf_script *script = container_of(tool, struct perf_script, tool);
2928
2929 if (script->cpus) {
2930 pr_warning("Extra cpu map event, ignoring.\n");
2931 return 0;
2932 }
2933
2934 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2935 if (!script->cpus)
2936 return -ENOMEM;
2937
2938 return set_maps(script);
2939}
2940
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002941#ifdef HAVE_AUXTRACE_SUPPORT
2942static int perf_script__process_auxtrace_info(struct perf_tool *tool,
2943 union perf_event *event,
2944 struct perf_session *session)
2945{
2946 int ret = perf_event__process_auxtrace_info(tool, event, session);
2947
2948 if (ret == 0) {
2949 struct perf_script *script = container_of(tool, struct perf_script, tool);
2950
2951 ret = perf_script__setup_per_event_dump(script);
2952 }
2953
2954 return ret;
2955}
2956#else
2957#define perf_script__process_auxtrace_info 0
2958#endif
2959
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002960int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002961{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002962 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002963 bool header = false;
2964 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002965 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002966 char *rec_script_path = NULL;
2967 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002968 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002969 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002970 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002971 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002972 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002973 struct perf_script script = {
2974 .tool = {
2975 .sample = process_sample_event,
2976 .mmap = perf_event__process_mmap,
2977 .mmap2 = perf_event__process_mmap2,
2978 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302979 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002980 .exit = perf_event__process_exit,
2981 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002982 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002983 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002984 .tracing_data = perf_event__process_tracing_data,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07002985 .feature = perf_event__process_feature,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002986 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002987 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002988 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002989 .auxtrace = perf_event__process_auxtrace,
2990 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002991 .stat = perf_event__process_stat_event,
2992 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002993 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002994 .thread_map = process_thread_map_event,
2995 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002996 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002997 .ordering_requires_timestamps = true,
2998 },
2999 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003000 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003001 .mode = PERF_DATA_MODE_READ,
3002 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003003 const struct option options[] = {
3004 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3005 "dump raw trace in ASCII"),
3006 OPT_INCR('v', "verbose", &verbose,
3007 "be more verbose (show symbol address, etc)"),
3008 OPT_BOOLEAN('L', "Latency", &latency_format,
3009 "show latency attributes (irqs/preemption disabled, etc)"),
3010 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3011 list_available_scripts),
3012 OPT_CALLBACK('s', "script", NULL, "name",
3013 "script file name (lang:script name, script name, or *)",
3014 parse_scriptname),
3015 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3016 "generate perf-script.xx script in specified language"),
3017 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3018 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3019 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003020 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3021 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003022 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3023 "file", "vmlinux pathname"),
3024 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3025 "file", "kallsyms pathname"),
3026 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3027 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003028 OPT_CALLBACK(0, "symfs", NULL, "directory",
3029 "Look for files with symbols relative to this directory",
3030 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003031 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003032 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003033 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003034 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003035 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Andi Kleenb1491ac2017-09-05 11:40:57 -07003036 "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
Kan Liang49d58f02017-08-29 13:11:11 -04003037 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
Andi Kleen48d02a12017-02-23 15:46:34 -08003038 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003039 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3040 "system-wide collection from all CPUs"),
3041 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3042 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07003043 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3044 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003045 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3046 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3047 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003048 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3049 "only consider symbols in these pids"),
3050 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3051 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003052 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3053 "Set the maximum stack depth when parsing the callchain, "
3054 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003055 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003056 OPT_BOOLEAN('I', "show-info", &show_full_info,
3057 "display extended information from perf.data file"),
3058 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3059 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003060 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3061 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003062 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3063 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003064 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3065 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303066 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3067 "Show namespace events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003068 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3069 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003070 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003071 OPT_INTEGER(0, "max-blocks", &max_blocks,
3072 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03003073 OPT_BOOLEAN(0, "ns", &nanosecs,
3074 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003075 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
3076 "Instruction Tracing options",
3077 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003078 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3079 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003080 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3081 "Enable symbol demangling"),
3082 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3083 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003084 OPT_STRING(0, "time", &script.time_str, "str",
3085 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003086 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3087 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003088 OPT_END()
3089 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003090 const char * const script_subcommands[] = { "record", "report", NULL };
3091 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003092 "perf script [<options>]",
3093 "perf script [<options>] record <script> [<record-options>] <command>",
3094 "perf script [<options>] report <script> [script-args]",
3095 "perf script [<options>] <script> [<record-options>] <command>",
3096 "perf script [<options>] <top-script> [script-args]",
3097 NULL
3098 };
Tom Zanussi38752942009-12-15 02:53:39 -06003099
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003100 perf_set_singlethreaded();
3101
Tom Zanussib5b87312010-11-10 08:16:51 -06003102 setup_scripting();
3103
Yunlong Song40cae2b2015-03-18 21:35:54 +08003104 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003105 PARSE_OPT_STOP_AT_NON_OPTION);
3106
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003107 data.file.path = input_name;
3108 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003109
Tom Zanussib5b87312010-11-10 08:16:51 -06003110 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3111 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3112 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003113 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003114 }
3115
Tom Zanussib5b87312010-11-10 08:16:51 -06003116 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3117 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3118 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003119 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003120 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003121 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003122 return -1;
3123 }
Tom Zanussi38752942009-12-15 02:53:39 -06003124 }
3125
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003126 if (itrace_synth_opts.callchain &&
3127 itrace_synth_opts.callchain_sz > scripting_max_stack)
3128 scripting_max_stack = itrace_synth_opts.callchain_sz;
3129
Ben Hutchings44e668c2010-10-10 16:10:03 +01003130 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003131 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003132
Tom Zanussib5b87312010-11-10 08:16:51 -06003133 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003134 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003135 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003136 pid_t pid;
3137
Tom Zanussib5b87312010-11-10 08:16:51 -06003138 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3139 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3140
3141 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003142 usage_with_options_msg(script_usage, options,
3143 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003144 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003145 }
3146
Tom Zanussib5b87312010-11-10 08:16:51 -06003147 if (is_top_script(argv[0])) {
3148 rep_args = argc - 1;
3149 } else {
3150 int rec_args;
3151
3152 rep_args = has_required_arg(rep_script_path);
3153 rec_args = (argc - 1) - rep_args;
3154 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003155 usage_with_options_msg(script_usage, options,
3156 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003157 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003158 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003159 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003160 }
3161
3162 if (pipe(live_pipe) < 0) {
3163 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003164 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003165 }
3166
3167 pid = fork();
3168 if (pid < 0) {
3169 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003170 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003171 }
3172
3173 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003174 j = 0;
3175
Tom Zanussia0cccc22010-04-01 23:59:25 -05003176 dup2(live_pipe[1], 1);
3177 close(live_pipe[0]);
3178
Robert Richter317df652011-11-25 15:05:25 +01003179 if (is_top_script(argv[0])) {
3180 system_wide = true;
3181 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003182 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3183 err = -1;
3184 goto out;
3185 }
Robert Richter317df652011-11-25 15:05:25 +01003186 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003187
3188 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003189 if (!__argv) {
3190 pr_err("malloc failed\n");
3191 err = -ENOMEM;
3192 goto out;
3193 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003194
Tom Zanussib5b87312010-11-10 08:16:51 -06003195 __argv[j++] = "/bin/sh";
3196 __argv[j++] = rec_script_path;
3197 if (system_wide)
3198 __argv[j++] = "-a";
3199 __argv[j++] = "-q";
3200 __argv[j++] = "-o";
3201 __argv[j++] = "-";
3202 for (i = rep_args + 1; i < argc; i++)
3203 __argv[j++] = argv[i];
3204 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003205
3206 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003207 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003208 exit(-1);
3209 }
3210
3211 dup2(live_pipe[0], 0);
3212 close(live_pipe[1]);
3213
Tom Zanussib5b87312010-11-10 08:16:51 -06003214 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003215 if (!__argv) {
3216 pr_err("malloc failed\n");
3217 err = -ENOMEM;
3218 goto out;
3219 }
3220
Tom Zanussib5b87312010-11-10 08:16:51 -06003221 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003222 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003223 __argv[j++] = rep_script_path;
3224 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003225 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003226 __argv[j++] = "-i";
3227 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003228 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003229
3230 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003231 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003232 exit(-1);
3233 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003234
Tom Zanussib5b87312010-11-10 08:16:51 -06003235 if (rec_script_path)
3236 script_path = rec_script_path;
3237 if (rep_script_path)
3238 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003239
Tom Zanussib5b87312010-11-10 08:16:51 -06003240 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003241 j = 0;
3242
Robert Richter317df652011-11-25 15:05:25 +01003243 if (!rec_script_path)
3244 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003245 else if (!system_wide) {
3246 if (have_cmd(argc - 1, &argv[1]) != 0) {
3247 err = -1;
3248 goto out;
3249 }
3250 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003251
3252 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003253 if (!__argv) {
3254 pr_err("malloc failed\n");
3255 err = -ENOMEM;
3256 goto out;
3257 }
3258
Tom Zanussib5b87312010-11-10 08:16:51 -06003259 __argv[j++] = "/bin/sh";
3260 __argv[j++] = script_path;
3261 if (system_wide)
3262 __argv[j++] = "-a";
3263 for (i = 2; i < argc; i++)
3264 __argv[j++] = argv[i];
3265 __argv[j++] = NULL;
3266
3267 execvp("/bin/sh", (char **)__argv);
3268 free(__argv);
3269 exit(-1);
3270 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003271
Tom Zanussicf4fee52010-03-03 01:04:33 -06003272 if (!script_name)
3273 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003274
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003275 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003276 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003277 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003278
Jiri Olsae90debd2013-12-09 11:02:50 +01003279 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003280 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003281 perf_session__fprintf_info(session, stdout, show_full_info);
3282 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003283 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003284 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003285 if (show_full_info)
3286 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003287
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003288 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003289 goto out_delete;
3290
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003291 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003292 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003293
Adrian Huntere2167082016-06-23 16:40:58 +03003294 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3295 itrace_synth_opts.thread_stack = true;
3296
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003297 session->itrace_synth_opts = &itrace_synth_opts;
3298
Anton Blanchard5d67be92011-07-04 21:57:50 +10003299 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003300 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3301 if (err < 0)
3302 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003303 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003304 }
3305
David Ahern1424dc92011-03-09 22:23:28 -07003306 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003307 symbol_conf.use_callchain = true;
3308 else
3309 symbol_conf.use_callchain = false;
3310
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003311 if (session->tevent.pevent &&
3312 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003313 machine__resolve_kernel_addr,
3314 &session->machines.host) < 0) {
3315 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003316 err = -1;
3317 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003318 }
3319
Tom Zanussi956ffd02009-11-25 01:15:46 -06003320 if (generate_script_lang) {
3321 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003322 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003323
David Ahern2c9e45f72011-03-17 10:03:21 -06003324 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003325 fprintf(stderr,
3326 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003327 err = -EINVAL;
3328 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003329 }
3330
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003331 input = open(data.file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003332 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003333 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003334 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003335 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003336 }
3337
3338 err = fstat(input, &perf_stat);
3339 if (err < 0) {
3340 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003341 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003342 }
3343
3344 if (!perf_stat.st_size) {
3345 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003346 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003347 }
3348
3349 scripting_ops = script_spec__lookup(generate_script_lang);
3350 if (!scripting_ops) {
3351 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003352 err = -ENOENT;
3353 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003354 }
3355
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003356 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003357 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003358 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003359 }
3360
3361 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003362 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003363 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003364 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003365 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003366 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003367 }
3368
David Ahern9cbdb702011-04-06 21:54:20 -06003369
3370 err = perf_session__check_output_opt(session);
3371 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003372 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003373
David Aherna91f4c42016-11-29 10:15:43 -07003374 /* needs to be parsed after looking up reference time */
3375 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
3376 pr_err("Invalid time string\n");
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003377 err = -EINVAL;
3378 goto out_delete;
David Aherna91f4c42016-11-29 10:15:43 -07003379 }
3380
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003381 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003382
Adrian Hunterd445dd22014-08-15 22:08:37 +03003383 flush_scripting();
3384
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003385out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01003386 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003387 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003388
3389 if (script_started)
3390 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003391out:
3392 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003393}