blob: 8d5fe092525c66fb0cf303a1990e2c52083e940d [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"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010013#include "util/map.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020014#include "util/symbol.h"
15#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010016#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020017#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070018#include "util/evlist.h"
19#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080020#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020021#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030022#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010023#include "util/cpumap.h"
24#include "util/thread_map.h"
25#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080026#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030027#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030028#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070029#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010030#include "util/path.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030031#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100032#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030033#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030034#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030035#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010036#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010037#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080038#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030039#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030040#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030041#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030042#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030043#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030044#include <sys/types.h>
45#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030046#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030047#include <unistd.h>
Andi Kleenb585ebd2018-09-20 11:05:36 -070048#include <subcmd/pager.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020049
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030050#include "sane_ctype.h"
51
Tom Zanussi956ffd02009-11-25 01:15:46 -060052static char const *script_name;
53static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020054static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020055static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020056static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070057static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090058static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010059static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030060static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030061static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100062static const char *cpu_list;
63static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010064static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080065static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060066
Adrian Hunter44cbe722015-09-25 16:15:50 +030067unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030068
David Ahern745f43e2011-03-09 22:23:26 -070069enum perf_output_field {
70 PERF_OUTPUT_COMM = 1U << 0,
71 PERF_OUTPUT_TID = 1U << 1,
72 PERF_OUTPUT_PID = 1U << 2,
73 PERF_OUTPUT_TIME = 1U << 3,
74 PERF_OUTPUT_CPU = 1U << 4,
75 PERF_OUTPUT_EVNAME = 1U << 5,
76 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060077 PERF_OUTPUT_IP = 1U << 7,
78 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060079 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060080 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090081 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020082 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020083 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020084 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020085 PERF_OUTPUT_BRSTACK = 1U << 15,
86 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010087 PERF_OUTPUT_DATA_SRC = 1U << 17,
88 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000089 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030090 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030091 PERF_OUTPUT_INSN = 1U << 21,
92 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080093 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070094 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030095 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -040096 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -070097 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -080098 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +010099 PERF_OUTPUT_MISC = 1U << 29,
Andi Kleendd2e18e2018-12-03 16:18:48 -0800100 PERF_OUTPUT_SRCCODE = 1U << 30,
David Ahern745f43e2011-03-09 22:23:26 -0700101};
102
103struct output_option {
104 const char *str;
105 enum perf_output_field field;
106} all_output_options[] = {
107 {.str = "comm", .field = PERF_OUTPUT_COMM},
108 {.str = "tid", .field = PERF_OUTPUT_TID},
109 {.str = "pid", .field = PERF_OUTPUT_PID},
110 {.str = "time", .field = PERF_OUTPUT_TIME},
111 {.str = "cpu", .field = PERF_OUTPUT_CPU},
112 {.str = "event", .field = PERF_OUTPUT_EVNAME},
113 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600114 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700115 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600116 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600117 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900118 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200119 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200120 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200121 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700122 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200123 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
124 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100125 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
126 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000127 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300128 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300129 {.str = "insn", .field = PERF_OUTPUT_INSN},
130 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800131 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700132 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300133 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400134 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800135 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100136 {.str = "misc", .field = PERF_OUTPUT_MISC},
Andi Kleendd2e18e2018-12-03 16:18:48 -0800137 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
David Ahern745f43e2011-03-09 22:23:26 -0700138};
139
Adrian Hunter14057202017-06-21 13:17:19 +0300140enum {
141 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
142 OUTPUT_TYPE_MAX
143};
144
David Ahern745f43e2011-03-09 22:23:26 -0700145/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600146static struct {
147 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600148 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400149 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600150 u64 fields;
151 u64 invalid_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300152} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700153
David Ahern2c9e45f72011-03-17 10:03:21 -0600154 [PERF_TYPE_HARDWARE] = {
155 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700156
David Ahern2c9e45f72011-03-17 10:03:21 -0600157 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
158 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600159 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530160 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
161 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600162
Wang Nan30372f02016-02-24 11:20:45 +0000163 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600164 },
165
166 [PERF_TYPE_SOFTWARE] = {
167 .user_set = false,
168
169 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
170 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600171 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530172 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
173 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
174 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600175
176 .invalid_fields = PERF_OUTPUT_TRACE,
177 },
178
179 [PERF_TYPE_TRACEPOINT] = {
180 .user_set = false,
181
182 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
183 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000184 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600185 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700186
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530187 [PERF_TYPE_HW_CACHE] = {
188 .user_set = false,
189
190 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
191 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
192 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
193 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
194 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
195
196 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
197 },
198
Arun Sharma0817a6a2011-04-14 10:38:18 -0700199 [PERF_TYPE_RAW] = {
200 .user_set = false,
201
202 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
203 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600204 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530205 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
206 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
207 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
208 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700209
Wang Nan30372f02016-02-24 11:20:45 +0000210 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700211 },
Wang Nan27cfef02015-12-08 02:25:43 +0000212
213 [PERF_TYPE_BREAKPOINT] = {
214 .user_set = false,
215
216 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
217 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
218 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530219 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
220 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000221
Wang Nan30372f02016-02-24 11:20:45 +0000222 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000223 },
Adrian Hunter14057202017-06-21 13:17:19 +0300224
225 [OUTPUT_TYPE_SYNTH] = {
226 .user_set = false,
227
228 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
229 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
230 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530231 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
232 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300233
234 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
235 },
David Ahern1424dc92011-03-09 22:23:28 -0700236};
David Ahern745f43e2011-03-09 22:23:26 -0700237
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300238struct perf_evsel_script {
239 char *filename;
240 FILE *fp;
241 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800242 /* For metric output */
243 u64 val;
244 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300245};
246
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800247static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
248{
249 return (struct perf_evsel_script *)evsel->priv;
250}
251
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300252static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100253 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300254{
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800255 struct perf_evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300256
257 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100258 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300259 goto out_free;
260 es->fp = fopen(es->filename, "w");
261 if (es->fp == NULL)
262 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300263 }
264
265 return es;
266out_free_filename:
267 zfree(&es->filename);
268out_free:
269 free(es);
270 return NULL;
271}
272
273static void perf_evsel_script__delete(struct perf_evsel_script *es)
274{
275 zfree(&es->filename);
276 fclose(es->fp);
277 es->fp = NULL;
278 free(es);
279}
280
281static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
282{
283 struct stat st;
284
285 fstat(fileno(es->fp), &st);
286 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
287 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
288}
289
Adrian Hunter14057202017-06-21 13:17:19 +0300290static inline int output_type(unsigned int type)
291{
292 switch (type) {
293 case PERF_TYPE_SYNTH:
294 return OUTPUT_TYPE_SYNTH;
295 default:
296 return type;
297 }
298}
299
300static inline unsigned int attr_type(unsigned int type)
301{
302 switch (type) {
303 case OUTPUT_TYPE_SYNTH:
304 return PERF_TYPE_SYNTH;
305 default:
306 return type;
307 }
308}
309
David Ahern2c9e45f72011-03-17 10:03:21 -0600310static bool output_set_by_user(void)
311{
312 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300313 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600314 if (output[j].user_set)
315 return true;
316 }
317 return false;
318}
David Ahern745f43e2011-03-09 22:23:26 -0700319
David Ahern9cbdb702011-04-06 21:54:20 -0600320static const char *output_field2str(enum perf_output_field field)
321{
322 int i, imax = ARRAY_SIZE(all_output_options);
323 const char *str = "";
324
325 for (i = 0; i < imax; ++i) {
326 if (all_output_options[i].field == field) {
327 str = all_output_options[i].str;
328 break;
329 }
330 }
331 return str;
332}
333
Adrian Hunter14057202017-06-21 13:17:19 +0300334#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700335
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300336static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
337 u64 sample_type, const char *sample_msg,
338 enum perf_output_field field,
339 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700340{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300341 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300342 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600343 const char *evname;
344
345 if (attr->sample_type & sample_type)
346 return 0;
347
348 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300349 if (allow_user_set)
350 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300351 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600352 pr_err("Samples for '%s' event do not have %s attribute set. "
353 "Cannot print '%s' field.\n",
354 evname, sample_msg, output_field2str(field));
355 return -1;
356 }
357
358 /* user did not ask for it explicitly so remove from the default list */
359 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300360 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600361 pr_debug("Samples for '%s' event do not have %s attribute set. "
362 "Skipping '%s' field.\n",
363 evname, sample_msg, output_field2str(field));
364
365 return 0;
366}
367
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300368static int perf_evsel__check_stype(struct perf_evsel *evsel,
369 u64 sample_type, const char *sample_msg,
370 enum perf_output_field field)
371{
372 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
373 false);
374}
375
David Ahern9cbdb702011-04-06 21:54:20 -0600376static int perf_evsel__check_attr(struct perf_evsel *evsel,
377 struct perf_session *session)
378{
379 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300380 bool allow_user_set;
381
Jiri Olsae099eba2016-01-05 22:09:09 +0100382 if (perf_header__has_feat(&session->header, HEADER_STAT))
383 return 0;
384
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300385 allow_user_set = perf_header__has_feat(&session->header,
386 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600387
David Ahern1424dc92011-03-09 22:23:28 -0700388 if (PRINT_FIELD(TRACE) &&
389 !perf_session__has_traces(session, "record -R"))
390 return -EINVAL;
391
David Ahern787bef12011-05-27 14:28:43 -0600392 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300393 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
394 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700395 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700396 }
David Ahern7cec0922011-05-30 13:08:23 -0600397
398 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300399 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
400 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600401 return -EINVAL;
402
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100403 if (PRINT_FIELD(DATA_SRC) &&
404 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
405 PERF_OUTPUT_DATA_SRC))
406 return -EINVAL;
407
408 if (PRINT_FIELD(WEIGHT) &&
409 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
410 PERF_OUTPUT_WEIGHT))
411 return -EINVAL;
412
Andi Kleen37fed3d2018-09-18 05:32:09 -0700413 if (PRINT_FIELD(SYM) &&
414 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600415 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700416 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600417 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600418 return -EINVAL;
419 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900420 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
421 pr_err("Display of offsets requested but symbol is not"
422 "selected.\n");
423 return -EINVAL;
424 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700425 if (PRINT_FIELD(DSO) &&
426 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
427 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600428 return -EINVAL;
429 }
Andi Kleendd2e18e2018-12-03 16:18:48 -0800430 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
Adrian Huntercc8fae12013-12-06 09:42:57 +0200431 pr_err("Display of source line number requested but sample IP is not\n"
432 "selected. Hence, no address to lookup the source line number.\n");
433 return -EINVAL;
434 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800435 if (PRINT_FIELD(BRSTACKINSN) &&
436 !(perf_evlist__combined_branch_type(session->evlist) &
437 PERF_SAMPLE_BRANCH_ANY)) {
438 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
439 "Hint: run 'perf record -b ...'\n");
440 return -EINVAL;
441 }
David Ahern1424dc92011-03-09 22:23:28 -0700442 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300443 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
444 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700445 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700446
447 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300448 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
449 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700450 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700451
452 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300453 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
454 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700455 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600456
Stephane Eranianfc36f942015-08-31 18:41:10 +0200457 if (PRINT_FIELD(IREGS) &&
458 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
459 PERF_OUTPUT_IREGS))
460 return -EINVAL;
461
Andi Kleenb1491ac2017-09-05 11:40:57 -0700462 if (PRINT_FIELD(UREGS) &&
463 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
464 PERF_OUTPUT_UREGS))
465 return -EINVAL;
466
Kan Liang49d58f02017-08-29 13:11:11 -0400467 if (PRINT_FIELD(PHYS_ADDR) &&
468 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
469 PERF_OUTPUT_PHYS_ADDR))
470 return -EINVAL;
471
David Ahern9cbdb702011-04-06 21:54:20 -0600472 return 0;
473}
474
Adrian Hunter7ea95722013-11-01 15:51:30 +0200475static void set_print_ip_opts(struct perf_event_attr *attr)
476{
Adrian Hunter14057202017-06-21 13:17:19 +0300477 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200478
479 output[type].print_ip_opts = 0;
480 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300481 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200482
483 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300484 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200485
486 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300487 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200488
489 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300490 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200491
492 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300493 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200494}
495
David Ahern9cbdb702011-04-06 21:54:20 -0600496/*
497 * verify all user requested events exist and the samples
498 * have the expected data
499 */
500static int perf_session__check_output_opt(struct perf_session *session)
501{
He Kuang40f20e52016-05-16 04:51:19 +0000502 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600503 struct perf_evsel *evsel;
504
Adrian Hunter14057202017-06-21 13:17:19 +0300505 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
506 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600507
508 /*
509 * even if fields is set to 0 (ie., show nothing) event must
510 * exist if user explicitly includes it on the command line
511 */
Adrian Hunter14057202017-06-21 13:17:19 +0300512 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
513 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600514 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300515 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600516 event_type(j));
517 return -1;
518 }
519
520 if (evsel && output[j].fields &&
521 perf_evsel__check_attr(evsel, session))
522 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400523
524 if (evsel == NULL)
525 continue;
526
Adrian Hunter7ea95722013-11-01 15:51:30 +0200527 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700528 }
529
Adrian Hunter98526ee2014-07-31 09:00:59 +0300530 if (!no_callchain) {
531 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000532 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300533
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300534 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000535 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300536 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300537 use_callchain = true;
538 break;
539 }
540 }
He Kuang71ac8992016-08-04 11:25:43 +0000541 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300542 symbol_conf.use_callchain = false;
543 }
544
David Ahern80b8b492013-11-19 21:07:37 -0700545 /*
546 * set default for tracepoints to print symbols only
547 * if callchains are present
548 */
549 if (symbol_conf.use_callchain &&
550 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700551 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700552
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300553 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000554 if (evsel->attr.type != j)
555 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700556
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300557 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000558 output[j].fields |= PERF_OUTPUT_IP;
559 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530560 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000561 output[j].fields |= PERF_OUTPUT_DSO;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300562 set_print_ip_opts(&evsel->attr);
He Kuang40f20e52016-05-16 04:51:19 +0000563 goto out;
564 }
David Ahern80b8b492013-11-19 21:07:37 -0700565 }
566 }
567
568out:
David Ahern1424dc92011-03-09 22:23:28 -0700569 return 0;
570}
David Ahern745f43e2011-03-09 22:23:26 -0700571
Milian Wolff9add8fe2018-11-07 23:34:37 +0100572static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
573 FILE *fp
574)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200575{
Andi Kleenb1491ac2017-09-05 11:40:57 -0700576 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300577 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700578
579 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300580 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700581
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300582 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700583
584 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
585 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300586 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700587 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300588
Milian Wolffb07d16f2018-11-07 10:37:05 +0100589 fprintf(fp, "\n");
590
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300591 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700592}
593
Milian Wolff9add8fe2018-11-07 23:34:37 +0100594static int perf_sample__fprintf_iregs(struct perf_sample *sample,
595 struct perf_event_attr *attr, FILE *fp)
596{
597 return perf_sample__fprintf_regs(&sample->intr_regs,
598 attr->sample_regs_intr, fp);
599}
600
601static int perf_sample__fprintf_uregs(struct perf_sample *sample,
602 struct perf_event_attr *attr, FILE *fp)
603{
604 return perf_sample__fprintf_regs(&sample->user_regs,
605 attr->sample_regs_user, fp);
606}
607
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300608static int perf_sample__fprintf_start(struct perf_sample *sample,
609 struct thread *thread,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100610 struct perf_evsel *evsel,
611 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700612{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300613 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700614 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700615 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300616 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700617
David Ahern745f43e2011-03-09 22:23:26 -0700618 if (PRINT_FIELD(COMM)) {
619 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300620 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300621 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300622 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700623 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300624 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700625 }
David Ahernc70c94b2011-03-09 22:23:25 -0700626
David Ahern745f43e2011-03-09 22:23:26 -0700627 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300628 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700629 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300630 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700631 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300632 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700633
David Ahern745f43e2011-03-09 22:23:26 -0700634 if (PRINT_FIELD(CPU)) {
635 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300636 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700637 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300638 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700639 }
David Ahernc70c94b2011-03-09 22:23:25 -0700640
Jiri Olsa28a0b392018-01-07 17:03:52 +0100641 if (PRINT_FIELD(MISC)) {
642 int ret = 0;
643
644 #define has(m) \
645 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
646
647 if (has(KERNEL))
648 ret += fprintf(fp, "K");
649 if (has(USER))
650 ret += fprintf(fp, "U");
651 if (has(HYPERVISOR))
652 ret += fprintf(fp, "H");
653 if (has(GUEST_KERNEL))
654 ret += fprintf(fp, "G");
655 if (has(GUEST_USER))
656 ret += fprintf(fp, "g");
657
658 switch (type) {
659 case PERF_RECORD_MMAP:
660 case PERF_RECORD_MMAP2:
661 if (has(MMAP_DATA))
662 ret += fprintf(fp, "M");
663 break;
664 case PERF_RECORD_COMM:
665 if (has(COMM_EXEC))
666 ret += fprintf(fp, "E");
667 break;
668 case PERF_RECORD_SWITCH:
669 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300670 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100671 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300672 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
673 ret += fprintf(fp, "p");
674 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100675 default:
676 break;
677 }
678
679 #undef has
680
681 ret += fprintf(fp, "%*s", 6 - ret, " ");
682 printed += ret;
683 }
684
David Ahern745f43e2011-03-09 22:23:26 -0700685 if (PRINT_FIELD(TIME)) {
686 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300687 secs = nsecs / NSEC_PER_SEC;
688 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900689
Adrian Hunter83e19862015-09-25 16:15:36 +0300690 if (nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300691 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900692 else {
693 char sample_time[32];
694 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300695 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900696 }
David Ahern745f43e2011-03-09 22:23:26 -0700697 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300698
699 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700700}
701
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200702static inline char
703mispred_str(struct branch_entry *br)
704{
705 if (!(br->flags.mispred || br->flags.predicted))
706 return '-';
707
708 return br->flags.predicted ? 'P' : 'M';
709}
710
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300711static int perf_sample__fprintf_brstack(struct perf_sample *sample,
712 struct thread *thread,
713 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200714{
715 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700716 struct addr_location alf, alt;
717 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300718 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200719
720 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300721 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200722
723 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700724 from = br->entries[i].from;
725 to = br->entries[i].to;
726
727 if (PRINT_FIELD(DSO)) {
728 memset(&alf, 0, sizeof(alf));
729 memset(&alt, 0, sizeof(alt));
Adrian Hunter692d0e62018-11-06 23:07:12 +0200730 thread__find_map_fb(thread, sample->cpumode, from, &alf);
731 thread__find_map_fb(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700732 }
733
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300734 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700735 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300736 printed += fprintf(fp, "(");
737 printed += map__fprintf_dsoname(alf.map, fp);
738 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700739 }
740
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300741 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700742 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300743 printed += fprintf(fp, "(");
744 printed += map__fprintf_dsoname(alt.map, fp);
745 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700746 }
747
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300748 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200749 mispred_str( br->entries + i),
750 br->entries[i].flags.in_tx? 'X' : '-',
751 br->entries[i].flags.abort? 'A' : '-',
752 br->entries[i].flags.cycles);
753 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300754
755 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200756}
757
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300758static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
759 struct thread *thread,
760 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200761{
762 struct branch_stack *br = sample->branch_stack;
763 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200764 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300765 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200766
767 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300768 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200769
770 for (i = 0; i < br->nr; i++) {
771
772 memset(&alf, 0, sizeof(alf));
773 memset(&alt, 0, sizeof(alt));
774 from = br->entries[i].from;
775 to = br->entries[i].to;
776
Adrian Hunter692d0e62018-11-06 23:07:12 +0200777 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
778 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200779
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300780 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700781 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300782 printed += fprintf(fp, "(");
783 printed += map__fprintf_dsoname(alf.map, fp);
784 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700785 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300786 printed += fprintf(fp, "%c", '/');
787 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700788 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300789 printed += fprintf(fp, "(");
790 printed += map__fprintf_dsoname(alt.map, fp);
791 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700792 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300793 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200794 mispred_str( br->entries + i),
795 br->entries[i].flags.in_tx? 'X' : '-',
796 br->entries[i].flags.abort? 'A' : '-',
797 br->entries[i].flags.cycles);
798 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300799
800 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200801}
802
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300803static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
804 struct thread *thread,
805 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700806{
807 struct branch_stack *br = sample->branch_stack;
808 struct addr_location alf, alt;
809 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300810 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700811
812 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300813 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700814
815 for (i = 0; i < br->nr; i++) {
816
817 memset(&alf, 0, sizeof(alf));
818 memset(&alt, 0, sizeof(alt));
819 from = br->entries[i].from;
820 to = br->entries[i].to;
821
Adrian Hunter692d0e62018-11-06 23:07:12 +0200822 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300823 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700824 from = map__map_ip(alf.map, from);
825
Adrian Hunter692d0e62018-11-06 23:07:12 +0200826 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300827 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700828 to = map__map_ip(alt.map, to);
829
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300830 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700831 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300832 printed += fprintf(fp, "(");
833 printed += map__fprintf_dsoname(alf.map, fp);
834 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700835 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300836 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700837 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300838 printed += fprintf(fp, "(");
839 printed += map__fprintf_dsoname(alt.map, fp);
840 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700841 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300842 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700843 mispred_str(br->entries + i),
844 br->entries[i].flags.in_tx ? 'X' : '-',
845 br->entries[i].flags.abort ? 'A' : '-',
846 br->entries[i].flags.cycles);
847 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300848
849 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700850}
Andi Kleen48d02a12017-02-23 15:46:34 -0800851#define MAXBB 16384UL
852
853static int grab_bb(u8 *buffer, u64 start, u64 end,
854 struct machine *machine, struct thread *thread,
855 bool *is64bit, u8 *cpumode, bool last)
856{
857 long offset, len;
858 struct addr_location al;
859 bool kernel;
860
861 if (!start || !end)
862 return 0;
863
864 kernel = machine__kernel_ip(machine, start);
865 if (kernel)
866 *cpumode = PERF_RECORD_MISC_KERNEL;
867 else
868 *cpumode = PERF_RECORD_MISC_USER;
869
870 /*
871 * Block overlaps between kernel and user.
872 * This can happen due to ring filtering
873 * On Intel CPUs the entry into the kernel is filtered,
874 * but the exit is not. Let the caller patch it up.
875 */
876 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300877 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800878 return -ENXIO;
879 }
880
881 memset(&al, 0, sizeof(al));
882 if (end - start > MAXBB - MAXINSN) {
883 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300884 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800885 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300886 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800887 return 0;
888 }
889
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300890 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300891 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800892 return 0;
893 }
894 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300895 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800896 return 0;
897 }
898
899 /* Load maps to ensure dso->is_64_bit has been updated */
900 map__load(al.map);
901
902 offset = al.map->map_ip(al.map, start);
903 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
904 end - start + MAXINSN);
905
906 *is64bit = al.map->dso->is_64_bit;
907 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300908 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800909 start, end);
910 return len;
911}
912
Andi Kleendd2e18e2018-12-03 16:18:48 -0800913static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
914{
915 struct addr_location al;
916 int ret = 0;
917
918 memset(&al, 0, sizeof(al));
919 thread__find_map(thread, cpumode, addr, &al);
920 if (!al.map)
921 return 0;
922 ret = map__fprintf_srccode(al.map, al.addr, stdout,
923 &thread->srccode_state);
924 if (ret)
925 ret += printf("\n");
926 return ret;
927}
928
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300929static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
930 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -0700931 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -0800932{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300933 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
934 dump_insn(x, ip, inbuf, len, NULL),
935 en->flags.predicted ? " PRED" : "",
936 en->flags.mispred ? " MISPRED" : "",
937 en->flags.in_tx ? " INTX" : "",
938 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800939 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -0700940 *total_cycles += en->flags.cycles;
941 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800942 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300943 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800944 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300945 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800946}
947
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300948static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
949 u8 cpumode, int cpu, struct symbol **lastsym,
950 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800951{
952 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300953 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800954
955 memset(&al, 0, sizeof(al));
956
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300957 thread__find_map(thread, cpumode, addr, &al);
958
Andi Kleen48d02a12017-02-23 15:46:34 -0800959 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300960 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800961
962 al.cpu = cpu;
963 al.sym = NULL;
964 if (al.map)
965 al.sym = map__find_symbol(al.map, al.addr);
966
967 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300968 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800969
970 if (al.addr < al.sym->end)
971 off = al.addr - al.sym->start;
972 else
973 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300974 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800975 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300976 printed += fprintf(fp, "%+d", off);
977 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800978 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300979 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
980 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800981 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300982
983 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800984}
985
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300986static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
987 struct thread *thread,
988 struct perf_event_attr *attr,
989 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800990{
991 struct branch_stack *br = sample->branch_stack;
992 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300993 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800994 struct perf_insn x;
995 u8 buffer[MAXBB];
996 unsigned off;
997 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -0700998 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800999
1000 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001001 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001002 nr = br->nr;
1003 if (max_blocks && nr > max_blocks + 1)
1004 nr = max_blocks + 1;
1005
1006 x.thread = thread;
1007 x.cpu = sample->cpu;
1008
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001009 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -08001010
1011 /* Handle first from jump, of which we don't know the entry. */
1012 len = grab_bb(buffer, br->entries[nr-1].from,
1013 br->entries[nr-1].from,
1014 machine, thread, &x.is64bit, &x.cpumode, false);
1015 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001016 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1017 x.cpumode, x.cpu, &lastsym, attr, fp);
1018 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001019 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001020 if (PRINT_FIELD(SRCCODE))
1021 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
Andi Kleen48d02a12017-02-23 15:46:34 -08001022 }
1023
1024 /* Print all blocks */
1025 for (i = nr - 2; i >= 0; i--) {
1026 if (br->entries[i].from || br->entries[i].to)
1027 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1028 br->entries[i].from,
1029 br->entries[i].to);
1030 start = br->entries[i + 1].to;
1031 end = br->entries[i].from;
1032
1033 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1034 /* Patch up missing kernel transfers due to ring filters */
1035 if (len == -ENXIO && i > 0) {
1036 end = br->entries[--i].from;
1037 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1038 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1039 }
1040 if (len <= 0)
1041 continue;
1042
1043 insn = 0;
1044 for (off = 0;; off += ilen) {
1045 uint64_t ip = start + off;
1046
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001047 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001048 if (ip == end) {
Andi Kleenfe571202018-09-24 10:07:32 -07001049 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp,
1050 &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001051 if (PRINT_FIELD(SRCCODE))
1052 printed += print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001053 break;
1054 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001055 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1056 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001057 if (ilen == 0)
1058 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001059 if (PRINT_FIELD(SRCCODE))
1060 print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001061 insn++;
1062 }
1063 }
1064 }
1065
1066 /*
1067 * Hit the branch? In this case we are already done, and the target
1068 * has not been executed yet.
1069 */
1070 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001071 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001072 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001073 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001074
1075 /*
1076 * Print final block upto sample
Andi Kleen61f61152018-11-19 21:06:17 -08001077 *
1078 * Due to pipeline delays the LBRs might be missing a branch
1079 * or two, which can result in very large or negative blocks
1080 * between final branch and sample. When this happens just
1081 * continue walking after the last TO until we hit a branch.
Andi Kleen48d02a12017-02-23 15:46:34 -08001082 */
1083 start = br->entries[0].to;
1084 end = sample->ip;
Andi Kleen61f61152018-11-19 21:06:17 -08001085 if (end < start) {
1086 /* Missing jump. Scan 128 bytes for the next branch */
1087 end = start + 128;
1088 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001089 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001090 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001091 if (len <= 0) {
1092 /* Print at least last IP if basic block did not work */
1093 len = grab_bb(buffer, sample->ip, sample->ip,
1094 machine, thread, &x.is64bit, &x.cpumode, false);
1095 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001096 goto out;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001097 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001098 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001099 if (PRINT_FIELD(SRCCODE))
1100 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001101 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001102 }
1103 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001104 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1105 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001106 if (ilen == 0)
1107 break;
Andi Kleen61f61152018-11-19 21:06:17 -08001108 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1109 /*
1110 * Hit a missing branch. Just stop.
1111 */
1112 printed += fprintf(fp, "\t... not reaching sample ...\n");
1113 break;
1114 }
Andi Kleendd2e18e2018-12-03 16:18:48 -08001115 if (PRINT_FIELD(SRCCODE))
1116 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001117 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001118out:
1119 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001120}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001121
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001122static int perf_sample__fprintf_addr(struct perf_sample *sample,
1123 struct thread *thread,
1124 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001125{
1126 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001127 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001128
1129 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001130 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001131
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001132 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001133
1134 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001135 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001136 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001137 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001138 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001139 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001140 }
1141
1142 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001143 printed += fprintf(fp, " (");
1144 printed += map__fprintf_dsoname(al.map, fp);
1145 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001146 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001147out:
1148 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001149}
1150
Andi Kleen99f753f2018-09-20 11:05:39 -07001151static const char *resolve_branch_sym(struct perf_sample *sample,
1152 struct perf_evsel *evsel,
1153 struct thread *thread,
1154 struct addr_location *al,
1155 u64 *ip)
1156{
1157 struct addr_location addr_al;
1158 struct perf_event_attr *attr = &evsel->attr;
1159 const char *name = NULL;
1160
1161 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1162 if (sample_addr_correlates_sym(attr)) {
1163 thread__resolve(thread, &addr_al, sample);
1164 if (addr_al.sym)
1165 name = addr_al.sym->name;
1166 else
1167 *ip = sample->addr;
1168 } else {
1169 *ip = sample->addr;
1170 }
1171 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1172 if (al->sym)
1173 name = al->sym->name;
1174 else
1175 *ip = sample->ip;
1176 }
1177 return name;
1178}
1179
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001180static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1181 struct perf_evsel *evsel,
1182 struct thread *thread,
1183 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001184{
1185 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter256d92b2018-12-21 14:06:19 +02001186 size_t depth = thread_stack__depth(thread, sample->cpu);
Adrian Huntere2167082016-06-23 16:40:58 +03001187 const char *name = NULL;
1188 static int spacing;
1189 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001190 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001191 u64 ip = 0;
1192
1193 /*
1194 * The 'return' has already been popped off the stack so the depth has
1195 * to be adjusted to match the 'call'.
1196 */
1197 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1198 depth += 1;
1199
Andi Kleen99f753f2018-09-20 11:05:39 -07001200 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001201
Andi Kleena78cdee2018-09-18 05:32:10 -07001202 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1203 dlen += fprintf(fp, "(");
1204 dlen += map__fprintf_dsoname(al->map, fp);
1205 dlen += fprintf(fp, ")\t");
1206 }
1207
Adrian Huntere2167082016-06-23 16:40:58 +03001208 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001209 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001210 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001211 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001212
1213 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001214 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001215
1216 /*
1217 * Try to keep the output length from changing frequently so that the
1218 * output lines up more nicely.
1219 */
1220 if (len > spacing || (len && len < spacing - 52))
1221 spacing = round_up(len + 4, 32);
1222
1223 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001224 len += fprintf(fp, "%*s", spacing - len, "");
1225
Andi Kleena78cdee2018-09-18 05:32:10 -07001226 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001227}
1228
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001229static int perf_sample__fprintf_insn(struct perf_sample *sample,
1230 struct perf_event_attr *attr,
1231 struct thread *thread,
1232 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001233{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001234 int printed = 0;
1235
Andi Kleen224e2c92016-10-07 16:42:27 +03001236 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001237 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen224e2c92016-10-07 16:42:27 +03001238 if (PRINT_FIELD(INSN)) {
1239 int i;
1240
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001241 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001242 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001243 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001244 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001245 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001246 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1247
1248 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001249}
1250
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001251static int perf_sample__fprintf_bts(struct perf_sample *sample,
1252 struct perf_evsel *evsel,
1253 struct thread *thread,
1254 struct addr_location *al,
1255 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001256{
1257 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001258 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001259 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001260 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001261
Adrian Huntere2167082016-06-23 16:40:58 +03001262 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001263 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001264
Akihiro Nagai95582592012-01-30 13:43:09 +09001265 /* print branch_from information */
1266 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001267 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001268 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001269
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001270 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001271 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001272 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001273 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001274
1275 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001276 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001277 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001278 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001279 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001280 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001281 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001282 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001283
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001284 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001285 }
1286
Akihiro Nagai95582592012-01-30 13:43:09 +09001287 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001288 if (PRINT_FIELD(ADDR) ||
1289 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001290 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001291 printed += fprintf(fp, " => ");
1292 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001293 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001294
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001295 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001296 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001297
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001298 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001299 printed += fprintf(fp, "\n");
1300 if (PRINT_FIELD(SRCCODE)) {
1301 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1302 &thread->srccode_state);
1303 if (ret) {
1304 printed += ret;
1305 printed += printf("\n");
1306 }
1307 }
1308 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001309}
1310
Adrian Hunter055cd332016-06-23 16:40:56 +03001311static struct {
1312 u32 flags;
1313 const char *name;
1314} sample_flags[] = {
1315 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1316 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1317 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1318 {PERF_IP_FLAG_BRANCH, "jmp"},
1319 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1320 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1321 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1322 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1323 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1324 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1325 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1326 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1327 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1328 {0, NULL}
1329};
1330
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001331static const char *sample_flags_to_name(u32 flags)
1332{
1333 int i;
1334
1335 for (i = 0; sample_flags[i].name ; i++) {
1336 if (sample_flags[i].flags == flags)
1337 return sample_flags[i].name;
1338 }
1339
1340 return NULL;
1341}
1342
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001343static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001344{
1345 const char *chars = PERF_IP_FLAG_CHARS;
1346 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001347 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1348 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001349 char str[33];
1350 int i, pos = 0;
1351
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001352 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1353 if (name)
1354 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1355
1356 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1357 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1358 if (name)
1359 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1360 }
1361
1362 if (flags & PERF_IP_FLAG_TRACE_END) {
1363 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1364 if (name)
1365 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001366 }
1367
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001368 for (i = 0; i < n; i++, flags >>= 1) {
1369 if (flags & 1)
1370 str[pos++] = chars[i];
1371 }
1372 for (; i < 32; i++, flags >>= 1) {
1373 if (flags & 1)
1374 str[pos++] = '?';
1375 }
1376 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001377
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001378 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001379}
1380
Wang Nan30372f02016-02-24 11:20:45 +00001381struct printer_data {
1382 int line_no;
1383 bool hit_nul;
1384 bool is_printable;
1385};
1386
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001387static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1388 unsigned int val,
1389 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001390{
1391 unsigned char ch = (unsigned char)val;
1392 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001393 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001394
1395 switch (op) {
1396 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001397 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001398 break;
1399 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001400 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001401 " ");
1402 break;
1403 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001404 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001405 break;
1406 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001407 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001408 break;
1409 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001410 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001411 break;
1412 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001413 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001414 break;
1415 case BINARY_PRINT_CHAR_DATA:
1416 if (printer_data->hit_nul && ch)
1417 printer_data->is_printable = false;
1418
1419 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001420 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001421
1422 if (!printer_data->is_printable)
1423 break;
1424
1425 if (ch == '\0')
1426 printer_data->hit_nul = true;
1427 else
1428 printer_data->is_printable = false;
1429 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001430 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001431 }
1432 break;
1433 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001434 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001435 break;
1436 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001437 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001438 printer_data->line_no++;
1439 break;
1440 case BINARY_PRINT_DATA_END:
1441 default:
1442 break;
1443 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001444
1445 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001446}
1447
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001448static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001449{
1450 unsigned int nr_bytes = sample->raw_size;
1451 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001452 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1453 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001454
1455 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001456 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1457
1458 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001459}
1460
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001461static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001462{
1463 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001464 return fprintf(fp, "%*s", spacing - len, "");
1465
1466 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001467}
1468
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001469static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001470{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001471 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001472}
1473
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001474static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001475{
1476 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1477 int len;
1478
1479 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001480 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001481
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001482 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001483 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001484 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001485}
1486
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001487static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001488{
1489 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1490 int len;
1491
1492 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001493 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001494
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001495 len = fprintf(fp, " hints: %#x extensions: %#x ",
1496 data->hints, data->extensions);
1497 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001498}
1499
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001500static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001501{
1502 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1503 int len;
1504
1505 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001506 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001507
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001508 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1509 data->hw, data->cstate, data->subcstate);
1510 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001511}
1512
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001513static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001514{
1515 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1516 int len;
1517
1518 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001519 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001520
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001521 len = fprintf(fp, " IP: %u ", data->ip);
1522 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001523}
1524
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001525static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001526{
1527 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1528 int len;
1529
1530 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001531 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001532
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001533 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001534 data->deepest_cstate, data->last_cstate,
1535 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001536 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001537}
1538
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001539static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001540{
1541 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1542 unsigned int percent, freq;
1543 int len;
1544
1545 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001546 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001547
1548 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001549 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001550 if (data->max_nonturbo) {
1551 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001552 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001553 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001554 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001555}
1556
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001557static int perf_sample__fprintf_synth(struct perf_sample *sample,
1558 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001559{
1560 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001561 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001562 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001563 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001564 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001565 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001566 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001567 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001568 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001569 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001570 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001571 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001572 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001573 default:
1574 break;
1575 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001576
1577 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001578}
1579
Jiri Olsa809e9422015-11-26 18:55:21 +01001580struct perf_script {
1581 struct perf_tool tool;
1582 struct perf_session *session;
1583 bool show_task_events;
1584 bool show_mmap_events;
1585 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301586 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001587 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001588 bool show_round_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001589 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001590 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001591 struct cpu_map *cpus;
1592 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001593 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001594 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001595 struct perf_time_interval *ptime_range;
1596 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001597 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001598};
1599
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001600static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1601{
1602 struct perf_evsel *evsel;
1603 int max = 0;
1604
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001605 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001606 int len = strlen(perf_evsel__name(evsel));
1607
1608 max = MAX(len, max);
1609 }
1610
1611 return max;
1612}
1613
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001614static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001615{
1616 struct mem_info mi = { .data_src.val = data_src };
1617 char decode[100];
1618 char out[100];
1619 static int maxlen;
1620 int len;
1621
1622 perf_script__meminfo_scnprintf(decode, 100, &mi);
1623
1624 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1625 if (maxlen < len)
1626 maxlen = len;
1627
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001628 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001629}
1630
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001631struct metric_ctx {
1632 struct perf_sample *sample;
1633 struct thread *thread;
1634 struct perf_evsel *evsel;
1635 FILE *fp;
1636};
1637
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001638static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1639 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001640 const char *fmt,
1641 const char *unit, double val)
1642{
1643 struct metric_ctx *mctx = ctx;
1644
1645 if (!fmt)
1646 return;
1647 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001648 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001649 fputs("\tmetric: ", mctx->fp);
1650 if (color)
1651 color_fprintf(mctx->fp, color, fmt, val);
1652 else
1653 printf(fmt, val);
1654 fprintf(mctx->fp, " %s\n", unit);
1655}
1656
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001657static void script_new_line(struct perf_stat_config *config __maybe_unused,
1658 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001659{
1660 struct metric_ctx *mctx = ctx;
1661
1662 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001663 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001664 fputs("\tmetric: ", mctx->fp);
1665}
1666
1667static void perf_sample__fprint_metric(struct perf_script *script,
1668 struct thread *thread,
1669 struct perf_evsel *evsel,
1670 struct perf_sample *sample,
1671 FILE *fp)
1672{
1673 struct perf_stat_output_ctx ctx = {
1674 .print_metric = script_print_metric,
1675 .new_line = script_new_line,
1676 .ctx = &(struct metric_ctx) {
1677 .sample = sample,
1678 .thread = thread,
1679 .evsel = evsel,
1680 .fp = fp,
1681 },
1682 .force_header = false,
1683 };
1684 struct perf_evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001685 u64 val;
1686
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001687 if (!evsel->stats)
1688 perf_evlist__alloc_stats(script->session->evlist, false);
1689 if (evsel_script(evsel->leader)->gnum++ == 0)
1690 perf_stat__reset_shadow_stats();
1691 val = sample->period * evsel->scale;
1692 perf_stat__update_shadow_stats(evsel,
1693 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001694 sample->cpu,
1695 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001696 evsel_script(evsel)->val = val;
1697 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1698 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001699 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001700 evsel_script(ev2)->val,
1701 sample->cpu,
1702 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001703 NULL,
1704 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001705 }
1706 evsel_script(evsel->leader)->gnum = 0;
1707 }
1708}
1709
Andi Kleen99f753f2018-09-20 11:05:39 -07001710static bool show_event(struct perf_sample *sample,
1711 struct perf_evsel *evsel,
1712 struct thread *thread,
1713 struct addr_location *al)
1714{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001715 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001716
1717 if (!symbol_conf.graph_function)
1718 return true;
1719
1720 if (thread->filter) {
1721 if (depth <= thread->filter_entry_depth) {
1722 thread->filter = false;
1723 return false;
1724 }
1725 return true;
1726 } else {
1727 const char *s = symbol_conf.graph_function;
1728 u64 ip;
1729 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1730 &ip);
1731 unsigned nlen;
1732
1733 if (!name)
1734 return false;
1735 nlen = strlen(name);
1736 while (*s) {
1737 unsigned len = strcspn(s, ",");
1738 if (nlen == len && !strncmp(name, s, len)) {
1739 thread->filter = true;
1740 thread->filter_entry_depth = depth;
1741 return true;
1742 }
1743 s += len;
1744 if (*s == ',')
1745 s++;
1746 }
1747 return false;
1748 }
1749}
1750
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001751static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001752 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001753 struct addr_location *al,
1754 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001755{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001756 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001757 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001758 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001759 struct perf_evsel_script *es = evsel->priv;
1760 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001761
Adrian Hunter14057202017-06-21 13:17:19 +03001762 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001763 return;
1764
Andi Kleen99f753f2018-09-20 11:05:39 -07001765 if (!show_event(sample, evsel, thread, al))
1766 return;
1767
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001768 ++es->samples;
1769
Jiri Olsa28a0b392018-01-07 17:03:52 +01001770 perf_sample__fprintf_start(sample, thread, evsel,
1771 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001772
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001773 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001774 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001775
Namhyung Kime944d3d2013-11-18 14:34:52 +09001776 if (PRINT_FIELD(EVNAME)) {
1777 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001778
1779 if (!script->name_width)
1780 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1781
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001782 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001783 }
1784
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001785 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001786 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001787
Akihiro Nagai95582592012-01-30 13:43:09 +09001788 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001789 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001790 return;
1791 }
1792
Andi Kleen96167162019-01-17 11:48:34 -08001793 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001794 event_format__fprintf(evsel->tp_format, sample->cpu,
1795 sample->raw_data, sample->raw_size, fp);
1796 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001797
1798 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001799 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001800
David Ahern7cec0922011-05-30 13:08:23 -06001801 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001802 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001803
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001804 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001805 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001806
1807 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001808 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001809
David Ahern787bef12011-05-27 14:28:43 -06001810 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001811 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001812
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001813 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001814 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001815 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001816 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001817
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001818 fputc(cursor ? '\n' : ' ', fp);
1819 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001820 }
1821
Stephane Eranianfc36f942015-08-31 18:41:10 +02001822 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001823 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001824
Andi Kleenb1491ac2017-09-05 11:40:57 -07001825 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001826 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001827
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001828 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001829 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001830 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001831 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001832 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001833 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001834
Wang Nan30372f02016-02-24 11:20:45 +00001835 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001836 perf_sample__fprintf_bpf_output(sample, fp);
1837 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001838
1839 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001840 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1841 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001842
Andi Kleendd2e18e2018-12-03 16:18:48 -08001843 if (PRINT_FIELD(SRCCODE)) {
1844 if (map__fprintf_srccode(al->map, al->addr, stdout,
1845 &thread->srccode_state))
1846 printf("\n");
1847 }
1848
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001849 if (PRINT_FIELD(METRIC))
1850 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001851
1852 if (verbose)
1853 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001854}
1855
Tom Zanussi956ffd02009-11-25 01:15:46 -06001856static struct scripting_ops *scripting_ops;
1857
Jiri Olsa36e33c52016-01-06 11:49:56 +01001858static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1859{
1860 int nthreads = thread_map__nr(counter->threads);
1861 int ncpus = perf_evsel__nr_cpus(counter);
1862 int cpu, thread;
1863 static int header_printed;
1864
1865 if (counter->system_wide)
1866 nthreads = 1;
1867
1868 if (!header_printed) {
1869 printf("%3s %8s %15s %15s %15s %15s %s\n",
1870 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1871 header_printed = 1;
1872 }
1873
1874 for (thread = 0; thread < nthreads; thread++) {
1875 for (cpu = 0; cpu < ncpus; cpu++) {
1876 struct perf_counts_values *counts;
1877
1878 counts = perf_counts(counter->counts, cpu, thread);
1879
1880 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1881 counter->cpus->map[cpu],
1882 thread_map__pid(counter->threads, thread),
1883 counts->val,
1884 counts->ena,
1885 counts->run,
1886 tstamp,
1887 perf_evsel__name(counter));
1888 }
1889 }
1890}
1891
Jiri Olsae099eba2016-01-05 22:09:09 +01001892static void process_stat(struct perf_evsel *counter, u64 tstamp)
1893{
1894 if (scripting_ops && scripting_ops->process_stat)
1895 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001896 else
1897 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001898}
1899
1900static void process_stat_interval(u64 tstamp)
1901{
1902 if (scripting_ops && scripting_ops->process_stat_interval)
1903 scripting_ops->process_stat_interval(tstamp);
1904}
1905
Tom Zanussi956ffd02009-11-25 01:15:46 -06001906static void setup_scripting(void)
1907{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001908 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001909 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001910}
1911
Adrian Hunterd445dd22014-08-15 22:08:37 +03001912static int flush_scripting(void)
1913{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001914 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001915}
1916
Tom Zanussi956ffd02009-11-25 01:15:46 -06001917static int cleanup_scripting(void)
1918{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001919 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001920
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001921 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001922}
1923
Jiri Olsa809e9422015-11-26 18:55:21 +01001924static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001925 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001926 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001927 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001928 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001929{
Jiri Olsa809e9422015-11-26 18:55:21 +01001930 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001931 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001932
Jin Yao2ab046c2017-12-08 21:13:46 +08001933 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1934 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001935 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001936 }
David Aherna91f4c42016-11-29 10:15:43 -07001937
David Ahern1424dc92011-03-09 22:23:28 -07001938 if (debug_mode) {
1939 if (sample->time < last_timestamp) {
1940 pr_err("Samples misordered, previous: %" PRIu64
1941 " this: %" PRIu64 "\n", last_timestamp,
1942 sample->time);
1943 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001944 }
David Ahern1424dc92011-03-09 22:23:28 -07001945 last_timestamp = sample->time;
1946 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001947 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001948
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001949 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001950 pr_err("problem processing %d event, skipping it.\n",
1951 event->header.type);
1952 return -1;
1953 }
1954
1955 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001956 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001957
Anton Blanchard5d67be92011-07-04 21:57:50 +10001958 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001959 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001960
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001961 if (scripting_ops)
1962 scripting_ops->process_event(event, sample, evsel, &al);
1963 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001964 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001965
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001966out_put:
1967 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001968 return 0;
1969}
1970
Adrian Hunter7ea95722013-11-01 15:51:30 +02001971static int process_attr(struct perf_tool *tool, union perf_event *event,
1972 struct perf_evlist **pevlist)
1973{
1974 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1975 struct perf_evlist *evlist;
1976 struct perf_evsel *evsel, *pos;
1977 int err;
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301978 static struct perf_evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001979
1980 err = perf_event__process_attr(tool, event, pevlist);
1981 if (err)
1982 return err;
1983
1984 evlist = *pevlist;
1985 evsel = perf_evlist__last(*pevlist);
1986
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301987 if (!evsel->priv) {
1988 if (scr->per_event_dump) {
1989 evsel->priv = perf_evsel_script__new(evsel,
1990 scr->session->data);
1991 } else {
1992 es = zalloc(sizeof(*es));
1993 if (!es)
1994 return -ENOMEM;
1995 es->fp = stdout;
1996 evsel->priv = es;
1997 }
1998 }
1999
Adrian Hunter14057202017-06-21 13:17:19 +03002000 if (evsel->attr.type >= PERF_TYPE_MAX &&
2001 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002002 return 0;
2003
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002004 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02002005 if (pos->attr.type == evsel->attr.type && pos != evsel)
2006 return 0;
2007 }
2008
2009 set_print_ip_opts(&evsel->attr);
2010
Jiri Olsad2b5a312015-10-16 12:41:25 +02002011 if (evsel->attr.sample_type)
2012 err = perf_evsel__check_attr(evsel, scr->session);
2013
2014 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002015}
2016
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002017static int process_comm_event(struct perf_tool *tool,
2018 union perf_event *event,
2019 struct perf_sample *sample,
2020 struct machine *machine)
2021{
2022 struct thread *thread;
2023 struct perf_script *script = container_of(tool, struct perf_script, tool);
2024 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002025 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002026 int ret = -1;
2027
2028 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2029 if (thread == NULL) {
2030 pr_debug("problem processing COMM event, skipping it.\n");
2031 return -1;
2032 }
2033
2034 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2035 goto out;
2036
2037 if (!evsel->attr.sample_id_all) {
2038 sample->cpu = 0;
2039 sample->time = 0;
2040 sample->tid = event->comm.tid;
2041 sample->pid = event->comm.pid;
2042 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002043 perf_sample__fprintf_start(sample, thread, evsel,
2044 PERF_RECORD_COMM, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002045 perf_event__fprintf(event, stdout);
2046 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002047out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002048 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002049 return ret;
2050}
2051
Hari Bathini96a44bb2017-03-08 02:12:06 +05302052static int process_namespaces_event(struct perf_tool *tool,
2053 union perf_event *event,
2054 struct perf_sample *sample,
2055 struct machine *machine)
2056{
2057 struct thread *thread;
2058 struct perf_script *script = container_of(tool, struct perf_script, tool);
2059 struct perf_session *session = script->session;
2060 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2061 int ret = -1;
2062
2063 thread = machine__findnew_thread(machine, event->namespaces.pid,
2064 event->namespaces.tid);
2065 if (thread == NULL) {
2066 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2067 return -1;
2068 }
2069
2070 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2071 goto out;
2072
2073 if (!evsel->attr.sample_id_all) {
2074 sample->cpu = 0;
2075 sample->time = 0;
2076 sample->tid = event->namespaces.tid;
2077 sample->pid = event->namespaces.pid;
2078 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002079 perf_sample__fprintf_start(sample, thread, evsel,
2080 PERF_RECORD_NAMESPACES, stdout);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302081 perf_event__fprintf(event, stdout);
2082 ret = 0;
2083out:
2084 thread__put(thread);
2085 return ret;
2086}
2087
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002088static int process_fork_event(struct perf_tool *tool,
2089 union perf_event *event,
2090 struct perf_sample *sample,
2091 struct machine *machine)
2092{
2093 struct thread *thread;
2094 struct perf_script *script = container_of(tool, struct perf_script, tool);
2095 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002096 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002097
2098 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2099 return -1;
2100
2101 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2102 if (thread == NULL) {
2103 pr_debug("problem processing FORK event, skipping it.\n");
2104 return -1;
2105 }
2106
2107 if (!evsel->attr.sample_id_all) {
2108 sample->cpu = 0;
2109 sample->time = event->fork.time;
2110 sample->tid = event->fork.tid;
2111 sample->pid = event->fork.pid;
2112 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002113 perf_sample__fprintf_start(sample, thread, evsel,
2114 PERF_RECORD_FORK, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002115 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002116 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002117
2118 return 0;
2119}
2120static int process_exit_event(struct perf_tool *tool,
2121 union perf_event *event,
2122 struct perf_sample *sample,
2123 struct machine *machine)
2124{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002125 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002126 struct thread *thread;
2127 struct perf_script *script = container_of(tool, struct perf_script, tool);
2128 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002129 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002130
2131 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2132 if (thread == NULL) {
2133 pr_debug("problem processing EXIT event, skipping it.\n");
2134 return -1;
2135 }
2136
2137 if (!evsel->attr.sample_id_all) {
2138 sample->cpu = 0;
2139 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002140 sample->tid = event->fork.tid;
2141 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002142 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002143 perf_sample__fprintf_start(sample, thread, evsel,
2144 PERF_RECORD_EXIT, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002145 perf_event__fprintf(event, stdout);
2146
2147 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002148 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002149
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002150 thread__put(thread);
2151 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002152}
2153
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002154static int process_mmap_event(struct perf_tool *tool,
2155 union perf_event *event,
2156 struct perf_sample *sample,
2157 struct machine *machine)
2158{
2159 struct thread *thread;
2160 struct perf_script *script = container_of(tool, struct perf_script, tool);
2161 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002162 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002163
2164 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2165 return -1;
2166
2167 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2168 if (thread == NULL) {
2169 pr_debug("problem processing MMAP event, skipping it.\n");
2170 return -1;
2171 }
2172
2173 if (!evsel->attr.sample_id_all) {
2174 sample->cpu = 0;
2175 sample->time = 0;
2176 sample->tid = event->mmap.tid;
2177 sample->pid = event->mmap.pid;
2178 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002179 perf_sample__fprintf_start(sample, thread, evsel,
2180 PERF_RECORD_MMAP, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002181 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002182 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002183 return 0;
2184}
2185
2186static int process_mmap2_event(struct perf_tool *tool,
2187 union perf_event *event,
2188 struct perf_sample *sample,
2189 struct machine *machine)
2190{
2191 struct thread *thread;
2192 struct perf_script *script = container_of(tool, struct perf_script, tool);
2193 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002194 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002195
2196 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2197 return -1;
2198
2199 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2200 if (thread == NULL) {
2201 pr_debug("problem processing MMAP2 event, skipping it.\n");
2202 return -1;
2203 }
2204
2205 if (!evsel->attr.sample_id_all) {
2206 sample->cpu = 0;
2207 sample->time = 0;
2208 sample->tid = event->mmap2.tid;
2209 sample->pid = event->mmap2.pid;
2210 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002211 perf_sample__fprintf_start(sample, thread, evsel,
2212 PERF_RECORD_MMAP2, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002213 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002214 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002215 return 0;
2216}
2217
Adrian Hunter7c148982015-07-21 12:44:06 +03002218static int process_switch_event(struct perf_tool *tool,
2219 union perf_event *event,
2220 struct perf_sample *sample,
2221 struct machine *machine)
2222{
2223 struct thread *thread;
2224 struct perf_script *script = container_of(tool, struct perf_script, tool);
2225 struct perf_session *session = script->session;
2226 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2227
2228 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2229 return -1;
2230
2231 thread = machine__findnew_thread(machine, sample->pid,
2232 sample->tid);
2233 if (thread == NULL) {
2234 pr_debug("problem processing SWITCH event, skipping it.\n");
2235 return -1;
2236 }
2237
Jiri Olsa28a0b392018-01-07 17:03:52 +01002238 perf_sample__fprintf_start(sample, thread, evsel,
2239 PERF_RECORD_SWITCH, stdout);
Adrian Hunter7c148982015-07-21 12:44:06 +03002240 perf_event__fprintf(event, stdout);
2241 thread__put(thread);
2242 return 0;
2243}
2244
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002245static int
2246process_lost_event(struct perf_tool *tool,
2247 union perf_event *event,
2248 struct perf_sample *sample,
2249 struct machine *machine)
2250{
2251 struct perf_script *script = container_of(tool, struct perf_script, tool);
2252 struct perf_session *session = script->session;
2253 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2254 struct thread *thread;
2255
2256 thread = machine__findnew_thread(machine, sample->pid,
2257 sample->tid);
2258 if (thread == NULL)
2259 return -1;
2260
2261 perf_sample__fprintf_start(sample, thread, evsel,
2262 PERF_RECORD_LOST, stdout);
2263 perf_event__fprintf(event, stdout);
2264 thread__put(thread);
2265 return 0;
2266}
2267
Jiri Olsa3233b372018-02-06 19:17:59 +01002268static int
2269process_finished_round_event(struct perf_tool *tool __maybe_unused,
2270 union perf_event *event,
2271 struct ordered_events *oe __maybe_unused)
2272
2273{
2274 perf_event__fprintf(event, stdout);
2275 return 0;
2276}
2277
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002278static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002279{
2280 session_done = 1;
2281}
2282
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002283static void perf_script__fclose_per_event_dump(struct perf_script *script)
2284{
2285 struct perf_evlist *evlist = script->session->evlist;
2286 struct perf_evsel *evsel;
2287
2288 evlist__for_each_entry(evlist, evsel) {
2289 if (!evsel->priv)
2290 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002291 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002292 evsel->priv = NULL;
2293 }
2294}
2295
2296static int perf_script__fopen_per_event_dump(struct perf_script *script)
2297{
2298 struct perf_evsel *evsel;
2299
2300 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002301 /*
2302 * Already setup? I.e. we may be called twice in cases like
2303 * Intel PT, one for the intel_pt// and dummy events, then
2304 * for the evsels syntheized from the auxtrace info.
2305 *
2306 * Ses perf_script__process_auxtrace_info.
2307 */
2308 if (evsel->priv != NULL)
2309 continue;
2310
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002311 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002312 if (evsel->priv == NULL)
2313 goto out_err_fclose;
2314 }
2315
2316 return 0;
2317
2318out_err_fclose:
2319 perf_script__fclose_per_event_dump(script);
2320 return -1;
2321}
2322
2323static int perf_script__setup_per_event_dump(struct perf_script *script)
2324{
2325 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002326 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002327
2328 if (script->per_event_dump)
2329 return perf_script__fopen_per_event_dump(script);
2330
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002331 es_stdout.fp = stdout;
2332
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002333 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002334 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002335
2336 return 0;
2337}
2338
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002339static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2340{
2341 struct perf_evsel *evsel;
2342
2343 evlist__for_each_entry(script->session->evlist, evsel) {
2344 struct perf_evsel_script *es = evsel->priv;
2345
2346 perf_evsel_script__fprintf(es, stdout);
2347 perf_evsel_script__delete(es);
2348 evsel->priv = NULL;
2349 }
2350}
2351
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002352static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002353{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002354 int ret;
2355
Tom Zanussic239da32010-04-01 23:59:18 -05002356 signal(SIGINT, sig_handler);
2357
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002358 perf_stat__init_shadow_stats();
2359
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002360 /* override event processing functions */
2361 if (script->show_task_events) {
2362 script->tool.comm = process_comm_event;
2363 script->tool.fork = process_fork_event;
2364 script->tool.exit = process_exit_event;
2365 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002366 if (script->show_mmap_events) {
2367 script->tool.mmap = process_mmap_event;
2368 script->tool.mmap2 = process_mmap2_event;
2369 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002370 if (script->show_switch_events)
2371 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302372 if (script->show_namespace_events)
2373 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002374 if (script->show_lost_events)
2375 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002376 if (script->show_round_events) {
2377 script->tool.ordered_events = false;
2378 script->tool.finished_round = process_finished_round_event;
2379 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002380
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002381 if (perf_script__setup_per_event_dump(script)) {
2382 pr_err("Couldn't create the per event dump files\n");
2383 return -1;
2384 }
2385
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002386 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002387
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002388 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002389 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002390
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002391 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002392 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002393
2394 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002395}
2396
Tom Zanussi956ffd02009-11-25 01:15:46 -06002397struct script_spec {
2398 struct list_head node;
2399 struct scripting_ops *ops;
2400 char spec[0];
2401};
2402
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002403static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002404
2405static struct script_spec *script_spec__new(const char *spec,
2406 struct scripting_ops *ops)
2407{
2408 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2409
2410 if (s != NULL) {
2411 strcpy(s->spec, spec);
2412 s->ops = ops;
2413 }
2414
2415 return s;
2416}
2417
Tom Zanussi956ffd02009-11-25 01:15:46 -06002418static void script_spec__add(struct script_spec *s)
2419{
2420 list_add_tail(&s->node, &script_specs);
2421}
2422
2423static struct script_spec *script_spec__find(const char *spec)
2424{
2425 struct script_spec *s;
2426
2427 list_for_each_entry(s, &script_specs, node)
2428 if (strcasecmp(s->spec, spec) == 0)
2429 return s;
2430 return NULL;
2431}
2432
Tom Zanussi956ffd02009-11-25 01:15:46 -06002433int script_spec_register(const char *spec, struct scripting_ops *ops)
2434{
2435 struct script_spec *s;
2436
2437 s = script_spec__find(spec);
2438 if (s)
2439 return -1;
2440
Taeung Song8560bae2016-02-26 00:13:10 +09002441 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002442 if (!s)
2443 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002444 else
2445 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002446
2447 return 0;
2448}
2449
2450static struct scripting_ops *script_spec__lookup(const char *spec)
2451{
2452 struct script_spec *s = script_spec__find(spec);
2453 if (!s)
2454 return NULL;
2455
2456 return s->ops;
2457}
2458
2459static void list_available_languages(void)
2460{
2461 struct script_spec *s;
2462
2463 fprintf(stderr, "\n");
2464 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002465 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002466
2467 list_for_each_entry(s, &script_specs, node)
2468 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2469
2470 fprintf(stderr, "\n");
2471}
2472
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002473static int parse_scriptname(const struct option *opt __maybe_unused,
2474 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002475{
2476 char spec[PATH_MAX];
2477 const char *script, *ext;
2478 int len;
2479
Tom Zanussif526d682010-01-27 02:27:52 -06002480 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002481 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002482 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002483 }
2484
2485 script = strchr(str, ':');
2486 if (script) {
2487 len = script - str;
2488 if (len >= PATH_MAX) {
2489 fprintf(stderr, "invalid language specifier");
2490 return -1;
2491 }
2492 strncpy(spec, str, len);
2493 spec[len] = '\0';
2494 scripting_ops = script_spec__lookup(spec);
2495 if (!scripting_ops) {
2496 fprintf(stderr, "invalid language specifier");
2497 return -1;
2498 }
2499 script++;
2500 } else {
2501 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002502 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002503 if (!ext) {
2504 fprintf(stderr, "invalid script extension");
2505 return -1;
2506 }
2507 scripting_ops = script_spec__lookup(++ext);
2508 if (!scripting_ops) {
2509 fprintf(stderr, "invalid script extension");
2510 return -1;
2511 }
2512 }
2513
2514 script_name = strdup(script);
2515
2516 return 0;
2517}
2518
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002519static int parse_output_fields(const struct option *opt __maybe_unused,
2520 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002521{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002522 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002523 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002524 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002525 int rc = 0;
2526 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002527 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002528 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002529
2530 if (!str)
2531 return -ENOMEM;
2532
David Ahern2c9e45f72011-03-17 10:03:21 -06002533 /* first word can state for which event type the user is specifying
2534 * the fields. If no type exists, the specified fields apply to all
2535 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002536 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002537 tok = strchr(str, ':');
2538 if (tok) {
2539 *tok = '\0';
2540 tok++;
2541 if (!strcmp(str, "hw"))
2542 type = PERF_TYPE_HARDWARE;
2543 else if (!strcmp(str, "sw"))
2544 type = PERF_TYPE_SOFTWARE;
2545 else if (!strcmp(str, "trace"))
2546 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002547 else if (!strcmp(str, "raw"))
2548 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002549 else if (!strcmp(str, "break"))
2550 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002551 else if (!strcmp(str, "synth"))
2552 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002553 else {
2554 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002555 rc = -EINVAL;
2556 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002557 }
2558
2559 if (output[type].user_set)
2560 pr_warning("Overriding previous field request for %s events.\n",
2561 event_type(type));
2562
2563 output[type].fields = 0;
2564 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002565 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002566
2567 } else {
2568 tok = str;
2569 if (strlen(str) == 0) {
2570 fprintf(stderr,
2571 "Cannot set fields to 'none' for all event types.\n");
2572 rc = -EINVAL;
2573 goto out;
2574 }
2575
Andi Kleen36ce5652017-06-02 08:48:10 -07002576 /* Don't override defaults for +- */
2577 if (strchr(str, '+') || strchr(str, '-'))
2578 goto parse;
2579
David Ahern2c9e45f72011-03-17 10:03:21 -06002580 if (output_set_by_user())
2581 pr_warning("Overriding previous field request for all events.\n");
2582
Adrian Hunter14057202017-06-21 13:17:19 +03002583 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002584 output[j].fields = 0;
2585 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002586 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002587 }
David Ahern1424dc92011-03-09 22:23:28 -07002588 }
2589
Andi Kleen36ce5652017-06-02 08:48:10 -07002590parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002591 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002592 if (*tok == '+') {
2593 if (change == SET)
2594 goto out_badmix;
2595 change = ADD;
2596 tok++;
2597 } else if (*tok == '-') {
2598 if (change == SET)
2599 goto out_badmix;
2600 change = REMOVE;
2601 tok++;
2602 } else {
2603 if (change != SET && change != DEFAULT)
2604 goto out_badmix;
2605 change = SET;
2606 }
2607
David Ahern745f43e2011-03-09 22:23:26 -07002608 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002609 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002610 break;
David Ahern745f43e2011-03-09 22:23:26 -07002611 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002612 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002613 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002614 continue;
2615 }
David Ahern745f43e2011-03-09 22:23:26 -07002616 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002617 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002618 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002619 goto out;
2620 }
2621
2622 if (type == -1) {
2623 /* add user option to all events types for
2624 * which it is valid
2625 */
Adrian Hunter14057202017-06-21 13:17:19 +03002626 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002627 if (output[j].invalid_fields & all_output_options[i].field) {
2628 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2629 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002630 } else {
2631 if (change == REMOVE)
2632 output[j].fields &= ~all_output_options[i].field;
2633 else
2634 output[j].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002635 output[j].user_set = true;
2636 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002637 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002638 }
2639 } else {
2640 if (output[type].invalid_fields & all_output_options[i].field) {
2641 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2642 all_output_options[i].str, event_type(type));
2643
2644 rc = -EINVAL;
2645 goto out;
2646 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002647 output[type].user_set = true;
2648 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002649 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002650 }
2651
2652 if (type >= 0) {
2653 if (output[type].fields == 0) {
2654 pr_debug("No fields requested for %s type. "
2655 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002656 }
David Ahern1424dc92011-03-09 22:23:28 -07002657 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002658 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002659
Andi Kleen36ce5652017-06-02 08:48:10 -07002660out_badmix:
2661 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2662 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002663out:
David Ahern745f43e2011-03-09 22:23:26 -07002664 free(str);
2665 return rc;
2666}
2667
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002668#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2669 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2670 if ((lang_dirent->d_type == DT_DIR || \
2671 (lang_dirent->d_type == DT_UNKNOWN && \
2672 is_directory(scripts_path, lang_dirent))) && \
2673 (strcmp(lang_dirent->d_name, ".")) && \
2674 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002675
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002676#define for_each_script(lang_path, lang_dir, script_dirent) \
2677 while ((script_dirent = readdir(lang_dir)) != NULL) \
2678 if (script_dirent->d_type != DT_DIR && \
2679 (script_dirent->d_type != DT_UNKNOWN || \
2680 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002681
2682
2683#define RECORD_SUFFIX "-record"
2684#define REPORT_SUFFIX "-report"
2685
2686struct script_desc {
2687 struct list_head node;
2688 char *name;
2689 char *half_liner;
2690 char *args;
2691};
2692
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002693static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002694
2695static struct script_desc *script_desc__new(const char *name)
2696{
2697 struct script_desc *s = zalloc(sizeof(*s));
2698
Tom Zanussib5b87312010-11-10 08:16:51 -06002699 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002700 s->name = strdup(name);
2701
2702 return s;
2703}
2704
2705static void script_desc__delete(struct script_desc *s)
2706{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002707 zfree(&s->name);
2708 zfree(&s->half_liner);
2709 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002710 free(s);
2711}
2712
2713static void script_desc__add(struct script_desc *s)
2714{
2715 list_add_tail(&s->node, &script_descs);
2716}
2717
2718static struct script_desc *script_desc__find(const char *name)
2719{
2720 struct script_desc *s;
2721
2722 list_for_each_entry(s, &script_descs, node)
2723 if (strcasecmp(s->name, name) == 0)
2724 return s;
2725 return NULL;
2726}
2727
2728static struct script_desc *script_desc__findnew(const char *name)
2729{
2730 struct script_desc *s = script_desc__find(name);
2731
2732 if (s)
2733 return s;
2734
2735 s = script_desc__new(name);
2736 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002737 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002738
2739 script_desc__add(s);
2740
2741 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002742}
2743
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002744static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002745{
2746 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002747 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002748
2749 if (strlen(str) > suffix_len) {
2750 p = str + strlen(str) - suffix_len;
2751 if (!strncmp(p, suffix, suffix_len))
2752 return p;
2753 }
2754
2755 return NULL;
2756}
2757
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002758static int read_script_info(struct script_desc *desc, const char *filename)
2759{
2760 char line[BUFSIZ], *p;
2761 FILE *fp;
2762
2763 fp = fopen(filename, "r");
2764 if (!fp)
2765 return -1;
2766
2767 while (fgets(line, sizeof(line), fp)) {
2768 p = ltrim(line);
2769 if (strlen(p) == 0)
2770 continue;
2771 if (*p != '#')
2772 continue;
2773 p++;
2774 if (strlen(p) && *p == '!')
2775 continue;
2776
2777 p = ltrim(p);
2778 if (strlen(p) && p[strlen(p) - 1] == '\n')
2779 p[strlen(p) - 1] = '\0';
2780
2781 if (!strncmp(p, "description:", strlen("description:"))) {
2782 p += strlen("description:");
2783 desc->half_liner = strdup(ltrim(p));
2784 continue;
2785 }
2786
2787 if (!strncmp(p, "args:", strlen("args:"))) {
2788 p += strlen("args:");
2789 desc->args = strdup(ltrim(p));
2790 continue;
2791 }
2792 }
2793
2794 fclose(fp);
2795
2796 return 0;
2797}
2798
Robert Richter38efb532011-11-25 11:38:40 +01002799static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2800{
2801 char *script_root, *str;
2802
2803 script_root = strdup(script_dirent->d_name);
2804 if (!script_root)
2805 return NULL;
2806
2807 str = (char *)ends_with(script_root, suffix);
2808 if (!str) {
2809 free(script_root);
2810 return NULL;
2811 }
2812
2813 *str = '\0';
2814 return script_root;
2815}
2816
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002817static int list_available_scripts(const struct option *opt __maybe_unused,
2818 const char *s __maybe_unused,
2819 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002820{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002821 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002822 char scripts_path[MAXPATHLEN];
2823 DIR *scripts_dir, *lang_dir;
2824 char script_path[MAXPATHLEN];
2825 char lang_path[MAXPATHLEN];
2826 struct script_desc *desc;
2827 char first_half[BUFSIZ];
2828 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002829
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002830 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002831
2832 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002833 if (!scripts_dir) {
2834 fprintf(stdout,
2835 "open(%s) failed.\n"
2836 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2837 scripts_path);
2838 exit(-1);
2839 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002840
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002841 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002842 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2843 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -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, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002850 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002851 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002852 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2853 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002854 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002855 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002856 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002857 }
2858 }
2859
2860 fprintf(stdout, "List of available trace scripts:\n");
2861 list_for_each_entry(desc, &script_descs, node) {
2862 sprintf(first_half, "%s %s", desc->name,
2863 desc->args ? desc->args : "");
2864 fprintf(stdout, " %-36s %s\n", first_half,
2865 desc->half_liner ? desc->half_liner : "");
2866 }
2867
2868 exit(0);
2869}
2870
Feng Tange5f37052012-09-07 16:42:26 +08002871/*
Feng Tang49e639e2012-10-30 11:56:03 +08002872 * Some scripts specify the required events in their "xxx-record" file,
2873 * this function will check if the events in perf.data match those
2874 * mentioned in the "xxx-record".
2875 *
2876 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2877 * which is covered well now. And new parsing code should be added to
2878 * cover the future complexing formats like event groups etc.
2879 */
2880static int check_ev_match(char *dir_name, char *scriptname,
2881 struct perf_session *session)
2882{
2883 char filename[MAXPATHLEN], evname[128];
2884 char line[BUFSIZ], *p;
2885 struct perf_evsel *pos;
2886 int match, len;
2887 FILE *fp;
2888
Jiri Olsa77f18152018-03-19 09:29:01 +01002889 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08002890
2891 fp = fopen(filename, "r");
2892 if (!fp)
2893 return -1;
2894
2895 while (fgets(line, sizeof(line), fp)) {
2896 p = ltrim(line);
2897 if (*p == '#')
2898 continue;
2899
2900 while (strlen(p)) {
2901 p = strstr(p, "-e");
2902 if (!p)
2903 break;
2904
2905 p += 2;
2906 p = ltrim(p);
2907 len = strcspn(p, " \t");
2908 if (!len)
2909 break;
2910
2911 snprintf(evname, len + 1, "%s", p);
2912
2913 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002914 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002915 if (!strcmp(perf_evsel__name(pos), evname)) {
2916 match = 1;
2917 break;
2918 }
2919 }
2920
2921 if (!match) {
2922 fclose(fp);
2923 return -1;
2924 }
2925 }
2926 }
2927
2928 fclose(fp);
2929 return 0;
2930}
2931
2932/*
Feng Tange5f37052012-09-07 16:42:26 +08002933 * Return -1 if none is found, otherwise the actual scripts number.
2934 *
2935 * Currently the only user of this function is the script browser, which
2936 * will list all statically runnable scripts, select one, execute it and
2937 * show the output in a perf browser.
2938 */
2939int find_scripts(char **scripts_array, char **scripts_path_array)
2940{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002941 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002942 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002943 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002944 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002945 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002946 .file = {
2947 .path = input_name,
2948 },
2949 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002950 };
Feng Tange5f37052012-09-07 16:42:26 +08002951 char *temp;
2952 int i = 0;
2953
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002954 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002955 if (!session)
2956 return -1;
2957
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002958 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002959
2960 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002961 if (!scripts_dir) {
2962 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002963 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002964 }
Feng Tange5f37052012-09-07 16:42:26 +08002965
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002966 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002967 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
2968 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08002969#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08002970 if (strstr(lang_path, "perl"))
2971 continue;
2972#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08002973#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08002974 if (strstr(lang_path, "python"))
2975 continue;
2976#endif
2977
2978 lang_dir = opendir(lang_path);
2979 if (!lang_dir)
2980 continue;
2981
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002982 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002983 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002984 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002985 continue;
2986 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002987 script_dirent->d_name);
2988 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002989 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002990 (temp - script_dirent->d_name) + 1,
2991 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002992
2993 if (check_ev_match(lang_path,
2994 scripts_array[i], session))
2995 continue;
2996
Feng Tange5f37052012-09-07 16:42:26 +08002997 i++;
2998 }
Feng Tang49e639e2012-10-30 11:56:03 +08002999 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003000 }
3001
Feng Tang49e639e2012-10-30 11:56:03 +08003002 closedir(scripts_dir);
3003 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003004 return i;
3005}
3006
Tom Zanussi38752942009-12-15 02:53:39 -06003007static char *get_script_path(const char *script_root, const char *suffix)
3008{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003009 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003010 char scripts_path[MAXPATHLEN];
3011 char script_path[MAXPATHLEN];
3012 DIR *scripts_dir, *lang_dir;
3013 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003014 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003015
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003016 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003017
3018 scripts_dir = opendir(scripts_path);
3019 if (!scripts_dir)
3020 return NULL;
3021
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003022 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003023 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3024 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003025 lang_dir = opendir(lang_path);
3026 if (!lang_dir)
3027 continue;
3028
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003029 for_each_script(lang_path, lang_dir, script_dirent) {
3030 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003031 if (__script_root && !strcmp(script_root, __script_root)) {
3032 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003033 closedir(lang_dir);
3034 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003035 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3036 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003037 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003038 }
3039 free(__script_root);
3040 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003041 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003042 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003043 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003044
Robert Richter38efb532011-11-25 11:38:40 +01003045 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003046}
3047
Tom Zanussib5b87312010-11-10 08:16:51 -06003048static bool is_top_script(const char *script_path)
3049{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003050 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003051}
3052
3053static int has_required_arg(char *script_path)
3054{
3055 struct script_desc *desc;
3056 int n_args = 0;
3057 char *p;
3058
3059 desc = script_desc__new(NULL);
3060
3061 if (read_script_info(desc, script_path))
3062 goto out;
3063
3064 if (!desc->args)
3065 goto out;
3066
3067 for (p = desc->args; *p; p++)
3068 if (*p == '<')
3069 n_args++;
3070out:
3071 script_desc__delete(desc);
3072
3073 return n_args;
3074}
3075
David Ahernd54b1a92012-08-26 12:24:46 -06003076static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003077{
3078 char **__argv = malloc(sizeof(const char *) * argc);
3079
David Ahernd54b1a92012-08-26 12:24:46 -06003080 if (!__argv) {
3081 pr_err("malloc failed\n");
3082 return -1;
3083 }
3084
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003085 memcpy(__argv, argv, sizeof(const char *) * argc);
3086 argc = parse_options(argc, (const char **)__argv, record_options,
3087 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3088 free(__argv);
3089
David Ahernd54b1a92012-08-26 12:24:46 -06003090 system_wide = (argc == 0);
3091
3092 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003093}
3094
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003095static void script__setup_sample_type(struct perf_script *script)
3096{
3097 struct perf_session *session = script->session;
3098 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3099
3100 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3101 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003102 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003103 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003104 dwarf_callchain_users = true;
3105 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003106 callchain_param.record_mode = CALLCHAIN_LBR;
3107 else
3108 callchain_param.record_mode = CALLCHAIN_FP;
3109 }
3110}
3111
Jiri Olsa89f16882018-09-13 14:54:03 +02003112static int process_stat_round_event(struct perf_session *session,
3113 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003114{
3115 struct stat_round_event *round = &event->stat_round;
3116 struct perf_evsel *counter;
3117
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003118 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003119 perf_stat_process_counter(&stat_config, counter);
3120 process_stat(counter, round->time);
3121 }
3122
3123 process_stat_interval(round->time);
3124 return 0;
3125}
3126
Jiri Olsa89f16882018-09-13 14:54:03 +02003127static int process_stat_config_event(struct perf_session *session __maybe_unused,
3128 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003129{
3130 perf_event__read_stat_config(&stat_config, &event->stat_config);
3131 return 0;
3132}
3133
Jiri Olsacfc88742016-01-05 22:09:06 +01003134static int set_maps(struct perf_script *script)
3135{
3136 struct perf_evlist *evlist = script->session->evlist;
3137
3138 if (!script->cpus || !script->threads)
3139 return 0;
3140
3141 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3142 return -EINVAL;
3143
3144 perf_evlist__set_maps(evlist, script->cpus, script->threads);
3145
3146 if (perf_evlist__alloc_stats(evlist, true))
3147 return -ENOMEM;
3148
3149 script->allocated = true;
3150 return 0;
3151}
3152
3153static
Jiri Olsa89f16882018-09-13 14:54:03 +02003154int process_thread_map_event(struct perf_session *session,
3155 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003156{
Jiri Olsa89f16882018-09-13 14:54:03 +02003157 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003158 struct perf_script *script = container_of(tool, struct perf_script, tool);
3159
3160 if (script->threads) {
3161 pr_warning("Extra thread map event, ignoring.\n");
3162 return 0;
3163 }
3164
3165 script->threads = thread_map__new_event(&event->thread_map);
3166 if (!script->threads)
3167 return -ENOMEM;
3168
3169 return set_maps(script);
3170}
3171
3172static
Jiri Olsa89f16882018-09-13 14:54:03 +02003173int process_cpu_map_event(struct perf_session *session,
3174 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003175{
Jiri Olsa89f16882018-09-13 14:54:03 +02003176 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003177 struct perf_script *script = container_of(tool, struct perf_script, tool);
3178
3179 if (script->cpus) {
3180 pr_warning("Extra cpu map event, ignoring.\n");
3181 return 0;
3182 }
3183
3184 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3185 if (!script->cpus)
3186 return -ENOMEM;
3187
3188 return set_maps(script);
3189}
3190
Jiri Olsa89f16882018-09-13 14:54:03 +02003191static int process_feature_event(struct perf_session *session,
3192 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303193{
3194 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003195 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303196 return 0;
3197}
3198
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003199#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003200static int perf_script__process_auxtrace_info(struct perf_session *session,
3201 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003202{
Jiri Olsa89f16882018-09-13 14:54:03 +02003203 struct perf_tool *tool = session->tool;
3204
3205 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003206
3207 if (ret == 0) {
3208 struct perf_script *script = container_of(tool, struct perf_script, tool);
3209
3210 ret = perf_script__setup_per_event_dump(script);
3211 }
3212
3213 return ret;
3214}
3215#else
3216#define perf_script__process_auxtrace_info 0
3217#endif
3218
Andi Kleenb585ebd2018-09-20 11:05:36 -07003219static int parse_insn_trace(const struct option *opt __maybe_unused,
3220 const char *str __maybe_unused,
3221 int unset __maybe_unused)
3222{
3223 parse_output_fields(NULL, "+insn,-event,-period", 0);
3224 itrace_parse_synth_opts(opt, "i0ns", 0);
3225 nanosecs = true;
3226 return 0;
3227}
3228
3229static int parse_xed(const struct option *opt __maybe_unused,
3230 const char *str __maybe_unused,
3231 int unset __maybe_unused)
3232{
3233 force_pager("xed -F insn: -A -64 | less");
3234 return 0;
3235}
3236
Andi Kleend1b15522018-09-20 11:05:38 -07003237static int parse_call_trace(const struct option *opt __maybe_unused,
3238 const char *str __maybe_unused,
3239 int unset __maybe_unused)
3240{
3241 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3242 itrace_parse_synth_opts(opt, "cewp", 0);
3243 nanosecs = true;
3244 return 0;
3245}
3246
3247static int parse_callret_trace(const struct option *opt __maybe_unused,
3248 const char *str __maybe_unused,
3249 int unset __maybe_unused)
3250{
3251 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3252 itrace_parse_synth_opts(opt, "crewp", 0);
3253 nanosecs = true;
3254 return 0;
3255}
3256
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003257int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003258{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003259 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003260 bool header = false;
3261 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003262 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003263 char *rec_script_path = NULL;
3264 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003265 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003266 struct itrace_synth_opts itrace_synth_opts = {
3267 .set = false,
3268 .default_no_sample = true,
3269 };
Tom Zanussib5b87312010-11-10 08:16:51 -06003270 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003271 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003272 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003273 struct perf_script script = {
3274 .tool = {
3275 .sample = process_sample_event,
3276 .mmap = perf_event__process_mmap,
3277 .mmap2 = perf_event__process_mmap2,
3278 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303279 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003280 .exit = perf_event__process_exit,
3281 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003282 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003283 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003284 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303285 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003286 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003287 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003288 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003289 .auxtrace = perf_event__process_auxtrace,
3290 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003291 .stat = perf_event__process_stat_event,
3292 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003293 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003294 .thread_map = process_thread_map_event,
3295 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003296 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003297 .ordering_requires_timestamps = true,
3298 },
3299 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003300 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003301 .mode = PERF_DATA_MODE_READ,
3302 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003303 const struct option options[] = {
3304 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3305 "dump raw trace in ASCII"),
3306 OPT_INCR('v', "verbose", &verbose,
3307 "be more verbose (show symbol address, etc)"),
3308 OPT_BOOLEAN('L', "Latency", &latency_format,
3309 "show latency attributes (irqs/preemption disabled, etc)"),
3310 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3311 list_available_scripts),
3312 OPT_CALLBACK('s', "script", NULL, "name",
3313 "script file name (lang:script name, script name, or *)",
3314 parse_scriptname),
3315 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3316 "generate perf-script.xx script in specified language"),
3317 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3318 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3319 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003320 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3321 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003322 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3323 "file", "vmlinux pathname"),
3324 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3325 "file", "kallsyms pathname"),
3326 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3327 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003328 OPT_CALLBACK(0, "symfs", NULL, "directory",
3329 "Look for files with symbols relative to this directory",
3330 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003331 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003332 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003333 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003334 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003335 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303336 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3337 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
3338 "callindent,insn,insnlen,synth,phys_addr,metric,misc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003339 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003340 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3341 "system-wide collection from all CPUs"),
3342 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3343 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003344 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3345 "Decode instructions from itrace", parse_insn_trace),
3346 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3347 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003348 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3349 "Decode calls from from itrace", parse_call_trace),
3350 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3351 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003352 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3353 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003354 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3355 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003356 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3357 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3358 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003359 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3360 "only consider symbols in these pids"),
3361 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3362 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003363 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3364 "Set the maximum stack depth when parsing the callchain, "
3365 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003366 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003367 OPT_BOOLEAN('I', "show-info", &show_full_info,
3368 "display extended information from perf.data file"),
3369 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3370 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003371 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3372 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003373 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3374 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003375 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3376 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303377 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3378 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003379 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3380 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003381 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3382 "Show round events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003383 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3384 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003385 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003386 OPT_INTEGER(0, "max-blocks", &max_blocks,
3387 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03003388 OPT_BOOLEAN(0, "ns", &nanosecs,
3389 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003390 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003391 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003392 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003393 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3394 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003395 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3396 "Enable symbol demangling"),
3397 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3398 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003399 OPT_STRING(0, "time", &script.time_str, "str",
3400 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003401 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3402 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003403 OPT_END()
3404 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003405 const char * const script_subcommands[] = { "record", "report", NULL };
3406 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003407 "perf script [<options>]",
3408 "perf script [<options>] record <script> [<record-options>] <command>",
3409 "perf script [<options>] report <script> [script-args]",
3410 "perf script [<options>] <script> [<record-options>] <command>",
3411 "perf script [<options>] <top-script> [script-args]",
3412 NULL
3413 };
Tom Zanussi38752942009-12-15 02:53:39 -06003414
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003415 perf_set_singlethreaded();
3416
Tom Zanussib5b87312010-11-10 08:16:51 -06003417 setup_scripting();
3418
Yunlong Song40cae2b2015-03-18 21:35:54 +08003419 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003420 PARSE_OPT_STOP_AT_NON_OPTION);
3421
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003422 data.file.path = input_name;
3423 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003424
Tom Zanussib5b87312010-11-10 08:16:51 -06003425 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3426 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3427 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003428 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003429 }
3430
Tom Zanussib5b87312010-11-10 08:16:51 -06003431 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3432 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3433 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003434 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003435 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003436 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003437 return -1;
3438 }
Tom Zanussi38752942009-12-15 02:53:39 -06003439 }
3440
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003441 if (itrace_synth_opts.callchain &&
3442 itrace_synth_opts.callchain_sz > scripting_max_stack)
3443 scripting_max_stack = itrace_synth_opts.callchain_sz;
3444
Ben Hutchings44e668c2010-10-10 16:10:03 +01003445 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003446 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003447
Tom Zanussib5b87312010-11-10 08:16:51 -06003448 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003449 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003450 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003451 pid_t pid;
3452
Tom Zanussib5b87312010-11-10 08:16:51 -06003453 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3454 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3455
3456 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003457 usage_with_options_msg(script_usage, options,
3458 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003459 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003460 }
3461
Tom Zanussib5b87312010-11-10 08:16:51 -06003462 if (is_top_script(argv[0])) {
3463 rep_args = argc - 1;
3464 } else {
3465 int rec_args;
3466
3467 rep_args = has_required_arg(rep_script_path);
3468 rec_args = (argc - 1) - rep_args;
3469 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003470 usage_with_options_msg(script_usage, options,
3471 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003472 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003473 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003474 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003475 }
3476
3477 if (pipe(live_pipe) < 0) {
3478 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003479 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003480 }
3481
3482 pid = fork();
3483 if (pid < 0) {
3484 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003485 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003486 }
3487
3488 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003489 j = 0;
3490
Tom Zanussia0cccc22010-04-01 23:59:25 -05003491 dup2(live_pipe[1], 1);
3492 close(live_pipe[0]);
3493
Robert Richter317df652011-11-25 15:05:25 +01003494 if (is_top_script(argv[0])) {
3495 system_wide = true;
3496 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003497 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3498 err = -1;
3499 goto out;
3500 }
Robert Richter317df652011-11-25 15:05:25 +01003501 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003502
3503 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003504 if (!__argv) {
3505 pr_err("malloc failed\n");
3506 err = -ENOMEM;
3507 goto out;
3508 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003509
Tom Zanussib5b87312010-11-10 08:16:51 -06003510 __argv[j++] = "/bin/sh";
3511 __argv[j++] = rec_script_path;
3512 if (system_wide)
3513 __argv[j++] = "-a";
3514 __argv[j++] = "-q";
3515 __argv[j++] = "-o";
3516 __argv[j++] = "-";
3517 for (i = rep_args + 1; i < argc; i++)
3518 __argv[j++] = argv[i];
3519 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003520
3521 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003522 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003523 exit(-1);
3524 }
3525
3526 dup2(live_pipe[0], 0);
3527 close(live_pipe[1]);
3528
Tom Zanussib5b87312010-11-10 08:16:51 -06003529 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003530 if (!__argv) {
3531 pr_err("malloc failed\n");
3532 err = -ENOMEM;
3533 goto out;
3534 }
3535
Tom Zanussib5b87312010-11-10 08:16:51 -06003536 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003537 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003538 __argv[j++] = rep_script_path;
3539 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003540 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003541 __argv[j++] = "-i";
3542 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003543 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003544
3545 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003546 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003547 exit(-1);
3548 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003549
Tom Zanussib5b87312010-11-10 08:16:51 -06003550 if (rec_script_path)
3551 script_path = rec_script_path;
3552 if (rep_script_path)
3553 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003554
Tom Zanussib5b87312010-11-10 08:16:51 -06003555 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003556 j = 0;
3557
Robert Richter317df652011-11-25 15:05:25 +01003558 if (!rec_script_path)
3559 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003560 else if (!system_wide) {
3561 if (have_cmd(argc - 1, &argv[1]) != 0) {
3562 err = -1;
3563 goto out;
3564 }
3565 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003566
3567 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003568 if (!__argv) {
3569 pr_err("malloc failed\n");
3570 err = -ENOMEM;
3571 goto out;
3572 }
3573
Tom Zanussib5b87312010-11-10 08:16:51 -06003574 __argv[j++] = "/bin/sh";
3575 __argv[j++] = script_path;
3576 if (system_wide)
3577 __argv[j++] = "-a";
3578 for (i = 2; i < argc; i++)
3579 __argv[j++] = argv[i];
3580 __argv[j++] = NULL;
3581
3582 execvp("/bin/sh", (char **)__argv);
3583 free(__argv);
3584 exit(-1);
3585 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003586
Milian Wolffc1c9b962018-10-21 21:14:23 +02003587 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003588 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003589 use_browser = 0;
3590 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003591
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003592 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003593 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003594 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003595
Jiri Olsae90debd2013-12-09 11:02:50 +01003596 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003597 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003598 perf_session__fprintf_info(session, stdout, show_full_info);
3599 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003600 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003601 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003602 if (show_full_info)
3603 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003604
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003605 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003606 goto out_delete;
3607
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003608 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003609 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003610
Andi Kleen99f753f2018-09-20 11:05:39 -07003611 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3612 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003613 itrace_synth_opts.thread_stack = true;
3614
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003615 session->itrace_synth_opts = &itrace_synth_opts;
3616
Anton Blanchard5d67be92011-07-04 21:57:50 +10003617 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003618 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3619 if (err < 0)
3620 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003621 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003622 }
3623
David Ahern1424dc92011-03-09 22:23:28 -07003624 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003625 symbol_conf.use_callchain = true;
3626 else
3627 symbol_conf.use_callchain = false;
3628
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003629 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003630 tep_set_function_resolver(session->tevent.pevent,
3631 machine__resolve_kernel_addr,
3632 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003633 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003634 err = -1;
3635 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003636 }
3637
Tom Zanussi956ffd02009-11-25 01:15:46 -06003638 if (generate_script_lang) {
3639 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003640 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003641
David Ahern2c9e45f72011-03-17 10:03:21 -06003642 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003643 fprintf(stderr,
3644 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003645 err = -EINVAL;
3646 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003647 }
3648
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003649 input = open(data.file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003650 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003651 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003652 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003653 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003654 }
3655
3656 err = fstat(input, &perf_stat);
3657 if (err < 0) {
3658 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003659 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003660 }
3661
3662 if (!perf_stat.st_size) {
3663 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003664 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003665 }
3666
3667 scripting_ops = script_spec__lookup(generate_script_lang);
3668 if (!scripting_ops) {
3669 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003670 err = -ENOENT;
3671 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003672 }
3673
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003674 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003675 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003676 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003677 }
3678
3679 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003680 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003681 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003682 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003683 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003684 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003685 }
3686
David Ahern9cbdb702011-04-06 21:54:20 -06003687
3688 err = perf_session__check_output_opt(session);
3689 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003690 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003691
Jin Yaocc2ef582018-01-10 23:00:33 +08003692 script.ptime_range = perf_time__range_alloc(script.time_str,
3693 &script.range_size);
3694 if (!script.ptime_range) {
3695 err = -ENOMEM;
3696 goto out_delete;
3697 }
3698
David Aherna91f4c42016-11-29 10:15:43 -07003699 /* needs to be parsed after looking up reference time */
Jin Yao2ab046c2017-12-08 21:13:46 +08003700 if (perf_time__parse_str(script.ptime_range, script.time_str) != 0) {
3701 if (session->evlist->first_sample_time == 0 &&
3702 session->evlist->last_sample_time == 0) {
Jin Yao1e2778e2018-01-10 23:00:27 +08003703 pr_err("HINT: no first/last sample time found in perf data.\n"
3704 "Please use latest perf binary to execute 'perf record'\n"
3705 "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
Jin Yao2ab046c2017-12-08 21:13:46 +08003706 err = -EINVAL;
3707 goto out_delete;
3708 }
3709
3710 script.range_num = perf_time__percent_parse_str(
Jin Yaocc2ef582018-01-10 23:00:33 +08003711 script.ptime_range, script.range_size,
Jin Yao2ab046c2017-12-08 21:13:46 +08003712 script.time_str,
3713 session->evlist->first_sample_time,
3714 session->evlist->last_sample_time);
3715
3716 if (script.range_num < 0) {
3717 pr_err("Invalid time string\n");
3718 err = -EINVAL;
3719 goto out_delete;
3720 }
3721 } else {
3722 script.range_num = 1;
David Aherna91f4c42016-11-29 10:15:43 -07003723 }
3724
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003725 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003726
Adrian Hunterd445dd22014-08-15 22:08:37 +03003727 flush_scripting();
3728
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003729out_delete:
Jin Yaocc2ef582018-01-10 23:00:33 +08003730 zfree(&script.ptime_range);
3731
Jiri Olsacfc88742016-01-05 22:09:06 +01003732 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003733 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003734
3735 if (script_started)
3736 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003737out:
3738 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003739}