blob: 3499d68e1d702d3f993a1838471a2e63fa189a72 [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
Jiri Olsa809e9422015-11-26 18:55:21 +01001483struct perf_script {
1484 struct perf_tool tool;
1485 struct perf_session *session;
1486 bool show_task_events;
1487 bool show_mmap_events;
1488 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301489 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001490 bool show_lost_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001491 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001492 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001493 struct cpu_map *cpus;
1494 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001495 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001496 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001497 struct perf_time_interval *ptime_range;
1498 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001499 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
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002083static int
2084process_lost_event(struct perf_tool *tool,
2085 union perf_event *event,
2086 struct perf_sample *sample,
2087 struct machine *machine)
2088{
2089 struct perf_script *script = container_of(tool, struct perf_script, tool);
2090 struct perf_session *session = script->session;
2091 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2092 struct thread *thread;
2093
2094 thread = machine__findnew_thread(machine, sample->pid,
2095 sample->tid);
2096 if (thread == NULL)
2097 return -1;
2098
2099 perf_sample__fprintf_start(sample, thread, evsel,
2100 PERF_RECORD_LOST, stdout);
2101 perf_event__fprintf(event, stdout);
2102 thread__put(thread);
2103 return 0;
2104}
2105
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002106static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002107{
2108 session_done = 1;
2109}
2110
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002111static void perf_script__fclose_per_event_dump(struct perf_script *script)
2112{
2113 struct perf_evlist *evlist = script->session->evlist;
2114 struct perf_evsel *evsel;
2115
2116 evlist__for_each_entry(evlist, evsel) {
2117 if (!evsel->priv)
2118 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002119 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002120 evsel->priv = NULL;
2121 }
2122}
2123
2124static int perf_script__fopen_per_event_dump(struct perf_script *script)
2125{
2126 struct perf_evsel *evsel;
2127
2128 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002129 /*
2130 * Already setup? I.e. we may be called twice in cases like
2131 * Intel PT, one for the intel_pt// and dummy events, then
2132 * for the evsels syntheized from the auxtrace info.
2133 *
2134 * Ses perf_script__process_auxtrace_info.
2135 */
2136 if (evsel->priv != NULL)
2137 continue;
2138
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002139 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002140 if (evsel->priv == NULL)
2141 goto out_err_fclose;
2142 }
2143
2144 return 0;
2145
2146out_err_fclose:
2147 perf_script__fclose_per_event_dump(script);
2148 return -1;
2149}
2150
2151static int perf_script__setup_per_event_dump(struct perf_script *script)
2152{
2153 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002154 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002155
2156 if (script->per_event_dump)
2157 return perf_script__fopen_per_event_dump(script);
2158
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002159 es_stdout.fp = stdout;
2160
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002161 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002162 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002163
2164 return 0;
2165}
2166
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002167static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2168{
2169 struct perf_evsel *evsel;
2170
2171 evlist__for_each_entry(script->session->evlist, evsel) {
2172 struct perf_evsel_script *es = evsel->priv;
2173
2174 perf_evsel_script__fprintf(es, stdout);
2175 perf_evsel_script__delete(es);
2176 evsel->priv = NULL;
2177 }
2178}
2179
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002180static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002181{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002182 int ret;
2183
Tom Zanussic239da32010-04-01 23:59:18 -05002184 signal(SIGINT, sig_handler);
2185
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002186 /* override event processing functions */
2187 if (script->show_task_events) {
2188 script->tool.comm = process_comm_event;
2189 script->tool.fork = process_fork_event;
2190 script->tool.exit = process_exit_event;
2191 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002192 if (script->show_mmap_events) {
2193 script->tool.mmap = process_mmap_event;
2194 script->tool.mmap2 = process_mmap2_event;
2195 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002196 if (script->show_switch_events)
2197 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302198 if (script->show_namespace_events)
2199 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002200 if (script->show_lost_events)
2201 script->tool.lost = process_lost_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002202
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002203 if (perf_script__setup_per_event_dump(script)) {
2204 pr_err("Couldn't create the per event dump files\n");
2205 return -1;
2206 }
2207
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002208 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002209
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002210 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002211 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002212
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002213 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002214 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002215
2216 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002217}
2218
Tom Zanussi956ffd02009-11-25 01:15:46 -06002219struct script_spec {
2220 struct list_head node;
2221 struct scripting_ops *ops;
2222 char spec[0];
2223};
2224
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002225static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002226
2227static struct script_spec *script_spec__new(const char *spec,
2228 struct scripting_ops *ops)
2229{
2230 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2231
2232 if (s != NULL) {
2233 strcpy(s->spec, spec);
2234 s->ops = ops;
2235 }
2236
2237 return s;
2238}
2239
Tom Zanussi956ffd02009-11-25 01:15:46 -06002240static void script_spec__add(struct script_spec *s)
2241{
2242 list_add_tail(&s->node, &script_specs);
2243}
2244
2245static struct script_spec *script_spec__find(const char *spec)
2246{
2247 struct script_spec *s;
2248
2249 list_for_each_entry(s, &script_specs, node)
2250 if (strcasecmp(s->spec, spec) == 0)
2251 return s;
2252 return NULL;
2253}
2254
Tom Zanussi956ffd02009-11-25 01:15:46 -06002255int script_spec_register(const char *spec, struct scripting_ops *ops)
2256{
2257 struct script_spec *s;
2258
2259 s = script_spec__find(spec);
2260 if (s)
2261 return -1;
2262
Taeung Song8560bae2016-02-26 00:13:10 +09002263 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002264 if (!s)
2265 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002266 else
2267 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002268
2269 return 0;
2270}
2271
2272static struct scripting_ops *script_spec__lookup(const char *spec)
2273{
2274 struct script_spec *s = script_spec__find(spec);
2275 if (!s)
2276 return NULL;
2277
2278 return s->ops;
2279}
2280
2281static void list_available_languages(void)
2282{
2283 struct script_spec *s;
2284
2285 fprintf(stderr, "\n");
2286 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002287 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002288
2289 list_for_each_entry(s, &script_specs, node)
2290 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2291
2292 fprintf(stderr, "\n");
2293}
2294
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002295static int parse_scriptname(const struct option *opt __maybe_unused,
2296 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002297{
2298 char spec[PATH_MAX];
2299 const char *script, *ext;
2300 int len;
2301
Tom Zanussif526d682010-01-27 02:27:52 -06002302 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002303 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002304 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002305 }
2306
2307 script = strchr(str, ':');
2308 if (script) {
2309 len = script - str;
2310 if (len >= PATH_MAX) {
2311 fprintf(stderr, "invalid language specifier");
2312 return -1;
2313 }
2314 strncpy(spec, str, len);
2315 spec[len] = '\0';
2316 scripting_ops = script_spec__lookup(spec);
2317 if (!scripting_ops) {
2318 fprintf(stderr, "invalid language specifier");
2319 return -1;
2320 }
2321 script++;
2322 } else {
2323 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002324 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002325 if (!ext) {
2326 fprintf(stderr, "invalid script extension");
2327 return -1;
2328 }
2329 scripting_ops = script_spec__lookup(++ext);
2330 if (!scripting_ops) {
2331 fprintf(stderr, "invalid script extension");
2332 return -1;
2333 }
2334 }
2335
2336 script_name = strdup(script);
2337
2338 return 0;
2339}
2340
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002341static int parse_output_fields(const struct option *opt __maybe_unused,
2342 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002343{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002344 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002345 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002346 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002347 int rc = 0;
2348 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002349 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002350 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002351
2352 if (!str)
2353 return -ENOMEM;
2354
David Ahern2c9e45f72011-03-17 10:03:21 -06002355 /* first word can state for which event type the user is specifying
2356 * the fields. If no type exists, the specified fields apply to all
2357 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002358 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002359 tok = strchr(str, ':');
2360 if (tok) {
2361 *tok = '\0';
2362 tok++;
2363 if (!strcmp(str, "hw"))
2364 type = PERF_TYPE_HARDWARE;
2365 else if (!strcmp(str, "sw"))
2366 type = PERF_TYPE_SOFTWARE;
2367 else if (!strcmp(str, "trace"))
2368 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002369 else if (!strcmp(str, "raw"))
2370 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002371 else if (!strcmp(str, "break"))
2372 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002373 else if (!strcmp(str, "synth"))
2374 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002375 else {
2376 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002377 rc = -EINVAL;
2378 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002379 }
2380
2381 if (output[type].user_set)
2382 pr_warning("Overriding previous field request for %s events.\n",
2383 event_type(type));
2384
2385 output[type].fields = 0;
2386 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002387 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002388
2389 } else {
2390 tok = str;
2391 if (strlen(str) == 0) {
2392 fprintf(stderr,
2393 "Cannot set fields to 'none' for all event types.\n");
2394 rc = -EINVAL;
2395 goto out;
2396 }
2397
Andi Kleen36ce5652017-06-02 08:48:10 -07002398 /* Don't override defaults for +- */
2399 if (strchr(str, '+') || strchr(str, '-'))
2400 goto parse;
2401
David Ahern2c9e45f72011-03-17 10:03:21 -06002402 if (output_set_by_user())
2403 pr_warning("Overriding previous field request for all events.\n");
2404
Adrian Hunter14057202017-06-21 13:17:19 +03002405 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002406 output[j].fields = 0;
2407 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002408 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002409 }
David Ahern1424dc92011-03-09 22:23:28 -07002410 }
2411
Andi Kleen36ce5652017-06-02 08:48:10 -07002412parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002413 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002414 if (*tok == '+') {
2415 if (change == SET)
2416 goto out_badmix;
2417 change = ADD;
2418 tok++;
2419 } else if (*tok == '-') {
2420 if (change == SET)
2421 goto out_badmix;
2422 change = REMOVE;
2423 tok++;
2424 } else {
2425 if (change != SET && change != DEFAULT)
2426 goto out_badmix;
2427 change = SET;
2428 }
2429
David Ahern745f43e2011-03-09 22:23:26 -07002430 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002431 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002432 break;
David Ahern745f43e2011-03-09 22:23:26 -07002433 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002434 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002435 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002436 continue;
2437 }
David Ahern745f43e2011-03-09 22:23:26 -07002438 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002439 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002440 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002441 goto out;
2442 }
2443
2444 if (type == -1) {
2445 /* add user option to all events types for
2446 * which it is valid
2447 */
Adrian Hunter14057202017-06-21 13:17:19 +03002448 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002449 if (output[j].invalid_fields & all_output_options[i].field) {
2450 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2451 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002452 } else {
2453 if (change == REMOVE)
2454 output[j].fields &= ~all_output_options[i].field;
2455 else
2456 output[j].fields |= all_output_options[i].field;
2457 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002458 }
2459 } else {
2460 if (output[type].invalid_fields & all_output_options[i].field) {
2461 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2462 all_output_options[i].str, event_type(type));
2463
2464 rc = -EINVAL;
2465 goto out;
2466 }
2467 output[type].fields |= all_output_options[i].field;
2468 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002469 }
2470
2471 if (type >= 0) {
2472 if (output[type].fields == 0) {
2473 pr_debug("No fields requested for %s type. "
2474 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002475 }
David Ahern1424dc92011-03-09 22:23:28 -07002476 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002477 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002478
Andi Kleen36ce5652017-06-02 08:48:10 -07002479out_badmix:
2480 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2481 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002482out:
David Ahern745f43e2011-03-09 22:23:26 -07002483 free(str);
2484 return rc;
2485}
2486
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002487#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2488 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2489 if ((lang_dirent->d_type == DT_DIR || \
2490 (lang_dirent->d_type == DT_UNKNOWN && \
2491 is_directory(scripts_path, lang_dirent))) && \
2492 (strcmp(lang_dirent->d_name, ".")) && \
2493 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002494
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002495#define for_each_script(lang_path, lang_dir, script_dirent) \
2496 while ((script_dirent = readdir(lang_dir)) != NULL) \
2497 if (script_dirent->d_type != DT_DIR && \
2498 (script_dirent->d_type != DT_UNKNOWN || \
2499 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002500
2501
2502#define RECORD_SUFFIX "-record"
2503#define REPORT_SUFFIX "-report"
2504
2505struct script_desc {
2506 struct list_head node;
2507 char *name;
2508 char *half_liner;
2509 char *args;
2510};
2511
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002512static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002513
2514static struct script_desc *script_desc__new(const char *name)
2515{
2516 struct script_desc *s = zalloc(sizeof(*s));
2517
Tom Zanussib5b87312010-11-10 08:16:51 -06002518 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002519 s->name = strdup(name);
2520
2521 return s;
2522}
2523
2524static void script_desc__delete(struct script_desc *s)
2525{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002526 zfree(&s->name);
2527 zfree(&s->half_liner);
2528 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002529 free(s);
2530}
2531
2532static void script_desc__add(struct script_desc *s)
2533{
2534 list_add_tail(&s->node, &script_descs);
2535}
2536
2537static struct script_desc *script_desc__find(const char *name)
2538{
2539 struct script_desc *s;
2540
2541 list_for_each_entry(s, &script_descs, node)
2542 if (strcasecmp(s->name, name) == 0)
2543 return s;
2544 return NULL;
2545}
2546
2547static struct script_desc *script_desc__findnew(const char *name)
2548{
2549 struct script_desc *s = script_desc__find(name);
2550
2551 if (s)
2552 return s;
2553
2554 s = script_desc__new(name);
2555 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002556 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002557
2558 script_desc__add(s);
2559
2560 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002561}
2562
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002563static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002564{
2565 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002566 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002567
2568 if (strlen(str) > suffix_len) {
2569 p = str + strlen(str) - suffix_len;
2570 if (!strncmp(p, suffix, suffix_len))
2571 return p;
2572 }
2573
2574 return NULL;
2575}
2576
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002577static int read_script_info(struct script_desc *desc, const char *filename)
2578{
2579 char line[BUFSIZ], *p;
2580 FILE *fp;
2581
2582 fp = fopen(filename, "r");
2583 if (!fp)
2584 return -1;
2585
2586 while (fgets(line, sizeof(line), fp)) {
2587 p = ltrim(line);
2588 if (strlen(p) == 0)
2589 continue;
2590 if (*p != '#')
2591 continue;
2592 p++;
2593 if (strlen(p) && *p == '!')
2594 continue;
2595
2596 p = ltrim(p);
2597 if (strlen(p) && p[strlen(p) - 1] == '\n')
2598 p[strlen(p) - 1] = '\0';
2599
2600 if (!strncmp(p, "description:", strlen("description:"))) {
2601 p += strlen("description:");
2602 desc->half_liner = strdup(ltrim(p));
2603 continue;
2604 }
2605
2606 if (!strncmp(p, "args:", strlen("args:"))) {
2607 p += strlen("args:");
2608 desc->args = strdup(ltrim(p));
2609 continue;
2610 }
2611 }
2612
2613 fclose(fp);
2614
2615 return 0;
2616}
2617
Robert Richter38efb532011-11-25 11:38:40 +01002618static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2619{
2620 char *script_root, *str;
2621
2622 script_root = strdup(script_dirent->d_name);
2623 if (!script_root)
2624 return NULL;
2625
2626 str = (char *)ends_with(script_root, suffix);
2627 if (!str) {
2628 free(script_root);
2629 return NULL;
2630 }
2631
2632 *str = '\0';
2633 return script_root;
2634}
2635
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002636static int list_available_scripts(const struct option *opt __maybe_unused,
2637 const char *s __maybe_unused,
2638 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002639{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002640 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002641 char scripts_path[MAXPATHLEN];
2642 DIR *scripts_dir, *lang_dir;
2643 char script_path[MAXPATHLEN];
2644 char lang_path[MAXPATHLEN];
2645 struct script_desc *desc;
2646 char first_half[BUFSIZ];
2647 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002648
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002649 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002650
2651 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002652 if (!scripts_dir) {
2653 fprintf(stdout,
2654 "open(%s) failed.\n"
2655 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2656 scripts_path);
2657 exit(-1);
2658 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002659
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002660 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002661 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002662 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002663 lang_dir = opendir(lang_path);
2664 if (!lang_dir)
2665 continue;
2666
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002667 for_each_script(lang_path, lang_dir, script_dirent) {
2668 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002669 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002670 desc = script_desc__findnew(script_root);
2671 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002672 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002673 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002674 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002675 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002676 }
2677 }
2678
2679 fprintf(stdout, "List of available trace scripts:\n");
2680 list_for_each_entry(desc, &script_descs, node) {
2681 sprintf(first_half, "%s %s", desc->name,
2682 desc->args ? desc->args : "");
2683 fprintf(stdout, " %-36s %s\n", first_half,
2684 desc->half_liner ? desc->half_liner : "");
2685 }
2686
2687 exit(0);
2688}
2689
Feng Tange5f37052012-09-07 16:42:26 +08002690/*
Feng Tang49e639e2012-10-30 11:56:03 +08002691 * Some scripts specify the required events in their "xxx-record" file,
2692 * this function will check if the events in perf.data match those
2693 * mentioned in the "xxx-record".
2694 *
2695 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2696 * which is covered well now. And new parsing code should be added to
2697 * cover the future complexing formats like event groups etc.
2698 */
2699static int check_ev_match(char *dir_name, char *scriptname,
2700 struct perf_session *session)
2701{
2702 char filename[MAXPATHLEN], evname[128];
2703 char line[BUFSIZ], *p;
2704 struct perf_evsel *pos;
2705 int match, len;
2706 FILE *fp;
2707
2708 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2709
2710 fp = fopen(filename, "r");
2711 if (!fp)
2712 return -1;
2713
2714 while (fgets(line, sizeof(line), fp)) {
2715 p = ltrim(line);
2716 if (*p == '#')
2717 continue;
2718
2719 while (strlen(p)) {
2720 p = strstr(p, "-e");
2721 if (!p)
2722 break;
2723
2724 p += 2;
2725 p = ltrim(p);
2726 len = strcspn(p, " \t");
2727 if (!len)
2728 break;
2729
2730 snprintf(evname, len + 1, "%s", p);
2731
2732 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002733 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002734 if (!strcmp(perf_evsel__name(pos), evname)) {
2735 match = 1;
2736 break;
2737 }
2738 }
2739
2740 if (!match) {
2741 fclose(fp);
2742 return -1;
2743 }
2744 }
2745 }
2746
2747 fclose(fp);
2748 return 0;
2749}
2750
2751/*
Feng Tange5f37052012-09-07 16:42:26 +08002752 * Return -1 if none is found, otherwise the actual scripts number.
2753 *
2754 * Currently the only user of this function is the script browser, which
2755 * will list all statically runnable scripts, select one, execute it and
2756 * show the output in a perf browser.
2757 */
2758int find_scripts(char **scripts_array, char **scripts_path_array)
2759{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002760 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002761 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002762 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002763 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002764 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002765 .file = {
2766 .path = input_name,
2767 },
2768 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002769 };
Feng Tange5f37052012-09-07 16:42:26 +08002770 char *temp;
2771 int i = 0;
2772
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002773 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002774 if (!session)
2775 return -1;
2776
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002777 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002778
2779 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002780 if (!scripts_dir) {
2781 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002782 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002783 }
Feng Tange5f37052012-09-07 16:42:26 +08002784
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002785 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002786 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002787 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002788#ifdef NO_LIBPERL
2789 if (strstr(lang_path, "perl"))
2790 continue;
2791#endif
2792#ifdef NO_LIBPYTHON
2793 if (strstr(lang_path, "python"))
2794 continue;
2795#endif
2796
2797 lang_dir = opendir(lang_path);
2798 if (!lang_dir)
2799 continue;
2800
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002801 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002802 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002803 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002804 continue;
2805 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002806 script_dirent->d_name);
2807 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002808 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002809 (temp - script_dirent->d_name) + 1,
2810 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002811
2812 if (check_ev_match(lang_path,
2813 scripts_array[i], session))
2814 continue;
2815
Feng Tange5f37052012-09-07 16:42:26 +08002816 i++;
2817 }
Feng Tang49e639e2012-10-30 11:56:03 +08002818 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002819 }
2820
Feng Tang49e639e2012-10-30 11:56:03 +08002821 closedir(scripts_dir);
2822 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002823 return i;
2824}
2825
Tom Zanussi38752942009-12-15 02:53:39 -06002826static char *get_script_path(const char *script_root, const char *suffix)
2827{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002828 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002829 char scripts_path[MAXPATHLEN];
2830 char script_path[MAXPATHLEN];
2831 DIR *scripts_dir, *lang_dir;
2832 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002833 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002834
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002835 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002836
2837 scripts_dir = opendir(scripts_path);
2838 if (!scripts_dir)
2839 return NULL;
2840
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002841 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002842 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002843 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002844 lang_dir = opendir(lang_path);
2845 if (!lang_dir)
2846 continue;
2847
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002848 for_each_script(lang_path, lang_dir, script_dirent) {
2849 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002850 if (__script_root && !strcmp(script_root, __script_root)) {
2851 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002852 closedir(lang_dir);
2853 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002854 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002855 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002856 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002857 }
2858 free(__script_root);
2859 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002860 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002861 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002862 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002863
Robert Richter38efb532011-11-25 11:38:40 +01002864 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002865}
2866
Tom Zanussib5b87312010-11-10 08:16:51 -06002867static bool is_top_script(const char *script_path)
2868{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002869 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002870}
2871
2872static int has_required_arg(char *script_path)
2873{
2874 struct script_desc *desc;
2875 int n_args = 0;
2876 char *p;
2877
2878 desc = script_desc__new(NULL);
2879
2880 if (read_script_info(desc, script_path))
2881 goto out;
2882
2883 if (!desc->args)
2884 goto out;
2885
2886 for (p = desc->args; *p; p++)
2887 if (*p == '<')
2888 n_args++;
2889out:
2890 script_desc__delete(desc);
2891
2892 return n_args;
2893}
2894
David Ahernd54b1a92012-08-26 12:24:46 -06002895static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002896{
2897 char **__argv = malloc(sizeof(const char *) * argc);
2898
David Ahernd54b1a92012-08-26 12:24:46 -06002899 if (!__argv) {
2900 pr_err("malloc failed\n");
2901 return -1;
2902 }
2903
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002904 memcpy(__argv, argv, sizeof(const char *) * argc);
2905 argc = parse_options(argc, (const char **)__argv, record_options,
2906 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2907 free(__argv);
2908
David Ahernd54b1a92012-08-26 12:24:46 -06002909 system_wide = (argc == 0);
2910
2911 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002912}
2913
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002914static void script__setup_sample_type(struct perf_script *script)
2915{
2916 struct perf_session *session = script->session;
2917 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2918
2919 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2920 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03002921 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002922 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03002923 dwarf_callchain_users = true;
2924 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002925 callchain_param.record_mode = CALLCHAIN_LBR;
2926 else
2927 callchain_param.record_mode = CALLCHAIN_FP;
2928 }
2929}
2930
Jiri Olsae099eba2016-01-05 22:09:09 +01002931static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2932 union perf_event *event,
2933 struct perf_session *session)
2934{
2935 struct stat_round_event *round = &event->stat_round;
2936 struct perf_evsel *counter;
2937
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002938 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002939 perf_stat_process_counter(&stat_config, counter);
2940 process_stat(counter, round->time);
2941 }
2942
2943 process_stat_interval(round->time);
2944 return 0;
2945}
2946
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002947static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2948 union perf_event *event,
2949 struct perf_session *session __maybe_unused)
2950{
2951 perf_event__read_stat_config(&stat_config, &event->stat_config);
2952 return 0;
2953}
2954
Jiri Olsacfc88742016-01-05 22:09:06 +01002955static int set_maps(struct perf_script *script)
2956{
2957 struct perf_evlist *evlist = script->session->evlist;
2958
2959 if (!script->cpus || !script->threads)
2960 return 0;
2961
2962 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2963 return -EINVAL;
2964
2965 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2966
2967 if (perf_evlist__alloc_stats(evlist, true))
2968 return -ENOMEM;
2969
2970 script->allocated = true;
2971 return 0;
2972}
2973
2974static
2975int process_thread_map_event(struct perf_tool *tool,
2976 union perf_event *event,
2977 struct perf_session *session __maybe_unused)
2978{
2979 struct perf_script *script = container_of(tool, struct perf_script, tool);
2980
2981 if (script->threads) {
2982 pr_warning("Extra thread map event, ignoring.\n");
2983 return 0;
2984 }
2985
2986 script->threads = thread_map__new_event(&event->thread_map);
2987 if (!script->threads)
2988 return -ENOMEM;
2989
2990 return set_maps(script);
2991}
2992
2993static
2994int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2995 union perf_event *event,
2996 struct perf_session *session __maybe_unused)
2997{
2998 struct perf_script *script = container_of(tool, struct perf_script, tool);
2999
3000 if (script->cpus) {
3001 pr_warning("Extra cpu map event, ignoring.\n");
3002 return 0;
3003 }
3004
3005 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3006 if (!script->cpus)
3007 return -ENOMEM;
3008
3009 return set_maps(script);
3010}
3011
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003012#ifdef HAVE_AUXTRACE_SUPPORT
3013static int perf_script__process_auxtrace_info(struct perf_tool *tool,
3014 union perf_event *event,
3015 struct perf_session *session)
3016{
3017 int ret = perf_event__process_auxtrace_info(tool, event, session);
3018
3019 if (ret == 0) {
3020 struct perf_script *script = container_of(tool, struct perf_script, tool);
3021
3022 ret = perf_script__setup_per_event_dump(script);
3023 }
3024
3025 return ret;
3026}
3027#else
3028#define perf_script__process_auxtrace_info 0
3029#endif
3030
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003031int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003032{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003033 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003034 bool header = false;
3035 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003036 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003037 char *rec_script_path = NULL;
3038 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003039 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003040 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06003041 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003042 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003043 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003044 struct perf_script script = {
3045 .tool = {
3046 .sample = process_sample_event,
3047 .mmap = perf_event__process_mmap,
3048 .mmap2 = perf_event__process_mmap2,
3049 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303050 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003051 .exit = perf_event__process_exit,
3052 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003053 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003054 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003055 .tracing_data = perf_event__process_tracing_data,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003056 .feature = perf_event__process_feature,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003057 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003058 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003059 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003060 .auxtrace = perf_event__process_auxtrace,
3061 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003062 .stat = perf_event__process_stat_event,
3063 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003064 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003065 .thread_map = process_thread_map_event,
3066 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003067 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003068 .ordering_requires_timestamps = true,
3069 },
3070 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003071 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003072 .mode = PERF_DATA_MODE_READ,
3073 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003074 const struct option options[] = {
3075 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3076 "dump raw trace in ASCII"),
3077 OPT_INCR('v', "verbose", &verbose,
3078 "be more verbose (show symbol address, etc)"),
3079 OPT_BOOLEAN('L', "Latency", &latency_format,
3080 "show latency attributes (irqs/preemption disabled, etc)"),
3081 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3082 list_available_scripts),
3083 OPT_CALLBACK('s', "script", NULL, "name",
3084 "script file name (lang:script name, script name, or *)",
3085 parse_scriptname),
3086 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3087 "generate perf-script.xx script in specified language"),
3088 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3089 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3090 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003091 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3092 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003093 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3094 "file", "vmlinux pathname"),
3095 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3096 "file", "kallsyms pathname"),
3097 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3098 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003099 OPT_CALLBACK(0, "symfs", NULL, "directory",
3100 "Look for files with symbols relative to this directory",
3101 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003102 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003103 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003104 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003105 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003106 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Andi Kleenb1491ac2017-09-05 11:40:57 -07003107 "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
Kan Liang49d58f02017-08-29 13:11:11 -04003108 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
Andi Kleen48d02a12017-02-23 15:46:34 -08003109 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003110 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3111 "system-wide collection from all CPUs"),
3112 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3113 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07003114 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3115 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003116 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3117 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3118 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003119 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3120 "only consider symbols in these pids"),
3121 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3122 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003123 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3124 "Set the maximum stack depth when parsing the callchain, "
3125 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003126 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003127 OPT_BOOLEAN('I', "show-info", &show_full_info,
3128 "display extended information from perf.data file"),
3129 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3130 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003131 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3132 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003133 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3134 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003135 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3136 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303137 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3138 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003139 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3140 "Show lost events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003141 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3142 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003143 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003144 OPT_INTEGER(0, "max-blocks", &max_blocks,
3145 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03003146 OPT_BOOLEAN(0, "ns", &nanosecs,
3147 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003148 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
3149 "Instruction Tracing options",
3150 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003151 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3152 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003153 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3154 "Enable symbol demangling"),
3155 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3156 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003157 OPT_STRING(0, "time", &script.time_str, "str",
3158 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003159 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3160 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003161 OPT_END()
3162 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003163 const char * const script_subcommands[] = { "record", "report", NULL };
3164 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003165 "perf script [<options>]",
3166 "perf script [<options>] record <script> [<record-options>] <command>",
3167 "perf script [<options>] report <script> [script-args]",
3168 "perf script [<options>] <script> [<record-options>] <command>",
3169 "perf script [<options>] <top-script> [script-args]",
3170 NULL
3171 };
Tom Zanussi38752942009-12-15 02:53:39 -06003172
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003173 perf_set_singlethreaded();
3174
Tom Zanussib5b87312010-11-10 08:16:51 -06003175 setup_scripting();
3176
Yunlong Song40cae2b2015-03-18 21:35:54 +08003177 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003178 PARSE_OPT_STOP_AT_NON_OPTION);
3179
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003180 data.file.path = input_name;
3181 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003182
Tom Zanussib5b87312010-11-10 08:16:51 -06003183 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3184 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3185 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003186 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003187 }
3188
Tom Zanussib5b87312010-11-10 08:16:51 -06003189 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3190 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3191 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003192 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003193 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003194 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003195 return -1;
3196 }
Tom Zanussi38752942009-12-15 02:53:39 -06003197 }
3198
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003199 if (itrace_synth_opts.callchain &&
3200 itrace_synth_opts.callchain_sz > scripting_max_stack)
3201 scripting_max_stack = itrace_synth_opts.callchain_sz;
3202
Ben Hutchings44e668c2010-10-10 16:10:03 +01003203 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003204 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003205
Tom Zanussib5b87312010-11-10 08:16:51 -06003206 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003207 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003208 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003209 pid_t pid;
3210
Tom Zanussib5b87312010-11-10 08:16:51 -06003211 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3212 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3213
3214 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003215 usage_with_options_msg(script_usage, options,
3216 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003217 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003218 }
3219
Tom Zanussib5b87312010-11-10 08:16:51 -06003220 if (is_top_script(argv[0])) {
3221 rep_args = argc - 1;
3222 } else {
3223 int rec_args;
3224
3225 rep_args = has_required_arg(rep_script_path);
3226 rec_args = (argc - 1) - rep_args;
3227 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003228 usage_with_options_msg(script_usage, options,
3229 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003230 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003231 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003232 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003233 }
3234
3235 if (pipe(live_pipe) < 0) {
3236 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003237 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003238 }
3239
3240 pid = fork();
3241 if (pid < 0) {
3242 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003243 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003244 }
3245
3246 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003247 j = 0;
3248
Tom Zanussia0cccc22010-04-01 23:59:25 -05003249 dup2(live_pipe[1], 1);
3250 close(live_pipe[0]);
3251
Robert Richter317df652011-11-25 15:05:25 +01003252 if (is_top_script(argv[0])) {
3253 system_wide = true;
3254 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003255 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3256 err = -1;
3257 goto out;
3258 }
Robert Richter317df652011-11-25 15:05:25 +01003259 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003260
3261 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003262 if (!__argv) {
3263 pr_err("malloc failed\n");
3264 err = -ENOMEM;
3265 goto out;
3266 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003267
Tom Zanussib5b87312010-11-10 08:16:51 -06003268 __argv[j++] = "/bin/sh";
3269 __argv[j++] = rec_script_path;
3270 if (system_wide)
3271 __argv[j++] = "-a";
3272 __argv[j++] = "-q";
3273 __argv[j++] = "-o";
3274 __argv[j++] = "-";
3275 for (i = rep_args + 1; i < argc; i++)
3276 __argv[j++] = argv[i];
3277 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003278
3279 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003280 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003281 exit(-1);
3282 }
3283
3284 dup2(live_pipe[0], 0);
3285 close(live_pipe[1]);
3286
Tom Zanussib5b87312010-11-10 08:16:51 -06003287 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003288 if (!__argv) {
3289 pr_err("malloc failed\n");
3290 err = -ENOMEM;
3291 goto out;
3292 }
3293
Tom Zanussib5b87312010-11-10 08:16:51 -06003294 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003295 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003296 __argv[j++] = rep_script_path;
3297 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003298 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003299 __argv[j++] = "-i";
3300 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003301 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003302
3303 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003304 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003305 exit(-1);
3306 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003307
Tom Zanussib5b87312010-11-10 08:16:51 -06003308 if (rec_script_path)
3309 script_path = rec_script_path;
3310 if (rep_script_path)
3311 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003312
Tom Zanussib5b87312010-11-10 08:16:51 -06003313 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003314 j = 0;
3315
Robert Richter317df652011-11-25 15:05:25 +01003316 if (!rec_script_path)
3317 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003318 else if (!system_wide) {
3319 if (have_cmd(argc - 1, &argv[1]) != 0) {
3320 err = -1;
3321 goto out;
3322 }
3323 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003324
3325 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003326 if (!__argv) {
3327 pr_err("malloc failed\n");
3328 err = -ENOMEM;
3329 goto out;
3330 }
3331
Tom Zanussib5b87312010-11-10 08:16:51 -06003332 __argv[j++] = "/bin/sh";
3333 __argv[j++] = script_path;
3334 if (system_wide)
3335 __argv[j++] = "-a";
3336 for (i = 2; i < argc; i++)
3337 __argv[j++] = argv[i];
3338 __argv[j++] = NULL;
3339
3340 execvp("/bin/sh", (char **)__argv);
3341 free(__argv);
3342 exit(-1);
3343 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003344
Tom Zanussicf4fee52010-03-03 01:04:33 -06003345 if (!script_name)
3346 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003347
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003348 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003349 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003350 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003351
Jiri Olsae90debd2013-12-09 11:02:50 +01003352 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003353 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003354 perf_session__fprintf_info(session, stdout, show_full_info);
3355 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003356 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003357 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003358 if (show_full_info)
3359 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003360
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003361 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003362 goto out_delete;
3363
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003364 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003365 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003366
Adrian Huntere2167082016-06-23 16:40:58 +03003367 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3368 itrace_synth_opts.thread_stack = true;
3369
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003370 session->itrace_synth_opts = &itrace_synth_opts;
3371
Anton Blanchard5d67be92011-07-04 21:57:50 +10003372 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003373 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3374 if (err < 0)
3375 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003376 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003377 }
3378
David Ahern1424dc92011-03-09 22:23:28 -07003379 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003380 symbol_conf.use_callchain = true;
3381 else
3382 symbol_conf.use_callchain = false;
3383
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003384 if (session->tevent.pevent &&
3385 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003386 machine__resolve_kernel_addr,
3387 &session->machines.host) < 0) {
3388 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003389 err = -1;
3390 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003391 }
3392
Tom Zanussi956ffd02009-11-25 01:15:46 -06003393 if (generate_script_lang) {
3394 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003395 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003396
David Ahern2c9e45f72011-03-17 10:03:21 -06003397 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003398 fprintf(stderr,
3399 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003400 err = -EINVAL;
3401 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003402 }
3403
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003404 input = open(data.file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003405 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003406 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003407 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003408 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003409 }
3410
3411 err = fstat(input, &perf_stat);
3412 if (err < 0) {
3413 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003414 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003415 }
3416
3417 if (!perf_stat.st_size) {
3418 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003419 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003420 }
3421
3422 scripting_ops = script_spec__lookup(generate_script_lang);
3423 if (!scripting_ops) {
3424 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003425 err = -ENOENT;
3426 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003427 }
3428
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003429 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003430 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003431 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003432 }
3433
3434 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003435 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003436 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003437 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003438 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003439 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003440 }
3441
David Ahern9cbdb702011-04-06 21:54:20 -06003442
3443 err = perf_session__check_output_opt(session);
3444 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003445 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003446
Jin Yaocc2ef582018-01-10 23:00:33 +08003447 script.ptime_range = perf_time__range_alloc(script.time_str,
3448 &script.range_size);
3449 if (!script.ptime_range) {
3450 err = -ENOMEM;
3451 goto out_delete;
3452 }
3453
David Aherna91f4c42016-11-29 10:15:43 -07003454 /* needs to be parsed after looking up reference time */
Jin Yao2ab046c2017-12-08 21:13:46 +08003455 if (perf_time__parse_str(script.ptime_range, script.time_str) != 0) {
3456 if (session->evlist->first_sample_time == 0 &&
3457 session->evlist->last_sample_time == 0) {
Jin Yao1e2778e2018-01-10 23:00:27 +08003458 pr_err("HINT: no first/last sample time found in perf data.\n"
3459 "Please use latest perf binary to execute 'perf record'\n"
3460 "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
Jin Yao2ab046c2017-12-08 21:13:46 +08003461 err = -EINVAL;
3462 goto out_delete;
3463 }
3464
3465 script.range_num = perf_time__percent_parse_str(
Jin Yaocc2ef582018-01-10 23:00:33 +08003466 script.ptime_range, script.range_size,
Jin Yao2ab046c2017-12-08 21:13:46 +08003467 script.time_str,
3468 session->evlist->first_sample_time,
3469 session->evlist->last_sample_time);
3470
3471 if (script.range_num < 0) {
3472 pr_err("Invalid time string\n");
3473 err = -EINVAL;
3474 goto out_delete;
3475 }
3476 } else {
3477 script.range_num = 1;
David Aherna91f4c42016-11-29 10:15:43 -07003478 }
3479
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003480 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003481
Adrian Hunterd445dd22014-08-15 22:08:37 +03003482 flush_scripting();
3483
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003484out_delete:
Jin Yaocc2ef582018-01-10 23:00:33 +08003485 zfree(&script.ptime_range);
3486
Jiri Olsacfc88742016-01-05 22:09:06 +01003487 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003488 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003489
3490 if (script_started)
3491 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003492out:
3493 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003494}