blob: 7adaa6c63a0bf1094fe7f6ad02f778d190095c94 [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"
Andi Kleen3ab481a2019-03-05 06:47:45 -080032#include "archinsn.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100033#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030034#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030035#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030036#include <linux/time64.h>
Andi Kleen3ab481a2019-03-05 06:47:45 -080037#include <sys/utsname.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010038#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010039#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080040#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030041#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030042#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030043#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030044#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030045#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030046#include <sys/types.h>
47#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030048#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030049#include <unistd.h>
Andi Kleenb585ebd2018-09-20 11:05:36 -070050#include <subcmd/pager.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020051
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030052#include "sane_ctype.h"
53
Tom Zanussi956ffd02009-11-25 01:15:46 -060054static char const *script_name;
55static char const *generate_script_lang;
Andi Kleen90b10f42019-03-14 15:50:00 -070056static bool reltime;
57static u64 initial_time;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020058static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020059static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020060static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070061static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090062static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010063static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030064static bool print_flags;
Anton Blanchard5d67be92011-07-04 21:57:50 +100065static const char *cpu_list;
66static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010067static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080068static int max_blocks;
Andi Kleen3ab481a2019-03-05 06:47:45 -080069static bool native_arch;
Tom Zanussi956ffd02009-11-25 01:15:46 -060070
Adrian Hunter44cbe722015-09-25 16:15:50 +030071unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030072
David Ahern745f43e2011-03-09 22:23:26 -070073enum perf_output_field {
74 PERF_OUTPUT_COMM = 1U << 0,
75 PERF_OUTPUT_TID = 1U << 1,
76 PERF_OUTPUT_PID = 1U << 2,
77 PERF_OUTPUT_TIME = 1U << 3,
78 PERF_OUTPUT_CPU = 1U << 4,
79 PERF_OUTPUT_EVNAME = 1U << 5,
80 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060081 PERF_OUTPUT_IP = 1U << 7,
82 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060083 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060084 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090085 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020086 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020087 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020088 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020089 PERF_OUTPUT_BRSTACK = 1U << 15,
90 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010091 PERF_OUTPUT_DATA_SRC = 1U << 17,
92 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000093 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030094 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030095 PERF_OUTPUT_INSN = 1U << 21,
96 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080097 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070098 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030099 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -0400100 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -0700101 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800102 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100103 PERF_OUTPUT_MISC = 1U << 29,
Andi Kleendd2e18e2018-12-03 16:18:48 -0800104 PERF_OUTPUT_SRCCODE = 1U << 30,
David Ahern745f43e2011-03-09 22:23:26 -0700105};
106
107struct output_option {
108 const char *str;
109 enum perf_output_field field;
110} all_output_options[] = {
111 {.str = "comm", .field = PERF_OUTPUT_COMM},
112 {.str = "tid", .field = PERF_OUTPUT_TID},
113 {.str = "pid", .field = PERF_OUTPUT_PID},
114 {.str = "time", .field = PERF_OUTPUT_TIME},
115 {.str = "cpu", .field = PERF_OUTPUT_CPU},
116 {.str = "event", .field = PERF_OUTPUT_EVNAME},
117 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600118 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700119 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600120 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600121 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900122 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200123 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200124 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200125 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700126 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200127 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
128 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100129 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
130 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000131 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300132 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300133 {.str = "insn", .field = PERF_OUTPUT_INSN},
134 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800135 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700136 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300137 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400138 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800139 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100140 {.str = "misc", .field = PERF_OUTPUT_MISC},
Andi Kleendd2e18e2018-12-03 16:18:48 -0800141 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
David Ahern745f43e2011-03-09 22:23:26 -0700142};
143
Adrian Hunter14057202017-06-21 13:17:19 +0300144enum {
145 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
146 OUTPUT_TYPE_MAX
147};
148
David Ahern745f43e2011-03-09 22:23:26 -0700149/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600150static struct {
151 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600152 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400153 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600154 u64 fields;
155 u64 invalid_fields;
Andi Kleen4b6ac812019-02-24 07:37:12 -0800156 u64 user_set_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300157} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700158
David Ahern2c9e45f72011-03-17 10:03:21 -0600159 [PERF_TYPE_HARDWARE] = {
160 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700161
David Ahern2c9e45f72011-03-17 10:03:21 -0600162 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
163 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600164 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530165 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
166 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600167
Wang Nan30372f02016-02-24 11:20:45 +0000168 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600169 },
170
171 [PERF_TYPE_SOFTWARE] = {
172 .user_set = false,
173
174 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
175 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600176 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530177 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
178 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
179 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600180
181 .invalid_fields = PERF_OUTPUT_TRACE,
182 },
183
184 [PERF_TYPE_TRACEPOINT] = {
185 .user_set = false,
186
187 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
188 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000189 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600190 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700191
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530192 [PERF_TYPE_HW_CACHE] = {
193 .user_set = false,
194
195 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
196 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
197 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
198 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
199 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
200
201 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
202 },
203
Arun Sharma0817a6a2011-04-14 10:38:18 -0700204 [PERF_TYPE_RAW] = {
205 .user_set = false,
206
207 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
208 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600209 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530210 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
211 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
212 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
213 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700214
Wang Nan30372f02016-02-24 11:20:45 +0000215 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700216 },
Wang Nan27cfef02015-12-08 02:25:43 +0000217
218 [PERF_TYPE_BREAKPOINT] = {
219 .user_set = false,
220
221 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
222 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
223 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530224 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
225 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000226
Wang Nan30372f02016-02-24 11:20:45 +0000227 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000228 },
Adrian Hunter14057202017-06-21 13:17:19 +0300229
230 [OUTPUT_TYPE_SYNTH] = {
231 .user_set = false,
232
233 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
234 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
235 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530236 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
237 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300238
239 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
240 },
David Ahern1424dc92011-03-09 22:23:28 -0700241};
David Ahern745f43e2011-03-09 22:23:26 -0700242
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300243struct perf_evsel_script {
244 char *filename;
245 FILE *fp;
246 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800247 /* For metric output */
248 u64 val;
249 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300250};
251
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800252static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
253{
254 return (struct perf_evsel_script *)evsel->priv;
255}
256
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300257static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100258 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300259{
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800260 struct perf_evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300261
262 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100263 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300264 goto out_free;
265 es->fp = fopen(es->filename, "w");
266 if (es->fp == NULL)
267 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300268 }
269
270 return es;
271out_free_filename:
272 zfree(&es->filename);
273out_free:
274 free(es);
275 return NULL;
276}
277
278static void perf_evsel_script__delete(struct perf_evsel_script *es)
279{
280 zfree(&es->filename);
281 fclose(es->fp);
282 es->fp = NULL;
283 free(es);
284}
285
286static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
287{
288 struct stat st;
289
290 fstat(fileno(es->fp), &st);
291 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
292 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
293}
294
Adrian Hunter14057202017-06-21 13:17:19 +0300295static inline int output_type(unsigned int type)
296{
297 switch (type) {
298 case PERF_TYPE_SYNTH:
299 return OUTPUT_TYPE_SYNTH;
300 default:
301 return type;
302 }
303}
304
305static inline unsigned int attr_type(unsigned int type)
306{
307 switch (type) {
308 case OUTPUT_TYPE_SYNTH:
309 return PERF_TYPE_SYNTH;
310 default:
311 return type;
312 }
313}
314
David Ahern2c9e45f72011-03-17 10:03:21 -0600315static bool output_set_by_user(void)
316{
317 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300318 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600319 if (output[j].user_set)
320 return true;
321 }
322 return false;
323}
David Ahern745f43e2011-03-09 22:23:26 -0700324
David Ahern9cbdb702011-04-06 21:54:20 -0600325static const char *output_field2str(enum perf_output_field field)
326{
327 int i, imax = ARRAY_SIZE(all_output_options);
328 const char *str = "";
329
330 for (i = 0; i < imax; ++i) {
331 if (all_output_options[i].field == field) {
332 str = all_output_options[i].str;
333 break;
334 }
335 }
336 return str;
337}
338
Adrian Hunter14057202017-06-21 13:17:19 +0300339#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700340
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300341static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
342 u64 sample_type, const char *sample_msg,
343 enum perf_output_field field,
344 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700345{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300346 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300347 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600348 const char *evname;
349
350 if (attr->sample_type & sample_type)
351 return 0;
352
Andi Kleen4b6ac812019-02-24 07:37:12 -0800353 if (output[type].user_set_fields & field) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300354 if (allow_user_set)
355 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300356 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600357 pr_err("Samples for '%s' event do not have %s attribute set. "
358 "Cannot print '%s' field.\n",
359 evname, sample_msg, output_field2str(field));
360 return -1;
361 }
362
363 /* user did not ask for it explicitly so remove from the default list */
364 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300365 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600366 pr_debug("Samples for '%s' event do not have %s attribute set. "
367 "Skipping '%s' field.\n",
368 evname, sample_msg, output_field2str(field));
369
370 return 0;
371}
372
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300373static int perf_evsel__check_stype(struct perf_evsel *evsel,
374 u64 sample_type, const char *sample_msg,
375 enum perf_output_field field)
376{
377 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
378 false);
379}
380
David Ahern9cbdb702011-04-06 21:54:20 -0600381static int perf_evsel__check_attr(struct perf_evsel *evsel,
382 struct perf_session *session)
383{
384 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300385 bool allow_user_set;
386
Jiri Olsae099eba2016-01-05 22:09:09 +0100387 if (perf_header__has_feat(&session->header, HEADER_STAT))
388 return 0;
389
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300390 allow_user_set = perf_header__has_feat(&session->header,
391 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600392
David Ahern1424dc92011-03-09 22:23:28 -0700393 if (PRINT_FIELD(TRACE) &&
394 !perf_session__has_traces(session, "record -R"))
395 return -EINVAL;
396
David Ahern787bef12011-05-27 14:28:43 -0600397 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300398 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
399 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700400 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700401 }
David Ahern7cec0922011-05-30 13:08:23 -0600402
403 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300404 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
405 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600406 return -EINVAL;
407
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100408 if (PRINT_FIELD(DATA_SRC) &&
409 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
410 PERF_OUTPUT_DATA_SRC))
411 return -EINVAL;
412
413 if (PRINT_FIELD(WEIGHT) &&
414 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
415 PERF_OUTPUT_WEIGHT))
416 return -EINVAL;
417
Andi Kleen37fed3d2018-09-18 05:32:09 -0700418 if (PRINT_FIELD(SYM) &&
419 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600420 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700421 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600422 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600423 return -EINVAL;
424 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900425 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
426 pr_err("Display of offsets requested but symbol is not"
427 "selected.\n");
428 return -EINVAL;
429 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700430 if (PRINT_FIELD(DSO) &&
431 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
432 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600433 return -EINVAL;
434 }
Andi Kleendd2e18e2018-12-03 16:18:48 -0800435 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
Adrian Huntercc8fae12013-12-06 09:42:57 +0200436 pr_err("Display of source line number requested but sample IP is not\n"
437 "selected. Hence, no address to lookup the source line number.\n");
438 return -EINVAL;
439 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800440 if (PRINT_FIELD(BRSTACKINSN) &&
441 !(perf_evlist__combined_branch_type(session->evlist) &
442 PERF_SAMPLE_BRANCH_ANY)) {
443 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
444 "Hint: run 'perf record -b ...'\n");
445 return -EINVAL;
446 }
David Ahern1424dc92011-03-09 22:23:28 -0700447 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300448 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
449 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700450 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700451
452 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300453 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
454 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700455 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700456
457 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300458 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
459 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700460 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600461
Stephane Eranianfc36f942015-08-31 18:41:10 +0200462 if (PRINT_FIELD(IREGS) &&
463 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
464 PERF_OUTPUT_IREGS))
465 return -EINVAL;
466
Andi Kleenb1491ac2017-09-05 11:40:57 -0700467 if (PRINT_FIELD(UREGS) &&
468 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
469 PERF_OUTPUT_UREGS))
470 return -EINVAL;
471
Kan Liang49d58f02017-08-29 13:11:11 -0400472 if (PRINT_FIELD(PHYS_ADDR) &&
473 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
474 PERF_OUTPUT_PHYS_ADDR))
475 return -EINVAL;
476
David Ahern9cbdb702011-04-06 21:54:20 -0600477 return 0;
478}
479
Adrian Hunter7ea95722013-11-01 15:51:30 +0200480static void set_print_ip_opts(struct perf_event_attr *attr)
481{
Adrian Hunter14057202017-06-21 13:17:19 +0300482 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200483
484 output[type].print_ip_opts = 0;
485 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300486 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200487
488 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300489 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200490
491 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300492 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200493
494 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300495 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200496
497 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300498 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200499}
500
David Ahern9cbdb702011-04-06 21:54:20 -0600501/*
502 * verify all user requested events exist and the samples
503 * have the expected data
504 */
505static int perf_session__check_output_opt(struct perf_session *session)
506{
He Kuang40f20e52016-05-16 04:51:19 +0000507 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600508 struct perf_evsel *evsel;
509
Adrian Hunter14057202017-06-21 13:17:19 +0300510 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
511 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600512
513 /*
514 * even if fields is set to 0 (ie., show nothing) event must
515 * exist if user explicitly includes it on the command line
516 */
Adrian Hunter14057202017-06-21 13:17:19 +0300517 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
518 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600519 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300520 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600521 event_type(j));
522 return -1;
523 }
524
525 if (evsel && output[j].fields &&
526 perf_evsel__check_attr(evsel, session))
527 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400528
529 if (evsel == NULL)
530 continue;
531
Adrian Hunter7ea95722013-11-01 15:51:30 +0200532 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700533 }
534
Adrian Hunter98526ee2014-07-31 09:00:59 +0300535 if (!no_callchain) {
536 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000537 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300538
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300539 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000540 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300541 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300542 use_callchain = true;
543 break;
544 }
545 }
He Kuang71ac8992016-08-04 11:25:43 +0000546 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300547 symbol_conf.use_callchain = false;
548 }
549
David Ahern80b8b492013-11-19 21:07:37 -0700550 /*
551 * set default for tracepoints to print symbols only
552 * if callchains are present
553 */
554 if (symbol_conf.use_callchain &&
555 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700556 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700557
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300558 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000559 if (evsel->attr.type != j)
560 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700561
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300562 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000563 output[j].fields |= PERF_OUTPUT_IP;
564 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530565 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000566 output[j].fields |= PERF_OUTPUT_DSO;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300567 set_print_ip_opts(&evsel->attr);
He Kuang40f20e52016-05-16 04:51:19 +0000568 goto out;
569 }
David Ahern80b8b492013-11-19 21:07:37 -0700570 }
571 }
572
573out:
David Ahern1424dc92011-03-09 22:23:28 -0700574 return 0;
575}
David Ahern745f43e2011-03-09 22:23:26 -0700576
Milian Wolff9add8fe2018-11-07 23:34:37 +0100577static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
578 FILE *fp
579)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200580{
Andi Kleenb1491ac2017-09-05 11:40:57 -0700581 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300582 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700583
584 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300585 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700586
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300587 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700588
589 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
590 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300591 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700592 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300593
Milian Wolffb07d16f2018-11-07 10:37:05 +0100594 fprintf(fp, "\n");
595
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300596 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700597}
598
Milian Wolff9add8fe2018-11-07 23:34:37 +0100599static int perf_sample__fprintf_iregs(struct perf_sample *sample,
600 struct perf_event_attr *attr, FILE *fp)
601{
602 return perf_sample__fprintf_regs(&sample->intr_regs,
603 attr->sample_regs_intr, fp);
604}
605
606static int perf_sample__fprintf_uregs(struct perf_sample *sample,
607 struct perf_event_attr *attr, FILE *fp)
608{
609 return perf_sample__fprintf_regs(&sample->user_regs,
610 attr->sample_regs_user, fp);
611}
612
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300613static int perf_sample__fprintf_start(struct perf_sample *sample,
614 struct thread *thread,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100615 struct perf_evsel *evsel,
616 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700617{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300618 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700619 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700620 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300621 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700622
David Ahern745f43e2011-03-09 22:23:26 -0700623 if (PRINT_FIELD(COMM)) {
624 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300625 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300626 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300627 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700628 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300629 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700630 }
David Ahernc70c94b2011-03-09 22:23:25 -0700631
David Ahern745f43e2011-03-09 22:23:26 -0700632 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300633 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700634 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300635 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700636 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300637 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700638
David Ahern745f43e2011-03-09 22:23:26 -0700639 if (PRINT_FIELD(CPU)) {
640 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300641 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700642 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300643 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700644 }
David Ahernc70c94b2011-03-09 22:23:25 -0700645
Jiri Olsa28a0b392018-01-07 17:03:52 +0100646 if (PRINT_FIELD(MISC)) {
647 int ret = 0;
648
649 #define has(m) \
650 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
651
652 if (has(KERNEL))
653 ret += fprintf(fp, "K");
654 if (has(USER))
655 ret += fprintf(fp, "U");
656 if (has(HYPERVISOR))
657 ret += fprintf(fp, "H");
658 if (has(GUEST_KERNEL))
659 ret += fprintf(fp, "G");
660 if (has(GUEST_USER))
661 ret += fprintf(fp, "g");
662
663 switch (type) {
664 case PERF_RECORD_MMAP:
665 case PERF_RECORD_MMAP2:
666 if (has(MMAP_DATA))
667 ret += fprintf(fp, "M");
668 break;
669 case PERF_RECORD_COMM:
670 if (has(COMM_EXEC))
671 ret += fprintf(fp, "E");
672 break;
673 case PERF_RECORD_SWITCH:
674 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300675 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100676 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300677 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
678 ret += fprintf(fp, "p");
679 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100680 default:
681 break;
682 }
683
684 #undef has
685
686 ret += fprintf(fp, "%*s", 6 - ret, " ");
687 printed += ret;
688 }
689
David Ahern745f43e2011-03-09 22:23:26 -0700690 if (PRINT_FIELD(TIME)) {
Andi Kleen90b10f42019-03-14 15:50:00 -0700691 u64 t = sample->time;
692 if (reltime) {
693 if (!initial_time)
694 initial_time = sample->time;
695 t = sample->time - initial_time;
696 }
697 nsecs = t;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300698 secs = nsecs / NSEC_PER_SEC;
699 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900700
Andi Kleen52bab882019-03-05 06:47:47 -0800701 if (symbol_conf.nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300702 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900703 else {
704 char sample_time[32];
Andi Kleen90b10f42019-03-14 15:50:00 -0700705 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300706 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900707 }
David Ahern745f43e2011-03-09 22:23:26 -0700708 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300709
710 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700711}
712
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200713static inline char
714mispred_str(struct branch_entry *br)
715{
716 if (!(br->flags.mispred || br->flags.predicted))
717 return '-';
718
719 return br->flags.predicted ? 'P' : 'M';
720}
721
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300722static int perf_sample__fprintf_brstack(struct perf_sample *sample,
723 struct thread *thread,
724 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200725{
726 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700727 struct addr_location alf, alt;
728 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300729 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200730
731 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300732 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200733
734 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700735 from = br->entries[i].from;
736 to = br->entries[i].to;
737
738 if (PRINT_FIELD(DSO)) {
739 memset(&alf, 0, sizeof(alf));
740 memset(&alt, 0, sizeof(alt));
Adrian Hunter692d0e62018-11-06 23:07:12 +0200741 thread__find_map_fb(thread, sample->cpumode, from, &alf);
742 thread__find_map_fb(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700743 }
744
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300745 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700746 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300747 printed += fprintf(fp, "(");
748 printed += map__fprintf_dsoname(alf.map, fp);
749 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700750 }
751
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300752 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700753 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300754 printed += fprintf(fp, "(");
755 printed += map__fprintf_dsoname(alt.map, fp);
756 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700757 }
758
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300759 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200760 mispred_str( br->entries + i),
761 br->entries[i].flags.in_tx? 'X' : '-',
762 br->entries[i].flags.abort? 'A' : '-',
763 br->entries[i].flags.cycles);
764 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300765
766 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200767}
768
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300769static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
770 struct thread *thread,
771 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200772{
773 struct branch_stack *br = sample->branch_stack;
774 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200775 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300776 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200777
778 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300779 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200780
781 for (i = 0; i < br->nr; i++) {
782
783 memset(&alf, 0, sizeof(alf));
784 memset(&alt, 0, sizeof(alt));
785 from = br->entries[i].from;
786 to = br->entries[i].to;
787
Adrian Hunter692d0e62018-11-06 23:07:12 +0200788 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
789 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200790
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300791 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700792 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300793 printed += fprintf(fp, "(");
794 printed += map__fprintf_dsoname(alf.map, fp);
795 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700796 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300797 printed += fprintf(fp, "%c", '/');
798 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700799 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300800 printed += fprintf(fp, "(");
801 printed += map__fprintf_dsoname(alt.map, fp);
802 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700803 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300804 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200805 mispred_str( br->entries + i),
806 br->entries[i].flags.in_tx? 'X' : '-',
807 br->entries[i].flags.abort? 'A' : '-',
808 br->entries[i].flags.cycles);
809 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300810
811 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200812}
813
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300814static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
815 struct thread *thread,
816 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700817{
818 struct branch_stack *br = sample->branch_stack;
819 struct addr_location alf, alt;
820 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300821 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700822
823 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300824 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700825
826 for (i = 0; i < br->nr; i++) {
827
828 memset(&alf, 0, sizeof(alf));
829 memset(&alt, 0, sizeof(alt));
830 from = br->entries[i].from;
831 to = br->entries[i].to;
832
Adrian Hunter692d0e62018-11-06 23:07:12 +0200833 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300834 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700835 from = map__map_ip(alf.map, from);
836
Adrian Hunter692d0e62018-11-06 23:07:12 +0200837 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300838 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700839 to = map__map_ip(alt.map, to);
840
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300841 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700842 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300843 printed += fprintf(fp, "(");
844 printed += map__fprintf_dsoname(alf.map, fp);
845 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700846 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300847 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700848 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300849 printed += fprintf(fp, "(");
850 printed += map__fprintf_dsoname(alt.map, fp);
851 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700852 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300853 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700854 mispred_str(br->entries + i),
855 br->entries[i].flags.in_tx ? 'X' : '-',
856 br->entries[i].flags.abort ? 'A' : '-',
857 br->entries[i].flags.cycles);
858 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300859
860 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700861}
Andi Kleen48d02a12017-02-23 15:46:34 -0800862#define MAXBB 16384UL
863
864static int grab_bb(u8 *buffer, u64 start, u64 end,
865 struct machine *machine, struct thread *thread,
866 bool *is64bit, u8 *cpumode, bool last)
867{
868 long offset, len;
869 struct addr_location al;
870 bool kernel;
871
872 if (!start || !end)
873 return 0;
874
875 kernel = machine__kernel_ip(machine, start);
876 if (kernel)
877 *cpumode = PERF_RECORD_MISC_KERNEL;
878 else
879 *cpumode = PERF_RECORD_MISC_USER;
880
881 /*
882 * Block overlaps between kernel and user.
883 * This can happen due to ring filtering
884 * On Intel CPUs the entry into the kernel is filtered,
885 * but the exit is not. Let the caller patch it up.
886 */
887 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300888 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800889 return -ENXIO;
890 }
891
892 memset(&al, 0, sizeof(al));
893 if (end - start > MAXBB - MAXINSN) {
894 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300895 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800896 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300897 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800898 return 0;
899 }
900
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300901 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300902 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800903 return 0;
904 }
905 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300906 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800907 return 0;
908 }
909
910 /* Load maps to ensure dso->is_64_bit has been updated */
911 map__load(al.map);
912
913 offset = al.map->map_ip(al.map, start);
914 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
915 end - start + MAXINSN);
916
917 *is64bit = al.map->dso->is_64_bit;
918 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300919 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800920 start, end);
921 return len;
922}
923
Andi Kleendd2e18e2018-12-03 16:18:48 -0800924static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
925{
926 struct addr_location al;
927 int ret = 0;
928
929 memset(&al, 0, sizeof(al));
930 thread__find_map(thread, cpumode, addr, &al);
931 if (!al.map)
932 return 0;
933 ret = map__fprintf_srccode(al.map, al.addr, stdout,
934 &thread->srccode_state);
935 if (ret)
936 ret += printf("\n");
937 return ret;
938}
939
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300940static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
941 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -0700942 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -0800943{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300944 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
945 dump_insn(x, ip, inbuf, len, NULL),
946 en->flags.predicted ? " PRED" : "",
947 en->flags.mispred ? " MISPRED" : "",
948 en->flags.in_tx ? " INTX" : "",
949 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800950 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -0700951 *total_cycles += en->flags.cycles;
952 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800953 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300954 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800955 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300956 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800957}
958
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300959static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
960 u8 cpumode, int cpu, struct symbol **lastsym,
961 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800962{
963 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300964 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800965
966 memset(&al, 0, sizeof(al));
967
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300968 thread__find_map(thread, cpumode, addr, &al);
969
Andi Kleen48d02a12017-02-23 15:46:34 -0800970 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300971 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800972
973 al.cpu = cpu;
974 al.sym = NULL;
975 if (al.map)
976 al.sym = map__find_symbol(al.map, al.addr);
977
978 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300979 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800980
981 if (al.addr < al.sym->end)
982 off = al.addr - al.sym->start;
983 else
984 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300985 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800986 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300987 printed += fprintf(fp, "%+d", off);
988 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800989 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300990 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
991 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800992 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300993
994 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800995}
996
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300997static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
998 struct thread *thread,
999 struct perf_event_attr *attr,
1000 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -08001001{
1002 struct branch_stack *br = sample->branch_stack;
1003 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001004 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001005 struct perf_insn x;
1006 u8 buffer[MAXBB];
1007 unsigned off;
1008 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -07001009 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001010
1011 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001012 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001013 nr = br->nr;
1014 if (max_blocks && nr > max_blocks + 1)
1015 nr = max_blocks + 1;
1016
1017 x.thread = thread;
1018 x.cpu = sample->cpu;
1019
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001020 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -08001021
1022 /* Handle first from jump, of which we don't know the entry. */
1023 len = grab_bb(buffer, br->entries[nr-1].from,
1024 br->entries[nr-1].from,
1025 machine, thread, &x.is64bit, &x.cpumode, false);
1026 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001027 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1028 x.cpumode, x.cpu, &lastsym, attr, fp);
1029 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001030 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001031 if (PRINT_FIELD(SRCCODE))
1032 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
Andi Kleen48d02a12017-02-23 15:46:34 -08001033 }
1034
1035 /* Print all blocks */
1036 for (i = nr - 2; i >= 0; i--) {
1037 if (br->entries[i].from || br->entries[i].to)
1038 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1039 br->entries[i].from,
1040 br->entries[i].to);
1041 start = br->entries[i + 1].to;
1042 end = br->entries[i].from;
1043
1044 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1045 /* Patch up missing kernel transfers due to ring filters */
1046 if (len == -ENXIO && i > 0) {
1047 end = br->entries[--i].from;
1048 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1049 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1050 }
1051 if (len <= 0)
1052 continue;
1053
1054 insn = 0;
1055 for (off = 0;; off += ilen) {
1056 uint64_t ip = start + off;
1057
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001058 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001059 if (ip == end) {
Andi Kleenfe571202018-09-24 10:07:32 -07001060 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp,
1061 &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001062 if (PRINT_FIELD(SRCCODE))
1063 printed += print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001064 break;
1065 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001066 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1067 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001068 if (ilen == 0)
1069 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001070 if (PRINT_FIELD(SRCCODE))
1071 print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001072 insn++;
1073 }
1074 }
1075 }
1076
1077 /*
1078 * Hit the branch? In this case we are already done, and the target
1079 * has not been executed yet.
1080 */
1081 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001082 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001083 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001084 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001085
1086 /*
1087 * Print final block upto sample
Andi Kleen61f61152018-11-19 21:06:17 -08001088 *
1089 * Due to pipeline delays the LBRs might be missing a branch
1090 * or two, which can result in very large or negative blocks
1091 * between final branch and sample. When this happens just
1092 * continue walking after the last TO until we hit a branch.
Andi Kleen48d02a12017-02-23 15:46:34 -08001093 */
1094 start = br->entries[0].to;
1095 end = sample->ip;
Andi Kleen61f61152018-11-19 21:06:17 -08001096 if (end < start) {
1097 /* Missing jump. Scan 128 bytes for the next branch */
1098 end = start + 128;
1099 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001100 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001101 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001102 if (len <= 0) {
1103 /* Print at least last IP if basic block did not work */
1104 len = grab_bb(buffer, sample->ip, sample->ip,
1105 machine, thread, &x.is64bit, &x.cpumode, false);
1106 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001107 goto out;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001108 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001109 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001110 if (PRINT_FIELD(SRCCODE))
1111 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001112 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001113 }
1114 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001115 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1116 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001117 if (ilen == 0)
1118 break;
Andi Kleen61f61152018-11-19 21:06:17 -08001119 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1120 /*
1121 * Hit a missing branch. Just stop.
1122 */
1123 printed += fprintf(fp, "\t... not reaching sample ...\n");
1124 break;
1125 }
Andi Kleendd2e18e2018-12-03 16:18:48 -08001126 if (PRINT_FIELD(SRCCODE))
1127 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001128 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001129out:
1130 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001131}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001132
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001133static int perf_sample__fprintf_addr(struct perf_sample *sample,
1134 struct thread *thread,
1135 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001136{
1137 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001138 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001139
1140 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001141 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001142
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001143 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001144
1145 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001146 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001147 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001148 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001149 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001150 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001151 }
1152
1153 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001154 printed += fprintf(fp, " (");
1155 printed += map__fprintf_dsoname(al.map, fp);
1156 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001157 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001158out:
1159 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001160}
1161
Andi Kleen99f753f2018-09-20 11:05:39 -07001162static const char *resolve_branch_sym(struct perf_sample *sample,
1163 struct perf_evsel *evsel,
1164 struct thread *thread,
1165 struct addr_location *al,
1166 u64 *ip)
1167{
1168 struct addr_location addr_al;
1169 struct perf_event_attr *attr = &evsel->attr;
1170 const char *name = NULL;
1171
1172 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1173 if (sample_addr_correlates_sym(attr)) {
1174 thread__resolve(thread, &addr_al, sample);
1175 if (addr_al.sym)
1176 name = addr_al.sym->name;
1177 else
1178 *ip = sample->addr;
1179 } else {
1180 *ip = sample->addr;
1181 }
1182 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1183 if (al->sym)
1184 name = al->sym->name;
1185 else
1186 *ip = sample->ip;
1187 }
1188 return name;
1189}
1190
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001191static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1192 struct perf_evsel *evsel,
1193 struct thread *thread,
1194 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001195{
1196 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter256d92b2018-12-21 14:06:19 +02001197 size_t depth = thread_stack__depth(thread, sample->cpu);
Adrian Huntere2167082016-06-23 16:40:58 +03001198 const char *name = NULL;
1199 static int spacing;
1200 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001201 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001202 u64 ip = 0;
1203
1204 /*
1205 * The 'return' has already been popped off the stack so the depth has
1206 * to be adjusted to match the 'call'.
1207 */
1208 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1209 depth += 1;
1210
Andi Kleen99f753f2018-09-20 11:05:39 -07001211 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001212
Andi Kleena78cdee2018-09-18 05:32:10 -07001213 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1214 dlen += fprintf(fp, "(");
1215 dlen += map__fprintf_dsoname(al->map, fp);
1216 dlen += fprintf(fp, ")\t");
1217 }
1218
Adrian Huntere2167082016-06-23 16:40:58 +03001219 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001220 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001221 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001222 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001223
1224 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001225 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001226
1227 /*
1228 * Try to keep the output length from changing frequently so that the
1229 * output lines up more nicely.
1230 */
1231 if (len > spacing || (len && len < spacing - 52))
1232 spacing = round_up(len + 4, 32);
1233
1234 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001235 len += fprintf(fp, "%*s", spacing - len, "");
1236
Andi Kleena78cdee2018-09-18 05:32:10 -07001237 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001238}
1239
Andi Kleen3ab481a2019-03-05 06:47:45 -08001240__weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1241 struct thread *thread __maybe_unused,
1242 struct machine *machine __maybe_unused)
1243{
1244}
1245
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001246static int perf_sample__fprintf_insn(struct perf_sample *sample,
1247 struct perf_event_attr *attr,
1248 struct thread *thread,
1249 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001250{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001251 int printed = 0;
1252
Andi Kleen3ab481a2019-03-05 06:47:45 -08001253 if (sample->insn_len == 0 && native_arch)
1254 arch_fetch_insn(sample, thread, machine);
1255
Andi Kleen224e2c92016-10-07 16:42:27 +03001256 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001257 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen3ab481a2019-03-05 06:47:45 -08001258 if (PRINT_FIELD(INSN) && sample->insn_len) {
Andi Kleen224e2c92016-10-07 16:42:27 +03001259 int i;
1260
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001261 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001262 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001263 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001264 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001265 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001266 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1267
1268 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001269}
1270
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001271static int perf_sample__fprintf_bts(struct perf_sample *sample,
1272 struct perf_evsel *evsel,
1273 struct thread *thread,
1274 struct addr_location *al,
1275 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001276{
1277 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001278 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001279 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001280 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001281
Adrian Huntere2167082016-06-23 16:40:58 +03001282 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001283 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001284
Akihiro Nagai95582592012-01-30 13:43:09 +09001285 /* print branch_from information */
1286 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001287 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001288 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001289
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001290 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001291 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001292 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001293 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001294
1295 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001296 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001297 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001298 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001299 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001300 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001301 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001302 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001303
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001304 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001305 }
1306
Akihiro Nagai95582592012-01-30 13:43:09 +09001307 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001308 if (PRINT_FIELD(ADDR) ||
1309 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001310 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001311 printed += fprintf(fp, " => ");
1312 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001313 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001314
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001315 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001316 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001317
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001318 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001319 printed += fprintf(fp, "\n");
1320 if (PRINT_FIELD(SRCCODE)) {
1321 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1322 &thread->srccode_state);
1323 if (ret) {
1324 printed += ret;
1325 printed += printf("\n");
1326 }
1327 }
1328 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001329}
1330
Adrian Hunter055cd332016-06-23 16:40:56 +03001331static struct {
1332 u32 flags;
1333 const char *name;
1334} sample_flags[] = {
1335 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1336 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1337 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1338 {PERF_IP_FLAG_BRANCH, "jmp"},
1339 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1340 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1341 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1342 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1343 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1344 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1345 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1346 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1347 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1348 {0, NULL}
1349};
1350
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001351static const char *sample_flags_to_name(u32 flags)
1352{
1353 int i;
1354
1355 for (i = 0; sample_flags[i].name ; i++) {
1356 if (sample_flags[i].flags == flags)
1357 return sample_flags[i].name;
1358 }
1359
1360 return NULL;
1361}
1362
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001363static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001364{
1365 const char *chars = PERF_IP_FLAG_CHARS;
1366 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001367 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1368 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001369 char str[33];
1370 int i, pos = 0;
1371
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001372 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1373 if (name)
1374 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1375
1376 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1377 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1378 if (name)
1379 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1380 }
1381
1382 if (flags & PERF_IP_FLAG_TRACE_END) {
1383 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1384 if (name)
1385 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001386 }
1387
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001388 for (i = 0; i < n; i++, flags >>= 1) {
1389 if (flags & 1)
1390 str[pos++] = chars[i];
1391 }
1392 for (; i < 32; i++, flags >>= 1) {
1393 if (flags & 1)
1394 str[pos++] = '?';
1395 }
1396 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001397
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001398 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001399}
1400
Wang Nan30372f02016-02-24 11:20:45 +00001401struct printer_data {
1402 int line_no;
1403 bool hit_nul;
1404 bool is_printable;
1405};
1406
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001407static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1408 unsigned int val,
1409 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001410{
1411 unsigned char ch = (unsigned char)val;
1412 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001413 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001414
1415 switch (op) {
1416 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001417 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001418 break;
1419 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001420 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001421 " ");
1422 break;
1423 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001424 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001425 break;
1426 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001427 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001428 break;
1429 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001430 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001431 break;
1432 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001433 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001434 break;
1435 case BINARY_PRINT_CHAR_DATA:
1436 if (printer_data->hit_nul && ch)
1437 printer_data->is_printable = false;
1438
1439 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001440 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001441
1442 if (!printer_data->is_printable)
1443 break;
1444
1445 if (ch == '\0')
1446 printer_data->hit_nul = true;
1447 else
1448 printer_data->is_printable = false;
1449 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001450 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001451 }
1452 break;
1453 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001454 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001455 break;
1456 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001457 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001458 printer_data->line_no++;
1459 break;
1460 case BINARY_PRINT_DATA_END:
1461 default:
1462 break;
1463 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001464
1465 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001466}
1467
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001468static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001469{
1470 unsigned int nr_bytes = sample->raw_size;
1471 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001472 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1473 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001474
1475 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001476 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1477
1478 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001479}
1480
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001481static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001482{
1483 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001484 return fprintf(fp, "%*s", spacing - len, "");
1485
1486 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001487}
1488
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001489static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001490{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001491 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001492}
1493
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001494static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001495{
1496 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1497 int len;
1498
1499 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001500 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001501
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001502 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001503 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001504 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001505}
1506
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001507static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001508{
1509 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1510 int len;
1511
1512 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001513 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001514
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001515 len = fprintf(fp, " hints: %#x extensions: %#x ",
1516 data->hints, data->extensions);
1517 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001518}
1519
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001520static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001521{
1522 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1523 int len;
1524
1525 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001526 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001527
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001528 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1529 data->hw, data->cstate, data->subcstate);
1530 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001531}
1532
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001533static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001534{
1535 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1536 int len;
1537
1538 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001539 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001540
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001541 len = fprintf(fp, " IP: %u ", data->ip);
1542 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001543}
1544
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001545static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001546{
1547 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1548 int len;
1549
1550 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001551 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001552
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001553 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001554 data->deepest_cstate, data->last_cstate,
1555 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001556 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001557}
1558
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001559static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001560{
1561 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1562 unsigned int percent, freq;
1563 int len;
1564
1565 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001566 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001567
1568 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001569 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001570 if (data->max_nonturbo) {
1571 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001572 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001573 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001574 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001575}
1576
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001577static int perf_sample__fprintf_synth(struct perf_sample *sample,
1578 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001579{
1580 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001581 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001582 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001583 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001584 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001585 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001586 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001587 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001588 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001589 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001590 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001591 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001592 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001593 default:
1594 break;
1595 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001596
1597 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001598}
1599
Jiri Olsa809e9422015-11-26 18:55:21 +01001600struct perf_script {
1601 struct perf_tool tool;
1602 struct perf_session *session;
1603 bool show_task_events;
1604 bool show_mmap_events;
1605 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301606 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001607 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001608 bool show_round_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001609 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001610 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001611 struct cpu_map *cpus;
1612 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001613 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001614 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001615 struct perf_time_interval *ptime_range;
1616 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001617 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001618};
1619
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001620static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1621{
1622 struct perf_evsel *evsel;
1623 int max = 0;
1624
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001625 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001626 int len = strlen(perf_evsel__name(evsel));
1627
1628 max = MAX(len, max);
1629 }
1630
1631 return max;
1632}
1633
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001634static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001635{
1636 struct mem_info mi = { .data_src.val = data_src };
1637 char decode[100];
1638 char out[100];
1639 static int maxlen;
1640 int len;
1641
1642 perf_script__meminfo_scnprintf(decode, 100, &mi);
1643
1644 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1645 if (maxlen < len)
1646 maxlen = len;
1647
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001648 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001649}
1650
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001651struct metric_ctx {
1652 struct perf_sample *sample;
1653 struct thread *thread;
1654 struct perf_evsel *evsel;
1655 FILE *fp;
1656};
1657
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001658static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1659 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001660 const char *fmt,
1661 const char *unit, double val)
1662{
1663 struct metric_ctx *mctx = ctx;
1664
1665 if (!fmt)
1666 return;
1667 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001668 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001669 fputs("\tmetric: ", mctx->fp);
1670 if (color)
1671 color_fprintf(mctx->fp, color, fmt, val);
1672 else
1673 printf(fmt, val);
1674 fprintf(mctx->fp, " %s\n", unit);
1675}
1676
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001677static void script_new_line(struct perf_stat_config *config __maybe_unused,
1678 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001679{
1680 struct metric_ctx *mctx = ctx;
1681
1682 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001683 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001684 fputs("\tmetric: ", mctx->fp);
1685}
1686
1687static void perf_sample__fprint_metric(struct perf_script *script,
1688 struct thread *thread,
1689 struct perf_evsel *evsel,
1690 struct perf_sample *sample,
1691 FILE *fp)
1692{
1693 struct perf_stat_output_ctx ctx = {
1694 .print_metric = script_print_metric,
1695 .new_line = script_new_line,
1696 .ctx = &(struct metric_ctx) {
1697 .sample = sample,
1698 .thread = thread,
1699 .evsel = evsel,
1700 .fp = fp,
1701 },
1702 .force_header = false,
1703 };
1704 struct perf_evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001705 u64 val;
1706
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001707 if (!evsel->stats)
1708 perf_evlist__alloc_stats(script->session->evlist, false);
1709 if (evsel_script(evsel->leader)->gnum++ == 0)
1710 perf_stat__reset_shadow_stats();
1711 val = sample->period * evsel->scale;
1712 perf_stat__update_shadow_stats(evsel,
1713 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001714 sample->cpu,
1715 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001716 evsel_script(evsel)->val = val;
1717 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1718 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001719 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001720 evsel_script(ev2)->val,
1721 sample->cpu,
1722 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001723 NULL,
1724 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001725 }
1726 evsel_script(evsel->leader)->gnum = 0;
1727 }
1728}
1729
Andi Kleen99f753f2018-09-20 11:05:39 -07001730static bool show_event(struct perf_sample *sample,
1731 struct perf_evsel *evsel,
1732 struct thread *thread,
1733 struct addr_location *al)
1734{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001735 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001736
1737 if (!symbol_conf.graph_function)
1738 return true;
1739
1740 if (thread->filter) {
1741 if (depth <= thread->filter_entry_depth) {
1742 thread->filter = false;
1743 return false;
1744 }
1745 return true;
1746 } else {
1747 const char *s = symbol_conf.graph_function;
1748 u64 ip;
1749 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1750 &ip);
1751 unsigned nlen;
1752
1753 if (!name)
1754 return false;
1755 nlen = strlen(name);
1756 while (*s) {
1757 unsigned len = strcspn(s, ",");
1758 if (nlen == len && !strncmp(name, s, len)) {
1759 thread->filter = true;
1760 thread->filter_entry_depth = depth;
1761 return true;
1762 }
1763 s += len;
1764 if (*s == ',')
1765 s++;
1766 }
1767 return false;
1768 }
1769}
1770
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001771static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001772 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001773 struct addr_location *al,
1774 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001775{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001776 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001777 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001778 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001779 struct perf_evsel_script *es = evsel->priv;
1780 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001781
Adrian Hunter14057202017-06-21 13:17:19 +03001782 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001783 return;
1784
Andi Kleen99f753f2018-09-20 11:05:39 -07001785 if (!show_event(sample, evsel, thread, al))
1786 return;
1787
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001788 ++es->samples;
1789
Jiri Olsa28a0b392018-01-07 17:03:52 +01001790 perf_sample__fprintf_start(sample, thread, evsel,
1791 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001792
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001793 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001794 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001795
Namhyung Kime944d3d2013-11-18 14:34:52 +09001796 if (PRINT_FIELD(EVNAME)) {
1797 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001798
1799 if (!script->name_width)
1800 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1801
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001802 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001803 }
1804
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001805 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001806 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001807
Akihiro Nagai95582592012-01-30 13:43:09 +09001808 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001809 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001810 return;
1811 }
1812
Andi Kleen96167162019-01-17 11:48:34 -08001813 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001814 event_format__fprintf(evsel->tp_format, sample->cpu,
1815 sample->raw_data, sample->raw_size, fp);
1816 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001817
1818 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001819 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001820
David Ahern7cec0922011-05-30 13:08:23 -06001821 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001822 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001823
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001824 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001825 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001826
1827 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001828 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001829
David Ahern787bef12011-05-27 14:28:43 -06001830 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001831 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001832
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001833 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001834 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001835 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001836 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001837
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001838 fputc(cursor ? '\n' : ' ', fp);
1839 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001840 }
1841
Stephane Eranianfc36f942015-08-31 18:41:10 +02001842 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001843 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001844
Andi Kleenb1491ac2017-09-05 11:40:57 -07001845 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001846 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001847
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001848 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001849 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001850 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001851 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001852 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001853 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001854
Wang Nan30372f02016-02-24 11:20:45 +00001855 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001856 perf_sample__fprintf_bpf_output(sample, fp);
1857 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001858
1859 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001860 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1861 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001862
Andi Kleendd2e18e2018-12-03 16:18:48 -08001863 if (PRINT_FIELD(SRCCODE)) {
1864 if (map__fprintf_srccode(al->map, al->addr, stdout,
1865 &thread->srccode_state))
1866 printf("\n");
1867 }
1868
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001869 if (PRINT_FIELD(METRIC))
1870 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001871
1872 if (verbose)
1873 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001874}
1875
Tom Zanussi956ffd02009-11-25 01:15:46 -06001876static struct scripting_ops *scripting_ops;
1877
Jiri Olsa36e33c52016-01-06 11:49:56 +01001878static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1879{
1880 int nthreads = thread_map__nr(counter->threads);
1881 int ncpus = perf_evsel__nr_cpus(counter);
1882 int cpu, thread;
1883 static int header_printed;
1884
1885 if (counter->system_wide)
1886 nthreads = 1;
1887
1888 if (!header_printed) {
1889 printf("%3s %8s %15s %15s %15s %15s %s\n",
1890 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1891 header_printed = 1;
1892 }
1893
1894 for (thread = 0; thread < nthreads; thread++) {
1895 for (cpu = 0; cpu < ncpus; cpu++) {
1896 struct perf_counts_values *counts;
1897
1898 counts = perf_counts(counter->counts, cpu, thread);
1899
1900 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1901 counter->cpus->map[cpu],
1902 thread_map__pid(counter->threads, thread),
1903 counts->val,
1904 counts->ena,
1905 counts->run,
1906 tstamp,
1907 perf_evsel__name(counter));
1908 }
1909 }
1910}
1911
Jiri Olsae099eba2016-01-05 22:09:09 +01001912static void process_stat(struct perf_evsel *counter, u64 tstamp)
1913{
1914 if (scripting_ops && scripting_ops->process_stat)
1915 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001916 else
1917 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001918}
1919
1920static void process_stat_interval(u64 tstamp)
1921{
1922 if (scripting_ops && scripting_ops->process_stat_interval)
1923 scripting_ops->process_stat_interval(tstamp);
1924}
1925
Tom Zanussi956ffd02009-11-25 01:15:46 -06001926static void setup_scripting(void)
1927{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001928 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001929 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001930}
1931
Adrian Hunterd445dd22014-08-15 22:08:37 +03001932static int flush_scripting(void)
1933{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001934 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001935}
1936
Tom Zanussi956ffd02009-11-25 01:15:46 -06001937static int cleanup_scripting(void)
1938{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001939 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001940
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001941 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001942}
1943
Andi Kleene87e5482019-03-11 07:44:52 -07001944static bool filter_cpu(struct perf_sample *sample)
1945{
1946 if (cpu_list)
1947 return !test_bit(sample->cpu, cpu_bitmap);
1948 return false;
1949}
1950
Jiri Olsa809e9422015-11-26 18:55:21 +01001951static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001952 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001953 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001954 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001955 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001956{
Jiri Olsa809e9422015-11-26 18:55:21 +01001957 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001958 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001959
Jin Yao2ab046c2017-12-08 21:13:46 +08001960 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1961 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001962 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001963 }
David Aherna91f4c42016-11-29 10:15:43 -07001964
David Ahern1424dc92011-03-09 22:23:28 -07001965 if (debug_mode) {
1966 if (sample->time < last_timestamp) {
1967 pr_err("Samples misordered, previous: %" PRIu64
1968 " this: %" PRIu64 "\n", last_timestamp,
1969 sample->time);
1970 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001971 }
David Ahern1424dc92011-03-09 22:23:28 -07001972 last_timestamp = sample->time;
1973 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001974 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001975
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001976 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001977 pr_err("problem processing %d event, skipping it.\n",
1978 event->header.type);
1979 return -1;
1980 }
1981
1982 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001983 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001984
Andi Kleene87e5482019-03-11 07:44:52 -07001985 if (filter_cpu(sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001986 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001987
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001988 if (scripting_ops)
1989 scripting_ops->process_event(event, sample, evsel, &al);
1990 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001991 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001992
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001993out_put:
1994 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001995 return 0;
1996}
1997
Adrian Hunter7ea95722013-11-01 15:51:30 +02001998static int process_attr(struct perf_tool *tool, union perf_event *event,
1999 struct perf_evlist **pevlist)
2000{
2001 struct perf_script *scr = container_of(tool, struct perf_script, tool);
2002 struct perf_evlist *evlist;
2003 struct perf_evsel *evsel, *pos;
2004 int err;
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302005 static struct perf_evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002006
2007 err = perf_event__process_attr(tool, event, pevlist);
2008 if (err)
2009 return err;
2010
2011 evlist = *pevlist;
2012 evsel = perf_evlist__last(*pevlist);
2013
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302014 if (!evsel->priv) {
2015 if (scr->per_event_dump) {
2016 evsel->priv = perf_evsel_script__new(evsel,
2017 scr->session->data);
2018 } else {
2019 es = zalloc(sizeof(*es));
2020 if (!es)
2021 return -ENOMEM;
2022 es->fp = stdout;
2023 evsel->priv = es;
2024 }
2025 }
2026
Adrian Hunter14057202017-06-21 13:17:19 +03002027 if (evsel->attr.type >= PERF_TYPE_MAX &&
2028 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002029 return 0;
2030
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002031 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02002032 if (pos->attr.type == evsel->attr.type && pos != evsel)
2033 return 0;
2034 }
2035
2036 set_print_ip_opts(&evsel->attr);
2037
Jiri Olsad2b5a312015-10-16 12:41:25 +02002038 if (evsel->attr.sample_type)
2039 err = perf_evsel__check_attr(evsel, scr->session);
2040
2041 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002042}
2043
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002044static int process_comm_event(struct perf_tool *tool,
2045 union perf_event *event,
2046 struct perf_sample *sample,
2047 struct machine *machine)
2048{
2049 struct thread *thread;
2050 struct perf_script *script = container_of(tool, struct perf_script, tool);
2051 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002052 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002053 int ret = -1;
2054
2055 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2056 if (thread == NULL) {
2057 pr_debug("problem processing COMM event, skipping it.\n");
2058 return -1;
2059 }
2060
2061 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2062 goto out;
2063
2064 if (!evsel->attr.sample_id_all) {
2065 sample->cpu = 0;
2066 sample->time = 0;
2067 sample->tid = event->comm.tid;
2068 sample->pid = event->comm.pid;
2069 }
Andi Kleene87e5482019-03-11 07:44:52 -07002070 if (!filter_cpu(sample)) {
2071 perf_sample__fprintf_start(sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01002072 PERF_RECORD_COMM, stdout);
Andi Kleene87e5482019-03-11 07:44:52 -07002073 perf_event__fprintf(event, stdout);
2074 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002075 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002076out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002077 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002078 return ret;
2079}
2080
Hari Bathini96a44bb2017-03-08 02:12:06 +05302081static int process_namespaces_event(struct perf_tool *tool,
2082 union perf_event *event,
2083 struct perf_sample *sample,
2084 struct machine *machine)
2085{
2086 struct thread *thread;
2087 struct perf_script *script = container_of(tool, struct perf_script, tool);
2088 struct perf_session *session = script->session;
2089 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2090 int ret = -1;
2091
2092 thread = machine__findnew_thread(machine, event->namespaces.pid,
2093 event->namespaces.tid);
2094 if (thread == NULL) {
2095 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2096 return -1;
2097 }
2098
2099 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2100 goto out;
2101
2102 if (!evsel->attr.sample_id_all) {
2103 sample->cpu = 0;
2104 sample->time = 0;
2105 sample->tid = event->namespaces.tid;
2106 sample->pid = event->namespaces.pid;
2107 }
Andi Kleene87e5482019-03-11 07:44:52 -07002108 if (!filter_cpu(sample)) {
2109 perf_sample__fprintf_start(sample, thread, evsel,
2110 PERF_RECORD_NAMESPACES, stdout);
2111 perf_event__fprintf(event, stdout);
2112 }
Hari Bathini96a44bb2017-03-08 02:12:06 +05302113 ret = 0;
2114out:
2115 thread__put(thread);
2116 return ret;
2117}
2118
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002119static int process_fork_event(struct perf_tool *tool,
2120 union perf_event *event,
2121 struct perf_sample *sample,
2122 struct machine *machine)
2123{
2124 struct thread *thread;
2125 struct perf_script *script = container_of(tool, struct perf_script, tool);
2126 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002127 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002128
2129 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2130 return -1;
2131
2132 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2133 if (thread == NULL) {
2134 pr_debug("problem processing FORK event, skipping it.\n");
2135 return -1;
2136 }
2137
2138 if (!evsel->attr.sample_id_all) {
2139 sample->cpu = 0;
2140 sample->time = event->fork.time;
2141 sample->tid = event->fork.tid;
2142 sample->pid = event->fork.pid;
2143 }
Andi Kleene87e5482019-03-11 07:44:52 -07002144 if (!filter_cpu(sample)) {
2145 perf_sample__fprintf_start(sample, thread, evsel,
2146 PERF_RECORD_FORK, stdout);
2147 perf_event__fprintf(event, stdout);
2148 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002149 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002150
2151 return 0;
2152}
2153static int process_exit_event(struct perf_tool *tool,
2154 union perf_event *event,
2155 struct perf_sample *sample,
2156 struct machine *machine)
2157{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002158 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002159 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 Kimad7ebb92013-11-26 17:51:12 +09002163
2164 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2165 if (thread == NULL) {
2166 pr_debug("problem processing EXIT event, skipping it.\n");
2167 return -1;
2168 }
2169
2170 if (!evsel->attr.sample_id_all) {
2171 sample->cpu = 0;
2172 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002173 sample->tid = event->fork.tid;
2174 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002175 }
Andi Kleene87e5482019-03-11 07:44:52 -07002176 if (!filter_cpu(sample)) {
2177 perf_sample__fprintf_start(sample, thread, evsel,
2178 PERF_RECORD_EXIT, stdout);
2179 perf_event__fprintf(event, stdout);
2180 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002181
2182 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002183 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002184
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002185 thread__put(thread);
2186 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002187}
2188
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002189static int process_mmap_event(struct perf_tool *tool,
2190 union perf_event *event,
2191 struct perf_sample *sample,
2192 struct machine *machine)
2193{
2194 struct thread *thread;
2195 struct perf_script *script = container_of(tool, struct perf_script, tool);
2196 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002197 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002198
2199 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2200 return -1;
2201
2202 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2203 if (thread == NULL) {
2204 pr_debug("problem processing MMAP event, skipping it.\n");
2205 return -1;
2206 }
2207
2208 if (!evsel->attr.sample_id_all) {
2209 sample->cpu = 0;
2210 sample->time = 0;
2211 sample->tid = event->mmap.tid;
2212 sample->pid = event->mmap.pid;
2213 }
Andi Kleene87e5482019-03-11 07:44:52 -07002214 if (!filter_cpu(sample)) {
2215 perf_sample__fprintf_start(sample, thread, evsel,
2216 PERF_RECORD_MMAP, stdout);
2217 perf_event__fprintf(event, stdout);
2218 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002219 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002220 return 0;
2221}
2222
2223static int process_mmap2_event(struct perf_tool *tool,
2224 union perf_event *event,
2225 struct perf_sample *sample,
2226 struct machine *machine)
2227{
2228 struct thread *thread;
2229 struct perf_script *script = container_of(tool, struct perf_script, tool);
2230 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002231 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002232
2233 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2234 return -1;
2235
2236 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2237 if (thread == NULL) {
2238 pr_debug("problem processing MMAP2 event, skipping it.\n");
2239 return -1;
2240 }
2241
2242 if (!evsel->attr.sample_id_all) {
2243 sample->cpu = 0;
2244 sample->time = 0;
2245 sample->tid = event->mmap2.tid;
2246 sample->pid = event->mmap2.pid;
2247 }
Andi Kleene87e5482019-03-11 07:44:52 -07002248 if (!filter_cpu(sample)) {
2249 perf_sample__fprintf_start(sample, thread, evsel,
2250 PERF_RECORD_MMAP2, stdout);
2251 perf_event__fprintf(event, stdout);
2252 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002253 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002254 return 0;
2255}
2256
Adrian Hunter7c148982015-07-21 12:44:06 +03002257static int process_switch_event(struct perf_tool *tool,
2258 union perf_event *event,
2259 struct perf_sample *sample,
2260 struct machine *machine)
2261{
2262 struct thread *thread;
2263 struct perf_script *script = container_of(tool, struct perf_script, tool);
2264 struct perf_session *session = script->session;
2265 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2266
2267 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2268 return -1;
2269
2270 thread = machine__findnew_thread(machine, sample->pid,
2271 sample->tid);
2272 if (thread == NULL) {
2273 pr_debug("problem processing SWITCH event, skipping it.\n");
2274 return -1;
2275 }
2276
Andi Kleene87e5482019-03-11 07:44:52 -07002277 if (!filter_cpu(sample)) {
2278 perf_sample__fprintf_start(sample, thread, evsel,
2279 PERF_RECORD_SWITCH, stdout);
2280 perf_event__fprintf(event, stdout);
2281 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002282 thread__put(thread);
2283 return 0;
2284}
2285
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002286static int
2287process_lost_event(struct perf_tool *tool,
2288 union perf_event *event,
2289 struct perf_sample *sample,
2290 struct machine *machine)
2291{
2292 struct perf_script *script = container_of(tool, struct perf_script, tool);
2293 struct perf_session *session = script->session;
2294 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2295 struct thread *thread;
2296
2297 thread = machine__findnew_thread(machine, sample->pid,
2298 sample->tid);
2299 if (thread == NULL)
2300 return -1;
2301
Andi Kleene87e5482019-03-11 07:44:52 -07002302 if (!filter_cpu(sample)) {
2303 perf_sample__fprintf_start(sample, thread, evsel,
2304 PERF_RECORD_LOST, stdout);
2305 perf_event__fprintf(event, stdout);
2306 }
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002307 thread__put(thread);
2308 return 0;
2309}
2310
Jiri Olsa3233b372018-02-06 19:17:59 +01002311static int
2312process_finished_round_event(struct perf_tool *tool __maybe_unused,
2313 union perf_event *event,
2314 struct ordered_events *oe __maybe_unused)
2315
2316{
2317 perf_event__fprintf(event, stdout);
2318 return 0;
2319}
2320
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002321static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002322{
2323 session_done = 1;
2324}
2325
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002326static void perf_script__fclose_per_event_dump(struct perf_script *script)
2327{
2328 struct perf_evlist *evlist = script->session->evlist;
2329 struct perf_evsel *evsel;
2330
2331 evlist__for_each_entry(evlist, evsel) {
2332 if (!evsel->priv)
2333 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002334 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002335 evsel->priv = NULL;
2336 }
2337}
2338
2339static int perf_script__fopen_per_event_dump(struct perf_script *script)
2340{
2341 struct perf_evsel *evsel;
2342
2343 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002344 /*
2345 * Already setup? I.e. we may be called twice in cases like
2346 * Intel PT, one for the intel_pt// and dummy events, then
2347 * for the evsels syntheized from the auxtrace info.
2348 *
2349 * Ses perf_script__process_auxtrace_info.
2350 */
2351 if (evsel->priv != NULL)
2352 continue;
2353
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002354 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002355 if (evsel->priv == NULL)
2356 goto out_err_fclose;
2357 }
2358
2359 return 0;
2360
2361out_err_fclose:
2362 perf_script__fclose_per_event_dump(script);
2363 return -1;
2364}
2365
2366static int perf_script__setup_per_event_dump(struct perf_script *script)
2367{
2368 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002369 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002370
2371 if (script->per_event_dump)
2372 return perf_script__fopen_per_event_dump(script);
2373
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002374 es_stdout.fp = stdout;
2375
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002376 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002377 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002378
2379 return 0;
2380}
2381
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002382static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2383{
2384 struct perf_evsel *evsel;
2385
2386 evlist__for_each_entry(script->session->evlist, evsel) {
2387 struct perf_evsel_script *es = evsel->priv;
2388
2389 perf_evsel_script__fprintf(es, stdout);
2390 perf_evsel_script__delete(es);
2391 evsel->priv = NULL;
2392 }
2393}
2394
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002395static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002396{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002397 int ret;
2398
Tom Zanussic239da32010-04-01 23:59:18 -05002399 signal(SIGINT, sig_handler);
2400
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002401 perf_stat__init_shadow_stats();
2402
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002403 /* override event processing functions */
2404 if (script->show_task_events) {
2405 script->tool.comm = process_comm_event;
2406 script->tool.fork = process_fork_event;
2407 script->tool.exit = process_exit_event;
2408 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002409 if (script->show_mmap_events) {
2410 script->tool.mmap = process_mmap_event;
2411 script->tool.mmap2 = process_mmap2_event;
2412 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002413 if (script->show_switch_events)
2414 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302415 if (script->show_namespace_events)
2416 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002417 if (script->show_lost_events)
2418 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002419 if (script->show_round_events) {
2420 script->tool.ordered_events = false;
2421 script->tool.finished_round = process_finished_round_event;
2422 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002423
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002424 if (perf_script__setup_per_event_dump(script)) {
2425 pr_err("Couldn't create the per event dump files\n");
2426 return -1;
2427 }
2428
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002429 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002430
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002431 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002432 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002433
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002434 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002435 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002436
2437 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002438}
2439
Tom Zanussi956ffd02009-11-25 01:15:46 -06002440struct script_spec {
2441 struct list_head node;
2442 struct scripting_ops *ops;
2443 char spec[0];
2444};
2445
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002446static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002447
2448static struct script_spec *script_spec__new(const char *spec,
2449 struct scripting_ops *ops)
2450{
2451 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2452
2453 if (s != NULL) {
2454 strcpy(s->spec, spec);
2455 s->ops = ops;
2456 }
2457
2458 return s;
2459}
2460
Tom Zanussi956ffd02009-11-25 01:15:46 -06002461static void script_spec__add(struct script_spec *s)
2462{
2463 list_add_tail(&s->node, &script_specs);
2464}
2465
2466static struct script_spec *script_spec__find(const char *spec)
2467{
2468 struct script_spec *s;
2469
2470 list_for_each_entry(s, &script_specs, node)
2471 if (strcasecmp(s->spec, spec) == 0)
2472 return s;
2473 return NULL;
2474}
2475
Tom Zanussi956ffd02009-11-25 01:15:46 -06002476int script_spec_register(const char *spec, struct scripting_ops *ops)
2477{
2478 struct script_spec *s;
2479
2480 s = script_spec__find(spec);
2481 if (s)
2482 return -1;
2483
Taeung Song8560bae2016-02-26 00:13:10 +09002484 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002485 if (!s)
2486 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002487 else
2488 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002489
2490 return 0;
2491}
2492
2493static struct scripting_ops *script_spec__lookup(const char *spec)
2494{
2495 struct script_spec *s = script_spec__find(spec);
2496 if (!s)
2497 return NULL;
2498
2499 return s->ops;
2500}
2501
2502static void list_available_languages(void)
2503{
2504 struct script_spec *s;
2505
2506 fprintf(stderr, "\n");
2507 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002508 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002509
2510 list_for_each_entry(s, &script_specs, node)
2511 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2512
2513 fprintf(stderr, "\n");
2514}
2515
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002516static int parse_scriptname(const struct option *opt __maybe_unused,
2517 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002518{
2519 char spec[PATH_MAX];
2520 const char *script, *ext;
2521 int len;
2522
Tom Zanussif526d682010-01-27 02:27:52 -06002523 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002524 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002525 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002526 }
2527
2528 script = strchr(str, ':');
2529 if (script) {
2530 len = script - str;
2531 if (len >= PATH_MAX) {
2532 fprintf(stderr, "invalid language specifier");
2533 return -1;
2534 }
2535 strncpy(spec, str, len);
2536 spec[len] = '\0';
2537 scripting_ops = script_spec__lookup(spec);
2538 if (!scripting_ops) {
2539 fprintf(stderr, "invalid language specifier");
2540 return -1;
2541 }
2542 script++;
2543 } else {
2544 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002545 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002546 if (!ext) {
2547 fprintf(stderr, "invalid script extension");
2548 return -1;
2549 }
2550 scripting_ops = script_spec__lookup(++ext);
2551 if (!scripting_ops) {
2552 fprintf(stderr, "invalid script extension");
2553 return -1;
2554 }
2555 }
2556
2557 script_name = strdup(script);
2558
2559 return 0;
2560}
2561
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002562static int parse_output_fields(const struct option *opt __maybe_unused,
2563 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002564{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002565 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002566 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002567 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002568 int rc = 0;
2569 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002570 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002571 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002572
2573 if (!str)
2574 return -ENOMEM;
2575
David Ahern2c9e45f72011-03-17 10:03:21 -06002576 /* first word can state for which event type the user is specifying
2577 * the fields. If no type exists, the specified fields apply to all
2578 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002579 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002580 tok = strchr(str, ':');
2581 if (tok) {
2582 *tok = '\0';
2583 tok++;
2584 if (!strcmp(str, "hw"))
2585 type = PERF_TYPE_HARDWARE;
2586 else if (!strcmp(str, "sw"))
2587 type = PERF_TYPE_SOFTWARE;
2588 else if (!strcmp(str, "trace"))
2589 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002590 else if (!strcmp(str, "raw"))
2591 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002592 else if (!strcmp(str, "break"))
2593 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002594 else if (!strcmp(str, "synth"))
2595 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002596 else {
2597 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002598 rc = -EINVAL;
2599 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002600 }
2601
2602 if (output[type].user_set)
2603 pr_warning("Overriding previous field request for %s events.\n",
2604 event_type(type));
2605
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002606 /* Don't override defaults for +- */
2607 if (strchr(tok, '+') || strchr(tok, '-'))
2608 goto parse;
2609
David Ahern2c9e45f72011-03-17 10:03:21 -06002610 output[type].fields = 0;
2611 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002612 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002613
2614 } else {
2615 tok = str;
2616 if (strlen(str) == 0) {
2617 fprintf(stderr,
2618 "Cannot set fields to 'none' for all event types.\n");
2619 rc = -EINVAL;
2620 goto out;
2621 }
2622
Andi Kleen36ce5652017-06-02 08:48:10 -07002623 /* Don't override defaults for +- */
2624 if (strchr(str, '+') || strchr(str, '-'))
2625 goto parse;
2626
David Ahern2c9e45f72011-03-17 10:03:21 -06002627 if (output_set_by_user())
2628 pr_warning("Overriding previous field request for all events.\n");
2629
Adrian Hunter14057202017-06-21 13:17:19 +03002630 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002631 output[j].fields = 0;
2632 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002633 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002634 }
David Ahern1424dc92011-03-09 22:23:28 -07002635 }
2636
Andi Kleen36ce5652017-06-02 08:48:10 -07002637parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002638 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002639 if (*tok == '+') {
2640 if (change == SET)
2641 goto out_badmix;
2642 change = ADD;
2643 tok++;
2644 } else if (*tok == '-') {
2645 if (change == SET)
2646 goto out_badmix;
2647 change = REMOVE;
2648 tok++;
2649 } else {
2650 if (change != SET && change != DEFAULT)
2651 goto out_badmix;
2652 change = SET;
2653 }
2654
David Ahern745f43e2011-03-09 22:23:26 -07002655 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002656 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002657 break;
David Ahern745f43e2011-03-09 22:23:26 -07002658 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002659 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002660 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002661 continue;
2662 }
David Ahern745f43e2011-03-09 22:23:26 -07002663 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002664 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002665 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002666 goto out;
2667 }
2668
2669 if (type == -1) {
2670 /* add user option to all events types for
2671 * which it is valid
2672 */
Adrian Hunter14057202017-06-21 13:17:19 +03002673 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002674 if (output[j].invalid_fields & all_output_options[i].field) {
2675 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2676 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002677 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002678 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002679 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002680 output[j].user_set_fields &= ~all_output_options[i].field;
2681 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002682 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002683 output[j].user_set_fields |= all_output_options[i].field;
2684 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002685 output[j].user_set = true;
2686 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002687 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002688 }
2689 } else {
2690 if (output[type].invalid_fields & all_output_options[i].field) {
2691 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2692 all_output_options[i].str, event_type(type));
2693
2694 rc = -EINVAL;
2695 goto out;
2696 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002697 if (change == REMOVE)
2698 output[type].fields &= ~all_output_options[i].field;
2699 else
2700 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002701 output[type].user_set = true;
2702 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002703 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002704 }
2705
2706 if (type >= 0) {
2707 if (output[type].fields == 0) {
2708 pr_debug("No fields requested for %s type. "
2709 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002710 }
David Ahern1424dc92011-03-09 22:23:28 -07002711 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002712 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002713
Andi Kleen36ce5652017-06-02 08:48:10 -07002714out_badmix:
2715 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2716 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002717out:
David Ahern745f43e2011-03-09 22:23:26 -07002718 free(str);
2719 return rc;
2720}
2721
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002722#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2723 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2724 if ((lang_dirent->d_type == DT_DIR || \
2725 (lang_dirent->d_type == DT_UNKNOWN && \
2726 is_directory(scripts_path, lang_dirent))) && \
2727 (strcmp(lang_dirent->d_name, ".")) && \
2728 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002729
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002730#define for_each_script(lang_path, lang_dir, script_dirent) \
2731 while ((script_dirent = readdir(lang_dir)) != NULL) \
2732 if (script_dirent->d_type != DT_DIR && \
2733 (script_dirent->d_type != DT_UNKNOWN || \
2734 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002735
2736
2737#define RECORD_SUFFIX "-record"
2738#define REPORT_SUFFIX "-report"
2739
2740struct script_desc {
2741 struct list_head node;
2742 char *name;
2743 char *half_liner;
2744 char *args;
2745};
2746
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002747static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002748
2749static struct script_desc *script_desc__new(const char *name)
2750{
2751 struct script_desc *s = zalloc(sizeof(*s));
2752
Tom Zanussib5b87312010-11-10 08:16:51 -06002753 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002754 s->name = strdup(name);
2755
2756 return s;
2757}
2758
2759static void script_desc__delete(struct script_desc *s)
2760{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002761 zfree(&s->name);
2762 zfree(&s->half_liner);
2763 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002764 free(s);
2765}
2766
2767static void script_desc__add(struct script_desc *s)
2768{
2769 list_add_tail(&s->node, &script_descs);
2770}
2771
2772static struct script_desc *script_desc__find(const char *name)
2773{
2774 struct script_desc *s;
2775
2776 list_for_each_entry(s, &script_descs, node)
2777 if (strcasecmp(s->name, name) == 0)
2778 return s;
2779 return NULL;
2780}
2781
2782static struct script_desc *script_desc__findnew(const char *name)
2783{
2784 struct script_desc *s = script_desc__find(name);
2785
2786 if (s)
2787 return s;
2788
2789 s = script_desc__new(name);
2790 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002791 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002792
2793 script_desc__add(s);
2794
2795 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002796}
2797
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002798static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002799{
2800 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002801 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002802
2803 if (strlen(str) > suffix_len) {
2804 p = str + strlen(str) - suffix_len;
2805 if (!strncmp(p, suffix, suffix_len))
2806 return p;
2807 }
2808
2809 return NULL;
2810}
2811
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002812static int read_script_info(struct script_desc *desc, const char *filename)
2813{
2814 char line[BUFSIZ], *p;
2815 FILE *fp;
2816
2817 fp = fopen(filename, "r");
2818 if (!fp)
2819 return -1;
2820
2821 while (fgets(line, sizeof(line), fp)) {
2822 p = ltrim(line);
2823 if (strlen(p) == 0)
2824 continue;
2825 if (*p != '#')
2826 continue;
2827 p++;
2828 if (strlen(p) && *p == '!')
2829 continue;
2830
2831 p = ltrim(p);
2832 if (strlen(p) && p[strlen(p) - 1] == '\n')
2833 p[strlen(p) - 1] = '\0';
2834
2835 if (!strncmp(p, "description:", strlen("description:"))) {
2836 p += strlen("description:");
2837 desc->half_liner = strdup(ltrim(p));
2838 continue;
2839 }
2840
2841 if (!strncmp(p, "args:", strlen("args:"))) {
2842 p += strlen("args:");
2843 desc->args = strdup(ltrim(p));
2844 continue;
2845 }
2846 }
2847
2848 fclose(fp);
2849
2850 return 0;
2851}
2852
Robert Richter38efb532011-11-25 11:38:40 +01002853static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2854{
2855 char *script_root, *str;
2856
2857 script_root = strdup(script_dirent->d_name);
2858 if (!script_root)
2859 return NULL;
2860
2861 str = (char *)ends_with(script_root, suffix);
2862 if (!str) {
2863 free(script_root);
2864 return NULL;
2865 }
2866
2867 *str = '\0';
2868 return script_root;
2869}
2870
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002871static int list_available_scripts(const struct option *opt __maybe_unused,
2872 const char *s __maybe_unused,
2873 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002874{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002875 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002876 char scripts_path[MAXPATHLEN];
2877 DIR *scripts_dir, *lang_dir;
2878 char script_path[MAXPATHLEN];
2879 char lang_path[MAXPATHLEN];
2880 struct script_desc *desc;
2881 char first_half[BUFSIZ];
2882 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002883
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002884 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002885
2886 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002887 if (!scripts_dir) {
2888 fprintf(stdout,
2889 "open(%s) failed.\n"
2890 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2891 scripts_path);
2892 exit(-1);
2893 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002894
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002895 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002896 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2897 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002898 lang_dir = opendir(lang_path);
2899 if (!lang_dir)
2900 continue;
2901
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002902 for_each_script(lang_path, lang_dir, script_dirent) {
2903 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002904 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002905 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002906 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2907 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002908 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002909 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002910 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002911 }
2912 }
2913
2914 fprintf(stdout, "List of available trace scripts:\n");
2915 list_for_each_entry(desc, &script_descs, node) {
2916 sprintf(first_half, "%s %s", desc->name,
2917 desc->args ? desc->args : "");
2918 fprintf(stdout, " %-36s %s\n", first_half,
2919 desc->half_liner ? desc->half_liner : "");
2920 }
2921
2922 exit(0);
2923}
2924
Feng Tange5f37052012-09-07 16:42:26 +08002925/*
Feng Tang49e639e2012-10-30 11:56:03 +08002926 * Some scripts specify the required events in their "xxx-record" file,
2927 * this function will check if the events in perf.data match those
2928 * mentioned in the "xxx-record".
2929 *
2930 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2931 * which is covered well now. And new parsing code should be added to
2932 * cover the future complexing formats like event groups etc.
2933 */
2934static int check_ev_match(char *dir_name, char *scriptname,
2935 struct perf_session *session)
2936{
2937 char filename[MAXPATHLEN], evname[128];
2938 char line[BUFSIZ], *p;
2939 struct perf_evsel *pos;
2940 int match, len;
2941 FILE *fp;
2942
Jiri Olsa77f18152018-03-19 09:29:01 +01002943 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08002944
2945 fp = fopen(filename, "r");
2946 if (!fp)
2947 return -1;
2948
2949 while (fgets(line, sizeof(line), fp)) {
2950 p = ltrim(line);
2951 if (*p == '#')
2952 continue;
2953
2954 while (strlen(p)) {
2955 p = strstr(p, "-e");
2956 if (!p)
2957 break;
2958
2959 p += 2;
2960 p = ltrim(p);
2961 len = strcspn(p, " \t");
2962 if (!len)
2963 break;
2964
2965 snprintf(evname, len + 1, "%s", p);
2966
2967 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002968 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002969 if (!strcmp(perf_evsel__name(pos), evname)) {
2970 match = 1;
2971 break;
2972 }
2973 }
2974
2975 if (!match) {
2976 fclose(fp);
2977 return -1;
2978 }
2979 }
2980 }
2981
2982 fclose(fp);
2983 return 0;
2984}
2985
2986/*
Feng Tange5f37052012-09-07 16:42:26 +08002987 * Return -1 if none is found, otherwise the actual scripts number.
2988 *
2989 * Currently the only user of this function is the script browser, which
2990 * will list all statically runnable scripts, select one, execute it and
2991 * show the output in a perf browser.
2992 */
Andi Kleen905e4af2019-03-11 07:45:01 -07002993int find_scripts(char **scripts_array, char **scripts_path_array, int num,
2994 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08002995{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002996 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002997 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002998 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002999 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003000 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003001 .path = input_name,
3002 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003003 };
Feng Tange5f37052012-09-07 16:42:26 +08003004 char *temp;
3005 int i = 0;
3006
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003007 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08003008 if (!session)
3009 return -1;
3010
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003011 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003012
3013 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003014 if (!scripts_dir) {
3015 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003016 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003017 }
Feng Tange5f37052012-09-07 16:42:26 +08003018
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003019 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003020 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3021 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003022#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003023 if (strstr(lang_path, "perl"))
3024 continue;
3025#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003026#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003027 if (strstr(lang_path, "python"))
3028 continue;
3029#endif
3030
3031 lang_dir = opendir(lang_path);
3032 if (!lang_dir)
3033 continue;
3034
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003035 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003036 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003037 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003038 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003039 if (i >= num)
3040 break;
3041 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3042 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003043 script_dirent->d_name);
3044 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003045 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003046 (temp - script_dirent->d_name) + 1,
3047 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003048
3049 if (check_ev_match(lang_path,
3050 scripts_array[i], session))
3051 continue;
3052
Feng Tange5f37052012-09-07 16:42:26 +08003053 i++;
3054 }
Feng Tang49e639e2012-10-30 11:56:03 +08003055 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003056 }
3057
Feng Tang49e639e2012-10-30 11:56:03 +08003058 closedir(scripts_dir);
3059 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003060 return i;
3061}
3062
Tom Zanussi38752942009-12-15 02:53:39 -06003063static char *get_script_path(const char *script_root, const char *suffix)
3064{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003065 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003066 char scripts_path[MAXPATHLEN];
3067 char script_path[MAXPATHLEN];
3068 DIR *scripts_dir, *lang_dir;
3069 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003070 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003071
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003072 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003073
3074 scripts_dir = opendir(scripts_path);
3075 if (!scripts_dir)
3076 return NULL;
3077
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003078 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003079 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3080 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003081 lang_dir = opendir(lang_path);
3082 if (!lang_dir)
3083 continue;
3084
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003085 for_each_script(lang_path, lang_dir, script_dirent) {
3086 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003087 if (__script_root && !strcmp(script_root, __script_root)) {
3088 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003089 closedir(lang_dir);
3090 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003091 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3092 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003093 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003094 }
3095 free(__script_root);
3096 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003097 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003098 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003099 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003100
Robert Richter38efb532011-11-25 11:38:40 +01003101 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003102}
3103
Tom Zanussib5b87312010-11-10 08:16:51 -06003104static bool is_top_script(const char *script_path)
3105{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003106 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003107}
3108
3109static int has_required_arg(char *script_path)
3110{
3111 struct script_desc *desc;
3112 int n_args = 0;
3113 char *p;
3114
3115 desc = script_desc__new(NULL);
3116
3117 if (read_script_info(desc, script_path))
3118 goto out;
3119
3120 if (!desc->args)
3121 goto out;
3122
3123 for (p = desc->args; *p; p++)
3124 if (*p == '<')
3125 n_args++;
3126out:
3127 script_desc__delete(desc);
3128
3129 return n_args;
3130}
3131
David Ahernd54b1a92012-08-26 12:24:46 -06003132static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003133{
3134 char **__argv = malloc(sizeof(const char *) * argc);
3135
David Ahernd54b1a92012-08-26 12:24:46 -06003136 if (!__argv) {
3137 pr_err("malloc failed\n");
3138 return -1;
3139 }
3140
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003141 memcpy(__argv, argv, sizeof(const char *) * argc);
3142 argc = parse_options(argc, (const char **)__argv, record_options,
3143 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3144 free(__argv);
3145
David Ahernd54b1a92012-08-26 12:24:46 -06003146 system_wide = (argc == 0);
3147
3148 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003149}
3150
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003151static void script__setup_sample_type(struct perf_script *script)
3152{
3153 struct perf_session *session = script->session;
3154 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3155
3156 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3157 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003158 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003159 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003160 dwarf_callchain_users = true;
3161 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003162 callchain_param.record_mode = CALLCHAIN_LBR;
3163 else
3164 callchain_param.record_mode = CALLCHAIN_FP;
3165 }
3166}
3167
Jiri Olsa89f16882018-09-13 14:54:03 +02003168static int process_stat_round_event(struct perf_session *session,
3169 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003170{
3171 struct stat_round_event *round = &event->stat_round;
3172 struct perf_evsel *counter;
3173
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003174 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003175 perf_stat_process_counter(&stat_config, counter);
3176 process_stat(counter, round->time);
3177 }
3178
3179 process_stat_interval(round->time);
3180 return 0;
3181}
3182
Jiri Olsa89f16882018-09-13 14:54:03 +02003183static int process_stat_config_event(struct perf_session *session __maybe_unused,
3184 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003185{
3186 perf_event__read_stat_config(&stat_config, &event->stat_config);
3187 return 0;
3188}
3189
Jiri Olsacfc88742016-01-05 22:09:06 +01003190static int set_maps(struct perf_script *script)
3191{
3192 struct perf_evlist *evlist = script->session->evlist;
3193
3194 if (!script->cpus || !script->threads)
3195 return 0;
3196
3197 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3198 return -EINVAL;
3199
3200 perf_evlist__set_maps(evlist, script->cpus, script->threads);
3201
3202 if (perf_evlist__alloc_stats(evlist, true))
3203 return -ENOMEM;
3204
3205 script->allocated = true;
3206 return 0;
3207}
3208
3209static
Jiri Olsa89f16882018-09-13 14:54:03 +02003210int process_thread_map_event(struct perf_session *session,
3211 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003212{
Jiri Olsa89f16882018-09-13 14:54:03 +02003213 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003214 struct perf_script *script = container_of(tool, struct perf_script, tool);
3215
3216 if (script->threads) {
3217 pr_warning("Extra thread map event, ignoring.\n");
3218 return 0;
3219 }
3220
3221 script->threads = thread_map__new_event(&event->thread_map);
3222 if (!script->threads)
3223 return -ENOMEM;
3224
3225 return set_maps(script);
3226}
3227
3228static
Jiri Olsa89f16882018-09-13 14:54:03 +02003229int process_cpu_map_event(struct perf_session *session,
3230 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003231{
Jiri Olsa89f16882018-09-13 14:54:03 +02003232 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003233 struct perf_script *script = container_of(tool, struct perf_script, tool);
3234
3235 if (script->cpus) {
3236 pr_warning("Extra cpu map event, ignoring.\n");
3237 return 0;
3238 }
3239
3240 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3241 if (!script->cpus)
3242 return -ENOMEM;
3243
3244 return set_maps(script);
3245}
3246
Jiri Olsa89f16882018-09-13 14:54:03 +02003247static int process_feature_event(struct perf_session *session,
3248 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303249{
3250 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003251 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303252 return 0;
3253}
3254
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003255#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003256static int perf_script__process_auxtrace_info(struct perf_session *session,
3257 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003258{
Jiri Olsa89f16882018-09-13 14:54:03 +02003259 struct perf_tool *tool = session->tool;
3260
3261 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003262
3263 if (ret == 0) {
3264 struct perf_script *script = container_of(tool, struct perf_script, tool);
3265
3266 ret = perf_script__setup_per_event_dump(script);
3267 }
3268
3269 return ret;
3270}
3271#else
3272#define perf_script__process_auxtrace_info 0
3273#endif
3274
Andi Kleenb585ebd2018-09-20 11:05:36 -07003275static int parse_insn_trace(const struct option *opt __maybe_unused,
3276 const char *str __maybe_unused,
3277 int unset __maybe_unused)
3278{
3279 parse_output_fields(NULL, "+insn,-event,-period", 0);
3280 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003281 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003282 return 0;
3283}
3284
3285static int parse_xed(const struct option *opt __maybe_unused,
3286 const char *str __maybe_unused,
3287 int unset __maybe_unused)
3288{
3289 force_pager("xed -F insn: -A -64 | less");
3290 return 0;
3291}
3292
Andi Kleend1b15522018-09-20 11:05:38 -07003293static int parse_call_trace(const struct option *opt __maybe_unused,
3294 const char *str __maybe_unused,
3295 int unset __maybe_unused)
3296{
3297 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3298 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003299 symbol_conf.nanosecs = true;
Jiri Olsa1c492422019-05-08 15:20:05 +02003300 symbol_conf.pad_output_len_dso = 50;
Andi Kleend1b15522018-09-20 11:05:38 -07003301 return 0;
3302}
3303
3304static int parse_callret_trace(const struct option *opt __maybe_unused,
3305 const char *str __maybe_unused,
3306 int unset __maybe_unused)
3307{
3308 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3309 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003310 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003311 return 0;
3312}
3313
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003314int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003315{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003316 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003317 bool header = false;
3318 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003319 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003320 char *rec_script_path = NULL;
3321 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003322 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003323 struct itrace_synth_opts itrace_synth_opts = {
3324 .set = false,
3325 .default_no_sample = true,
3326 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003327 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003328 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003329 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003330 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003331 struct perf_script script = {
3332 .tool = {
3333 .sample = process_sample_event,
3334 .mmap = perf_event__process_mmap,
3335 .mmap2 = perf_event__process_mmap2,
3336 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303337 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003338 .exit = perf_event__process_exit,
3339 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003340 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003341 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003342 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303343 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003344 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003345 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003346 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003347 .auxtrace = perf_event__process_auxtrace,
3348 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003349 .stat = perf_event__process_stat_event,
3350 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003351 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003352 .thread_map = process_thread_map_event,
3353 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003354 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003355 .ordering_requires_timestamps = true,
3356 },
3357 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003358 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003359 .mode = PERF_DATA_MODE_READ,
3360 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003361 const struct option options[] = {
3362 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3363 "dump raw trace in ASCII"),
3364 OPT_INCR('v', "verbose", &verbose,
3365 "be more verbose (show symbol address, etc)"),
3366 OPT_BOOLEAN('L', "Latency", &latency_format,
3367 "show latency attributes (irqs/preemption disabled, etc)"),
3368 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3369 list_available_scripts),
3370 OPT_CALLBACK('s', "script", NULL, "name",
3371 "script file name (lang:script name, script name, or *)",
3372 parse_scriptname),
3373 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3374 "generate perf-script.xx script in specified language"),
3375 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3376 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3377 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003378 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3379 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003380 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3381 "file", "vmlinux pathname"),
3382 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3383 "file", "kallsyms pathname"),
3384 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3385 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003386 OPT_CALLBACK(0, "symfs", NULL, "directory",
3387 "Look for files with symbols relative to this directory",
3388 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003389 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003390 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003391 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003392 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003393 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303394 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3395 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
3396 "callindent,insn,insnlen,synth,phys_addr,metric,misc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003397 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003398 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3399 "system-wide collection from all CPUs"),
3400 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3401 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003402 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3403 "Decode instructions from itrace", parse_insn_trace),
3404 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3405 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003406 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3407 "Decode calls from from itrace", parse_call_trace),
3408 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3409 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003410 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3411 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003412 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3413 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003414 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3415 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3416 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003417 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3418 "only consider symbols in these pids"),
3419 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3420 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003421 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3422 "Set the maximum stack depth when parsing the callchain, "
3423 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003424 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Andi Kleen90b10f42019-03-14 15:50:00 -07003425 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003426 OPT_BOOLEAN('I', "show-info", &show_full_info,
3427 "display extended information from perf.data file"),
3428 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3429 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003430 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3431 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003432 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3433 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003434 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3435 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303436 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3437 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003438 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3439 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003440 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3441 "Show round events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003442 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3443 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003444 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003445 OPT_INTEGER(0, "max-blocks", &max_blocks,
3446 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003447 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003448 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003449 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003450 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003451 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003452 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3453 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003454 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3455 "Enable symbol demangling"),
3456 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3457 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003458 OPT_STRING(0, "time", &script.time_str, "str",
3459 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003460 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3461 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003462 OPT_END()
3463 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003464 const char * const script_subcommands[] = { "record", "report", NULL };
3465 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003466 "perf script [<options>]",
3467 "perf script [<options>] record <script> [<record-options>] <command>",
3468 "perf script [<options>] report <script> [script-args]",
3469 "perf script [<options>] <script> [<record-options>] <command>",
3470 "perf script [<options>] <top-script> [script-args]",
3471 NULL
3472 };
Tom Zanussi38752942009-12-15 02:53:39 -06003473
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003474 perf_set_singlethreaded();
3475
Tom Zanussib5b87312010-11-10 08:16:51 -06003476 setup_scripting();
3477
Yunlong Song40cae2b2015-03-18 21:35:54 +08003478 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003479 PARSE_OPT_STOP_AT_NON_OPTION);
3480
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003481 data.path = input_name;
3482 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003483
Tom Zanussib5b87312010-11-10 08:16:51 -06003484 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3485 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3486 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003487 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003488 }
3489
Tom Zanussib5b87312010-11-10 08:16:51 -06003490 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3491 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3492 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003493 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003494 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003495 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003496 return -1;
3497 }
Tom Zanussi38752942009-12-15 02:53:39 -06003498 }
3499
Andi Kleen90b10f42019-03-14 15:50:00 -07003500 if (script.time_str && reltime) {
3501 fprintf(stderr, "Don't combine --reltime with --time\n");
3502 return -1;
3503 }
3504
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003505 if (itrace_synth_opts.callchain &&
3506 itrace_synth_opts.callchain_sz > scripting_max_stack)
3507 scripting_max_stack = itrace_synth_opts.callchain_sz;
3508
Ben Hutchings44e668c2010-10-10 16:10:03 +01003509 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003510 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003511
Tom Zanussib5b87312010-11-10 08:16:51 -06003512 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003513 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003514 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003515 pid_t pid;
3516
Tom Zanussib5b87312010-11-10 08:16:51 -06003517 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3518 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3519
3520 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003521 usage_with_options_msg(script_usage, options,
3522 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003523 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003524 }
3525
Tom Zanussib5b87312010-11-10 08:16:51 -06003526 if (is_top_script(argv[0])) {
3527 rep_args = argc - 1;
3528 } else {
3529 int rec_args;
3530
3531 rep_args = has_required_arg(rep_script_path);
3532 rec_args = (argc - 1) - rep_args;
3533 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003534 usage_with_options_msg(script_usage, options,
3535 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003536 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003537 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003538 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003539 }
3540
3541 if (pipe(live_pipe) < 0) {
3542 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003543 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003544 }
3545
3546 pid = fork();
3547 if (pid < 0) {
3548 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003549 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003550 }
3551
3552 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003553 j = 0;
3554
Tom Zanussia0cccc22010-04-01 23:59:25 -05003555 dup2(live_pipe[1], 1);
3556 close(live_pipe[0]);
3557
Robert Richter317df652011-11-25 15:05:25 +01003558 if (is_top_script(argv[0])) {
3559 system_wide = true;
3560 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003561 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3562 err = -1;
3563 goto out;
3564 }
Robert Richter317df652011-11-25 15:05:25 +01003565 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003566
3567 __argv = malloc((argc + 6) * 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 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003573
Tom Zanussib5b87312010-11-10 08:16:51 -06003574 __argv[j++] = "/bin/sh";
3575 __argv[j++] = rec_script_path;
3576 if (system_wide)
3577 __argv[j++] = "-a";
3578 __argv[j++] = "-q";
3579 __argv[j++] = "-o";
3580 __argv[j++] = "-";
3581 for (i = rep_args + 1; i < argc; i++)
3582 __argv[j++] = argv[i];
3583 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003584
3585 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003586 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003587 exit(-1);
3588 }
3589
3590 dup2(live_pipe[0], 0);
3591 close(live_pipe[1]);
3592
Tom Zanussib5b87312010-11-10 08:16:51 -06003593 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003594 if (!__argv) {
3595 pr_err("malloc failed\n");
3596 err = -ENOMEM;
3597 goto out;
3598 }
3599
Tom Zanussib5b87312010-11-10 08:16:51 -06003600 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003601 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003602 __argv[j++] = rep_script_path;
3603 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003604 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003605 __argv[j++] = "-i";
3606 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003607 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003608
3609 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003610 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003611 exit(-1);
3612 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003613
Tom Zanussib5b87312010-11-10 08:16:51 -06003614 if (rec_script_path)
3615 script_path = rec_script_path;
3616 if (rep_script_path)
3617 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003618
Tom Zanussib5b87312010-11-10 08:16:51 -06003619 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003620 j = 0;
3621
Robert Richter317df652011-11-25 15:05:25 +01003622 if (!rec_script_path)
3623 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003624 else if (!system_wide) {
3625 if (have_cmd(argc - 1, &argv[1]) != 0) {
3626 err = -1;
3627 goto out;
3628 }
3629 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003630
3631 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003632 if (!__argv) {
3633 pr_err("malloc failed\n");
3634 err = -ENOMEM;
3635 goto out;
3636 }
3637
Tom Zanussib5b87312010-11-10 08:16:51 -06003638 __argv[j++] = "/bin/sh";
3639 __argv[j++] = script_path;
3640 if (system_wide)
3641 __argv[j++] = "-a";
3642 for (i = 2; i < argc; i++)
3643 __argv[j++] = argv[i];
3644 __argv[j++] = NULL;
3645
3646 execvp("/bin/sh", (char **)__argv);
3647 free(__argv);
3648 exit(-1);
3649 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003650
Milian Wolffc1c9b962018-10-21 21:14:23 +02003651 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003652 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003653 use_browser = 0;
3654 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003655
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003656 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003657 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003658 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003659
Jiri Olsae90debd2013-12-09 11:02:50 +01003660 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003661 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003662 perf_session__fprintf_info(session, stdout, show_full_info);
3663 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003664 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003665 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003666 if (show_full_info)
3667 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003668
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003669 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003670 goto out_delete;
3671
Andi Kleen3ab481a2019-03-05 06:47:45 -08003672 uname(&uts);
3673 if (!strcmp(uts.machine, session->header.env.arch) ||
3674 (!strcmp(uts.machine, "x86_64") &&
3675 !strcmp(session->header.env.arch, "i386")))
3676 native_arch = true;
3677
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003678 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003679 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003680
Andi Kleen99f753f2018-09-20 11:05:39 -07003681 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3682 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003683 itrace_synth_opts.thread_stack = true;
3684
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003685 session->itrace_synth_opts = &itrace_synth_opts;
3686
Anton Blanchard5d67be92011-07-04 21:57:50 +10003687 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003688 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3689 if (err < 0)
3690 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003691 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003692 }
3693
David Ahern1424dc92011-03-09 22:23:28 -07003694 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003695 symbol_conf.use_callchain = true;
3696 else
3697 symbol_conf.use_callchain = false;
3698
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003699 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003700 tep_set_function_resolver(session->tevent.pevent,
3701 machine__resolve_kernel_addr,
3702 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003703 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003704 err = -1;
3705 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003706 }
3707
Tom Zanussi956ffd02009-11-25 01:15:46 -06003708 if (generate_script_lang) {
3709 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003710 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003711
David Ahern2c9e45f72011-03-17 10:03:21 -06003712 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003713 fprintf(stderr,
3714 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003715 err = -EINVAL;
3716 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003717 }
3718
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003719 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003720 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003721 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003722 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003723 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003724 }
3725
3726 err = fstat(input, &perf_stat);
3727 if (err < 0) {
3728 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003729 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003730 }
3731
3732 if (!perf_stat.st_size) {
3733 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003734 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003735 }
3736
3737 scripting_ops = script_spec__lookup(generate_script_lang);
3738 if (!scripting_ops) {
3739 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003740 err = -ENOENT;
3741 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003742 }
3743
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003744 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003745 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003746 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003747 }
3748
3749 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003750 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003751 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003752 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003753 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003754 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003755 }
3756
David Ahern9cbdb702011-04-06 21:54:20 -06003757
3758 err = perf_session__check_output_opt(session);
3759 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003760 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003761
Jin Yao284c4e12019-03-01 18:13:06 +08003762 if (script.time_str) {
3763 err = perf_time__parse_for_ranges(script.time_str, session,
3764 &script.ptime_range,
3765 &script.range_size,
3766 &script.range_num);
3767 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08003768 goto out_delete;
David Aherna91f4c42016-11-29 10:15:43 -07003769 }
3770
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003771 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003772
Adrian Hunterd445dd22014-08-15 22:08:37 +03003773 flush_scripting();
3774
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003775out_delete:
Jin Yao284c4e12019-03-01 18:13:06 +08003776 if (script.ptime_range)
3777 zfree(&script.ptime_range);
Jin Yaocc2ef582018-01-10 23:00:33 +08003778
Jiri Olsacfc88742016-01-05 22:09:06 +01003779 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003780 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003781
3782 if (script_started)
3783 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003784out:
3785 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003786}