blob: bb603495cf4a5161e82973cb46049a3ffaa23b4e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002#include "builtin.h"
3
Andrea Gelminib7eead82010-08-05 15:51:38 +02004#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02005#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02006#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06007#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02008#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06009#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +020010#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020013#include "util/symbol.h"
14#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010015#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020016#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070017#include "util/evlist.h"
18#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080019#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020020#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030021#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010022#include "util/cpumap.h"
23#include "util/thread_map.h"
24#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080025#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030026#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030027#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070028#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010029#include "util/path.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030030#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100031#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030032#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030033#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030034#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010035#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010036#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080037#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030038#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030039#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030040#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030041#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030042#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030043#include <sys/types.h>
44#include <sys/stat.h>
45#include <unistd.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020046
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030047#include "sane_ctype.h"
48
Tom Zanussi956ffd02009-11-25 01:15:46 -060049static char const *script_name;
50static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020051static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020052static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020053static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070054static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090055static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010056static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030057static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030058static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100059static const char *cpu_list;
60static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010061static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080062static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060063
Adrian Hunter44cbe722015-09-25 16:15:50 +030064unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030065
David Ahern745f43e2011-03-09 22:23:26 -070066enum perf_output_field {
67 PERF_OUTPUT_COMM = 1U << 0,
68 PERF_OUTPUT_TID = 1U << 1,
69 PERF_OUTPUT_PID = 1U << 2,
70 PERF_OUTPUT_TIME = 1U << 3,
71 PERF_OUTPUT_CPU = 1U << 4,
72 PERF_OUTPUT_EVNAME = 1U << 5,
73 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060074 PERF_OUTPUT_IP = 1U << 7,
75 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060076 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060077 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090078 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020079 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020080 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020081 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020082 PERF_OUTPUT_BRSTACK = 1U << 15,
83 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010084 PERF_OUTPUT_DATA_SRC = 1U << 17,
85 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000086 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030087 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030088 PERF_OUTPUT_INSN = 1U << 21,
89 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080090 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070091 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030092 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -040093 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -070094 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -080095 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +010096 PERF_OUTPUT_MISC = 1U << 29,
David Ahern745f43e2011-03-09 22:23:26 -070097};
98
99struct output_option {
100 const char *str;
101 enum perf_output_field field;
102} all_output_options[] = {
103 {.str = "comm", .field = PERF_OUTPUT_COMM},
104 {.str = "tid", .field = PERF_OUTPUT_TID},
105 {.str = "pid", .field = PERF_OUTPUT_PID},
106 {.str = "time", .field = PERF_OUTPUT_TIME},
107 {.str = "cpu", .field = PERF_OUTPUT_CPU},
108 {.str = "event", .field = PERF_OUTPUT_EVNAME},
109 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600110 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700111 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600112 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600113 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900114 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200115 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200116 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200117 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700118 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200119 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
120 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100121 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
122 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000123 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300124 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300125 {.str = "insn", .field = PERF_OUTPUT_INSN},
126 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800127 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700128 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300129 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400130 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800131 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100132 {.str = "misc", .field = PERF_OUTPUT_MISC},
David Ahern745f43e2011-03-09 22:23:26 -0700133};
134
Adrian Hunter14057202017-06-21 13:17:19 +0300135enum {
136 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
137 OUTPUT_TYPE_MAX
138};
139
David Ahern745f43e2011-03-09 22:23:26 -0700140/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600141static struct {
142 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600143 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400144 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600145 u64 fields;
146 u64 invalid_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300147} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700148
David Ahern2c9e45f72011-03-17 10:03:21 -0600149 [PERF_TYPE_HARDWARE] = {
150 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700151
David Ahern2c9e45f72011-03-17 10:03:21 -0600152 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
153 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600154 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200155 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
156 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600157
Wang Nan30372f02016-02-24 11:20:45 +0000158 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600159 },
160
161 [PERF_TYPE_SOFTWARE] = {
162 .user_set = false,
163
164 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
165 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600166 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200167 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000168 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600169
170 .invalid_fields = PERF_OUTPUT_TRACE,
171 },
172
173 [PERF_TYPE_TRACEPOINT] = {
174 .user_set = false,
175
176 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
177 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000178 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600179 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700180
181 [PERF_TYPE_RAW] = {
182 .user_set = false,
183
184 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
185 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600186 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200187 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100188 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
Kan Liang49d58f02017-08-29 13:11:11 -0400189 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT |
190 PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700191
Wang Nan30372f02016-02-24 11:20:45 +0000192 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700193 },
Wang Nan27cfef02015-12-08 02:25:43 +0000194
195 [PERF_TYPE_BREAKPOINT] = {
196 .user_set = false,
197
198 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
199 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
200 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
201 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
202 PERF_OUTPUT_PERIOD,
203
Wang Nan30372f02016-02-24 11:20:45 +0000204 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000205 },
Adrian Hunter14057202017-06-21 13:17:19 +0300206
207 [OUTPUT_TYPE_SYNTH] = {
208 .user_set = false,
209
210 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
211 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
212 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Adrian Hunter47e78082017-05-26 11:17:22 +0300213 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
214 PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300215
216 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
217 },
David Ahern1424dc92011-03-09 22:23:28 -0700218};
David Ahern745f43e2011-03-09 22:23:26 -0700219
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300220struct perf_evsel_script {
221 char *filename;
222 FILE *fp;
223 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800224 /* For metric output */
225 u64 val;
226 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300227};
228
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800229static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
230{
231 return (struct perf_evsel_script *)evsel->priv;
232}
233
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300234static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100235 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300236{
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800237 struct perf_evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300238
239 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100240 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300241 goto out_free;
242 es->fp = fopen(es->filename, "w");
243 if (es->fp == NULL)
244 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300245 }
246
247 return es;
248out_free_filename:
249 zfree(&es->filename);
250out_free:
251 free(es);
252 return NULL;
253}
254
255static void perf_evsel_script__delete(struct perf_evsel_script *es)
256{
257 zfree(&es->filename);
258 fclose(es->fp);
259 es->fp = NULL;
260 free(es);
261}
262
263static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
264{
265 struct stat st;
266
267 fstat(fileno(es->fp), &st);
268 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
269 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
270}
271
Adrian Hunter14057202017-06-21 13:17:19 +0300272static inline int output_type(unsigned int type)
273{
274 switch (type) {
275 case PERF_TYPE_SYNTH:
276 return OUTPUT_TYPE_SYNTH;
277 default:
278 return type;
279 }
280}
281
282static inline unsigned int attr_type(unsigned int type)
283{
284 switch (type) {
285 case OUTPUT_TYPE_SYNTH:
286 return PERF_TYPE_SYNTH;
287 default:
288 return type;
289 }
290}
291
David Ahern2c9e45f72011-03-17 10:03:21 -0600292static bool output_set_by_user(void)
293{
294 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300295 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600296 if (output[j].user_set)
297 return true;
298 }
299 return false;
300}
David Ahern745f43e2011-03-09 22:23:26 -0700301
David Ahern9cbdb702011-04-06 21:54:20 -0600302static const char *output_field2str(enum perf_output_field field)
303{
304 int i, imax = ARRAY_SIZE(all_output_options);
305 const char *str = "";
306
307 for (i = 0; i < imax; ++i) {
308 if (all_output_options[i].field == field) {
309 str = all_output_options[i].str;
310 break;
311 }
312 }
313 return str;
314}
315
Adrian Hunter14057202017-06-21 13:17:19 +0300316#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700317
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300318static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
319 u64 sample_type, const char *sample_msg,
320 enum perf_output_field field,
321 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700322{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300323 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300324 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600325 const char *evname;
326
327 if (attr->sample_type & sample_type)
328 return 0;
329
330 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300331 if (allow_user_set)
332 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300333 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600334 pr_err("Samples for '%s' event do not have %s attribute set. "
335 "Cannot print '%s' field.\n",
336 evname, sample_msg, output_field2str(field));
337 return -1;
338 }
339
340 /* user did not ask for it explicitly so remove from the default list */
341 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300342 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600343 pr_debug("Samples for '%s' event do not have %s attribute set. "
344 "Skipping '%s' field.\n",
345 evname, sample_msg, output_field2str(field));
346
347 return 0;
348}
349
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300350static int perf_evsel__check_stype(struct perf_evsel *evsel,
351 u64 sample_type, const char *sample_msg,
352 enum perf_output_field field)
353{
354 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
355 false);
356}
357
David Ahern9cbdb702011-04-06 21:54:20 -0600358static int perf_evsel__check_attr(struct perf_evsel *evsel,
359 struct perf_session *session)
360{
361 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300362 bool allow_user_set;
363
Jiri Olsae099eba2016-01-05 22:09:09 +0100364 if (perf_header__has_feat(&session->header, HEADER_STAT))
365 return 0;
366
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300367 allow_user_set = perf_header__has_feat(&session->header,
368 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600369
David Ahern1424dc92011-03-09 22:23:28 -0700370 if (PRINT_FIELD(TRACE) &&
371 !perf_session__has_traces(session, "record -R"))
372 return -EINVAL;
373
David Ahern787bef12011-05-27 14:28:43 -0600374 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300375 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
376 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700377 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700378 }
David Ahern7cec0922011-05-30 13:08:23 -0600379
380 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300381 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
382 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600383 return -EINVAL;
384
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100385 if (PRINT_FIELD(DATA_SRC) &&
386 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
387 PERF_OUTPUT_DATA_SRC))
388 return -EINVAL;
389
390 if (PRINT_FIELD(WEIGHT) &&
391 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
392 PERF_OUTPUT_WEIGHT))
393 return -EINVAL;
394
David Ahern7cec0922011-05-30 13:08:23 -0600395 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
396 pr_err("Display of symbols requested but neither sample IP nor "
397 "sample address\nis selected. Hence, no addresses to convert "
398 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600399 return -EINVAL;
400 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900401 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
402 pr_err("Display of offsets requested but symbol is not"
403 "selected.\n");
404 return -EINVAL;
405 }
Mark Santaniello55b9b502017-06-19 09:38:24 -0700406 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR) &&
Mark Santaniello106dacd2017-06-19 09:38:25 -0700407 !PRINT_FIELD(BRSTACK) && !PRINT_FIELD(BRSTACKSYM) && !PRINT_FIELD(BRSTACKOFF)) {
408 pr_err("Display of DSO requested but no address to convert. Select\n"
409 "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
David Ahern610723f2011-05-27 14:28:44 -0600410 return -EINVAL;
411 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200412 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
413 pr_err("Display of source line number requested but sample IP is not\n"
414 "selected. Hence, no address to lookup the source line number.\n");
415 return -EINVAL;
416 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800417 if (PRINT_FIELD(BRSTACKINSN) &&
418 !(perf_evlist__combined_branch_type(session->evlist) &
419 PERF_SAMPLE_BRANCH_ANY)) {
420 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
421 "Hint: run 'perf record -b ...'\n");
422 return -EINVAL;
423 }
David Ahern1424dc92011-03-09 22:23:28 -0700424 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300425 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
426 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700427 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700428
429 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300430 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
431 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700432 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700433
434 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300435 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
436 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700437 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600438
Stephane Eranianfc36f942015-08-31 18:41:10 +0200439 if (PRINT_FIELD(IREGS) &&
440 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
441 PERF_OUTPUT_IREGS))
442 return -EINVAL;
443
Andi Kleenb1491ac2017-09-05 11:40:57 -0700444 if (PRINT_FIELD(UREGS) &&
445 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
446 PERF_OUTPUT_UREGS))
447 return -EINVAL;
448
Kan Liang49d58f02017-08-29 13:11:11 -0400449 if (PRINT_FIELD(PHYS_ADDR) &&
450 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
451 PERF_OUTPUT_PHYS_ADDR))
452 return -EINVAL;
453
David Ahern9cbdb702011-04-06 21:54:20 -0600454 return 0;
455}
456
Adrian Hunter7ea95722013-11-01 15:51:30 +0200457static void set_print_ip_opts(struct perf_event_attr *attr)
458{
Adrian Hunter14057202017-06-21 13:17:19 +0300459 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200460
461 output[type].print_ip_opts = 0;
462 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300463 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200464
465 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300466 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200467
468 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300469 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200470
471 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300472 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200473
474 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300475 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200476}
477
David Ahern9cbdb702011-04-06 21:54:20 -0600478/*
479 * verify all user requested events exist and the samples
480 * have the expected data
481 */
482static int perf_session__check_output_opt(struct perf_session *session)
483{
He Kuang40f20e52016-05-16 04:51:19 +0000484 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600485 struct perf_evsel *evsel;
486
Adrian Hunter14057202017-06-21 13:17:19 +0300487 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
488 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600489
490 /*
491 * even if fields is set to 0 (ie., show nothing) event must
492 * exist if user explicitly includes it on the command line
493 */
Adrian Hunter14057202017-06-21 13:17:19 +0300494 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
495 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600496 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300497 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600498 event_type(j));
499 return -1;
500 }
501
502 if (evsel && output[j].fields &&
503 perf_evsel__check_attr(evsel, session))
504 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400505
506 if (evsel == NULL)
507 continue;
508
Adrian Hunter7ea95722013-11-01 15:51:30 +0200509 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700510 }
511
Adrian Hunter98526ee2014-07-31 09:00:59 +0300512 if (!no_callchain) {
513 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000514 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300515
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300516 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000517 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300518 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
519 use_callchain = true;
520 break;
521 }
522 }
He Kuang71ac8992016-08-04 11:25:43 +0000523 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300524 symbol_conf.use_callchain = false;
525 }
526
David Ahern80b8b492013-11-19 21:07:37 -0700527 /*
528 * set default for tracepoints to print symbols only
529 * if callchains are present
530 */
531 if (symbol_conf.use_callchain &&
532 !output[PERF_TYPE_TRACEPOINT].user_set) {
533 struct perf_event_attr *attr;
534
535 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700536
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300537 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000538 if (evsel->attr.type != j)
539 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700540
He Kuang40f20e52016-05-16 04:51:19 +0000541 attr = &evsel->attr;
542
543 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
544 output[j].fields |= PERF_OUTPUT_IP;
545 output[j].fields |= PERF_OUTPUT_SYM;
546 output[j].fields |= PERF_OUTPUT_DSO;
547 set_print_ip_opts(attr);
548 goto out;
549 }
David Ahern80b8b492013-11-19 21:07:37 -0700550 }
551 }
552
553out:
David Ahern1424dc92011-03-09 22:23:28 -0700554 return 0;
555}
David Ahern745f43e2011-03-09 22:23:26 -0700556
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300557static int perf_sample__fprintf_iregs(struct perf_sample *sample,
558 struct perf_event_attr *attr, FILE *fp)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200559{
560 struct regs_dump *regs = &sample->intr_regs;
561 uint64_t mask = attr->sample_regs_intr;
562 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300563 int printed = 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200564
565 if (!regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300566 return 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200567
568 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
569 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300570 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Stephane Eranianfc36f942015-08-31 18:41:10 +0200571 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300572
573 return printed;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200574}
575
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300576static int perf_sample__fprintf_uregs(struct perf_sample *sample,
577 struct perf_event_attr *attr, FILE *fp)
Andi Kleenb1491ac2017-09-05 11:40:57 -0700578{
579 struct regs_dump *regs = &sample->user_regs;
580 uint64_t mask = attr->sample_regs_user;
581 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300582 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700583
584 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300585 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700586
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300587 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700588
589 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
590 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300591 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700592 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300593
594 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700595}
596
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300597static int perf_sample__fprintf_start(struct perf_sample *sample,
598 struct thread *thread,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100599 struct perf_evsel *evsel,
600 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700601{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300602 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700603 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700604 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300605 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700606
David Ahern745f43e2011-03-09 22:23:26 -0700607 if (PRINT_FIELD(COMM)) {
608 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300609 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600610 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300611 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700612 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300613 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700614 }
David Ahernc70c94b2011-03-09 22:23:25 -0700615
David Ahern745f43e2011-03-09 22:23:26 -0700616 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300617 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700618 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300619 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700620 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300621 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700622
David Ahern745f43e2011-03-09 22:23:26 -0700623 if (PRINT_FIELD(CPU)) {
624 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300625 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700626 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300627 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700628 }
David Ahernc70c94b2011-03-09 22:23:25 -0700629
Jiri Olsa28a0b392018-01-07 17:03:52 +0100630 if (PRINT_FIELD(MISC)) {
631 int ret = 0;
632
633 #define has(m) \
634 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
635
636 if (has(KERNEL))
637 ret += fprintf(fp, "K");
638 if (has(USER))
639 ret += fprintf(fp, "U");
640 if (has(HYPERVISOR))
641 ret += fprintf(fp, "H");
642 if (has(GUEST_KERNEL))
643 ret += fprintf(fp, "G");
644 if (has(GUEST_USER))
645 ret += fprintf(fp, "g");
646
647 switch (type) {
648 case PERF_RECORD_MMAP:
649 case PERF_RECORD_MMAP2:
650 if (has(MMAP_DATA))
651 ret += fprintf(fp, "M");
652 break;
653 case PERF_RECORD_COMM:
654 if (has(COMM_EXEC))
655 ret += fprintf(fp, "E");
656 break;
657 case PERF_RECORD_SWITCH:
658 case PERF_RECORD_SWITCH_CPU_WIDE:
659 if (has(SWITCH_OUT))
660 ret += fprintf(fp, "S");
661 default:
662 break;
663 }
664
665 #undef has
666
667 ret += fprintf(fp, "%*s", 6 - ret, " ");
668 printed += ret;
669 }
670
David Ahern745f43e2011-03-09 22:23:26 -0700671 if (PRINT_FIELD(TIME)) {
672 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300673 secs = nsecs / NSEC_PER_SEC;
674 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900675
Adrian Hunter83e19862015-09-25 16:15:36 +0300676 if (nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300677 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900678 else {
679 char sample_time[32];
680 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300681 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900682 }
David Ahern745f43e2011-03-09 22:23:26 -0700683 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300684
685 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700686}
687
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200688static inline char
689mispred_str(struct branch_entry *br)
690{
691 if (!(br->flags.mispred || br->flags.predicted))
692 return '-';
693
694 return br->flags.predicted ? 'P' : 'M';
695}
696
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300697static int perf_sample__fprintf_brstack(struct perf_sample *sample,
698 struct thread *thread,
699 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200700{
701 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700702 struct addr_location alf, alt;
703 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300704 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200705
706 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300707 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200708
709 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700710 from = br->entries[i].from;
711 to = br->entries[i].to;
712
713 if (PRINT_FIELD(DSO)) {
714 memset(&alf, 0, sizeof(alf));
715 memset(&alt, 0, sizeof(alt));
716 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
717 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
718 }
719
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300720 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700721 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300722 printed += fprintf(fp, "(");
723 printed += map__fprintf_dsoname(alf.map, fp);
724 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700725 }
726
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300727 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700728 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300729 printed += fprintf(fp, "(");
730 printed += map__fprintf_dsoname(alt.map, fp);
731 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700732 }
733
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300734 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200735 mispred_str( br->entries + i),
736 br->entries[i].flags.in_tx? 'X' : '-',
737 br->entries[i].flags.abort? 'A' : '-',
738 br->entries[i].flags.cycles);
739 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300740
741 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200742}
743
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300744static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
745 struct thread *thread,
746 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200747{
748 struct branch_stack *br = sample->branch_stack;
749 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200750 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300751 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200752
753 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300754 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200755
756 for (i = 0; i < br->nr; i++) {
757
758 memset(&alf, 0, sizeof(alf));
759 memset(&alt, 0, sizeof(alt));
760 from = br->entries[i].from;
761 to = br->entries[i].to;
762
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300763 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200764 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300765 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200766
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300767 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200768 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300769 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200770
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300771 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700772 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300773 printed += fprintf(fp, "(");
774 printed += map__fprintf_dsoname(alf.map, fp);
775 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700776 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300777 printed += fprintf(fp, "%c", '/');
778 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700779 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300780 printed += fprintf(fp, "(");
781 printed += map__fprintf_dsoname(alt.map, fp);
782 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700783 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300784 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200785 mispred_str( br->entries + i),
786 br->entries[i].flags.in_tx? 'X' : '-',
787 br->entries[i].flags.abort? 'A' : '-',
788 br->entries[i].flags.cycles);
789 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300790
791 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200792}
793
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300794static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
795 struct thread *thread,
796 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700797{
798 struct branch_stack *br = sample->branch_stack;
799 struct addr_location alf, alt;
800 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300801 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700802
803 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300804 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700805
806 for (i = 0; i < br->nr; i++) {
807
808 memset(&alf, 0, sizeof(alf));
809 memset(&alt, 0, sizeof(alt));
810 from = br->entries[i].from;
811 to = br->entries[i].to;
812
813 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
814 if (alf.map && !alf.map->dso->adjust_symbols)
815 from = map__map_ip(alf.map, from);
816
817 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
818 if (alt.map && !alt.map->dso->adjust_symbols)
819 to = map__map_ip(alt.map, to);
820
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300821 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700822 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300823 printed += fprintf(fp, "(");
824 printed += map__fprintf_dsoname(alf.map, fp);
825 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700826 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300827 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700828 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300829 printed += fprintf(fp, "(");
830 printed += map__fprintf_dsoname(alt.map, fp);
831 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700832 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300833 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700834 mispred_str(br->entries + i),
835 br->entries[i].flags.in_tx ? 'X' : '-',
836 br->entries[i].flags.abort ? 'A' : '-',
837 br->entries[i].flags.cycles);
838 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300839
840 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700841}
Andi Kleen48d02a12017-02-23 15:46:34 -0800842#define MAXBB 16384UL
843
844static int grab_bb(u8 *buffer, u64 start, u64 end,
845 struct machine *machine, struct thread *thread,
846 bool *is64bit, u8 *cpumode, bool last)
847{
848 long offset, len;
849 struct addr_location al;
850 bool kernel;
851
852 if (!start || !end)
853 return 0;
854
855 kernel = machine__kernel_ip(machine, start);
856 if (kernel)
857 *cpumode = PERF_RECORD_MISC_KERNEL;
858 else
859 *cpumode = PERF_RECORD_MISC_USER;
860
861 /*
862 * Block overlaps between kernel and user.
863 * This can happen due to ring filtering
864 * On Intel CPUs the entry into the kernel is filtered,
865 * but the exit is not. Let the caller patch it up.
866 */
867 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300868 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800869 return -ENXIO;
870 }
871
872 memset(&al, 0, sizeof(al));
873 if (end - start > MAXBB - MAXINSN) {
874 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300875 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800876 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300877 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800878 return 0;
879 }
880
881 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
882 if (!al.map || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300883 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800884 return 0;
885 }
886 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300887 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800888 return 0;
889 }
890
891 /* Load maps to ensure dso->is_64_bit has been updated */
892 map__load(al.map);
893
894 offset = al.map->map_ip(al.map, start);
895 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
896 end - start + MAXINSN);
897
898 *is64bit = al.map->dso->is_64_bit;
899 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300900 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800901 start, end);
902 return len;
903}
904
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300905static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
906 struct perf_insn *x, u8 *inbuf, int len,
907 int insn, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800908{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300909 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
910 dump_insn(x, ip, inbuf, len, NULL),
911 en->flags.predicted ? " PRED" : "",
912 en->flags.mispred ? " MISPRED" : "",
913 en->flags.in_tx ? " INTX" : "",
914 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800915 if (en->flags.cycles) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300916 printed += fprintf(fp, " %d cycles", en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800917 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300918 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800919 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300920 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800921}
922
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300923static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
924 u8 cpumode, int cpu, struct symbol **lastsym,
925 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800926{
927 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300928 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800929
930 memset(&al, 0, sizeof(al));
931
932 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
933 if (!al.map)
934 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
935 addr, &al);
936 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300937 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800938
939 al.cpu = cpu;
940 al.sym = NULL;
941 if (al.map)
942 al.sym = map__find_symbol(al.map, al.addr);
943
944 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300945 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800946
947 if (al.addr < al.sym->end)
948 off = al.addr - al.sym->start;
949 else
950 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300951 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800952 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300953 printed += fprintf(fp, "%+d", off);
954 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800955 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300956 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
957 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800958 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300959
960 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800961}
962
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300963static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
964 struct thread *thread,
965 struct perf_event_attr *attr,
966 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800967{
968 struct branch_stack *br = sample->branch_stack;
969 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300970 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800971 struct perf_insn x;
972 u8 buffer[MAXBB];
973 unsigned off;
974 struct symbol *lastsym = NULL;
975
976 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300977 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800978 nr = br->nr;
979 if (max_blocks && nr > max_blocks + 1)
980 nr = max_blocks + 1;
981
982 x.thread = thread;
983 x.cpu = sample->cpu;
984
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300985 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -0800986
987 /* Handle first from jump, of which we don't know the entry. */
988 len = grab_bb(buffer, br->entries[nr-1].from,
989 br->entries[nr-1].from,
990 machine, thread, &x.is64bit, &x.cpumode, false);
991 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300992 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
993 x.cpumode, x.cpu, &lastsym, attr, fp);
994 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
995 &x, buffer, len, 0, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800996 }
997
998 /* Print all blocks */
999 for (i = nr - 2; i >= 0; i--) {
1000 if (br->entries[i].from || br->entries[i].to)
1001 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1002 br->entries[i].from,
1003 br->entries[i].to);
1004 start = br->entries[i + 1].to;
1005 end = br->entries[i].from;
1006
1007 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1008 /* Patch up missing kernel transfers due to ring filters */
1009 if (len == -ENXIO && i > 0) {
1010 end = br->entries[--i].from;
1011 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1012 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1013 }
1014 if (len <= 0)
1015 continue;
1016
1017 insn = 0;
1018 for (off = 0;; off += ilen) {
1019 uint64_t ip = start + off;
1020
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001021 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001022 if (ip == end) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001023 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001024 break;
1025 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001026 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1027 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001028 if (ilen == 0)
1029 break;
1030 insn++;
1031 }
1032 }
1033 }
1034
1035 /*
1036 * Hit the branch? In this case we are already done, and the target
1037 * has not been executed yet.
1038 */
1039 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001040 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001041 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001042 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001043
1044 /*
1045 * Print final block upto sample
1046 */
1047 start = br->entries[0].to;
1048 end = sample->ip;
1049 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001050 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001051 if (len <= 0) {
1052 /* Print at least last IP if basic block did not work */
1053 len = grab_bb(buffer, sample->ip, sample->ip,
1054 machine, thread, &x.is64bit, &x.cpumode, false);
1055 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001056 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001057
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001058 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001059 dump_insn(&x, sample->ip, buffer, len, NULL));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001060 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001061 }
1062 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001063 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1064 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001065 if (ilen == 0)
1066 break;
1067 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001068out:
1069 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001070}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001071
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001072static int perf_sample__fprintf_addr(struct perf_sample *sample,
1073 struct thread *thread,
1074 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001075{
1076 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001077 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001078
1079 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001080 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001081
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001082 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001083
1084 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001085 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001086 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001087 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001088 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001089 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001090 }
1091
1092 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001093 printed += fprintf(fp, " (");
1094 printed += map__fprintf_dsoname(al.map, fp);
1095 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001096 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001097out:
1098 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001099}
1100
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001101static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1102 struct perf_evsel *evsel,
1103 struct thread *thread,
1104 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001105{
1106 struct perf_event_attr *attr = &evsel->attr;
1107 size_t depth = thread_stack__depth(thread);
1108 struct addr_location addr_al;
1109 const char *name = NULL;
1110 static int spacing;
1111 int len = 0;
1112 u64 ip = 0;
1113
1114 /*
1115 * The 'return' has already been popped off the stack so the depth has
1116 * to be adjusted to match the 'call'.
1117 */
1118 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1119 depth += 1;
1120
1121 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1122 if (sample_addr_correlates_sym(attr)) {
1123 thread__resolve(thread, &addr_al, sample);
1124 if (addr_al.sym)
1125 name = addr_al.sym->name;
1126 else
1127 ip = sample->addr;
1128 } else {
1129 ip = sample->addr;
1130 }
1131 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1132 if (al->sym)
1133 name = al->sym->name;
1134 else
1135 ip = sample->ip;
1136 }
1137
1138 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001139 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001140 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001141 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001142
1143 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001144 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001145
1146 /*
1147 * Try to keep the output length from changing frequently so that the
1148 * output lines up more nicely.
1149 */
1150 if (len > spacing || (len && len < spacing - 52))
1151 spacing = round_up(len + 4, 32);
1152
1153 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001154 len += fprintf(fp, "%*s", spacing - len, "");
1155
1156 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001157}
1158
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001159static int perf_sample__fprintf_insn(struct perf_sample *sample,
1160 struct perf_event_attr *attr,
1161 struct thread *thread,
1162 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001163{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001164 int printed = 0;
1165
Andi Kleen224e2c92016-10-07 16:42:27 +03001166 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001167 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen224e2c92016-10-07 16:42:27 +03001168 if (PRINT_FIELD(INSN)) {
1169 int i;
1170
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001171 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001172 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001173 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001174 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001175 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001176 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1177
1178 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001179}
1180
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001181static int perf_sample__fprintf_bts(struct perf_sample *sample,
1182 struct perf_evsel *evsel,
1183 struct thread *thread,
1184 struct addr_location *al,
1185 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001186{
1187 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001188 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001189 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001190 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001191
Adrian Huntere2167082016-06-23 16:40:58 +03001192 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001193 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001194
Akihiro Nagai95582592012-01-30 13:43:09 +09001195 /* print branch_from information */
1196 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001197 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001198 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001199
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001200 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001201 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001202 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001203 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001204
1205 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001206 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001207 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001208 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001209 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001210 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001211 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001212 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001213
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001214 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001215 }
1216
Akihiro Nagai95582592012-01-30 13:43:09 +09001217 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001218 if (PRINT_FIELD(ADDR) ||
1219 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001220 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001221 printed += fprintf(fp, " => ");
1222 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001223 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001224
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001225 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001226 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001227
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001228 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1229 return printed + fprintf(fp, "\n");
Akihiro Nagai95582592012-01-30 13:43:09 +09001230}
1231
Adrian Hunter055cd332016-06-23 16:40:56 +03001232static struct {
1233 u32 flags;
1234 const char *name;
1235} sample_flags[] = {
1236 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1237 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1238 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1239 {PERF_IP_FLAG_BRANCH, "jmp"},
1240 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1241 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1242 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1243 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1244 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1245 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1246 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1247 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1248 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1249 {0, NULL}
1250};
1251
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001252static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001253{
1254 const char *chars = PERF_IP_FLAG_CHARS;
1255 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001256 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1257 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001258 char str[33];
1259 int i, pos = 0;
1260
Adrian Hunter055cd332016-06-23 16:40:56 +03001261 for (i = 0; sample_flags[i].name ; i++) {
1262 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1263 name = sample_flags[i].name;
1264 break;
1265 }
1266 }
1267
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001268 for (i = 0; i < n; i++, flags >>= 1) {
1269 if (flags & 1)
1270 str[pos++] = chars[i];
1271 }
1272 for (; i < 32; i++, flags >>= 1) {
1273 if (flags & 1)
1274 str[pos++] = '?';
1275 }
1276 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001277
1278 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001279 return fprintf(fp, " %-7s%4s ", name, in_tx ? "(x)" : "");
1280
1281 return fprintf(fp, " %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001282}
1283
Wang Nan30372f02016-02-24 11:20:45 +00001284struct printer_data {
1285 int line_no;
1286 bool hit_nul;
1287 bool is_printable;
1288};
1289
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001290static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1291 unsigned int val,
1292 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001293{
1294 unsigned char ch = (unsigned char)val;
1295 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001296 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001297
1298 switch (op) {
1299 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001300 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001301 break;
1302 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001303 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001304 " ");
1305 break;
1306 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001307 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001308 break;
1309 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001310 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001311 break;
1312 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001313 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001314 break;
1315 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001316 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001317 break;
1318 case BINARY_PRINT_CHAR_DATA:
1319 if (printer_data->hit_nul && ch)
1320 printer_data->is_printable = false;
1321
1322 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001323 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001324
1325 if (!printer_data->is_printable)
1326 break;
1327
1328 if (ch == '\0')
1329 printer_data->hit_nul = true;
1330 else
1331 printer_data->is_printable = false;
1332 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001333 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001334 }
1335 break;
1336 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001337 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001338 break;
1339 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001340 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001341 printer_data->line_no++;
1342 break;
1343 case BINARY_PRINT_DATA_END:
1344 default:
1345 break;
1346 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001347
1348 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001349}
1350
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001351static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001352{
1353 unsigned int nr_bytes = sample->raw_size;
1354 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001355 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1356 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001357
1358 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001359 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1360
1361 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001362}
1363
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001364static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001365{
1366 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001367 return fprintf(fp, "%*s", spacing - len, "");
1368
1369 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001370}
1371
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001372static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001373{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001374 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001375}
1376
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001377static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001378{
1379 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1380 int len;
1381
1382 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001383 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001384
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001385 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001386 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001387 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001388}
1389
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001390static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001391{
1392 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1393 int len;
1394
1395 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001396 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001397
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001398 len = fprintf(fp, " hints: %#x extensions: %#x ",
1399 data->hints, data->extensions);
1400 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001401}
1402
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001403static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001404{
1405 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1406 int len;
1407
1408 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001409 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001410
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001411 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1412 data->hw, data->cstate, data->subcstate);
1413 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001414}
1415
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001416static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001417{
1418 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1419 int len;
1420
1421 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001422 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001423
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001424 len = fprintf(fp, " IP: %u ", data->ip);
1425 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001426}
1427
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001428static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001429{
1430 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1431 int len;
1432
1433 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001434 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001435
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001436 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001437 data->deepest_cstate, data->last_cstate,
1438 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001439 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001440}
1441
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001442static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001443{
1444 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1445 unsigned int percent, freq;
1446 int len;
1447
1448 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001449 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001450
1451 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001452 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001453 if (data->max_nonturbo) {
1454 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001455 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001456 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001457 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001458}
1459
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001460static int perf_sample__fprintf_synth(struct perf_sample *sample,
1461 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001462{
1463 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001464 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001465 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001466 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001467 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001468 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001469 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001470 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001471 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001472 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001473 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001474 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001475 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001476 default:
1477 break;
1478 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001479
1480 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001481}
1482
Jin Yao2ab046c2017-12-08 21:13:46 +08001483#define PTIME_RANGE_MAX 10
1484
Jiri Olsa809e9422015-11-26 18:55:21 +01001485struct perf_script {
1486 struct perf_tool tool;
1487 struct perf_session *session;
1488 bool show_task_events;
1489 bool show_mmap_events;
1490 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301491 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001492 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001493 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001494 struct cpu_map *cpus;
1495 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001496 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001497 const char *time_str;
Jin Yao2ab046c2017-12-08 21:13:46 +08001498 struct perf_time_interval ptime_range[PTIME_RANGE_MAX];
1499 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001500};
1501
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001502static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1503{
1504 struct perf_evsel *evsel;
1505 int max = 0;
1506
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001507 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001508 int len = strlen(perf_evsel__name(evsel));
1509
1510 max = MAX(len, max);
1511 }
1512
1513 return max;
1514}
1515
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001516static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001517{
1518 struct mem_info mi = { .data_src.val = data_src };
1519 char decode[100];
1520 char out[100];
1521 static int maxlen;
1522 int len;
1523
1524 perf_script__meminfo_scnprintf(decode, 100, &mi);
1525
1526 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1527 if (maxlen < len)
1528 maxlen = len;
1529
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001530 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001531}
1532
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001533struct metric_ctx {
1534 struct perf_sample *sample;
1535 struct thread *thread;
1536 struct perf_evsel *evsel;
1537 FILE *fp;
1538};
1539
1540static void script_print_metric(void *ctx, const char *color,
1541 const char *fmt,
1542 const char *unit, double val)
1543{
1544 struct metric_ctx *mctx = ctx;
1545
1546 if (!fmt)
1547 return;
1548 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001549 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001550 fputs("\tmetric: ", mctx->fp);
1551 if (color)
1552 color_fprintf(mctx->fp, color, fmt, val);
1553 else
1554 printf(fmt, val);
1555 fprintf(mctx->fp, " %s\n", unit);
1556}
1557
1558static void script_new_line(void *ctx)
1559{
1560 struct metric_ctx *mctx = ctx;
1561
1562 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001563 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001564 fputs("\tmetric: ", mctx->fp);
1565}
1566
1567static void perf_sample__fprint_metric(struct perf_script *script,
1568 struct thread *thread,
1569 struct perf_evsel *evsel,
1570 struct perf_sample *sample,
1571 FILE *fp)
1572{
1573 struct perf_stat_output_ctx ctx = {
1574 .print_metric = script_print_metric,
1575 .new_line = script_new_line,
1576 .ctx = &(struct metric_ctx) {
1577 .sample = sample,
1578 .thread = thread,
1579 .evsel = evsel,
1580 .fp = fp,
1581 },
1582 .force_header = false,
1583 };
1584 struct perf_evsel *ev2;
1585 static bool init;
1586 u64 val;
1587
1588 if (!init) {
1589 perf_stat__init_shadow_stats();
1590 init = true;
1591 }
1592 if (!evsel->stats)
1593 perf_evlist__alloc_stats(script->session->evlist, false);
1594 if (evsel_script(evsel->leader)->gnum++ == 0)
1595 perf_stat__reset_shadow_stats();
1596 val = sample->period * evsel->scale;
1597 perf_stat__update_shadow_stats(evsel,
1598 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001599 sample->cpu,
1600 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001601 evsel_script(evsel)->val = val;
1602 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1603 for_each_group_member (ev2, evsel->leader) {
1604 perf_stat__print_shadow_stats(ev2,
1605 evsel_script(ev2)->val,
1606 sample->cpu,
1607 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001608 NULL,
1609 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001610 }
1611 evsel_script(evsel->leader)->gnum = 0;
1612 }
1613}
1614
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001615static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001616 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001617 struct addr_location *al,
1618 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001619{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001620 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001621 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001622 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001623 struct perf_evsel_script *es = evsel->priv;
1624 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001625
Adrian Hunter14057202017-06-21 13:17:19 +03001626 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001627 return;
1628
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001629 ++es->samples;
1630
Jiri Olsa28a0b392018-01-07 17:03:52 +01001631 perf_sample__fprintf_start(sample, thread, evsel,
1632 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001633
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001634 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001635 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001636
Namhyung Kime944d3d2013-11-18 14:34:52 +09001637 if (PRINT_FIELD(EVNAME)) {
1638 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001639
1640 if (!script->name_width)
1641 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1642
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001643 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001644 }
1645
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001646 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001647 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001648
Akihiro Nagai95582592012-01-30 13:43:09 +09001649 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001650 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001651 return;
1652 }
1653
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001654 if (PRINT_FIELD(TRACE)) {
1655 event_format__fprintf(evsel->tp_format, sample->cpu,
1656 sample->raw_data, sample->raw_size, fp);
1657 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001658
1659 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001660 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001661
David Ahern7cec0922011-05-30 13:08:23 -06001662 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001663 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001664
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001665 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001666 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001667
1668 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001669 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001670
David Ahern787bef12011-05-27 14:28:43 -06001671 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001672 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001673
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001674 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001675 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001676 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001677 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001678
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001679 fputc(cursor ? '\n' : ' ', fp);
1680 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001681 }
1682
Stephane Eranianfc36f942015-08-31 18:41:10 +02001683 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001684 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001685
Andi Kleenb1491ac2017-09-05 11:40:57 -07001686 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001687 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001688
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001689 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001690 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001691 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001692 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001693 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001694 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001695
Wang Nan30372f02016-02-24 11:20:45 +00001696 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001697 perf_sample__fprintf_bpf_output(sample, fp);
1698 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001699
1700 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001701 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1702 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001703
1704 if (PRINT_FIELD(METRIC))
1705 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001706}
1707
Tom Zanussi956ffd02009-11-25 01:15:46 -06001708static struct scripting_ops *scripting_ops;
1709
Jiri Olsa36e33c52016-01-06 11:49:56 +01001710static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1711{
1712 int nthreads = thread_map__nr(counter->threads);
1713 int ncpus = perf_evsel__nr_cpus(counter);
1714 int cpu, thread;
1715 static int header_printed;
1716
1717 if (counter->system_wide)
1718 nthreads = 1;
1719
1720 if (!header_printed) {
1721 printf("%3s %8s %15s %15s %15s %15s %s\n",
1722 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1723 header_printed = 1;
1724 }
1725
1726 for (thread = 0; thread < nthreads; thread++) {
1727 for (cpu = 0; cpu < ncpus; cpu++) {
1728 struct perf_counts_values *counts;
1729
1730 counts = perf_counts(counter->counts, cpu, thread);
1731
1732 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1733 counter->cpus->map[cpu],
1734 thread_map__pid(counter->threads, thread),
1735 counts->val,
1736 counts->ena,
1737 counts->run,
1738 tstamp,
1739 perf_evsel__name(counter));
1740 }
1741 }
1742}
1743
Jiri Olsae099eba2016-01-05 22:09:09 +01001744static void process_stat(struct perf_evsel *counter, u64 tstamp)
1745{
1746 if (scripting_ops && scripting_ops->process_stat)
1747 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001748 else
1749 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001750}
1751
1752static void process_stat_interval(u64 tstamp)
1753{
1754 if (scripting_ops && scripting_ops->process_stat_interval)
1755 scripting_ops->process_stat_interval(tstamp);
1756}
1757
Tom Zanussi956ffd02009-11-25 01:15:46 -06001758static void setup_scripting(void)
1759{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001760 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001761 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001762}
1763
Adrian Hunterd445dd22014-08-15 22:08:37 +03001764static int flush_scripting(void)
1765{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001766 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001767}
1768
Tom Zanussi956ffd02009-11-25 01:15:46 -06001769static int cleanup_scripting(void)
1770{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001771 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001772
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001773 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001774}
1775
Jiri Olsa809e9422015-11-26 18:55:21 +01001776static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001777 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001778 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001779 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001780 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001781{
Jiri Olsa809e9422015-11-26 18:55:21 +01001782 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001783 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001784
Jin Yao2ab046c2017-12-08 21:13:46 +08001785 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1786 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001787 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001788 }
David Aherna91f4c42016-11-29 10:15:43 -07001789
David Ahern1424dc92011-03-09 22:23:28 -07001790 if (debug_mode) {
1791 if (sample->time < last_timestamp) {
1792 pr_err("Samples misordered, previous: %" PRIu64
1793 " this: %" PRIu64 "\n", last_timestamp,
1794 sample->time);
1795 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001796 }
David Ahern1424dc92011-03-09 22:23:28 -07001797 last_timestamp = sample->time;
1798 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001799 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001800
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001801 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001802 pr_err("problem processing %d event, skipping it.\n",
1803 event->header.type);
1804 return -1;
1805 }
1806
1807 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001808 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001809
Anton Blanchard5d67be92011-07-04 21:57:50 +10001810 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001811 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001812
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001813 if (scripting_ops)
1814 scripting_ops->process_event(event, sample, evsel, &al);
1815 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001816 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001817
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001818out_put:
1819 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001820 return 0;
1821}
1822
Adrian Hunter7ea95722013-11-01 15:51:30 +02001823static int process_attr(struct perf_tool *tool, union perf_event *event,
1824 struct perf_evlist **pevlist)
1825{
1826 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1827 struct perf_evlist *evlist;
1828 struct perf_evsel *evsel, *pos;
1829 int err;
1830
1831 err = perf_event__process_attr(tool, event, pevlist);
1832 if (err)
1833 return err;
1834
1835 evlist = *pevlist;
1836 evsel = perf_evlist__last(*pevlist);
1837
Adrian Hunter14057202017-06-21 13:17:19 +03001838 if (evsel->attr.type >= PERF_TYPE_MAX &&
1839 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001840 return 0;
1841
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001842 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001843 if (pos->attr.type == evsel->attr.type && pos != evsel)
1844 return 0;
1845 }
1846
1847 set_print_ip_opts(&evsel->attr);
1848
Jiri Olsad2b5a312015-10-16 12:41:25 +02001849 if (evsel->attr.sample_type)
1850 err = perf_evsel__check_attr(evsel, scr->session);
1851
1852 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001853}
1854
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001855static int process_comm_event(struct perf_tool *tool,
1856 union perf_event *event,
1857 struct perf_sample *sample,
1858 struct machine *machine)
1859{
1860 struct thread *thread;
1861 struct perf_script *script = container_of(tool, struct perf_script, tool);
1862 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001863 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001864 int ret = -1;
1865
1866 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1867 if (thread == NULL) {
1868 pr_debug("problem processing COMM event, skipping it.\n");
1869 return -1;
1870 }
1871
1872 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1873 goto out;
1874
1875 if (!evsel->attr.sample_id_all) {
1876 sample->cpu = 0;
1877 sample->time = 0;
1878 sample->tid = event->comm.tid;
1879 sample->pid = event->comm.pid;
1880 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01001881 perf_sample__fprintf_start(sample, thread, evsel,
1882 PERF_RECORD_COMM, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001883 perf_event__fprintf(event, stdout);
1884 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001885out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001886 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001887 return ret;
1888}
1889
Hari Bathini96a44bb2017-03-08 02:12:06 +05301890static int process_namespaces_event(struct perf_tool *tool,
1891 union perf_event *event,
1892 struct perf_sample *sample,
1893 struct machine *machine)
1894{
1895 struct thread *thread;
1896 struct perf_script *script = container_of(tool, struct perf_script, tool);
1897 struct perf_session *session = script->session;
1898 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1899 int ret = -1;
1900
1901 thread = machine__findnew_thread(machine, event->namespaces.pid,
1902 event->namespaces.tid);
1903 if (thread == NULL) {
1904 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1905 return -1;
1906 }
1907
1908 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1909 goto out;
1910
1911 if (!evsel->attr.sample_id_all) {
1912 sample->cpu = 0;
1913 sample->time = 0;
1914 sample->tid = event->namespaces.tid;
1915 sample->pid = event->namespaces.pid;
1916 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01001917 perf_sample__fprintf_start(sample, thread, evsel,
1918 PERF_RECORD_NAMESPACES, stdout);
Hari Bathini96a44bb2017-03-08 02:12:06 +05301919 perf_event__fprintf(event, stdout);
1920 ret = 0;
1921out:
1922 thread__put(thread);
1923 return ret;
1924}
1925
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001926static int process_fork_event(struct perf_tool *tool,
1927 union perf_event *event,
1928 struct perf_sample *sample,
1929 struct machine *machine)
1930{
1931 struct thread *thread;
1932 struct perf_script *script = container_of(tool, struct perf_script, tool);
1933 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001934 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001935
1936 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1937 return -1;
1938
1939 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1940 if (thread == NULL) {
1941 pr_debug("problem processing FORK event, skipping it.\n");
1942 return -1;
1943 }
1944
1945 if (!evsel->attr.sample_id_all) {
1946 sample->cpu = 0;
1947 sample->time = event->fork.time;
1948 sample->tid = event->fork.tid;
1949 sample->pid = event->fork.pid;
1950 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01001951 perf_sample__fprintf_start(sample, thread, evsel,
1952 PERF_RECORD_FORK, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001953 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001954 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001955
1956 return 0;
1957}
1958static int process_exit_event(struct perf_tool *tool,
1959 union perf_event *event,
1960 struct perf_sample *sample,
1961 struct machine *machine)
1962{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001963 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001964 struct thread *thread;
1965 struct perf_script *script = container_of(tool, struct perf_script, tool);
1966 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001967 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001968
1969 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1970 if (thread == NULL) {
1971 pr_debug("problem processing EXIT event, skipping it.\n");
1972 return -1;
1973 }
1974
1975 if (!evsel->attr.sample_id_all) {
1976 sample->cpu = 0;
1977 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001978 sample->tid = event->fork.tid;
1979 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001980 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01001981 perf_sample__fprintf_start(sample, thread, evsel,
1982 PERF_RECORD_EXIT, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001983 perf_event__fprintf(event, stdout);
1984
1985 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001986 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001987
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001988 thread__put(thread);
1989 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001990}
1991
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001992static int process_mmap_event(struct perf_tool *tool,
1993 union perf_event *event,
1994 struct perf_sample *sample,
1995 struct machine *machine)
1996{
1997 struct thread *thread;
1998 struct perf_script *script = container_of(tool, struct perf_script, tool);
1999 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002000 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002001
2002 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2003 return -1;
2004
2005 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2006 if (thread == NULL) {
2007 pr_debug("problem processing MMAP event, skipping it.\n");
2008 return -1;
2009 }
2010
2011 if (!evsel->attr.sample_id_all) {
2012 sample->cpu = 0;
2013 sample->time = 0;
2014 sample->tid = event->mmap.tid;
2015 sample->pid = event->mmap.pid;
2016 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002017 perf_sample__fprintf_start(sample, thread, evsel,
2018 PERF_RECORD_MMAP, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002019 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002020 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002021 return 0;
2022}
2023
2024static int process_mmap2_event(struct perf_tool *tool,
2025 union perf_event *event,
2026 struct perf_sample *sample,
2027 struct machine *machine)
2028{
2029 struct thread *thread;
2030 struct perf_script *script = container_of(tool, struct perf_script, tool);
2031 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002032 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002033
2034 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2035 return -1;
2036
2037 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2038 if (thread == NULL) {
2039 pr_debug("problem processing MMAP2 event, skipping it.\n");
2040 return -1;
2041 }
2042
2043 if (!evsel->attr.sample_id_all) {
2044 sample->cpu = 0;
2045 sample->time = 0;
2046 sample->tid = event->mmap2.tid;
2047 sample->pid = event->mmap2.pid;
2048 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002049 perf_sample__fprintf_start(sample, thread, evsel,
2050 PERF_RECORD_MMAP2, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002051 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002052 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002053 return 0;
2054}
2055
Adrian Hunter7c148982015-07-21 12:44:06 +03002056static int process_switch_event(struct perf_tool *tool,
2057 union perf_event *event,
2058 struct perf_sample *sample,
2059 struct machine *machine)
2060{
2061 struct thread *thread;
2062 struct perf_script *script = container_of(tool, struct perf_script, tool);
2063 struct perf_session *session = script->session;
2064 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2065
2066 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2067 return -1;
2068
2069 thread = machine__findnew_thread(machine, sample->pid,
2070 sample->tid);
2071 if (thread == NULL) {
2072 pr_debug("problem processing SWITCH event, skipping it.\n");
2073 return -1;
2074 }
2075
Jiri Olsa28a0b392018-01-07 17:03:52 +01002076 perf_sample__fprintf_start(sample, thread, evsel,
2077 PERF_RECORD_SWITCH, stdout);
Adrian Hunter7c148982015-07-21 12:44:06 +03002078 perf_event__fprintf(event, stdout);
2079 thread__put(thread);
2080 return 0;
2081}
2082
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002083static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002084{
2085 session_done = 1;
2086}
2087
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002088static void perf_script__fclose_per_event_dump(struct perf_script *script)
2089{
2090 struct perf_evlist *evlist = script->session->evlist;
2091 struct perf_evsel *evsel;
2092
2093 evlist__for_each_entry(evlist, evsel) {
2094 if (!evsel->priv)
2095 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002096 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002097 evsel->priv = NULL;
2098 }
2099}
2100
2101static int perf_script__fopen_per_event_dump(struct perf_script *script)
2102{
2103 struct perf_evsel *evsel;
2104
2105 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002106 /*
2107 * Already setup? I.e. we may be called twice in cases like
2108 * Intel PT, one for the intel_pt// and dummy events, then
2109 * for the evsels syntheized from the auxtrace info.
2110 *
2111 * Ses perf_script__process_auxtrace_info.
2112 */
2113 if (evsel->priv != NULL)
2114 continue;
2115
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002116 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002117 if (evsel->priv == NULL)
2118 goto out_err_fclose;
2119 }
2120
2121 return 0;
2122
2123out_err_fclose:
2124 perf_script__fclose_per_event_dump(script);
2125 return -1;
2126}
2127
2128static int perf_script__setup_per_event_dump(struct perf_script *script)
2129{
2130 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002131 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002132
2133 if (script->per_event_dump)
2134 return perf_script__fopen_per_event_dump(script);
2135
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002136 es_stdout.fp = stdout;
2137
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002138 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002139 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002140
2141 return 0;
2142}
2143
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002144static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2145{
2146 struct perf_evsel *evsel;
2147
2148 evlist__for_each_entry(script->session->evlist, evsel) {
2149 struct perf_evsel_script *es = evsel->priv;
2150
2151 perf_evsel_script__fprintf(es, stdout);
2152 perf_evsel_script__delete(es);
2153 evsel->priv = NULL;
2154 }
2155}
2156
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002157static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002158{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002159 int ret;
2160
Tom Zanussic239da32010-04-01 23:59:18 -05002161 signal(SIGINT, sig_handler);
2162
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002163 /* override event processing functions */
2164 if (script->show_task_events) {
2165 script->tool.comm = process_comm_event;
2166 script->tool.fork = process_fork_event;
2167 script->tool.exit = process_exit_event;
2168 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002169 if (script->show_mmap_events) {
2170 script->tool.mmap = process_mmap_event;
2171 script->tool.mmap2 = process_mmap2_event;
2172 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002173 if (script->show_switch_events)
2174 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302175 if (script->show_namespace_events)
2176 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002177
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002178 if (perf_script__setup_per_event_dump(script)) {
2179 pr_err("Couldn't create the per event dump files\n");
2180 return -1;
2181 }
2182
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002183 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002184
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002185 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002186 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002187
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002188 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002189 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002190
2191 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002192}
2193
Tom Zanussi956ffd02009-11-25 01:15:46 -06002194struct script_spec {
2195 struct list_head node;
2196 struct scripting_ops *ops;
2197 char spec[0];
2198};
2199
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002200static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002201
2202static struct script_spec *script_spec__new(const char *spec,
2203 struct scripting_ops *ops)
2204{
2205 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2206
2207 if (s != NULL) {
2208 strcpy(s->spec, spec);
2209 s->ops = ops;
2210 }
2211
2212 return s;
2213}
2214
Tom Zanussi956ffd02009-11-25 01:15:46 -06002215static void script_spec__add(struct script_spec *s)
2216{
2217 list_add_tail(&s->node, &script_specs);
2218}
2219
2220static struct script_spec *script_spec__find(const char *spec)
2221{
2222 struct script_spec *s;
2223
2224 list_for_each_entry(s, &script_specs, node)
2225 if (strcasecmp(s->spec, spec) == 0)
2226 return s;
2227 return NULL;
2228}
2229
Tom Zanussi956ffd02009-11-25 01:15:46 -06002230int script_spec_register(const char *spec, struct scripting_ops *ops)
2231{
2232 struct script_spec *s;
2233
2234 s = script_spec__find(spec);
2235 if (s)
2236 return -1;
2237
Taeung Song8560bae2016-02-26 00:13:10 +09002238 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002239 if (!s)
2240 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002241 else
2242 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002243
2244 return 0;
2245}
2246
2247static struct scripting_ops *script_spec__lookup(const char *spec)
2248{
2249 struct script_spec *s = script_spec__find(spec);
2250 if (!s)
2251 return NULL;
2252
2253 return s->ops;
2254}
2255
2256static void list_available_languages(void)
2257{
2258 struct script_spec *s;
2259
2260 fprintf(stderr, "\n");
2261 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002262 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002263
2264 list_for_each_entry(s, &script_specs, node)
2265 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2266
2267 fprintf(stderr, "\n");
2268}
2269
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002270static int parse_scriptname(const struct option *opt __maybe_unused,
2271 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002272{
2273 char spec[PATH_MAX];
2274 const char *script, *ext;
2275 int len;
2276
Tom Zanussif526d682010-01-27 02:27:52 -06002277 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002278 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002279 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002280 }
2281
2282 script = strchr(str, ':');
2283 if (script) {
2284 len = script - str;
2285 if (len >= PATH_MAX) {
2286 fprintf(stderr, "invalid language specifier");
2287 return -1;
2288 }
2289 strncpy(spec, str, len);
2290 spec[len] = '\0';
2291 scripting_ops = script_spec__lookup(spec);
2292 if (!scripting_ops) {
2293 fprintf(stderr, "invalid language specifier");
2294 return -1;
2295 }
2296 script++;
2297 } else {
2298 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002299 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002300 if (!ext) {
2301 fprintf(stderr, "invalid script extension");
2302 return -1;
2303 }
2304 scripting_ops = script_spec__lookup(++ext);
2305 if (!scripting_ops) {
2306 fprintf(stderr, "invalid script extension");
2307 return -1;
2308 }
2309 }
2310
2311 script_name = strdup(script);
2312
2313 return 0;
2314}
2315
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002316static int parse_output_fields(const struct option *opt __maybe_unused,
2317 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002318{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002319 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002320 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002321 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002322 int rc = 0;
2323 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002324 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002325 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002326
2327 if (!str)
2328 return -ENOMEM;
2329
David Ahern2c9e45f72011-03-17 10:03:21 -06002330 /* first word can state for which event type the user is specifying
2331 * the fields. If no type exists, the specified fields apply to all
2332 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002333 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002334 tok = strchr(str, ':');
2335 if (tok) {
2336 *tok = '\0';
2337 tok++;
2338 if (!strcmp(str, "hw"))
2339 type = PERF_TYPE_HARDWARE;
2340 else if (!strcmp(str, "sw"))
2341 type = PERF_TYPE_SOFTWARE;
2342 else if (!strcmp(str, "trace"))
2343 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002344 else if (!strcmp(str, "raw"))
2345 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002346 else if (!strcmp(str, "break"))
2347 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002348 else if (!strcmp(str, "synth"))
2349 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002350 else {
2351 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002352 rc = -EINVAL;
2353 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002354 }
2355
2356 if (output[type].user_set)
2357 pr_warning("Overriding previous field request for %s events.\n",
2358 event_type(type));
2359
2360 output[type].fields = 0;
2361 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002362 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002363
2364 } else {
2365 tok = str;
2366 if (strlen(str) == 0) {
2367 fprintf(stderr,
2368 "Cannot set fields to 'none' for all event types.\n");
2369 rc = -EINVAL;
2370 goto out;
2371 }
2372
Andi Kleen36ce5652017-06-02 08:48:10 -07002373 /* Don't override defaults for +- */
2374 if (strchr(str, '+') || strchr(str, '-'))
2375 goto parse;
2376
David Ahern2c9e45f72011-03-17 10:03:21 -06002377 if (output_set_by_user())
2378 pr_warning("Overriding previous field request for all events.\n");
2379
Adrian Hunter14057202017-06-21 13:17:19 +03002380 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002381 output[j].fields = 0;
2382 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002383 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002384 }
David Ahern1424dc92011-03-09 22:23:28 -07002385 }
2386
Andi Kleen36ce5652017-06-02 08:48:10 -07002387parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002388 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002389 if (*tok == '+') {
2390 if (change == SET)
2391 goto out_badmix;
2392 change = ADD;
2393 tok++;
2394 } else if (*tok == '-') {
2395 if (change == SET)
2396 goto out_badmix;
2397 change = REMOVE;
2398 tok++;
2399 } else {
2400 if (change != SET && change != DEFAULT)
2401 goto out_badmix;
2402 change = SET;
2403 }
2404
David Ahern745f43e2011-03-09 22:23:26 -07002405 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002406 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002407 break;
David Ahern745f43e2011-03-09 22:23:26 -07002408 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002409 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002410 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002411 continue;
2412 }
David Ahern745f43e2011-03-09 22:23:26 -07002413 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002414 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002415 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002416 goto out;
2417 }
2418
2419 if (type == -1) {
2420 /* add user option to all events types for
2421 * which it is valid
2422 */
Adrian Hunter14057202017-06-21 13:17:19 +03002423 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002424 if (output[j].invalid_fields & all_output_options[i].field) {
2425 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2426 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002427 } else {
2428 if (change == REMOVE)
2429 output[j].fields &= ~all_output_options[i].field;
2430 else
2431 output[j].fields |= all_output_options[i].field;
2432 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002433 }
2434 } else {
2435 if (output[type].invalid_fields & all_output_options[i].field) {
2436 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2437 all_output_options[i].str, event_type(type));
2438
2439 rc = -EINVAL;
2440 goto out;
2441 }
2442 output[type].fields |= all_output_options[i].field;
2443 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002444 }
2445
2446 if (type >= 0) {
2447 if (output[type].fields == 0) {
2448 pr_debug("No fields requested for %s type. "
2449 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002450 }
David Ahern1424dc92011-03-09 22:23:28 -07002451 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002452 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002453
Andi Kleen36ce5652017-06-02 08:48:10 -07002454out_badmix:
2455 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2456 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002457out:
David Ahern745f43e2011-03-09 22:23:26 -07002458 free(str);
2459 return rc;
2460}
2461
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002462#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2463 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2464 if ((lang_dirent->d_type == DT_DIR || \
2465 (lang_dirent->d_type == DT_UNKNOWN && \
2466 is_directory(scripts_path, lang_dirent))) && \
2467 (strcmp(lang_dirent->d_name, ".")) && \
2468 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002469
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002470#define for_each_script(lang_path, lang_dir, script_dirent) \
2471 while ((script_dirent = readdir(lang_dir)) != NULL) \
2472 if (script_dirent->d_type != DT_DIR && \
2473 (script_dirent->d_type != DT_UNKNOWN || \
2474 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002475
2476
2477#define RECORD_SUFFIX "-record"
2478#define REPORT_SUFFIX "-report"
2479
2480struct script_desc {
2481 struct list_head node;
2482 char *name;
2483 char *half_liner;
2484 char *args;
2485};
2486
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002487static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002488
2489static struct script_desc *script_desc__new(const char *name)
2490{
2491 struct script_desc *s = zalloc(sizeof(*s));
2492
Tom Zanussib5b87312010-11-10 08:16:51 -06002493 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002494 s->name = strdup(name);
2495
2496 return s;
2497}
2498
2499static void script_desc__delete(struct script_desc *s)
2500{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002501 zfree(&s->name);
2502 zfree(&s->half_liner);
2503 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002504 free(s);
2505}
2506
2507static void script_desc__add(struct script_desc *s)
2508{
2509 list_add_tail(&s->node, &script_descs);
2510}
2511
2512static struct script_desc *script_desc__find(const char *name)
2513{
2514 struct script_desc *s;
2515
2516 list_for_each_entry(s, &script_descs, node)
2517 if (strcasecmp(s->name, name) == 0)
2518 return s;
2519 return NULL;
2520}
2521
2522static struct script_desc *script_desc__findnew(const char *name)
2523{
2524 struct script_desc *s = script_desc__find(name);
2525
2526 if (s)
2527 return s;
2528
2529 s = script_desc__new(name);
2530 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002531 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002532
2533 script_desc__add(s);
2534
2535 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002536}
2537
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002538static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002539{
2540 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002541 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002542
2543 if (strlen(str) > suffix_len) {
2544 p = str + strlen(str) - suffix_len;
2545 if (!strncmp(p, suffix, suffix_len))
2546 return p;
2547 }
2548
2549 return NULL;
2550}
2551
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002552static int read_script_info(struct script_desc *desc, const char *filename)
2553{
2554 char line[BUFSIZ], *p;
2555 FILE *fp;
2556
2557 fp = fopen(filename, "r");
2558 if (!fp)
2559 return -1;
2560
2561 while (fgets(line, sizeof(line), fp)) {
2562 p = ltrim(line);
2563 if (strlen(p) == 0)
2564 continue;
2565 if (*p != '#')
2566 continue;
2567 p++;
2568 if (strlen(p) && *p == '!')
2569 continue;
2570
2571 p = ltrim(p);
2572 if (strlen(p) && p[strlen(p) - 1] == '\n')
2573 p[strlen(p) - 1] = '\0';
2574
2575 if (!strncmp(p, "description:", strlen("description:"))) {
2576 p += strlen("description:");
2577 desc->half_liner = strdup(ltrim(p));
2578 continue;
2579 }
2580
2581 if (!strncmp(p, "args:", strlen("args:"))) {
2582 p += strlen("args:");
2583 desc->args = strdup(ltrim(p));
2584 continue;
2585 }
2586 }
2587
2588 fclose(fp);
2589
2590 return 0;
2591}
2592
Robert Richter38efb532011-11-25 11:38:40 +01002593static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2594{
2595 char *script_root, *str;
2596
2597 script_root = strdup(script_dirent->d_name);
2598 if (!script_root)
2599 return NULL;
2600
2601 str = (char *)ends_with(script_root, suffix);
2602 if (!str) {
2603 free(script_root);
2604 return NULL;
2605 }
2606
2607 *str = '\0';
2608 return script_root;
2609}
2610
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002611static int list_available_scripts(const struct option *opt __maybe_unused,
2612 const char *s __maybe_unused,
2613 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002614{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002615 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002616 char scripts_path[MAXPATHLEN];
2617 DIR *scripts_dir, *lang_dir;
2618 char script_path[MAXPATHLEN];
2619 char lang_path[MAXPATHLEN];
2620 struct script_desc *desc;
2621 char first_half[BUFSIZ];
2622 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002623
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002624 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002625
2626 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002627 if (!scripts_dir) {
2628 fprintf(stdout,
2629 "open(%s) failed.\n"
2630 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2631 scripts_path);
2632 exit(-1);
2633 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002634
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002635 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002636 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002637 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002638 lang_dir = opendir(lang_path);
2639 if (!lang_dir)
2640 continue;
2641
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002642 for_each_script(lang_path, lang_dir, script_dirent) {
2643 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002644 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002645 desc = script_desc__findnew(script_root);
2646 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002647 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002648 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002649 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002650 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002651 }
2652 }
2653
2654 fprintf(stdout, "List of available trace scripts:\n");
2655 list_for_each_entry(desc, &script_descs, node) {
2656 sprintf(first_half, "%s %s", desc->name,
2657 desc->args ? desc->args : "");
2658 fprintf(stdout, " %-36s %s\n", first_half,
2659 desc->half_liner ? desc->half_liner : "");
2660 }
2661
2662 exit(0);
2663}
2664
Feng Tange5f37052012-09-07 16:42:26 +08002665/*
Feng Tang49e639e2012-10-30 11:56:03 +08002666 * Some scripts specify the required events in their "xxx-record" file,
2667 * this function will check if the events in perf.data match those
2668 * mentioned in the "xxx-record".
2669 *
2670 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2671 * which is covered well now. And new parsing code should be added to
2672 * cover the future complexing formats like event groups etc.
2673 */
2674static int check_ev_match(char *dir_name, char *scriptname,
2675 struct perf_session *session)
2676{
2677 char filename[MAXPATHLEN], evname[128];
2678 char line[BUFSIZ], *p;
2679 struct perf_evsel *pos;
2680 int match, len;
2681 FILE *fp;
2682
2683 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2684
2685 fp = fopen(filename, "r");
2686 if (!fp)
2687 return -1;
2688
2689 while (fgets(line, sizeof(line), fp)) {
2690 p = ltrim(line);
2691 if (*p == '#')
2692 continue;
2693
2694 while (strlen(p)) {
2695 p = strstr(p, "-e");
2696 if (!p)
2697 break;
2698
2699 p += 2;
2700 p = ltrim(p);
2701 len = strcspn(p, " \t");
2702 if (!len)
2703 break;
2704
2705 snprintf(evname, len + 1, "%s", p);
2706
2707 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002708 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002709 if (!strcmp(perf_evsel__name(pos), evname)) {
2710 match = 1;
2711 break;
2712 }
2713 }
2714
2715 if (!match) {
2716 fclose(fp);
2717 return -1;
2718 }
2719 }
2720 }
2721
2722 fclose(fp);
2723 return 0;
2724}
2725
2726/*
Feng Tange5f37052012-09-07 16:42:26 +08002727 * Return -1 if none is found, otherwise the actual scripts number.
2728 *
2729 * Currently the only user of this function is the script browser, which
2730 * will list all statically runnable scripts, select one, execute it and
2731 * show the output in a perf browser.
2732 */
2733int find_scripts(char **scripts_array, char **scripts_path_array)
2734{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002735 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002736 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002737 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002738 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002739 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002740 .file = {
2741 .path = input_name,
2742 },
2743 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002744 };
Feng Tange5f37052012-09-07 16:42:26 +08002745 char *temp;
2746 int i = 0;
2747
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002748 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002749 if (!session)
2750 return -1;
2751
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002752 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002753
2754 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002755 if (!scripts_dir) {
2756 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002757 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002758 }
Feng Tange5f37052012-09-07 16:42:26 +08002759
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002760 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002761 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002762 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002763#ifdef NO_LIBPERL
2764 if (strstr(lang_path, "perl"))
2765 continue;
2766#endif
2767#ifdef NO_LIBPYTHON
2768 if (strstr(lang_path, "python"))
2769 continue;
2770#endif
2771
2772 lang_dir = opendir(lang_path);
2773 if (!lang_dir)
2774 continue;
2775
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002776 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002777 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002778 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002779 continue;
2780 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002781 script_dirent->d_name);
2782 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002783 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002784 (temp - script_dirent->d_name) + 1,
2785 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002786
2787 if (check_ev_match(lang_path,
2788 scripts_array[i], session))
2789 continue;
2790
Feng Tange5f37052012-09-07 16:42:26 +08002791 i++;
2792 }
Feng Tang49e639e2012-10-30 11:56:03 +08002793 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002794 }
2795
Feng Tang49e639e2012-10-30 11:56:03 +08002796 closedir(scripts_dir);
2797 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002798 return i;
2799}
2800
Tom Zanussi38752942009-12-15 02:53:39 -06002801static char *get_script_path(const char *script_root, const char *suffix)
2802{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002803 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002804 char scripts_path[MAXPATHLEN];
2805 char script_path[MAXPATHLEN];
2806 DIR *scripts_dir, *lang_dir;
2807 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002808 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002809
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002810 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002811
2812 scripts_dir = opendir(scripts_path);
2813 if (!scripts_dir)
2814 return NULL;
2815
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002816 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002817 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002818 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002819 lang_dir = opendir(lang_path);
2820 if (!lang_dir)
2821 continue;
2822
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002823 for_each_script(lang_path, lang_dir, script_dirent) {
2824 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002825 if (__script_root && !strcmp(script_root, __script_root)) {
2826 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002827 closedir(lang_dir);
2828 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002829 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002830 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002831 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002832 }
2833 free(__script_root);
2834 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002835 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002836 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002837 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002838
Robert Richter38efb532011-11-25 11:38:40 +01002839 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002840}
2841
Tom Zanussib5b87312010-11-10 08:16:51 -06002842static bool is_top_script(const char *script_path)
2843{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002844 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002845}
2846
2847static int has_required_arg(char *script_path)
2848{
2849 struct script_desc *desc;
2850 int n_args = 0;
2851 char *p;
2852
2853 desc = script_desc__new(NULL);
2854
2855 if (read_script_info(desc, script_path))
2856 goto out;
2857
2858 if (!desc->args)
2859 goto out;
2860
2861 for (p = desc->args; *p; p++)
2862 if (*p == '<')
2863 n_args++;
2864out:
2865 script_desc__delete(desc);
2866
2867 return n_args;
2868}
2869
David Ahernd54b1a92012-08-26 12:24:46 -06002870static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002871{
2872 char **__argv = malloc(sizeof(const char *) * argc);
2873
David Ahernd54b1a92012-08-26 12:24:46 -06002874 if (!__argv) {
2875 pr_err("malloc failed\n");
2876 return -1;
2877 }
2878
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002879 memcpy(__argv, argv, sizeof(const char *) * argc);
2880 argc = parse_options(argc, (const char **)__argv, record_options,
2881 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2882 free(__argv);
2883
David Ahernd54b1a92012-08-26 12:24:46 -06002884 system_wide = (argc == 0);
2885
2886 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002887}
2888
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002889static void script__setup_sample_type(struct perf_script *script)
2890{
2891 struct perf_session *session = script->session;
2892 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2893
2894 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2895 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2896 (sample_type & PERF_SAMPLE_STACK_USER))
2897 callchain_param.record_mode = CALLCHAIN_DWARF;
2898 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2899 callchain_param.record_mode = CALLCHAIN_LBR;
2900 else
2901 callchain_param.record_mode = CALLCHAIN_FP;
2902 }
2903}
2904
Jiri Olsae099eba2016-01-05 22:09:09 +01002905static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2906 union perf_event *event,
2907 struct perf_session *session)
2908{
2909 struct stat_round_event *round = &event->stat_round;
2910 struct perf_evsel *counter;
2911
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002912 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002913 perf_stat_process_counter(&stat_config, counter);
2914 process_stat(counter, round->time);
2915 }
2916
2917 process_stat_interval(round->time);
2918 return 0;
2919}
2920
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002921static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2922 union perf_event *event,
2923 struct perf_session *session __maybe_unused)
2924{
2925 perf_event__read_stat_config(&stat_config, &event->stat_config);
2926 return 0;
2927}
2928
Jiri Olsacfc88742016-01-05 22:09:06 +01002929static int set_maps(struct perf_script *script)
2930{
2931 struct perf_evlist *evlist = script->session->evlist;
2932
2933 if (!script->cpus || !script->threads)
2934 return 0;
2935
2936 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2937 return -EINVAL;
2938
2939 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2940
2941 if (perf_evlist__alloc_stats(evlist, true))
2942 return -ENOMEM;
2943
2944 script->allocated = true;
2945 return 0;
2946}
2947
2948static
2949int process_thread_map_event(struct perf_tool *tool,
2950 union perf_event *event,
2951 struct perf_session *session __maybe_unused)
2952{
2953 struct perf_script *script = container_of(tool, struct perf_script, tool);
2954
2955 if (script->threads) {
2956 pr_warning("Extra thread map event, ignoring.\n");
2957 return 0;
2958 }
2959
2960 script->threads = thread_map__new_event(&event->thread_map);
2961 if (!script->threads)
2962 return -ENOMEM;
2963
2964 return set_maps(script);
2965}
2966
2967static
2968int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2969 union perf_event *event,
2970 struct perf_session *session __maybe_unused)
2971{
2972 struct perf_script *script = container_of(tool, struct perf_script, tool);
2973
2974 if (script->cpus) {
2975 pr_warning("Extra cpu map event, ignoring.\n");
2976 return 0;
2977 }
2978
2979 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2980 if (!script->cpus)
2981 return -ENOMEM;
2982
2983 return set_maps(script);
2984}
2985
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002986#ifdef HAVE_AUXTRACE_SUPPORT
2987static int perf_script__process_auxtrace_info(struct perf_tool *tool,
2988 union perf_event *event,
2989 struct perf_session *session)
2990{
2991 int ret = perf_event__process_auxtrace_info(tool, event, session);
2992
2993 if (ret == 0) {
2994 struct perf_script *script = container_of(tool, struct perf_script, tool);
2995
2996 ret = perf_script__setup_per_event_dump(script);
2997 }
2998
2999 return ret;
3000}
3001#else
3002#define perf_script__process_auxtrace_info 0
3003#endif
3004
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003005int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003006{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003007 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003008 bool header = false;
3009 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003010 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003011 char *rec_script_path = NULL;
3012 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003013 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003014 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06003015 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003016 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003017 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003018 struct perf_script script = {
3019 .tool = {
3020 .sample = process_sample_event,
3021 .mmap = perf_event__process_mmap,
3022 .mmap2 = perf_event__process_mmap2,
3023 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303024 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003025 .exit = perf_event__process_exit,
3026 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003027 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003028 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003029 .tracing_data = perf_event__process_tracing_data,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003030 .feature = perf_event__process_feature,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003031 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003032 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003033 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003034 .auxtrace = perf_event__process_auxtrace,
3035 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003036 .stat = perf_event__process_stat_event,
3037 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003038 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003039 .thread_map = process_thread_map_event,
3040 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003041 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003042 .ordering_requires_timestamps = true,
3043 },
3044 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003045 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003046 .mode = PERF_DATA_MODE_READ,
3047 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003048 const struct option options[] = {
3049 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3050 "dump raw trace in ASCII"),
3051 OPT_INCR('v', "verbose", &verbose,
3052 "be more verbose (show symbol address, etc)"),
3053 OPT_BOOLEAN('L', "Latency", &latency_format,
3054 "show latency attributes (irqs/preemption disabled, etc)"),
3055 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3056 list_available_scripts),
3057 OPT_CALLBACK('s', "script", NULL, "name",
3058 "script file name (lang:script name, script name, or *)",
3059 parse_scriptname),
3060 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3061 "generate perf-script.xx script in specified language"),
3062 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3063 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3064 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003065 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3066 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003067 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3068 "file", "vmlinux pathname"),
3069 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3070 "file", "kallsyms pathname"),
3071 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3072 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003073 OPT_CALLBACK(0, "symfs", NULL, "directory",
3074 "Look for files with symbols relative to this directory",
3075 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003076 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003077 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003078 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003079 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003080 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Andi Kleenb1491ac2017-09-05 11:40:57 -07003081 "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
Kan Liang49d58f02017-08-29 13:11:11 -04003082 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
Andi Kleen48d02a12017-02-23 15:46:34 -08003083 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003084 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3085 "system-wide collection from all CPUs"),
3086 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3087 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07003088 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3089 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003090 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3091 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3092 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003093 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3094 "only consider symbols in these pids"),
3095 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3096 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003097 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3098 "Set the maximum stack depth when parsing the callchain, "
3099 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003100 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003101 OPT_BOOLEAN('I', "show-info", &show_full_info,
3102 "display extended information from perf.data file"),
3103 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3104 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003105 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3106 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003107 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3108 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003109 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3110 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303111 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3112 "Show namespace events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003113 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3114 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003115 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003116 OPT_INTEGER(0, "max-blocks", &max_blocks,
3117 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03003118 OPT_BOOLEAN(0, "ns", &nanosecs,
3119 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003120 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
3121 "Instruction Tracing options",
3122 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003123 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3124 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003125 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3126 "Enable symbol demangling"),
3127 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3128 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003129 OPT_STRING(0, "time", &script.time_str, "str",
3130 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003131 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3132 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003133 OPT_END()
3134 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003135 const char * const script_subcommands[] = { "record", "report", NULL };
3136 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003137 "perf script [<options>]",
3138 "perf script [<options>] record <script> [<record-options>] <command>",
3139 "perf script [<options>] report <script> [script-args]",
3140 "perf script [<options>] <script> [<record-options>] <command>",
3141 "perf script [<options>] <top-script> [script-args]",
3142 NULL
3143 };
Tom Zanussi38752942009-12-15 02:53:39 -06003144
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003145 perf_set_singlethreaded();
3146
Tom Zanussib5b87312010-11-10 08:16:51 -06003147 setup_scripting();
3148
Yunlong Song40cae2b2015-03-18 21:35:54 +08003149 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003150 PARSE_OPT_STOP_AT_NON_OPTION);
3151
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003152 data.file.path = input_name;
3153 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003154
Tom Zanussib5b87312010-11-10 08:16:51 -06003155 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3156 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3157 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003158 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003159 }
3160
Tom Zanussib5b87312010-11-10 08:16:51 -06003161 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3162 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3163 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003164 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003165 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003166 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003167 return -1;
3168 }
Tom Zanussi38752942009-12-15 02:53:39 -06003169 }
3170
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003171 if (itrace_synth_opts.callchain &&
3172 itrace_synth_opts.callchain_sz > scripting_max_stack)
3173 scripting_max_stack = itrace_synth_opts.callchain_sz;
3174
Ben Hutchings44e668c2010-10-10 16:10:03 +01003175 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003176 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003177
Tom Zanussib5b87312010-11-10 08:16:51 -06003178 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003179 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003180 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003181 pid_t pid;
3182
Tom Zanussib5b87312010-11-10 08:16:51 -06003183 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3184 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3185
3186 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003187 usage_with_options_msg(script_usage, options,
3188 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003189 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003190 }
3191
Tom Zanussib5b87312010-11-10 08:16:51 -06003192 if (is_top_script(argv[0])) {
3193 rep_args = argc - 1;
3194 } else {
3195 int rec_args;
3196
3197 rep_args = has_required_arg(rep_script_path);
3198 rec_args = (argc - 1) - rep_args;
3199 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003200 usage_with_options_msg(script_usage, options,
3201 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003202 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003203 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003204 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003205 }
3206
3207 if (pipe(live_pipe) < 0) {
3208 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003209 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003210 }
3211
3212 pid = fork();
3213 if (pid < 0) {
3214 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003215 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003216 }
3217
3218 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003219 j = 0;
3220
Tom Zanussia0cccc22010-04-01 23:59:25 -05003221 dup2(live_pipe[1], 1);
3222 close(live_pipe[0]);
3223
Robert Richter317df652011-11-25 15:05:25 +01003224 if (is_top_script(argv[0])) {
3225 system_wide = true;
3226 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003227 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3228 err = -1;
3229 goto out;
3230 }
Robert Richter317df652011-11-25 15:05:25 +01003231 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003232
3233 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003234 if (!__argv) {
3235 pr_err("malloc failed\n");
3236 err = -ENOMEM;
3237 goto out;
3238 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003239
Tom Zanussib5b87312010-11-10 08:16:51 -06003240 __argv[j++] = "/bin/sh";
3241 __argv[j++] = rec_script_path;
3242 if (system_wide)
3243 __argv[j++] = "-a";
3244 __argv[j++] = "-q";
3245 __argv[j++] = "-o";
3246 __argv[j++] = "-";
3247 for (i = rep_args + 1; i < argc; i++)
3248 __argv[j++] = argv[i];
3249 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003250
3251 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003252 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003253 exit(-1);
3254 }
3255
3256 dup2(live_pipe[0], 0);
3257 close(live_pipe[1]);
3258
Tom Zanussib5b87312010-11-10 08:16:51 -06003259 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003260 if (!__argv) {
3261 pr_err("malloc failed\n");
3262 err = -ENOMEM;
3263 goto out;
3264 }
3265
Tom Zanussib5b87312010-11-10 08:16:51 -06003266 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003267 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003268 __argv[j++] = rep_script_path;
3269 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003270 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003271 __argv[j++] = "-i";
3272 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003273 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003274
3275 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003276 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003277 exit(-1);
3278 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003279
Tom Zanussib5b87312010-11-10 08:16:51 -06003280 if (rec_script_path)
3281 script_path = rec_script_path;
3282 if (rep_script_path)
3283 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003284
Tom Zanussib5b87312010-11-10 08:16:51 -06003285 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003286 j = 0;
3287
Robert Richter317df652011-11-25 15:05:25 +01003288 if (!rec_script_path)
3289 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003290 else if (!system_wide) {
3291 if (have_cmd(argc - 1, &argv[1]) != 0) {
3292 err = -1;
3293 goto out;
3294 }
3295 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003296
3297 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003298 if (!__argv) {
3299 pr_err("malloc failed\n");
3300 err = -ENOMEM;
3301 goto out;
3302 }
3303
Tom Zanussib5b87312010-11-10 08:16:51 -06003304 __argv[j++] = "/bin/sh";
3305 __argv[j++] = script_path;
3306 if (system_wide)
3307 __argv[j++] = "-a";
3308 for (i = 2; i < argc; i++)
3309 __argv[j++] = argv[i];
3310 __argv[j++] = NULL;
3311
3312 execvp("/bin/sh", (char **)__argv);
3313 free(__argv);
3314 exit(-1);
3315 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003316
Tom Zanussicf4fee52010-03-03 01:04:33 -06003317 if (!script_name)
3318 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003319
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003320 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003321 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003322 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003323
Jiri Olsae90debd2013-12-09 11:02:50 +01003324 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003325 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003326 perf_session__fprintf_info(session, stdout, show_full_info);
3327 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003328 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003329 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003330 if (show_full_info)
3331 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003332
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003333 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003334 goto out_delete;
3335
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003336 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003337 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003338
Adrian Huntere2167082016-06-23 16:40:58 +03003339 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3340 itrace_synth_opts.thread_stack = true;
3341
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003342 session->itrace_synth_opts = &itrace_synth_opts;
3343
Anton Blanchard5d67be92011-07-04 21:57:50 +10003344 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003345 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3346 if (err < 0)
3347 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003348 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003349 }
3350
David Ahern1424dc92011-03-09 22:23:28 -07003351 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003352 symbol_conf.use_callchain = true;
3353 else
3354 symbol_conf.use_callchain = false;
3355
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003356 if (session->tevent.pevent &&
3357 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003358 machine__resolve_kernel_addr,
3359 &session->machines.host) < 0) {
3360 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003361 err = -1;
3362 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003363 }
3364
Tom Zanussi956ffd02009-11-25 01:15:46 -06003365 if (generate_script_lang) {
3366 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003367 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003368
David Ahern2c9e45f72011-03-17 10:03:21 -06003369 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003370 fprintf(stderr,
3371 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003372 err = -EINVAL;
3373 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003374 }
3375
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003376 input = open(data.file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003377 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003378 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003379 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003380 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003381 }
3382
3383 err = fstat(input, &perf_stat);
3384 if (err < 0) {
3385 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003386 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003387 }
3388
3389 if (!perf_stat.st_size) {
3390 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003391 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003392 }
3393
3394 scripting_ops = script_spec__lookup(generate_script_lang);
3395 if (!scripting_ops) {
3396 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003397 err = -ENOENT;
3398 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003399 }
3400
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003401 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003402 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003403 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003404 }
3405
3406 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003407 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003408 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003409 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003410 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003411 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003412 }
3413
David Ahern9cbdb702011-04-06 21:54:20 -06003414
3415 err = perf_session__check_output_opt(session);
3416 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003417 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003418
David Aherna91f4c42016-11-29 10:15:43 -07003419 /* needs to be parsed after looking up reference time */
Jin Yao2ab046c2017-12-08 21:13:46 +08003420 if (perf_time__parse_str(script.ptime_range, script.time_str) != 0) {
3421 if (session->evlist->first_sample_time == 0 &&
3422 session->evlist->last_sample_time == 0) {
3423 pr_err("No first/last sample time in perf data\n");
3424 err = -EINVAL;
3425 goto out_delete;
3426 }
3427
3428 script.range_num = perf_time__percent_parse_str(
3429 script.ptime_range, PTIME_RANGE_MAX,
3430 script.time_str,
3431 session->evlist->first_sample_time,
3432 session->evlist->last_sample_time);
3433
3434 if (script.range_num < 0) {
3435 pr_err("Invalid time string\n");
3436 err = -EINVAL;
3437 goto out_delete;
3438 }
3439 } else {
3440 script.range_num = 1;
David Aherna91f4c42016-11-29 10:15:43 -07003441 }
3442
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003443 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003444
Adrian Hunterd445dd22014-08-15 22:08:37 +03003445 flush_scripting();
3446
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003447out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01003448 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003449 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003450
3451 if (script_started)
3452 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003453out:
3454 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003455}