blob: 80c722ade852f97499eab992aab6ff7d8e886239 [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,
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300105 PERF_OUTPUT_IPC = 1U << 31,
David Ahern745f43e2011-03-09 22:23:26 -0700106};
107
108struct output_option {
109 const char *str;
110 enum perf_output_field field;
111} all_output_options[] = {
112 {.str = "comm", .field = PERF_OUTPUT_COMM},
113 {.str = "tid", .field = PERF_OUTPUT_TID},
114 {.str = "pid", .field = PERF_OUTPUT_PID},
115 {.str = "time", .field = PERF_OUTPUT_TIME},
116 {.str = "cpu", .field = PERF_OUTPUT_CPU},
117 {.str = "event", .field = PERF_OUTPUT_EVNAME},
118 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600119 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700120 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600121 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600122 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900123 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200124 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200125 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200126 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700127 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200128 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
129 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100130 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
131 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000132 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300133 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300134 {.str = "insn", .field = PERF_OUTPUT_INSN},
135 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800136 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700137 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300138 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400139 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800140 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100141 {.str = "misc", .field = PERF_OUTPUT_MISC},
Andi Kleendd2e18e2018-12-03 16:18:48 -0800142 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300143 {.str = "ipc", .field = PERF_OUTPUT_IPC},
David Ahern745f43e2011-03-09 22:23:26 -0700144};
145
Adrian Hunter14057202017-06-21 13:17:19 +0300146enum {
147 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
148 OUTPUT_TYPE_MAX
149};
150
David Ahern745f43e2011-03-09 22:23:26 -0700151/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600152static struct {
153 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600154 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400155 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600156 u64 fields;
157 u64 invalid_fields;
Andi Kleen4b6ac812019-02-24 07:37:12 -0800158 u64 user_set_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300159} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700160
David Ahern2c9e45f72011-03-17 10:03:21 -0600161 [PERF_TYPE_HARDWARE] = {
162 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700163
David Ahern2c9e45f72011-03-17 10:03:21 -0600164 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
165 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600166 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530167 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
168 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600169
Wang Nan30372f02016-02-24 11:20:45 +0000170 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600171 },
172
173 [PERF_TYPE_SOFTWARE] = {
174 .user_set = false,
175
176 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
177 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600178 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530179 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
180 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
181 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600182
183 .invalid_fields = PERF_OUTPUT_TRACE,
184 },
185
186 [PERF_TYPE_TRACEPOINT] = {
187 .user_set = false,
188
189 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
190 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000191 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600192 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700193
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530194 [PERF_TYPE_HW_CACHE] = {
195 .user_set = false,
196
197 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
198 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
199 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
200 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
201 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
202
203 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
204 },
205
Arun Sharma0817a6a2011-04-14 10:38:18 -0700206 [PERF_TYPE_RAW] = {
207 .user_set = false,
208
209 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
210 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600211 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530212 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
213 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
214 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
215 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700216
Wang Nan30372f02016-02-24 11:20:45 +0000217 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700218 },
Wang Nan27cfef02015-12-08 02:25:43 +0000219
220 [PERF_TYPE_BREAKPOINT] = {
221 .user_set = false,
222
223 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
224 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
225 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530226 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
227 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000228
Wang Nan30372f02016-02-24 11:20:45 +0000229 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000230 },
Adrian Hunter14057202017-06-21 13:17:19 +0300231
232 [OUTPUT_TYPE_SYNTH] = {
233 .user_set = false,
234
235 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
236 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
237 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530238 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
239 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300240
241 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
242 },
David Ahern1424dc92011-03-09 22:23:28 -0700243};
David Ahern745f43e2011-03-09 22:23:26 -0700244
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300245struct perf_evsel_script {
246 char *filename;
247 FILE *fp;
248 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800249 /* For metric output */
250 u64 val;
251 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300252};
253
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800254static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
255{
256 return (struct perf_evsel_script *)evsel->priv;
257}
258
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300259static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100260 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300261{
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800262 struct perf_evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300263
264 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100265 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300266 goto out_free;
267 es->fp = fopen(es->filename, "w");
268 if (es->fp == NULL)
269 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300270 }
271
272 return es;
273out_free_filename:
274 zfree(&es->filename);
275out_free:
276 free(es);
277 return NULL;
278}
279
280static void perf_evsel_script__delete(struct perf_evsel_script *es)
281{
282 zfree(&es->filename);
283 fclose(es->fp);
284 es->fp = NULL;
285 free(es);
286}
287
288static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
289{
290 struct stat st;
291
292 fstat(fileno(es->fp), &st);
293 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
294 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
295}
296
Adrian Hunter14057202017-06-21 13:17:19 +0300297static inline int output_type(unsigned int type)
298{
299 switch (type) {
300 case PERF_TYPE_SYNTH:
301 return OUTPUT_TYPE_SYNTH;
302 default:
303 return type;
304 }
305}
306
307static inline unsigned int attr_type(unsigned int type)
308{
309 switch (type) {
310 case OUTPUT_TYPE_SYNTH:
311 return PERF_TYPE_SYNTH;
312 default:
313 return type;
314 }
315}
316
David Ahern2c9e45f72011-03-17 10:03:21 -0600317static bool output_set_by_user(void)
318{
319 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300320 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600321 if (output[j].user_set)
322 return true;
323 }
324 return false;
325}
David Ahern745f43e2011-03-09 22:23:26 -0700326
David Ahern9cbdb702011-04-06 21:54:20 -0600327static const char *output_field2str(enum perf_output_field field)
328{
329 int i, imax = ARRAY_SIZE(all_output_options);
330 const char *str = "";
331
332 for (i = 0; i < imax; ++i) {
333 if (all_output_options[i].field == field) {
334 str = all_output_options[i].str;
335 break;
336 }
337 }
338 return str;
339}
340
Adrian Hunter14057202017-06-21 13:17:19 +0300341#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700342
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300343static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
344 u64 sample_type, const char *sample_msg,
345 enum perf_output_field field,
346 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700347{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300348 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300349 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600350 const char *evname;
351
352 if (attr->sample_type & sample_type)
353 return 0;
354
Andi Kleen4b6ac812019-02-24 07:37:12 -0800355 if (output[type].user_set_fields & field) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300356 if (allow_user_set)
357 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300358 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600359 pr_err("Samples for '%s' event do not have %s attribute set. "
360 "Cannot print '%s' field.\n",
361 evname, sample_msg, output_field2str(field));
362 return -1;
363 }
364
365 /* user did not ask for it explicitly so remove from the default list */
366 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300367 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600368 pr_debug("Samples for '%s' event do not have %s attribute set. "
369 "Skipping '%s' field.\n",
370 evname, sample_msg, output_field2str(field));
371
372 return 0;
373}
374
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300375static int perf_evsel__check_stype(struct perf_evsel *evsel,
376 u64 sample_type, const char *sample_msg,
377 enum perf_output_field field)
378{
379 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
380 false);
381}
382
David Ahern9cbdb702011-04-06 21:54:20 -0600383static int perf_evsel__check_attr(struct perf_evsel *evsel,
384 struct perf_session *session)
385{
386 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300387 bool allow_user_set;
388
Jiri Olsae099eba2016-01-05 22:09:09 +0100389 if (perf_header__has_feat(&session->header, HEADER_STAT))
390 return 0;
391
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300392 allow_user_set = perf_header__has_feat(&session->header,
393 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600394
David Ahern1424dc92011-03-09 22:23:28 -0700395 if (PRINT_FIELD(TRACE) &&
396 !perf_session__has_traces(session, "record -R"))
397 return -EINVAL;
398
David Ahern787bef12011-05-27 14:28:43 -0600399 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300400 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
401 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700402 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700403 }
David Ahern7cec0922011-05-30 13:08:23 -0600404
405 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300406 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
407 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600408 return -EINVAL;
409
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100410 if (PRINT_FIELD(DATA_SRC) &&
411 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
412 PERF_OUTPUT_DATA_SRC))
413 return -EINVAL;
414
415 if (PRINT_FIELD(WEIGHT) &&
416 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
417 PERF_OUTPUT_WEIGHT))
418 return -EINVAL;
419
Andi Kleen37fed3d2018-09-18 05:32:09 -0700420 if (PRINT_FIELD(SYM) &&
421 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600422 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700423 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600424 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600425 return -EINVAL;
426 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900427 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
428 pr_err("Display of offsets requested but symbol is not"
429 "selected.\n");
430 return -EINVAL;
431 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700432 if (PRINT_FIELD(DSO) &&
433 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
434 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600435 return -EINVAL;
436 }
Andi Kleendd2e18e2018-12-03 16:18:48 -0800437 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
Adrian Huntercc8fae12013-12-06 09:42:57 +0200438 pr_err("Display of source line number requested but sample IP is not\n"
439 "selected. Hence, no address to lookup the source line number.\n");
440 return -EINVAL;
441 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800442 if (PRINT_FIELD(BRSTACKINSN) &&
443 !(perf_evlist__combined_branch_type(session->evlist) &
444 PERF_SAMPLE_BRANCH_ANY)) {
445 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
446 "Hint: run 'perf record -b ...'\n");
447 return -EINVAL;
448 }
David Ahern1424dc92011-03-09 22:23:28 -0700449 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300450 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
451 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700452 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700453
454 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300455 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
456 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700457 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700458
459 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300460 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
461 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700462 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600463
Stephane Eranianfc36f942015-08-31 18:41:10 +0200464 if (PRINT_FIELD(IREGS) &&
465 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
466 PERF_OUTPUT_IREGS))
467 return -EINVAL;
468
Andi Kleenb1491ac2017-09-05 11:40:57 -0700469 if (PRINT_FIELD(UREGS) &&
470 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
471 PERF_OUTPUT_UREGS))
472 return -EINVAL;
473
Kan Liang49d58f02017-08-29 13:11:11 -0400474 if (PRINT_FIELD(PHYS_ADDR) &&
475 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
476 PERF_OUTPUT_PHYS_ADDR))
477 return -EINVAL;
478
David Ahern9cbdb702011-04-06 21:54:20 -0600479 return 0;
480}
481
Adrian Hunter7ea95722013-11-01 15:51:30 +0200482static void set_print_ip_opts(struct perf_event_attr *attr)
483{
Adrian Hunter14057202017-06-21 13:17:19 +0300484 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200485
486 output[type].print_ip_opts = 0;
487 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300488 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200489
490 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300491 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200492
493 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300494 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200495
496 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300497 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200498
499 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300500 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200501}
502
David Ahern9cbdb702011-04-06 21:54:20 -0600503/*
504 * verify all user requested events exist and the samples
505 * have the expected data
506 */
507static int perf_session__check_output_opt(struct perf_session *session)
508{
He Kuang40f20e52016-05-16 04:51:19 +0000509 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600510 struct perf_evsel *evsel;
511
Adrian Hunter14057202017-06-21 13:17:19 +0300512 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
513 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600514
515 /*
516 * even if fields is set to 0 (ie., show nothing) event must
517 * exist if user explicitly includes it on the command line
518 */
Adrian Hunter14057202017-06-21 13:17:19 +0300519 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
520 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600521 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300522 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600523 event_type(j));
524 return -1;
525 }
526
527 if (evsel && output[j].fields &&
528 perf_evsel__check_attr(evsel, session))
529 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400530
531 if (evsel == NULL)
532 continue;
533
Adrian Hunter7ea95722013-11-01 15:51:30 +0200534 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700535 }
536
Adrian Hunter98526ee2014-07-31 09:00:59 +0300537 if (!no_callchain) {
538 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000539 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300540
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300541 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000542 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300543 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300544 use_callchain = true;
545 break;
546 }
547 }
He Kuang71ac8992016-08-04 11:25:43 +0000548 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300549 symbol_conf.use_callchain = false;
550 }
551
David Ahern80b8b492013-11-19 21:07:37 -0700552 /*
553 * set default for tracepoints to print symbols only
554 * if callchains are present
555 */
556 if (symbol_conf.use_callchain &&
557 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700558 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700559
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300560 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000561 if (evsel->attr.type != j)
562 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700563
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300564 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000565 output[j].fields |= PERF_OUTPUT_IP;
566 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530567 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000568 output[j].fields |= PERF_OUTPUT_DSO;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300569 set_print_ip_opts(&evsel->attr);
He Kuang40f20e52016-05-16 04:51:19 +0000570 goto out;
571 }
David Ahern80b8b492013-11-19 21:07:37 -0700572 }
573 }
574
575out:
David Ahern1424dc92011-03-09 22:23:28 -0700576 return 0;
577}
David Ahern745f43e2011-03-09 22:23:26 -0700578
Milian Wolff9add8fe2018-11-07 23:34:37 +0100579static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
580 FILE *fp
581)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200582{
Andi Kleenb1491ac2017-09-05 11:40:57 -0700583 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300584 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700585
586 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300587 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700588
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300589 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700590
591 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
592 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300593 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700594 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300595
Milian Wolffb07d16f2018-11-07 10:37:05 +0100596 fprintf(fp, "\n");
597
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300598 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700599}
600
Milian Wolff9add8fe2018-11-07 23:34:37 +0100601static int perf_sample__fprintf_iregs(struct perf_sample *sample,
602 struct perf_event_attr *attr, FILE *fp)
603{
604 return perf_sample__fprintf_regs(&sample->intr_regs,
605 attr->sample_regs_intr, fp);
606}
607
608static int perf_sample__fprintf_uregs(struct perf_sample *sample,
609 struct perf_event_attr *attr, FILE *fp)
610{
611 return perf_sample__fprintf_regs(&sample->user_regs,
612 attr->sample_regs_user, fp);
613}
614
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300615static int perf_sample__fprintf_start(struct perf_sample *sample,
616 struct thread *thread,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100617 struct perf_evsel *evsel,
618 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700619{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300620 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700621 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700622 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300623 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700624
David Ahern745f43e2011-03-09 22:23:26 -0700625 if (PRINT_FIELD(COMM)) {
626 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300627 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300628 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300629 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700630 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300631 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700632 }
David Ahernc70c94b2011-03-09 22:23:25 -0700633
David Ahern745f43e2011-03-09 22:23:26 -0700634 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300635 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700636 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300637 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700638 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300639 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700640
David Ahern745f43e2011-03-09 22:23:26 -0700641 if (PRINT_FIELD(CPU)) {
642 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300643 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700644 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300645 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700646 }
David Ahernc70c94b2011-03-09 22:23:25 -0700647
Jiri Olsa28a0b392018-01-07 17:03:52 +0100648 if (PRINT_FIELD(MISC)) {
649 int ret = 0;
650
651 #define has(m) \
652 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
653
654 if (has(KERNEL))
655 ret += fprintf(fp, "K");
656 if (has(USER))
657 ret += fprintf(fp, "U");
658 if (has(HYPERVISOR))
659 ret += fprintf(fp, "H");
660 if (has(GUEST_KERNEL))
661 ret += fprintf(fp, "G");
662 if (has(GUEST_USER))
663 ret += fprintf(fp, "g");
664
665 switch (type) {
666 case PERF_RECORD_MMAP:
667 case PERF_RECORD_MMAP2:
668 if (has(MMAP_DATA))
669 ret += fprintf(fp, "M");
670 break;
671 case PERF_RECORD_COMM:
672 if (has(COMM_EXEC))
673 ret += fprintf(fp, "E");
674 break;
675 case PERF_RECORD_SWITCH:
676 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300677 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100678 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300679 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
680 ret += fprintf(fp, "p");
681 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100682 default:
683 break;
684 }
685
686 #undef has
687
688 ret += fprintf(fp, "%*s", 6 - ret, " ");
689 printed += ret;
690 }
691
David Ahern745f43e2011-03-09 22:23:26 -0700692 if (PRINT_FIELD(TIME)) {
Andi Kleen90b10f42019-03-14 15:50:00 -0700693 u64 t = sample->time;
694 if (reltime) {
695 if (!initial_time)
696 initial_time = sample->time;
697 t = sample->time - initial_time;
698 }
699 nsecs = t;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300700 secs = nsecs / NSEC_PER_SEC;
701 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900702
Andi Kleen52bab882019-03-05 06:47:47 -0800703 if (symbol_conf.nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300704 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900705 else {
706 char sample_time[32];
Andi Kleen90b10f42019-03-14 15:50:00 -0700707 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300708 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900709 }
David Ahern745f43e2011-03-09 22:23:26 -0700710 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300711
712 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700713}
714
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200715static inline char
716mispred_str(struct branch_entry *br)
717{
718 if (!(br->flags.mispred || br->flags.predicted))
719 return '-';
720
721 return br->flags.predicted ? 'P' : 'M';
722}
723
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300724static int perf_sample__fprintf_brstack(struct perf_sample *sample,
725 struct thread *thread,
726 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200727{
728 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700729 struct addr_location alf, alt;
730 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300731 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200732
733 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300734 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200735
736 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700737 from = br->entries[i].from;
738 to = br->entries[i].to;
739
740 if (PRINT_FIELD(DSO)) {
741 memset(&alf, 0, sizeof(alf));
742 memset(&alt, 0, sizeof(alt));
Adrian Hunter692d0e62018-11-06 23:07:12 +0200743 thread__find_map_fb(thread, sample->cpumode, from, &alf);
744 thread__find_map_fb(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700745 }
746
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300747 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700748 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300749 printed += fprintf(fp, "(");
750 printed += map__fprintf_dsoname(alf.map, fp);
751 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700752 }
753
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300754 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700755 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300756 printed += fprintf(fp, "(");
757 printed += map__fprintf_dsoname(alt.map, fp);
758 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700759 }
760
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300761 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200762 mispred_str( br->entries + i),
763 br->entries[i].flags.in_tx? 'X' : '-',
764 br->entries[i].flags.abort? 'A' : '-',
765 br->entries[i].flags.cycles);
766 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300767
768 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200769}
770
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300771static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
772 struct thread *thread,
773 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200774{
775 struct branch_stack *br = sample->branch_stack;
776 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200777 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300778 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200779
780 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300781 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200782
783 for (i = 0; i < br->nr; i++) {
784
785 memset(&alf, 0, sizeof(alf));
786 memset(&alt, 0, sizeof(alt));
787 from = br->entries[i].from;
788 to = br->entries[i].to;
789
Adrian Hunter692d0e62018-11-06 23:07:12 +0200790 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
791 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200792
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300793 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700794 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300795 printed += fprintf(fp, "(");
796 printed += map__fprintf_dsoname(alf.map, fp);
797 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700798 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300799 printed += fprintf(fp, "%c", '/');
800 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700801 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300802 printed += fprintf(fp, "(");
803 printed += map__fprintf_dsoname(alt.map, fp);
804 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700805 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300806 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200807 mispred_str( br->entries + i),
808 br->entries[i].flags.in_tx? 'X' : '-',
809 br->entries[i].flags.abort? 'A' : '-',
810 br->entries[i].flags.cycles);
811 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300812
813 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200814}
815
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300816static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
817 struct thread *thread,
818 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700819{
820 struct branch_stack *br = sample->branch_stack;
821 struct addr_location alf, alt;
822 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300823 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700824
825 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300826 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700827
828 for (i = 0; i < br->nr; i++) {
829
830 memset(&alf, 0, sizeof(alf));
831 memset(&alt, 0, sizeof(alt));
832 from = br->entries[i].from;
833 to = br->entries[i].to;
834
Adrian Hunter692d0e62018-11-06 23:07:12 +0200835 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300836 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700837 from = map__map_ip(alf.map, from);
838
Adrian Hunter692d0e62018-11-06 23:07:12 +0200839 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300840 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700841 to = map__map_ip(alt.map, to);
842
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300843 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700844 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300845 printed += fprintf(fp, "(");
846 printed += map__fprintf_dsoname(alf.map, fp);
847 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700848 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300849 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700850 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300851 printed += fprintf(fp, "(");
852 printed += map__fprintf_dsoname(alt.map, fp);
853 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700854 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300855 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700856 mispred_str(br->entries + i),
857 br->entries[i].flags.in_tx ? 'X' : '-',
858 br->entries[i].flags.abort ? 'A' : '-',
859 br->entries[i].flags.cycles);
860 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300861
862 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700863}
Andi Kleen48d02a12017-02-23 15:46:34 -0800864#define MAXBB 16384UL
865
866static int grab_bb(u8 *buffer, u64 start, u64 end,
867 struct machine *machine, struct thread *thread,
868 bool *is64bit, u8 *cpumode, bool last)
869{
870 long offset, len;
871 struct addr_location al;
872 bool kernel;
873
874 if (!start || !end)
875 return 0;
876
877 kernel = machine__kernel_ip(machine, start);
878 if (kernel)
879 *cpumode = PERF_RECORD_MISC_KERNEL;
880 else
881 *cpumode = PERF_RECORD_MISC_USER;
882
883 /*
884 * Block overlaps between kernel and user.
885 * This can happen due to ring filtering
886 * On Intel CPUs the entry into the kernel is filtered,
887 * but the exit is not. Let the caller patch it up.
888 */
889 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300890 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800891 return -ENXIO;
892 }
893
894 memset(&al, 0, sizeof(al));
895 if (end - start > MAXBB - MAXINSN) {
896 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300897 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800898 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300899 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800900 return 0;
901 }
902
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300903 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300904 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800905 return 0;
906 }
907 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300908 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800909 return 0;
910 }
911
912 /* Load maps to ensure dso->is_64_bit has been updated */
913 map__load(al.map);
914
915 offset = al.map->map_ip(al.map, start);
916 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
917 end - start + MAXINSN);
918
919 *is64bit = al.map->dso->is_64_bit;
920 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300921 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800922 start, end);
923 return len;
924}
925
Andi Kleendd2e18e2018-12-03 16:18:48 -0800926static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
927{
928 struct addr_location al;
929 int ret = 0;
930
931 memset(&al, 0, sizeof(al));
932 thread__find_map(thread, cpumode, addr, &al);
933 if (!al.map)
934 return 0;
935 ret = map__fprintf_srccode(al.map, al.addr, stdout,
936 &thread->srccode_state);
937 if (ret)
938 ret += printf("\n");
939 return ret;
940}
941
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300942static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
943 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -0700944 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -0800945{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300946 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
947 dump_insn(x, ip, inbuf, len, NULL),
948 en->flags.predicted ? " PRED" : "",
949 en->flags.mispred ? " MISPRED" : "",
950 en->flags.in_tx ? " INTX" : "",
951 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800952 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -0700953 *total_cycles += en->flags.cycles;
954 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800955 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300956 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800957 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300958 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800959}
960
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300961static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
962 u8 cpumode, int cpu, struct symbol **lastsym,
963 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800964{
965 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300966 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800967
968 memset(&al, 0, sizeof(al));
969
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300970 thread__find_map(thread, cpumode, addr, &al);
971
Andi Kleen48d02a12017-02-23 15:46:34 -0800972 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300973 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800974
975 al.cpu = cpu;
976 al.sym = NULL;
977 if (al.map)
978 al.sym = map__find_symbol(al.map, al.addr);
979
980 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300981 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800982
983 if (al.addr < al.sym->end)
984 off = al.addr - al.sym->start;
985 else
986 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300987 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800988 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300989 printed += fprintf(fp, "%+d", off);
990 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800991 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300992 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
993 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800994 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300995
996 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800997}
998
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300999static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1000 struct thread *thread,
1001 struct perf_event_attr *attr,
1002 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -08001003{
1004 struct branch_stack *br = sample->branch_stack;
1005 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001006 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001007 struct perf_insn x;
1008 u8 buffer[MAXBB];
1009 unsigned off;
1010 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -07001011 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001012
1013 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001014 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001015 nr = br->nr;
1016 if (max_blocks && nr > max_blocks + 1)
1017 nr = max_blocks + 1;
1018
1019 x.thread = thread;
1020 x.cpu = sample->cpu;
1021
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001022 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -08001023
1024 /* Handle first from jump, of which we don't know the entry. */
1025 len = grab_bb(buffer, br->entries[nr-1].from,
1026 br->entries[nr-1].from,
1027 machine, thread, &x.is64bit, &x.cpumode, false);
1028 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001029 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1030 x.cpumode, x.cpu, &lastsym, attr, fp);
1031 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001032 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001033 if (PRINT_FIELD(SRCCODE))
1034 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
Andi Kleen48d02a12017-02-23 15:46:34 -08001035 }
1036
1037 /* Print all blocks */
1038 for (i = nr - 2; i >= 0; i--) {
1039 if (br->entries[i].from || br->entries[i].to)
1040 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1041 br->entries[i].from,
1042 br->entries[i].to);
1043 start = br->entries[i + 1].to;
1044 end = br->entries[i].from;
1045
1046 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1047 /* Patch up missing kernel transfers due to ring filters */
1048 if (len == -ENXIO && i > 0) {
1049 end = br->entries[--i].from;
1050 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1051 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1052 }
1053 if (len <= 0)
1054 continue;
1055
1056 insn = 0;
1057 for (off = 0;; off += ilen) {
1058 uint64_t ip = start + off;
1059
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001060 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001061 if (ip == end) {
Andi Kleenfe571202018-09-24 10:07:32 -07001062 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp,
1063 &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001064 if (PRINT_FIELD(SRCCODE))
1065 printed += print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001066 break;
1067 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001068 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1069 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001070 if (ilen == 0)
1071 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001072 if (PRINT_FIELD(SRCCODE))
1073 print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001074 insn++;
1075 }
1076 }
1077 }
1078
1079 /*
1080 * Hit the branch? In this case we are already done, and the target
1081 * has not been executed yet.
1082 */
1083 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001084 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001085 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001086 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001087
1088 /*
1089 * Print final block upto sample
Andi Kleen61f61152018-11-19 21:06:17 -08001090 *
1091 * Due to pipeline delays the LBRs might be missing a branch
1092 * or two, which can result in very large or negative blocks
1093 * between final branch and sample. When this happens just
1094 * continue walking after the last TO until we hit a branch.
Andi Kleen48d02a12017-02-23 15:46:34 -08001095 */
1096 start = br->entries[0].to;
1097 end = sample->ip;
Andi Kleen61f61152018-11-19 21:06:17 -08001098 if (end < start) {
1099 /* Missing jump. Scan 128 bytes for the next branch */
1100 end = start + 128;
1101 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001102 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001103 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001104 if (len <= 0) {
1105 /* Print at least last IP if basic block did not work */
1106 len = grab_bb(buffer, sample->ip, sample->ip,
1107 machine, thread, &x.is64bit, &x.cpumode, false);
1108 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001109 goto out;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001110 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001111 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001112 if (PRINT_FIELD(SRCCODE))
1113 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001114 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001115 }
1116 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001117 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1118 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001119 if (ilen == 0)
1120 break;
Andi Kleen61f61152018-11-19 21:06:17 -08001121 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1122 /*
1123 * Hit a missing branch. Just stop.
1124 */
1125 printed += fprintf(fp, "\t... not reaching sample ...\n");
1126 break;
1127 }
Andi Kleendd2e18e2018-12-03 16:18:48 -08001128 if (PRINT_FIELD(SRCCODE))
1129 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001130 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001131out:
1132 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001133}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001134
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001135static int perf_sample__fprintf_addr(struct perf_sample *sample,
1136 struct thread *thread,
1137 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001138{
1139 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001140 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001141
1142 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001143 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001144
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001145 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001146
1147 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001148 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001149 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001150 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001151 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001152 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001153 }
1154
1155 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001156 printed += fprintf(fp, " (");
1157 printed += map__fprintf_dsoname(al.map, fp);
1158 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001159 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001160out:
1161 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001162}
1163
Andi Kleen99f753f2018-09-20 11:05:39 -07001164static const char *resolve_branch_sym(struct perf_sample *sample,
1165 struct perf_evsel *evsel,
1166 struct thread *thread,
1167 struct addr_location *al,
1168 u64 *ip)
1169{
1170 struct addr_location addr_al;
1171 struct perf_event_attr *attr = &evsel->attr;
1172 const char *name = NULL;
1173
1174 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1175 if (sample_addr_correlates_sym(attr)) {
1176 thread__resolve(thread, &addr_al, sample);
1177 if (addr_al.sym)
1178 name = addr_al.sym->name;
1179 else
1180 *ip = sample->addr;
1181 } else {
1182 *ip = sample->addr;
1183 }
1184 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1185 if (al->sym)
1186 name = al->sym->name;
1187 else
1188 *ip = sample->ip;
1189 }
1190 return name;
1191}
1192
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001193static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1194 struct perf_evsel *evsel,
1195 struct thread *thread,
1196 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001197{
1198 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter256d92b2018-12-21 14:06:19 +02001199 size_t depth = thread_stack__depth(thread, sample->cpu);
Adrian Huntere2167082016-06-23 16:40:58 +03001200 const char *name = NULL;
1201 static int spacing;
1202 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001203 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001204 u64 ip = 0;
1205
1206 /*
1207 * The 'return' has already been popped off the stack so the depth has
1208 * to be adjusted to match the 'call'.
1209 */
1210 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1211 depth += 1;
1212
Andi Kleen99f753f2018-09-20 11:05:39 -07001213 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001214
Andi Kleena78cdee2018-09-18 05:32:10 -07001215 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1216 dlen += fprintf(fp, "(");
1217 dlen += map__fprintf_dsoname(al->map, fp);
1218 dlen += fprintf(fp, ")\t");
1219 }
1220
Adrian Huntere2167082016-06-23 16:40:58 +03001221 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001222 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001223 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001224 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001225
1226 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001227 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001228
1229 /*
1230 * Try to keep the output length from changing frequently so that the
1231 * output lines up more nicely.
1232 */
1233 if (len > spacing || (len && len < spacing - 52))
1234 spacing = round_up(len + 4, 32);
1235
1236 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001237 len += fprintf(fp, "%*s", spacing - len, "");
1238
Andi Kleena78cdee2018-09-18 05:32:10 -07001239 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001240}
1241
Andi Kleen3ab481a2019-03-05 06:47:45 -08001242__weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1243 struct thread *thread __maybe_unused,
1244 struct machine *machine __maybe_unused)
1245{
1246}
1247
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001248static int perf_sample__fprintf_insn(struct perf_sample *sample,
1249 struct perf_event_attr *attr,
1250 struct thread *thread,
1251 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001252{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001253 int printed = 0;
1254
Andi Kleen3ab481a2019-03-05 06:47:45 -08001255 if (sample->insn_len == 0 && native_arch)
1256 arch_fetch_insn(sample, thread, machine);
1257
Andi Kleen224e2c92016-10-07 16:42:27 +03001258 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001259 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen3ab481a2019-03-05 06:47:45 -08001260 if (PRINT_FIELD(INSN) && sample->insn_len) {
Andi Kleen224e2c92016-10-07 16:42:27 +03001261 int i;
1262
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001263 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001264 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001265 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001266 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001267 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001268 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1269
1270 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001271}
1272
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001273static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1274 struct perf_event_attr *attr, FILE *fp)
1275{
1276 unsigned int ipc;
1277
1278 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1279 return 0;
1280
1281 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1282
1283 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1284 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1285}
1286
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001287static int perf_sample__fprintf_bts(struct perf_sample *sample,
1288 struct perf_evsel *evsel,
1289 struct thread *thread,
1290 struct addr_location *al,
1291 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001292{
1293 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001294 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001295 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001296 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001297
Adrian Huntere2167082016-06-23 16:40:58 +03001298 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001299 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001300
Akihiro Nagai95582592012-01-30 13:43:09 +09001301 /* print branch_from information */
1302 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001303 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001304 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001305
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001306 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001307 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001308 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001309 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001310
1311 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001312 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001313 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001314 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001315 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001316 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001317 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001318 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001319
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001320 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001321 }
1322
Akihiro Nagai95582592012-01-30 13:43:09 +09001323 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001324 if (PRINT_FIELD(ADDR) ||
1325 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001326 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001327 printed += fprintf(fp, " => ");
1328 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001329 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001330
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001331 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1332
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001333 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001334 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001335
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001336 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001337 printed += fprintf(fp, "\n");
1338 if (PRINT_FIELD(SRCCODE)) {
1339 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1340 &thread->srccode_state);
1341 if (ret) {
1342 printed += ret;
1343 printed += printf("\n");
1344 }
1345 }
1346 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001347}
1348
Adrian Hunter055cd332016-06-23 16:40:56 +03001349static struct {
1350 u32 flags;
1351 const char *name;
1352} sample_flags[] = {
1353 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1354 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1355 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1356 {PERF_IP_FLAG_BRANCH, "jmp"},
1357 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1358 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1359 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1360 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1361 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1362 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1363 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1364 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1365 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1366 {0, NULL}
1367};
1368
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001369static const char *sample_flags_to_name(u32 flags)
1370{
1371 int i;
1372
1373 for (i = 0; sample_flags[i].name ; i++) {
1374 if (sample_flags[i].flags == flags)
1375 return sample_flags[i].name;
1376 }
1377
1378 return NULL;
1379}
1380
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001381static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001382{
1383 const char *chars = PERF_IP_FLAG_CHARS;
1384 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001385 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1386 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001387 char str[33];
1388 int i, pos = 0;
1389
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001390 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1391 if (name)
1392 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1393
1394 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1395 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1396 if (name)
1397 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1398 }
1399
1400 if (flags & PERF_IP_FLAG_TRACE_END) {
1401 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1402 if (name)
1403 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001404 }
1405
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001406 for (i = 0; i < n; i++, flags >>= 1) {
1407 if (flags & 1)
1408 str[pos++] = chars[i];
1409 }
1410 for (; i < 32; i++, flags >>= 1) {
1411 if (flags & 1)
1412 str[pos++] = '?';
1413 }
1414 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001415
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001416 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001417}
1418
Wang Nan30372f02016-02-24 11:20:45 +00001419struct printer_data {
1420 int line_no;
1421 bool hit_nul;
1422 bool is_printable;
1423};
1424
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001425static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1426 unsigned int val,
1427 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001428{
1429 unsigned char ch = (unsigned char)val;
1430 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001431 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001432
1433 switch (op) {
1434 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001435 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001436 break;
1437 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001438 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001439 " ");
1440 break;
1441 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001442 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001443 break;
1444 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001445 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001446 break;
1447 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001448 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001449 break;
1450 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001451 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001452 break;
1453 case BINARY_PRINT_CHAR_DATA:
1454 if (printer_data->hit_nul && ch)
1455 printer_data->is_printable = false;
1456
1457 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001458 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001459
1460 if (!printer_data->is_printable)
1461 break;
1462
1463 if (ch == '\0')
1464 printer_data->hit_nul = true;
1465 else
1466 printer_data->is_printable = false;
1467 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001468 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001469 }
1470 break;
1471 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001472 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001473 break;
1474 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001475 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001476 printer_data->line_no++;
1477 break;
1478 case BINARY_PRINT_DATA_END:
1479 default:
1480 break;
1481 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001482
1483 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001484}
1485
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001486static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001487{
1488 unsigned int nr_bytes = sample->raw_size;
1489 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001490 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1491 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001492
1493 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001494 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1495
1496 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001497}
1498
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001499static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001500{
1501 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001502 return fprintf(fp, "%*s", spacing - len, "");
1503
1504 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001505}
1506
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001507static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001508{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001509 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001510}
1511
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001512static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001513{
1514 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1515 int len;
1516
1517 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001518 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001519
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001520 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001521 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001522 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001523}
1524
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001525static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001526{
1527 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1528 int len;
1529
1530 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001531 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001532
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001533 len = fprintf(fp, " hints: %#x extensions: %#x ",
1534 data->hints, data->extensions);
1535 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001536}
1537
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001538static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001539{
1540 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1541 int len;
1542
1543 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001544 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001545
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001546 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1547 data->hw, data->cstate, data->subcstate);
1548 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001549}
1550
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001551static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001552{
1553 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1554 int len;
1555
1556 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001557 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001558
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001559 len = fprintf(fp, " IP: %u ", data->ip);
1560 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001561}
1562
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001563static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001564{
1565 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1566 int len;
1567
1568 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001569 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001570
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001571 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001572 data->deepest_cstate, data->last_cstate,
1573 data->wake_reason);
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_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001578{
1579 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1580 unsigned int percent, freq;
1581 int len;
1582
1583 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001584 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001585
1586 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001587 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001588 if (data->max_nonturbo) {
1589 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001590 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001591 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001592 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001593}
1594
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001595static int perf_sample__fprintf_synth(struct perf_sample *sample,
1596 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001597{
1598 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001599 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001600 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001601 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001602 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001603 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001604 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001605 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001606 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001607 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001608 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001609 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001610 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001611 default:
1612 break;
1613 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001614
1615 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001616}
1617
Jiri Olsa809e9422015-11-26 18:55:21 +01001618struct perf_script {
1619 struct perf_tool tool;
1620 struct perf_session *session;
1621 bool show_task_events;
1622 bool show_mmap_events;
1623 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301624 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001625 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001626 bool show_round_events;
Jiri Olsa490c8cc2019-05-08 15:20:08 +02001627 bool show_bpf_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001628 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001629 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001630 struct cpu_map *cpus;
1631 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001632 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001633 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001634 struct perf_time_interval *ptime_range;
1635 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001636 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001637};
1638
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001639static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1640{
1641 struct perf_evsel *evsel;
1642 int max = 0;
1643
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001644 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001645 int len = strlen(perf_evsel__name(evsel));
1646
1647 max = MAX(len, max);
1648 }
1649
1650 return max;
1651}
1652
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001653static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001654{
1655 struct mem_info mi = { .data_src.val = data_src };
1656 char decode[100];
1657 char out[100];
1658 static int maxlen;
1659 int len;
1660
1661 perf_script__meminfo_scnprintf(decode, 100, &mi);
1662
1663 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1664 if (maxlen < len)
1665 maxlen = len;
1666
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001667 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001668}
1669
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001670struct metric_ctx {
1671 struct perf_sample *sample;
1672 struct thread *thread;
1673 struct perf_evsel *evsel;
1674 FILE *fp;
1675};
1676
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001677static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1678 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001679 const char *fmt,
1680 const char *unit, double val)
1681{
1682 struct metric_ctx *mctx = ctx;
1683
1684 if (!fmt)
1685 return;
1686 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001687 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001688 fputs("\tmetric: ", mctx->fp);
1689 if (color)
1690 color_fprintf(mctx->fp, color, fmt, val);
1691 else
1692 printf(fmt, val);
1693 fprintf(mctx->fp, " %s\n", unit);
1694}
1695
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001696static void script_new_line(struct perf_stat_config *config __maybe_unused,
1697 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001698{
1699 struct metric_ctx *mctx = ctx;
1700
1701 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001702 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001703 fputs("\tmetric: ", mctx->fp);
1704}
1705
1706static void perf_sample__fprint_metric(struct perf_script *script,
1707 struct thread *thread,
1708 struct perf_evsel *evsel,
1709 struct perf_sample *sample,
1710 FILE *fp)
1711{
1712 struct perf_stat_output_ctx ctx = {
1713 .print_metric = script_print_metric,
1714 .new_line = script_new_line,
1715 .ctx = &(struct metric_ctx) {
1716 .sample = sample,
1717 .thread = thread,
1718 .evsel = evsel,
1719 .fp = fp,
1720 },
1721 .force_header = false,
1722 };
1723 struct perf_evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001724 u64 val;
1725
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001726 if (!evsel->stats)
1727 perf_evlist__alloc_stats(script->session->evlist, false);
1728 if (evsel_script(evsel->leader)->gnum++ == 0)
1729 perf_stat__reset_shadow_stats();
1730 val = sample->period * evsel->scale;
1731 perf_stat__update_shadow_stats(evsel,
1732 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001733 sample->cpu,
1734 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001735 evsel_script(evsel)->val = val;
1736 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1737 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001738 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001739 evsel_script(ev2)->val,
1740 sample->cpu,
1741 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001742 NULL,
1743 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001744 }
1745 evsel_script(evsel->leader)->gnum = 0;
1746 }
1747}
1748
Andi Kleen99f753f2018-09-20 11:05:39 -07001749static bool show_event(struct perf_sample *sample,
1750 struct perf_evsel *evsel,
1751 struct thread *thread,
1752 struct addr_location *al)
1753{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001754 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001755
1756 if (!symbol_conf.graph_function)
1757 return true;
1758
1759 if (thread->filter) {
1760 if (depth <= thread->filter_entry_depth) {
1761 thread->filter = false;
1762 return false;
1763 }
1764 return true;
1765 } else {
1766 const char *s = symbol_conf.graph_function;
1767 u64 ip;
1768 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1769 &ip);
1770 unsigned nlen;
1771
1772 if (!name)
1773 return false;
1774 nlen = strlen(name);
1775 while (*s) {
1776 unsigned len = strcspn(s, ",");
1777 if (nlen == len && !strncmp(name, s, len)) {
1778 thread->filter = true;
1779 thread->filter_entry_depth = depth;
1780 return true;
1781 }
1782 s += len;
1783 if (*s == ',')
1784 s++;
1785 }
1786 return false;
1787 }
1788}
1789
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001790static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001791 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001792 struct addr_location *al,
1793 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001794{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001795 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001796 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001797 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001798 struct perf_evsel_script *es = evsel->priv;
1799 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001800
Adrian Hunter14057202017-06-21 13:17:19 +03001801 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001802 return;
1803
Andi Kleen99f753f2018-09-20 11:05:39 -07001804 if (!show_event(sample, evsel, thread, al))
1805 return;
1806
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001807 ++es->samples;
1808
Jiri Olsa28a0b392018-01-07 17:03:52 +01001809 perf_sample__fprintf_start(sample, thread, evsel,
1810 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001811
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001812 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001813 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001814
Namhyung Kime944d3d2013-11-18 14:34:52 +09001815 if (PRINT_FIELD(EVNAME)) {
1816 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001817
1818 if (!script->name_width)
1819 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1820
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001821 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001822 }
1823
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001824 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001825 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001826
Akihiro Nagai95582592012-01-30 13:43:09 +09001827 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001828 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001829 return;
1830 }
1831
Andi Kleen96167162019-01-17 11:48:34 -08001832 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001833 event_format__fprintf(evsel->tp_format, sample->cpu,
1834 sample->raw_data, sample->raw_size, fp);
1835 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001836
1837 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001838 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001839
David Ahern7cec0922011-05-30 13:08:23 -06001840 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001841 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001842
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001843 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001844 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001845
1846 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001847 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001848
David Ahern787bef12011-05-27 14:28:43 -06001849 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001850 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001851
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001852 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001853 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001854 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001855 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001856
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001857 fputc(cursor ? '\n' : ' ', fp);
1858 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001859 }
1860
Stephane Eranianfc36f942015-08-31 18:41:10 +02001861 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001862 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001863
Andi Kleenb1491ac2017-09-05 11:40:57 -07001864 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001865 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001866
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001867 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001868 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001869 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001870 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001871 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001872 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001873
Wang Nan30372f02016-02-24 11:20:45 +00001874 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001875 perf_sample__fprintf_bpf_output(sample, fp);
1876 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001877
1878 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001879 fprintf(fp, "%16" PRIx64, sample->phys_addr);
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001880
1881 perf_sample__fprintf_ipc(sample, attr, fp);
1882
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001883 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001884
Andi Kleendd2e18e2018-12-03 16:18:48 -08001885 if (PRINT_FIELD(SRCCODE)) {
1886 if (map__fprintf_srccode(al->map, al->addr, stdout,
1887 &thread->srccode_state))
1888 printf("\n");
1889 }
1890
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001891 if (PRINT_FIELD(METRIC))
1892 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001893
1894 if (verbose)
1895 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001896}
1897
Tom Zanussi956ffd02009-11-25 01:15:46 -06001898static struct scripting_ops *scripting_ops;
1899
Jiri Olsa36e33c52016-01-06 11:49:56 +01001900static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1901{
1902 int nthreads = thread_map__nr(counter->threads);
1903 int ncpus = perf_evsel__nr_cpus(counter);
1904 int cpu, thread;
1905 static int header_printed;
1906
1907 if (counter->system_wide)
1908 nthreads = 1;
1909
1910 if (!header_printed) {
1911 printf("%3s %8s %15s %15s %15s %15s %s\n",
1912 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1913 header_printed = 1;
1914 }
1915
1916 for (thread = 0; thread < nthreads; thread++) {
1917 for (cpu = 0; cpu < ncpus; cpu++) {
1918 struct perf_counts_values *counts;
1919
1920 counts = perf_counts(counter->counts, cpu, thread);
1921
1922 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1923 counter->cpus->map[cpu],
1924 thread_map__pid(counter->threads, thread),
1925 counts->val,
1926 counts->ena,
1927 counts->run,
1928 tstamp,
1929 perf_evsel__name(counter));
1930 }
1931 }
1932}
1933
Jiri Olsae099eba2016-01-05 22:09:09 +01001934static void process_stat(struct perf_evsel *counter, u64 tstamp)
1935{
1936 if (scripting_ops && scripting_ops->process_stat)
1937 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001938 else
1939 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001940}
1941
1942static void process_stat_interval(u64 tstamp)
1943{
1944 if (scripting_ops && scripting_ops->process_stat_interval)
1945 scripting_ops->process_stat_interval(tstamp);
1946}
1947
Tom Zanussi956ffd02009-11-25 01:15:46 -06001948static void setup_scripting(void)
1949{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001950 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001951 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001952}
1953
Adrian Hunterd445dd22014-08-15 22:08:37 +03001954static int flush_scripting(void)
1955{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001956 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001957}
1958
Tom Zanussi956ffd02009-11-25 01:15:46 -06001959static int cleanup_scripting(void)
1960{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001961 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001962
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001963 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001964}
1965
Andi Kleene87e5482019-03-11 07:44:52 -07001966static bool filter_cpu(struct perf_sample *sample)
1967{
1968 if (cpu_list)
1969 return !test_bit(sample->cpu, cpu_bitmap);
1970 return false;
1971}
1972
Jiri Olsa809e9422015-11-26 18:55:21 +01001973static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001974 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001975 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001976 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001977 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001978{
Jiri Olsa809e9422015-11-26 18:55:21 +01001979 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001980 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001981
Jin Yao2ab046c2017-12-08 21:13:46 +08001982 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1983 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001984 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001985 }
David Aherna91f4c42016-11-29 10:15:43 -07001986
David Ahern1424dc92011-03-09 22:23:28 -07001987 if (debug_mode) {
1988 if (sample->time < last_timestamp) {
1989 pr_err("Samples misordered, previous: %" PRIu64
1990 " this: %" PRIu64 "\n", last_timestamp,
1991 sample->time);
1992 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001993 }
David Ahern1424dc92011-03-09 22:23:28 -07001994 last_timestamp = sample->time;
1995 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001996 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001997
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001998 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001999 pr_err("problem processing %d event, skipping it.\n",
2000 event->header.type);
2001 return -1;
2002 }
2003
2004 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002005 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07002006
Andi Kleene87e5482019-03-11 07:44:52 -07002007 if (filter_cpu(sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002008 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002009
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002010 if (scripting_ops)
2011 scripting_ops->process_event(event, sample, evsel, &al);
2012 else
Andi Kleen48d02a12017-02-23 15:46:34 -08002013 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002014
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002015out_put:
2016 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002017 return 0;
2018}
2019
Adrian Hunter7ea95722013-11-01 15:51:30 +02002020static int process_attr(struct perf_tool *tool, union perf_event *event,
2021 struct perf_evlist **pevlist)
2022{
2023 struct perf_script *scr = container_of(tool, struct perf_script, tool);
2024 struct perf_evlist *evlist;
2025 struct perf_evsel *evsel, *pos;
2026 int err;
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302027 static struct perf_evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002028
2029 err = perf_event__process_attr(tool, event, pevlist);
2030 if (err)
2031 return err;
2032
2033 evlist = *pevlist;
2034 evsel = perf_evlist__last(*pevlist);
2035
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302036 if (!evsel->priv) {
2037 if (scr->per_event_dump) {
2038 evsel->priv = perf_evsel_script__new(evsel,
2039 scr->session->data);
2040 } else {
2041 es = zalloc(sizeof(*es));
2042 if (!es)
2043 return -ENOMEM;
2044 es->fp = stdout;
2045 evsel->priv = es;
2046 }
2047 }
2048
Adrian Hunter14057202017-06-21 13:17:19 +03002049 if (evsel->attr.type >= PERF_TYPE_MAX &&
2050 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002051 return 0;
2052
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002053 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02002054 if (pos->attr.type == evsel->attr.type && pos != evsel)
2055 return 0;
2056 }
2057
2058 set_print_ip_opts(&evsel->attr);
2059
Jiri Olsad2b5a312015-10-16 12:41:25 +02002060 if (evsel->attr.sample_type)
2061 err = perf_evsel__check_attr(evsel, scr->session);
2062
2063 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002064}
2065
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002066static int process_comm_event(struct perf_tool *tool,
2067 union perf_event *event,
2068 struct perf_sample *sample,
2069 struct machine *machine)
2070{
2071 struct thread *thread;
2072 struct perf_script *script = container_of(tool, struct perf_script, tool);
2073 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002074 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002075 int ret = -1;
2076
2077 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2078 if (thread == NULL) {
2079 pr_debug("problem processing COMM event, skipping it.\n");
2080 return -1;
2081 }
2082
2083 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2084 goto out;
2085
2086 if (!evsel->attr.sample_id_all) {
2087 sample->cpu = 0;
2088 sample->time = 0;
2089 sample->tid = event->comm.tid;
2090 sample->pid = event->comm.pid;
2091 }
Andi Kleene87e5482019-03-11 07:44:52 -07002092 if (!filter_cpu(sample)) {
2093 perf_sample__fprintf_start(sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01002094 PERF_RECORD_COMM, stdout);
Andi Kleene87e5482019-03-11 07:44:52 -07002095 perf_event__fprintf(event, stdout);
2096 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002097 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002098out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002099 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002100 return ret;
2101}
2102
Hari Bathini96a44bb2017-03-08 02:12:06 +05302103static int process_namespaces_event(struct perf_tool *tool,
2104 union perf_event *event,
2105 struct perf_sample *sample,
2106 struct machine *machine)
2107{
2108 struct thread *thread;
2109 struct perf_script *script = container_of(tool, struct perf_script, tool);
2110 struct perf_session *session = script->session;
2111 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2112 int ret = -1;
2113
2114 thread = machine__findnew_thread(machine, event->namespaces.pid,
2115 event->namespaces.tid);
2116 if (thread == NULL) {
2117 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2118 return -1;
2119 }
2120
2121 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2122 goto out;
2123
2124 if (!evsel->attr.sample_id_all) {
2125 sample->cpu = 0;
2126 sample->time = 0;
2127 sample->tid = event->namespaces.tid;
2128 sample->pid = event->namespaces.pid;
2129 }
Andi Kleene87e5482019-03-11 07:44:52 -07002130 if (!filter_cpu(sample)) {
2131 perf_sample__fprintf_start(sample, thread, evsel,
2132 PERF_RECORD_NAMESPACES, stdout);
2133 perf_event__fprintf(event, stdout);
2134 }
Hari Bathini96a44bb2017-03-08 02:12:06 +05302135 ret = 0;
2136out:
2137 thread__put(thread);
2138 return ret;
2139}
2140
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002141static int process_fork_event(struct perf_tool *tool,
2142 union perf_event *event,
2143 struct perf_sample *sample,
2144 struct machine *machine)
2145{
2146 struct thread *thread;
2147 struct perf_script *script = container_of(tool, struct perf_script, tool);
2148 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002149 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002150
2151 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2152 return -1;
2153
2154 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2155 if (thread == NULL) {
2156 pr_debug("problem processing FORK event, skipping it.\n");
2157 return -1;
2158 }
2159
2160 if (!evsel->attr.sample_id_all) {
2161 sample->cpu = 0;
2162 sample->time = event->fork.time;
2163 sample->tid = event->fork.tid;
2164 sample->pid = event->fork.pid;
2165 }
Andi Kleene87e5482019-03-11 07:44:52 -07002166 if (!filter_cpu(sample)) {
2167 perf_sample__fprintf_start(sample, thread, evsel,
2168 PERF_RECORD_FORK, stdout);
2169 perf_event__fprintf(event, stdout);
2170 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002171 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002172
2173 return 0;
2174}
2175static int process_exit_event(struct perf_tool *tool,
2176 union perf_event *event,
2177 struct perf_sample *sample,
2178 struct machine *machine)
2179{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002180 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002181 struct thread *thread;
2182 struct perf_script *script = container_of(tool, struct perf_script, tool);
2183 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002184 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002185
2186 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2187 if (thread == NULL) {
2188 pr_debug("problem processing EXIT event, skipping it.\n");
2189 return -1;
2190 }
2191
2192 if (!evsel->attr.sample_id_all) {
2193 sample->cpu = 0;
2194 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002195 sample->tid = event->fork.tid;
2196 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002197 }
Andi Kleene87e5482019-03-11 07:44:52 -07002198 if (!filter_cpu(sample)) {
2199 perf_sample__fprintf_start(sample, thread, evsel,
2200 PERF_RECORD_EXIT, stdout);
2201 perf_event__fprintf(event, stdout);
2202 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002203
2204 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002205 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002206
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002207 thread__put(thread);
2208 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002209}
2210
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002211static int process_mmap_event(struct perf_tool *tool,
2212 union perf_event *event,
2213 struct perf_sample *sample,
2214 struct machine *machine)
2215{
2216 struct thread *thread;
2217 struct perf_script *script = container_of(tool, struct perf_script, tool);
2218 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002219 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002220
2221 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2222 return -1;
2223
2224 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2225 if (thread == NULL) {
2226 pr_debug("problem processing MMAP event, skipping it.\n");
2227 return -1;
2228 }
2229
2230 if (!evsel->attr.sample_id_all) {
2231 sample->cpu = 0;
2232 sample->time = 0;
2233 sample->tid = event->mmap.tid;
2234 sample->pid = event->mmap.pid;
2235 }
Andi Kleene87e5482019-03-11 07:44:52 -07002236 if (!filter_cpu(sample)) {
2237 perf_sample__fprintf_start(sample, thread, evsel,
2238 PERF_RECORD_MMAP, stdout);
2239 perf_event__fprintf(event, stdout);
2240 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002241 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002242 return 0;
2243}
2244
2245static int process_mmap2_event(struct perf_tool *tool,
2246 union perf_event *event,
2247 struct perf_sample *sample,
2248 struct machine *machine)
2249{
2250 struct thread *thread;
2251 struct perf_script *script = container_of(tool, struct perf_script, tool);
2252 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002253 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002254
2255 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2256 return -1;
2257
2258 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2259 if (thread == NULL) {
2260 pr_debug("problem processing MMAP2 event, skipping it.\n");
2261 return -1;
2262 }
2263
2264 if (!evsel->attr.sample_id_all) {
2265 sample->cpu = 0;
2266 sample->time = 0;
2267 sample->tid = event->mmap2.tid;
2268 sample->pid = event->mmap2.pid;
2269 }
Andi Kleene87e5482019-03-11 07:44:52 -07002270 if (!filter_cpu(sample)) {
2271 perf_sample__fprintf_start(sample, thread, evsel,
2272 PERF_RECORD_MMAP2, stdout);
2273 perf_event__fprintf(event, stdout);
2274 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002275 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002276 return 0;
2277}
2278
Adrian Hunter7c148982015-07-21 12:44:06 +03002279static int process_switch_event(struct perf_tool *tool,
2280 union perf_event *event,
2281 struct perf_sample *sample,
2282 struct machine *machine)
2283{
2284 struct thread *thread;
2285 struct perf_script *script = container_of(tool, struct perf_script, tool);
2286 struct perf_session *session = script->session;
2287 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2288
2289 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2290 return -1;
2291
2292 thread = machine__findnew_thread(machine, sample->pid,
2293 sample->tid);
2294 if (thread == NULL) {
2295 pr_debug("problem processing SWITCH event, skipping it.\n");
2296 return -1;
2297 }
2298
Andi Kleene87e5482019-03-11 07:44:52 -07002299 if (!filter_cpu(sample)) {
2300 perf_sample__fprintf_start(sample, thread, evsel,
2301 PERF_RECORD_SWITCH, stdout);
2302 perf_event__fprintf(event, stdout);
2303 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002304 thread__put(thread);
2305 return 0;
2306}
2307
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002308static int
2309process_lost_event(struct perf_tool *tool,
2310 union perf_event *event,
2311 struct perf_sample *sample,
2312 struct machine *machine)
2313{
2314 struct perf_script *script = container_of(tool, struct perf_script, tool);
2315 struct perf_session *session = script->session;
2316 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2317 struct thread *thread;
2318
2319 thread = machine__findnew_thread(machine, sample->pid,
2320 sample->tid);
2321 if (thread == NULL)
2322 return -1;
2323
Andi Kleene87e5482019-03-11 07:44:52 -07002324 if (!filter_cpu(sample)) {
2325 perf_sample__fprintf_start(sample, thread, evsel,
2326 PERF_RECORD_LOST, stdout);
2327 perf_event__fprintf(event, stdout);
2328 }
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002329 thread__put(thread);
2330 return 0;
2331}
2332
Jiri Olsa3233b372018-02-06 19:17:59 +01002333static int
2334process_finished_round_event(struct perf_tool *tool __maybe_unused,
2335 union perf_event *event,
2336 struct ordered_events *oe __maybe_unused)
2337
2338{
2339 perf_event__fprintf(event, stdout);
2340 return 0;
2341}
2342
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002343static int
2344process_bpf_events(struct perf_tool *tool __maybe_unused,
2345 union perf_event *event,
2346 struct perf_sample *sample,
2347 struct machine *machine)
2348{
2349 struct thread *thread;
2350 struct perf_script *script = container_of(tool, struct perf_script, tool);
2351 struct perf_session *session = script->session;
2352 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2353
2354 if (machine__process_ksymbol(machine, event, sample) < 0)
2355 return -1;
2356
2357 if (!evsel->attr.sample_id_all) {
2358 perf_event__fprintf(event, stdout);
2359 return 0;
2360 }
2361
2362 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2363 if (thread == NULL) {
2364 pr_debug("problem processing MMAP event, skipping it.\n");
2365 return -1;
2366 }
2367
2368 if (!filter_cpu(sample)) {
2369 perf_sample__fprintf_start(sample, thread, evsel,
2370 event->header.type, stdout);
2371 perf_event__fprintf(event, stdout);
2372 }
2373
2374 thread__put(thread);
2375 return 0;
2376}
2377
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002378static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002379{
2380 session_done = 1;
2381}
2382
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002383static void perf_script__fclose_per_event_dump(struct perf_script *script)
2384{
2385 struct perf_evlist *evlist = script->session->evlist;
2386 struct perf_evsel *evsel;
2387
2388 evlist__for_each_entry(evlist, evsel) {
2389 if (!evsel->priv)
2390 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002391 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002392 evsel->priv = NULL;
2393 }
2394}
2395
2396static int perf_script__fopen_per_event_dump(struct perf_script *script)
2397{
2398 struct perf_evsel *evsel;
2399
2400 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002401 /*
2402 * Already setup? I.e. we may be called twice in cases like
2403 * Intel PT, one for the intel_pt// and dummy events, then
2404 * for the evsels syntheized from the auxtrace info.
2405 *
2406 * Ses perf_script__process_auxtrace_info.
2407 */
2408 if (evsel->priv != NULL)
2409 continue;
2410
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002411 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002412 if (evsel->priv == NULL)
2413 goto out_err_fclose;
2414 }
2415
2416 return 0;
2417
2418out_err_fclose:
2419 perf_script__fclose_per_event_dump(script);
2420 return -1;
2421}
2422
2423static int perf_script__setup_per_event_dump(struct perf_script *script)
2424{
2425 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002426 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002427
2428 if (script->per_event_dump)
2429 return perf_script__fopen_per_event_dump(script);
2430
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002431 es_stdout.fp = stdout;
2432
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002433 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002434 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002435
2436 return 0;
2437}
2438
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002439static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2440{
2441 struct perf_evsel *evsel;
2442
2443 evlist__for_each_entry(script->session->evlist, evsel) {
2444 struct perf_evsel_script *es = evsel->priv;
2445
2446 perf_evsel_script__fprintf(es, stdout);
2447 perf_evsel_script__delete(es);
2448 evsel->priv = NULL;
2449 }
2450}
2451
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002452static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002453{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002454 int ret;
2455
Tom Zanussic239da32010-04-01 23:59:18 -05002456 signal(SIGINT, sig_handler);
2457
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002458 perf_stat__init_shadow_stats();
2459
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002460 /* override event processing functions */
2461 if (script->show_task_events) {
2462 script->tool.comm = process_comm_event;
2463 script->tool.fork = process_fork_event;
2464 script->tool.exit = process_exit_event;
2465 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002466 if (script->show_mmap_events) {
2467 script->tool.mmap = process_mmap_event;
2468 script->tool.mmap2 = process_mmap2_event;
2469 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002470 if (script->show_switch_events)
2471 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302472 if (script->show_namespace_events)
2473 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002474 if (script->show_lost_events)
2475 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002476 if (script->show_round_events) {
2477 script->tool.ordered_events = false;
2478 script->tool.finished_round = process_finished_round_event;
2479 }
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002480 if (script->show_bpf_events) {
2481 script->tool.ksymbol = process_bpf_events;
2482 script->tool.bpf_event = process_bpf_events;
2483 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002484
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002485 if (perf_script__setup_per_event_dump(script)) {
2486 pr_err("Couldn't create the per event dump files\n");
2487 return -1;
2488 }
2489
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002490 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002491
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002492 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002493 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002494
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002495 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002496 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002497
2498 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002499}
2500
Tom Zanussi956ffd02009-11-25 01:15:46 -06002501struct script_spec {
2502 struct list_head node;
2503 struct scripting_ops *ops;
2504 char spec[0];
2505};
2506
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002507static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002508
2509static struct script_spec *script_spec__new(const char *spec,
2510 struct scripting_ops *ops)
2511{
2512 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2513
2514 if (s != NULL) {
2515 strcpy(s->spec, spec);
2516 s->ops = ops;
2517 }
2518
2519 return s;
2520}
2521
Tom Zanussi956ffd02009-11-25 01:15:46 -06002522static void script_spec__add(struct script_spec *s)
2523{
2524 list_add_tail(&s->node, &script_specs);
2525}
2526
2527static struct script_spec *script_spec__find(const char *spec)
2528{
2529 struct script_spec *s;
2530
2531 list_for_each_entry(s, &script_specs, node)
2532 if (strcasecmp(s->spec, spec) == 0)
2533 return s;
2534 return NULL;
2535}
2536
Tom Zanussi956ffd02009-11-25 01:15:46 -06002537int script_spec_register(const char *spec, struct scripting_ops *ops)
2538{
2539 struct script_spec *s;
2540
2541 s = script_spec__find(spec);
2542 if (s)
2543 return -1;
2544
Taeung Song8560bae2016-02-26 00:13:10 +09002545 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002546 if (!s)
2547 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002548 else
2549 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002550
2551 return 0;
2552}
2553
2554static struct scripting_ops *script_spec__lookup(const char *spec)
2555{
2556 struct script_spec *s = script_spec__find(spec);
2557 if (!s)
2558 return NULL;
2559
2560 return s->ops;
2561}
2562
2563static void list_available_languages(void)
2564{
2565 struct script_spec *s;
2566
2567 fprintf(stderr, "\n");
2568 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002569 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002570
2571 list_for_each_entry(s, &script_specs, node)
2572 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2573
2574 fprintf(stderr, "\n");
2575}
2576
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002577static int parse_scriptname(const struct option *opt __maybe_unused,
2578 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002579{
2580 char spec[PATH_MAX];
2581 const char *script, *ext;
2582 int len;
2583
Tom Zanussif526d682010-01-27 02:27:52 -06002584 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002585 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002586 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002587 }
2588
2589 script = strchr(str, ':');
2590 if (script) {
2591 len = script - str;
2592 if (len >= PATH_MAX) {
2593 fprintf(stderr, "invalid language specifier");
2594 return -1;
2595 }
2596 strncpy(spec, str, len);
2597 spec[len] = '\0';
2598 scripting_ops = script_spec__lookup(spec);
2599 if (!scripting_ops) {
2600 fprintf(stderr, "invalid language specifier");
2601 return -1;
2602 }
2603 script++;
2604 } else {
2605 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002606 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002607 if (!ext) {
2608 fprintf(stderr, "invalid script extension");
2609 return -1;
2610 }
2611 scripting_ops = script_spec__lookup(++ext);
2612 if (!scripting_ops) {
2613 fprintf(stderr, "invalid script extension");
2614 return -1;
2615 }
2616 }
2617
2618 script_name = strdup(script);
2619
2620 return 0;
2621}
2622
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002623static int parse_output_fields(const struct option *opt __maybe_unused,
2624 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002625{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002626 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002627 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002628 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002629 int rc = 0;
2630 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002631 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002632 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002633
2634 if (!str)
2635 return -ENOMEM;
2636
David Ahern2c9e45f72011-03-17 10:03:21 -06002637 /* first word can state for which event type the user is specifying
2638 * the fields. If no type exists, the specified fields apply to all
2639 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002640 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002641 tok = strchr(str, ':');
2642 if (tok) {
2643 *tok = '\0';
2644 tok++;
2645 if (!strcmp(str, "hw"))
2646 type = PERF_TYPE_HARDWARE;
2647 else if (!strcmp(str, "sw"))
2648 type = PERF_TYPE_SOFTWARE;
2649 else if (!strcmp(str, "trace"))
2650 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002651 else if (!strcmp(str, "raw"))
2652 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002653 else if (!strcmp(str, "break"))
2654 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002655 else if (!strcmp(str, "synth"))
2656 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002657 else {
2658 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002659 rc = -EINVAL;
2660 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002661 }
2662
2663 if (output[type].user_set)
2664 pr_warning("Overriding previous field request for %s events.\n",
2665 event_type(type));
2666
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002667 /* Don't override defaults for +- */
2668 if (strchr(tok, '+') || strchr(tok, '-'))
2669 goto parse;
2670
David Ahern2c9e45f72011-03-17 10:03:21 -06002671 output[type].fields = 0;
2672 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002673 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002674
2675 } else {
2676 tok = str;
2677 if (strlen(str) == 0) {
2678 fprintf(stderr,
2679 "Cannot set fields to 'none' for all event types.\n");
2680 rc = -EINVAL;
2681 goto out;
2682 }
2683
Andi Kleen36ce5652017-06-02 08:48:10 -07002684 /* Don't override defaults for +- */
2685 if (strchr(str, '+') || strchr(str, '-'))
2686 goto parse;
2687
David Ahern2c9e45f72011-03-17 10:03:21 -06002688 if (output_set_by_user())
2689 pr_warning("Overriding previous field request for all events.\n");
2690
Adrian Hunter14057202017-06-21 13:17:19 +03002691 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002692 output[j].fields = 0;
2693 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002694 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002695 }
David Ahern1424dc92011-03-09 22:23:28 -07002696 }
2697
Andi Kleen36ce5652017-06-02 08:48:10 -07002698parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002699 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002700 if (*tok == '+') {
2701 if (change == SET)
2702 goto out_badmix;
2703 change = ADD;
2704 tok++;
2705 } else if (*tok == '-') {
2706 if (change == SET)
2707 goto out_badmix;
2708 change = REMOVE;
2709 tok++;
2710 } else {
2711 if (change != SET && change != DEFAULT)
2712 goto out_badmix;
2713 change = SET;
2714 }
2715
David Ahern745f43e2011-03-09 22:23:26 -07002716 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002717 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002718 break;
David Ahern745f43e2011-03-09 22:23:26 -07002719 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002720 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002721 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002722 continue;
2723 }
David Ahern745f43e2011-03-09 22:23:26 -07002724 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002725 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002726 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002727 goto out;
2728 }
2729
2730 if (type == -1) {
2731 /* add user option to all events types for
2732 * which it is valid
2733 */
Adrian Hunter14057202017-06-21 13:17:19 +03002734 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002735 if (output[j].invalid_fields & all_output_options[i].field) {
2736 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2737 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002738 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002739 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002740 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002741 output[j].user_set_fields &= ~all_output_options[i].field;
2742 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002743 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002744 output[j].user_set_fields |= all_output_options[i].field;
2745 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002746 output[j].user_set = true;
2747 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002748 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002749 }
2750 } else {
2751 if (output[type].invalid_fields & all_output_options[i].field) {
2752 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2753 all_output_options[i].str, event_type(type));
2754
2755 rc = -EINVAL;
2756 goto out;
2757 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002758 if (change == REMOVE)
2759 output[type].fields &= ~all_output_options[i].field;
2760 else
2761 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002762 output[type].user_set = true;
2763 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002764 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002765 }
2766
2767 if (type >= 0) {
2768 if (output[type].fields == 0) {
2769 pr_debug("No fields requested for %s type. "
2770 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002771 }
David Ahern1424dc92011-03-09 22:23:28 -07002772 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002773 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002774
Andi Kleen36ce5652017-06-02 08:48:10 -07002775out_badmix:
2776 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2777 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002778out:
David Ahern745f43e2011-03-09 22:23:26 -07002779 free(str);
2780 return rc;
2781}
2782
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002783#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2784 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2785 if ((lang_dirent->d_type == DT_DIR || \
2786 (lang_dirent->d_type == DT_UNKNOWN && \
2787 is_directory(scripts_path, lang_dirent))) && \
2788 (strcmp(lang_dirent->d_name, ".")) && \
2789 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002790
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002791#define for_each_script(lang_path, lang_dir, script_dirent) \
2792 while ((script_dirent = readdir(lang_dir)) != NULL) \
2793 if (script_dirent->d_type != DT_DIR && \
2794 (script_dirent->d_type != DT_UNKNOWN || \
2795 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002796
2797
2798#define RECORD_SUFFIX "-record"
2799#define REPORT_SUFFIX "-report"
2800
2801struct script_desc {
2802 struct list_head node;
2803 char *name;
2804 char *half_liner;
2805 char *args;
2806};
2807
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002808static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002809
2810static struct script_desc *script_desc__new(const char *name)
2811{
2812 struct script_desc *s = zalloc(sizeof(*s));
2813
Tom Zanussib5b87312010-11-10 08:16:51 -06002814 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002815 s->name = strdup(name);
2816
2817 return s;
2818}
2819
2820static void script_desc__delete(struct script_desc *s)
2821{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002822 zfree(&s->name);
2823 zfree(&s->half_liner);
2824 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002825 free(s);
2826}
2827
2828static void script_desc__add(struct script_desc *s)
2829{
2830 list_add_tail(&s->node, &script_descs);
2831}
2832
2833static struct script_desc *script_desc__find(const char *name)
2834{
2835 struct script_desc *s;
2836
2837 list_for_each_entry(s, &script_descs, node)
2838 if (strcasecmp(s->name, name) == 0)
2839 return s;
2840 return NULL;
2841}
2842
2843static struct script_desc *script_desc__findnew(const char *name)
2844{
2845 struct script_desc *s = script_desc__find(name);
2846
2847 if (s)
2848 return s;
2849
2850 s = script_desc__new(name);
2851 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002852 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002853
2854 script_desc__add(s);
2855
2856 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002857}
2858
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002859static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002860{
2861 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002862 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002863
2864 if (strlen(str) > suffix_len) {
2865 p = str + strlen(str) - suffix_len;
2866 if (!strncmp(p, suffix, suffix_len))
2867 return p;
2868 }
2869
2870 return NULL;
2871}
2872
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002873static int read_script_info(struct script_desc *desc, const char *filename)
2874{
2875 char line[BUFSIZ], *p;
2876 FILE *fp;
2877
2878 fp = fopen(filename, "r");
2879 if (!fp)
2880 return -1;
2881
2882 while (fgets(line, sizeof(line), fp)) {
2883 p = ltrim(line);
2884 if (strlen(p) == 0)
2885 continue;
2886 if (*p != '#')
2887 continue;
2888 p++;
2889 if (strlen(p) && *p == '!')
2890 continue;
2891
2892 p = ltrim(p);
2893 if (strlen(p) && p[strlen(p) - 1] == '\n')
2894 p[strlen(p) - 1] = '\0';
2895
2896 if (!strncmp(p, "description:", strlen("description:"))) {
2897 p += strlen("description:");
2898 desc->half_liner = strdup(ltrim(p));
2899 continue;
2900 }
2901
2902 if (!strncmp(p, "args:", strlen("args:"))) {
2903 p += strlen("args:");
2904 desc->args = strdup(ltrim(p));
2905 continue;
2906 }
2907 }
2908
2909 fclose(fp);
2910
2911 return 0;
2912}
2913
Robert Richter38efb532011-11-25 11:38:40 +01002914static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2915{
2916 char *script_root, *str;
2917
2918 script_root = strdup(script_dirent->d_name);
2919 if (!script_root)
2920 return NULL;
2921
2922 str = (char *)ends_with(script_root, suffix);
2923 if (!str) {
2924 free(script_root);
2925 return NULL;
2926 }
2927
2928 *str = '\0';
2929 return script_root;
2930}
2931
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002932static int list_available_scripts(const struct option *opt __maybe_unused,
2933 const char *s __maybe_unused,
2934 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002935{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002936 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002937 char scripts_path[MAXPATHLEN];
2938 DIR *scripts_dir, *lang_dir;
2939 char script_path[MAXPATHLEN];
2940 char lang_path[MAXPATHLEN];
2941 struct script_desc *desc;
2942 char first_half[BUFSIZ];
2943 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002944
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002945 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002946
2947 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002948 if (!scripts_dir) {
2949 fprintf(stdout,
2950 "open(%s) failed.\n"
2951 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2952 scripts_path);
2953 exit(-1);
2954 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002955
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002956 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002957 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2958 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002959 lang_dir = opendir(lang_path);
2960 if (!lang_dir)
2961 continue;
2962
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002963 for_each_script(lang_path, lang_dir, script_dirent) {
2964 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002965 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002966 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002967 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2968 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002969 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002970 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002971 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002972 }
2973 }
2974
2975 fprintf(stdout, "List of available trace scripts:\n");
2976 list_for_each_entry(desc, &script_descs, node) {
2977 sprintf(first_half, "%s %s", desc->name,
2978 desc->args ? desc->args : "");
2979 fprintf(stdout, " %-36s %s\n", first_half,
2980 desc->half_liner ? desc->half_liner : "");
2981 }
2982
2983 exit(0);
2984}
2985
Feng Tange5f37052012-09-07 16:42:26 +08002986/*
Feng Tang49e639e2012-10-30 11:56:03 +08002987 * Some scripts specify the required events in their "xxx-record" file,
2988 * this function will check if the events in perf.data match those
2989 * mentioned in the "xxx-record".
2990 *
2991 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2992 * which is covered well now. And new parsing code should be added to
2993 * cover the future complexing formats like event groups etc.
2994 */
2995static int check_ev_match(char *dir_name, char *scriptname,
2996 struct perf_session *session)
2997{
2998 char filename[MAXPATHLEN], evname[128];
2999 char line[BUFSIZ], *p;
3000 struct perf_evsel *pos;
3001 int match, len;
3002 FILE *fp;
3003
Jiri Olsa77f18152018-03-19 09:29:01 +01003004 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08003005
3006 fp = fopen(filename, "r");
3007 if (!fp)
3008 return -1;
3009
3010 while (fgets(line, sizeof(line), fp)) {
3011 p = ltrim(line);
3012 if (*p == '#')
3013 continue;
3014
3015 while (strlen(p)) {
3016 p = strstr(p, "-e");
3017 if (!p)
3018 break;
3019
3020 p += 2;
3021 p = ltrim(p);
3022 len = strcspn(p, " \t");
3023 if (!len)
3024 break;
3025
3026 snprintf(evname, len + 1, "%s", p);
3027
3028 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003029 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08003030 if (!strcmp(perf_evsel__name(pos), evname)) {
3031 match = 1;
3032 break;
3033 }
3034 }
3035
3036 if (!match) {
3037 fclose(fp);
3038 return -1;
3039 }
3040 }
3041 }
3042
3043 fclose(fp);
3044 return 0;
3045}
3046
3047/*
Feng Tange5f37052012-09-07 16:42:26 +08003048 * Return -1 if none is found, otherwise the actual scripts number.
3049 *
3050 * Currently the only user of this function is the script browser, which
3051 * will list all statically runnable scripts, select one, execute it and
3052 * show the output in a perf browser.
3053 */
Andi Kleen905e4af2019-03-11 07:45:01 -07003054int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3055 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08003056{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003057 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08003058 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08003059 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08003060 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003061 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003062 .path = input_name,
3063 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003064 };
Feng Tange5f37052012-09-07 16:42:26 +08003065 char *temp;
3066 int i = 0;
3067
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003068 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08003069 if (!session)
3070 return -1;
3071
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003072 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003073
3074 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003075 if (!scripts_dir) {
3076 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003077 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003078 }
Feng Tange5f37052012-09-07 16:42:26 +08003079
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003080 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003081 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3082 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003083#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003084 if (strstr(lang_path, "perl"))
3085 continue;
3086#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003087#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003088 if (strstr(lang_path, "python"))
3089 continue;
3090#endif
3091
3092 lang_dir = opendir(lang_path);
3093 if (!lang_dir)
3094 continue;
3095
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003096 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003097 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003098 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003099 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003100 if (i >= num)
3101 break;
3102 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3103 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003104 script_dirent->d_name);
3105 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003106 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003107 (temp - script_dirent->d_name) + 1,
3108 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003109
3110 if (check_ev_match(lang_path,
3111 scripts_array[i], session))
3112 continue;
3113
Feng Tange5f37052012-09-07 16:42:26 +08003114 i++;
3115 }
Feng Tang49e639e2012-10-30 11:56:03 +08003116 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003117 }
3118
Feng Tang49e639e2012-10-30 11:56:03 +08003119 closedir(scripts_dir);
3120 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003121 return i;
3122}
3123
Tom Zanussi38752942009-12-15 02:53:39 -06003124static char *get_script_path(const char *script_root, const char *suffix)
3125{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003126 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003127 char scripts_path[MAXPATHLEN];
3128 char script_path[MAXPATHLEN];
3129 DIR *scripts_dir, *lang_dir;
3130 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003131 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003132
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003133 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003134
3135 scripts_dir = opendir(scripts_path);
3136 if (!scripts_dir)
3137 return NULL;
3138
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003139 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003140 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3141 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003142 lang_dir = opendir(lang_path);
3143 if (!lang_dir)
3144 continue;
3145
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003146 for_each_script(lang_path, lang_dir, script_dirent) {
3147 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003148 if (__script_root && !strcmp(script_root, __script_root)) {
3149 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003150 closedir(lang_dir);
3151 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003152 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3153 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003154 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003155 }
3156 free(__script_root);
3157 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003158 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003159 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003160 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003161
Robert Richter38efb532011-11-25 11:38:40 +01003162 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003163}
3164
Tom Zanussib5b87312010-11-10 08:16:51 -06003165static bool is_top_script(const char *script_path)
3166{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003167 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003168}
3169
3170static int has_required_arg(char *script_path)
3171{
3172 struct script_desc *desc;
3173 int n_args = 0;
3174 char *p;
3175
3176 desc = script_desc__new(NULL);
3177
3178 if (read_script_info(desc, script_path))
3179 goto out;
3180
3181 if (!desc->args)
3182 goto out;
3183
3184 for (p = desc->args; *p; p++)
3185 if (*p == '<')
3186 n_args++;
3187out:
3188 script_desc__delete(desc);
3189
3190 return n_args;
3191}
3192
David Ahernd54b1a92012-08-26 12:24:46 -06003193static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003194{
3195 char **__argv = malloc(sizeof(const char *) * argc);
3196
David Ahernd54b1a92012-08-26 12:24:46 -06003197 if (!__argv) {
3198 pr_err("malloc failed\n");
3199 return -1;
3200 }
3201
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003202 memcpy(__argv, argv, sizeof(const char *) * argc);
3203 argc = parse_options(argc, (const char **)__argv, record_options,
3204 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3205 free(__argv);
3206
David Ahernd54b1a92012-08-26 12:24:46 -06003207 system_wide = (argc == 0);
3208
3209 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003210}
3211
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003212static void script__setup_sample_type(struct perf_script *script)
3213{
3214 struct perf_session *session = script->session;
3215 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3216
3217 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3218 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003219 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003220 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003221 dwarf_callchain_users = true;
3222 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003223 callchain_param.record_mode = CALLCHAIN_LBR;
3224 else
3225 callchain_param.record_mode = CALLCHAIN_FP;
3226 }
3227}
3228
Jiri Olsa89f16882018-09-13 14:54:03 +02003229static int process_stat_round_event(struct perf_session *session,
3230 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003231{
3232 struct stat_round_event *round = &event->stat_round;
3233 struct perf_evsel *counter;
3234
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003235 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003236 perf_stat_process_counter(&stat_config, counter);
3237 process_stat(counter, round->time);
3238 }
3239
3240 process_stat_interval(round->time);
3241 return 0;
3242}
3243
Jiri Olsa89f16882018-09-13 14:54:03 +02003244static int process_stat_config_event(struct perf_session *session __maybe_unused,
3245 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003246{
3247 perf_event__read_stat_config(&stat_config, &event->stat_config);
3248 return 0;
3249}
3250
Jiri Olsacfc88742016-01-05 22:09:06 +01003251static int set_maps(struct perf_script *script)
3252{
3253 struct perf_evlist *evlist = script->session->evlist;
3254
3255 if (!script->cpus || !script->threads)
3256 return 0;
3257
3258 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3259 return -EINVAL;
3260
3261 perf_evlist__set_maps(evlist, script->cpus, script->threads);
3262
3263 if (perf_evlist__alloc_stats(evlist, true))
3264 return -ENOMEM;
3265
3266 script->allocated = true;
3267 return 0;
3268}
3269
3270static
Jiri Olsa89f16882018-09-13 14:54:03 +02003271int process_thread_map_event(struct perf_session *session,
3272 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003273{
Jiri Olsa89f16882018-09-13 14:54:03 +02003274 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003275 struct perf_script *script = container_of(tool, struct perf_script, tool);
3276
3277 if (script->threads) {
3278 pr_warning("Extra thread map event, ignoring.\n");
3279 return 0;
3280 }
3281
3282 script->threads = thread_map__new_event(&event->thread_map);
3283 if (!script->threads)
3284 return -ENOMEM;
3285
3286 return set_maps(script);
3287}
3288
3289static
Jiri Olsa89f16882018-09-13 14:54:03 +02003290int process_cpu_map_event(struct perf_session *session,
3291 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003292{
Jiri Olsa89f16882018-09-13 14:54:03 +02003293 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003294 struct perf_script *script = container_of(tool, struct perf_script, tool);
3295
3296 if (script->cpus) {
3297 pr_warning("Extra cpu map event, ignoring.\n");
3298 return 0;
3299 }
3300
3301 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3302 if (!script->cpus)
3303 return -ENOMEM;
3304
3305 return set_maps(script);
3306}
3307
Jiri Olsa89f16882018-09-13 14:54:03 +02003308static int process_feature_event(struct perf_session *session,
3309 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303310{
3311 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003312 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303313 return 0;
3314}
3315
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003316#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003317static int perf_script__process_auxtrace_info(struct perf_session *session,
3318 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003319{
Jiri Olsa89f16882018-09-13 14:54:03 +02003320 struct perf_tool *tool = session->tool;
3321
3322 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003323
3324 if (ret == 0) {
3325 struct perf_script *script = container_of(tool, struct perf_script, tool);
3326
3327 ret = perf_script__setup_per_event_dump(script);
3328 }
3329
3330 return ret;
3331}
3332#else
3333#define perf_script__process_auxtrace_info 0
3334#endif
3335
Andi Kleenb585ebd2018-09-20 11:05:36 -07003336static int parse_insn_trace(const struct option *opt __maybe_unused,
3337 const char *str __maybe_unused,
3338 int unset __maybe_unused)
3339{
3340 parse_output_fields(NULL, "+insn,-event,-period", 0);
3341 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003342 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003343 return 0;
3344}
3345
3346static int parse_xed(const struct option *opt __maybe_unused,
3347 const char *str __maybe_unused,
3348 int unset __maybe_unused)
3349{
3350 force_pager("xed -F insn: -A -64 | less");
3351 return 0;
3352}
3353
Andi Kleend1b15522018-09-20 11:05:38 -07003354static int parse_call_trace(const struct option *opt __maybe_unused,
3355 const char *str __maybe_unused,
3356 int unset __maybe_unused)
3357{
3358 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3359 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003360 symbol_conf.nanosecs = true;
Jiri Olsa1c492422019-05-08 15:20:05 +02003361 symbol_conf.pad_output_len_dso = 50;
Andi Kleend1b15522018-09-20 11:05:38 -07003362 return 0;
3363}
3364
3365static int parse_callret_trace(const struct option *opt __maybe_unused,
3366 const char *str __maybe_unused,
3367 int unset __maybe_unused)
3368{
3369 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3370 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003371 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003372 return 0;
3373}
3374
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003375int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003376{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003377 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003378 bool header = false;
3379 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003380 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003381 char *rec_script_path = NULL;
3382 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003383 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003384 struct itrace_synth_opts itrace_synth_opts = {
3385 .set = false,
3386 .default_no_sample = true,
3387 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003388 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003389 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003390 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003391 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003392 struct perf_script script = {
3393 .tool = {
3394 .sample = process_sample_event,
3395 .mmap = perf_event__process_mmap,
3396 .mmap2 = perf_event__process_mmap2,
3397 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303398 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003399 .exit = perf_event__process_exit,
3400 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003401 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003402 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003403 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303404 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003405 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003406 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003407 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003408 .auxtrace = perf_event__process_auxtrace,
3409 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003410 .stat = perf_event__process_stat_event,
3411 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003412 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003413 .thread_map = process_thread_map_event,
3414 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003415 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003416 .ordering_requires_timestamps = true,
3417 },
3418 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003419 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003420 .mode = PERF_DATA_MODE_READ,
3421 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003422 const struct option options[] = {
3423 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3424 "dump raw trace in ASCII"),
3425 OPT_INCR('v', "verbose", &verbose,
3426 "be more verbose (show symbol address, etc)"),
3427 OPT_BOOLEAN('L', "Latency", &latency_format,
3428 "show latency attributes (irqs/preemption disabled, etc)"),
3429 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3430 list_available_scripts),
3431 OPT_CALLBACK('s', "script", NULL, "name",
3432 "script file name (lang:script name, script name, or *)",
3433 parse_scriptname),
3434 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3435 "generate perf-script.xx script in specified language"),
3436 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3437 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3438 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003439 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3440 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003441 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3442 "file", "vmlinux pathname"),
3443 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3444 "file", "kallsyms pathname"),
3445 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3446 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003447 OPT_CALLBACK(0, "symfs", NULL, "directory",
3448 "Look for files with symbols relative to this directory",
3449 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003450 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003451 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003452 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003453 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003454 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303455 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3456 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
Adrian Hunter68fb45b2019-05-20 14:37:14 +03003457 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003458 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003459 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3460 "system-wide collection from all CPUs"),
3461 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3462 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003463 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3464 "Decode instructions from itrace", parse_insn_trace),
3465 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3466 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003467 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3468 "Decode calls from from itrace", parse_call_trace),
3469 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3470 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003471 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3472 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003473 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3474 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003475 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3476 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3477 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003478 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3479 "only consider symbols in these pids"),
3480 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3481 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003482 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3483 "Set the maximum stack depth when parsing the callchain, "
3484 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003485 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Andi Kleen90b10f42019-03-14 15:50:00 -07003486 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003487 OPT_BOOLEAN('I', "show-info", &show_full_info,
3488 "display extended information from perf.data file"),
3489 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3490 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003491 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3492 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003493 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3494 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003495 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3496 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303497 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3498 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003499 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3500 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003501 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3502 "Show round events (if recorded)"),
Jiri Olsa490c8cc2019-05-08 15:20:08 +02003503 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3504 "Show bpf related events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003505 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3506 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003507 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003508 OPT_INTEGER(0, "max-blocks", &max_blocks,
3509 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003510 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003511 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003512 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003513 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003514 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003515 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3516 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003517 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3518 "Enable symbol demangling"),
3519 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3520 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003521 OPT_STRING(0, "time", &script.time_str, "str",
3522 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003523 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3524 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003525 OPT_END()
3526 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003527 const char * const script_subcommands[] = { "record", "report", NULL };
3528 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003529 "perf script [<options>]",
3530 "perf script [<options>] record <script> [<record-options>] <command>",
3531 "perf script [<options>] report <script> [script-args]",
3532 "perf script [<options>] <script> [<record-options>] <command>",
3533 "perf script [<options>] <top-script> [script-args]",
3534 NULL
3535 };
Tom Zanussi38752942009-12-15 02:53:39 -06003536
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003537 perf_set_singlethreaded();
3538
Tom Zanussib5b87312010-11-10 08:16:51 -06003539 setup_scripting();
3540
Yunlong Song40cae2b2015-03-18 21:35:54 +08003541 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003542 PARSE_OPT_STOP_AT_NON_OPTION);
3543
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003544 data.path = input_name;
3545 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003546
Tom Zanussib5b87312010-11-10 08:16:51 -06003547 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3548 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3549 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003550 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003551 }
3552
Tom Zanussib5b87312010-11-10 08:16:51 -06003553 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3554 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3555 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003556 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003557 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003558 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003559 return -1;
3560 }
Tom Zanussi38752942009-12-15 02:53:39 -06003561 }
3562
Andi Kleen90b10f42019-03-14 15:50:00 -07003563 if (script.time_str && reltime) {
3564 fprintf(stderr, "Don't combine --reltime with --time\n");
3565 return -1;
3566 }
3567
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003568 if (itrace_synth_opts.callchain &&
3569 itrace_synth_opts.callchain_sz > scripting_max_stack)
3570 scripting_max_stack = itrace_synth_opts.callchain_sz;
3571
Ben Hutchings44e668c2010-10-10 16:10:03 +01003572 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003573 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003574
Tom Zanussib5b87312010-11-10 08:16:51 -06003575 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003576 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003577 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003578 pid_t pid;
3579
Tom Zanussib5b87312010-11-10 08:16:51 -06003580 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3581 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3582
3583 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003584 usage_with_options_msg(script_usage, options,
3585 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003586 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003587 }
3588
Tom Zanussib5b87312010-11-10 08:16:51 -06003589 if (is_top_script(argv[0])) {
3590 rep_args = argc - 1;
3591 } else {
3592 int rec_args;
3593
3594 rep_args = has_required_arg(rep_script_path);
3595 rec_args = (argc - 1) - rep_args;
3596 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003597 usage_with_options_msg(script_usage, options,
3598 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003599 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003600 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003601 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003602 }
3603
3604 if (pipe(live_pipe) < 0) {
3605 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003606 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003607 }
3608
3609 pid = fork();
3610 if (pid < 0) {
3611 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003612 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003613 }
3614
3615 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003616 j = 0;
3617
Tom Zanussia0cccc22010-04-01 23:59:25 -05003618 dup2(live_pipe[1], 1);
3619 close(live_pipe[0]);
3620
Robert Richter317df652011-11-25 15:05:25 +01003621 if (is_top_script(argv[0])) {
3622 system_wide = true;
3623 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003624 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3625 err = -1;
3626 goto out;
3627 }
Robert Richter317df652011-11-25 15:05:25 +01003628 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003629
3630 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003631 if (!__argv) {
3632 pr_err("malloc failed\n");
3633 err = -ENOMEM;
3634 goto out;
3635 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003636
Tom Zanussib5b87312010-11-10 08:16:51 -06003637 __argv[j++] = "/bin/sh";
3638 __argv[j++] = rec_script_path;
3639 if (system_wide)
3640 __argv[j++] = "-a";
3641 __argv[j++] = "-q";
3642 __argv[j++] = "-o";
3643 __argv[j++] = "-";
3644 for (i = rep_args + 1; i < argc; i++)
3645 __argv[j++] = argv[i];
3646 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003647
3648 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003649 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003650 exit(-1);
3651 }
3652
3653 dup2(live_pipe[0], 0);
3654 close(live_pipe[1]);
3655
Tom Zanussib5b87312010-11-10 08:16:51 -06003656 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003657 if (!__argv) {
3658 pr_err("malloc failed\n");
3659 err = -ENOMEM;
3660 goto out;
3661 }
3662
Tom Zanussib5b87312010-11-10 08:16:51 -06003663 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003664 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003665 __argv[j++] = rep_script_path;
3666 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003667 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003668 __argv[j++] = "-i";
3669 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003670 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003671
3672 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003673 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003674 exit(-1);
3675 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003676
Tom Zanussib5b87312010-11-10 08:16:51 -06003677 if (rec_script_path)
3678 script_path = rec_script_path;
3679 if (rep_script_path)
3680 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003681
Tom Zanussib5b87312010-11-10 08:16:51 -06003682 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003683 j = 0;
3684
Robert Richter317df652011-11-25 15:05:25 +01003685 if (!rec_script_path)
3686 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003687 else if (!system_wide) {
3688 if (have_cmd(argc - 1, &argv[1]) != 0) {
3689 err = -1;
3690 goto out;
3691 }
3692 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003693
3694 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003695 if (!__argv) {
3696 pr_err("malloc failed\n");
3697 err = -ENOMEM;
3698 goto out;
3699 }
3700
Tom Zanussib5b87312010-11-10 08:16:51 -06003701 __argv[j++] = "/bin/sh";
3702 __argv[j++] = script_path;
3703 if (system_wide)
3704 __argv[j++] = "-a";
3705 for (i = 2; i < argc; i++)
3706 __argv[j++] = argv[i];
3707 __argv[j++] = NULL;
3708
3709 execvp("/bin/sh", (char **)__argv);
3710 free(__argv);
3711 exit(-1);
3712 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003713
Milian Wolffc1c9b962018-10-21 21:14:23 +02003714 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003715 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003716 use_browser = 0;
3717 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003718
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003719 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003720 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003721 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003722
Jiri Olsae90debd2013-12-09 11:02:50 +01003723 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003724 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003725 perf_session__fprintf_info(session, stdout, show_full_info);
3726 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003727 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003728 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003729 if (show_full_info)
3730 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003731
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003732 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003733 goto out_delete;
3734
Andi Kleen3ab481a2019-03-05 06:47:45 -08003735 uname(&uts);
3736 if (!strcmp(uts.machine, session->header.env.arch) ||
3737 (!strcmp(uts.machine, "x86_64") &&
3738 !strcmp(session->header.env.arch, "i386")))
3739 native_arch = true;
3740
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003741 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003742 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003743
Andi Kleen99f753f2018-09-20 11:05:39 -07003744 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3745 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003746 itrace_synth_opts.thread_stack = true;
3747
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003748 session->itrace_synth_opts = &itrace_synth_opts;
3749
Anton Blanchard5d67be92011-07-04 21:57:50 +10003750 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003751 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3752 if (err < 0)
3753 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003754 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003755 }
3756
David Ahern1424dc92011-03-09 22:23:28 -07003757 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003758 symbol_conf.use_callchain = true;
3759 else
3760 symbol_conf.use_callchain = false;
3761
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003762 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003763 tep_set_function_resolver(session->tevent.pevent,
3764 machine__resolve_kernel_addr,
3765 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003766 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003767 err = -1;
3768 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003769 }
3770
Tom Zanussi956ffd02009-11-25 01:15:46 -06003771 if (generate_script_lang) {
3772 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003773 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003774
David Ahern2c9e45f72011-03-17 10:03:21 -06003775 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003776 fprintf(stderr,
3777 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003778 err = -EINVAL;
3779 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003780 }
3781
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003782 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003783 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003784 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003785 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003786 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003787 }
3788
3789 err = fstat(input, &perf_stat);
3790 if (err < 0) {
3791 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003792 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003793 }
3794
3795 if (!perf_stat.st_size) {
3796 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003797 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003798 }
3799
3800 scripting_ops = script_spec__lookup(generate_script_lang);
3801 if (!scripting_ops) {
3802 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003803 err = -ENOENT;
3804 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003805 }
3806
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003807 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003808 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003809 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003810 }
3811
3812 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003813 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003814 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003815 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003816 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003817 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003818 }
3819
David Ahern9cbdb702011-04-06 21:54:20 -06003820
3821 err = perf_session__check_output_opt(session);
3822 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003823 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003824
Jin Yao284c4e12019-03-01 18:13:06 +08003825 if (script.time_str) {
3826 err = perf_time__parse_for_ranges(script.time_str, session,
3827 &script.ptime_range,
3828 &script.range_size,
3829 &script.range_num);
3830 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08003831 goto out_delete;
David Aherna91f4c42016-11-29 10:15:43 -07003832 }
3833
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003834 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003835
Adrian Hunterd445dd22014-08-15 22:08:37 +03003836 flush_scripting();
3837
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003838out_delete:
Jin Yao284c4e12019-03-01 18:13:06 +08003839 if (script.ptime_range)
3840 zfree(&script.ptime_range);
Jin Yaocc2ef582018-01-10 23:00:33 +08003841
Jiri Olsacfc88742016-01-05 22:09:06 +01003842 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003843 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003844
3845 if (script_started)
3846 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003847out:
3848 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003849}