blob: 7aa59696e97aa6ae8d01d1fbd5a5017c95e2b8cf [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002#include "builtin.h"
3
Andrea Gelminib7eead82010-08-05 15:51:38 +02004#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02005#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02006#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06007#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02008#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06009#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +020010#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020013#include "util/symbol.h"
14#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010015#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020016#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070017#include "util/evlist.h"
18#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080019#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020020#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030021#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010022#include "util/cpumap.h"
23#include "util/thread_map.h"
24#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080025#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030026#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030027#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070028#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010029#include "util/path.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030030#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100031#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030032#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030033#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030034#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010035#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010036#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080037#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030038#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030039#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030040#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030041#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030042#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030043#include <sys/types.h>
44#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030045#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030046#include <unistd.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020047
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030048#include "sane_ctype.h"
49
Tom Zanussi956ffd02009-11-25 01:15:46 -060050static char const *script_name;
51static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020052static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020053static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020054static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070055static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090056static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010057static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030058static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030059static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100060static const char *cpu_list;
61static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010062static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080063static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060064
Adrian Hunter44cbe722015-09-25 16:15:50 +030065unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030066
David Ahern745f43e2011-03-09 22:23:26 -070067enum perf_output_field {
68 PERF_OUTPUT_COMM = 1U << 0,
69 PERF_OUTPUT_TID = 1U << 1,
70 PERF_OUTPUT_PID = 1U << 2,
71 PERF_OUTPUT_TIME = 1U << 3,
72 PERF_OUTPUT_CPU = 1U << 4,
73 PERF_OUTPUT_EVNAME = 1U << 5,
74 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060075 PERF_OUTPUT_IP = 1U << 7,
76 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060077 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060078 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090079 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020080 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020081 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020082 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020083 PERF_OUTPUT_BRSTACK = 1U << 15,
84 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010085 PERF_OUTPUT_DATA_SRC = 1U << 17,
86 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000087 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030088 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030089 PERF_OUTPUT_INSN = 1U << 21,
90 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080091 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070092 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030093 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -040094 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -070095 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -080096 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +010097 PERF_OUTPUT_MISC = 1U << 29,
David Ahern745f43e2011-03-09 22:23:26 -070098};
99
100struct output_option {
101 const char *str;
102 enum perf_output_field field;
103} all_output_options[] = {
104 {.str = "comm", .field = PERF_OUTPUT_COMM},
105 {.str = "tid", .field = PERF_OUTPUT_TID},
106 {.str = "pid", .field = PERF_OUTPUT_PID},
107 {.str = "time", .field = PERF_OUTPUT_TIME},
108 {.str = "cpu", .field = PERF_OUTPUT_CPU},
109 {.str = "event", .field = PERF_OUTPUT_EVNAME},
110 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600111 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700112 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600113 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600114 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900115 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200116 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200117 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200118 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700119 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200120 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
121 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100122 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
123 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000124 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300125 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300126 {.str = "insn", .field = PERF_OUTPUT_INSN},
127 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800128 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700129 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300130 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400131 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800132 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100133 {.str = "misc", .field = PERF_OUTPUT_MISC},
David Ahern745f43e2011-03-09 22:23:26 -0700134};
135
Adrian Hunter14057202017-06-21 13:17:19 +0300136enum {
137 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
138 OUTPUT_TYPE_MAX
139};
140
David Ahern745f43e2011-03-09 22:23:26 -0700141/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600142static struct {
143 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600144 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400145 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600146 u64 fields;
147 u64 invalid_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300148} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700149
David Ahern2c9e45f72011-03-17 10:03:21 -0600150 [PERF_TYPE_HARDWARE] = {
151 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700152
David Ahern2c9e45f72011-03-17 10:03:21 -0600153 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
154 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600155 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530156 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
157 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600158
Wang Nan30372f02016-02-24 11:20:45 +0000159 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600160 },
161
162 [PERF_TYPE_SOFTWARE] = {
163 .user_set = false,
164
165 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
166 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600167 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530168 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
169 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
170 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600171
172 .invalid_fields = PERF_OUTPUT_TRACE,
173 },
174
175 [PERF_TYPE_TRACEPOINT] = {
176 .user_set = false,
177
178 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
179 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000180 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600181 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700182
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530183 [PERF_TYPE_HW_CACHE] = {
184 .user_set = false,
185
186 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
187 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
188 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
189 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
190 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
191
192 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
193 },
194
Arun Sharma0817a6a2011-04-14 10:38:18 -0700195 [PERF_TYPE_RAW] = {
196 .user_set = false,
197
198 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
199 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600200 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530201 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
202 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
203 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
204 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700205
Wang Nan30372f02016-02-24 11:20:45 +0000206 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700207 },
Wang Nan27cfef02015-12-08 02:25:43 +0000208
209 [PERF_TYPE_BREAKPOINT] = {
210 .user_set = false,
211
212 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
213 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
214 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530215 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
216 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000217
Wang Nan30372f02016-02-24 11:20:45 +0000218 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000219 },
Adrian Hunter14057202017-06-21 13:17:19 +0300220
221 [OUTPUT_TYPE_SYNTH] = {
222 .user_set = false,
223
224 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
225 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
226 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530227 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
228 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300229
230 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
231 },
David Ahern1424dc92011-03-09 22:23:28 -0700232};
David Ahern745f43e2011-03-09 22:23:26 -0700233
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300234struct perf_evsel_script {
235 char *filename;
236 FILE *fp;
237 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800238 /* For metric output */
239 u64 val;
240 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300241};
242
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800243static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
244{
245 return (struct perf_evsel_script *)evsel->priv;
246}
247
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300248static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100249 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300250{
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800251 struct perf_evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300252
253 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100254 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300255 goto out_free;
256 es->fp = fopen(es->filename, "w");
257 if (es->fp == NULL)
258 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300259 }
260
261 return es;
262out_free_filename:
263 zfree(&es->filename);
264out_free:
265 free(es);
266 return NULL;
267}
268
269static void perf_evsel_script__delete(struct perf_evsel_script *es)
270{
271 zfree(&es->filename);
272 fclose(es->fp);
273 es->fp = NULL;
274 free(es);
275}
276
277static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
278{
279 struct stat st;
280
281 fstat(fileno(es->fp), &st);
282 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
283 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
284}
285
Adrian Hunter14057202017-06-21 13:17:19 +0300286static inline int output_type(unsigned int type)
287{
288 switch (type) {
289 case PERF_TYPE_SYNTH:
290 return OUTPUT_TYPE_SYNTH;
291 default:
292 return type;
293 }
294}
295
296static inline unsigned int attr_type(unsigned int type)
297{
298 switch (type) {
299 case OUTPUT_TYPE_SYNTH:
300 return PERF_TYPE_SYNTH;
301 default:
302 return type;
303 }
304}
305
David Ahern2c9e45f72011-03-17 10:03:21 -0600306static bool output_set_by_user(void)
307{
308 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300309 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600310 if (output[j].user_set)
311 return true;
312 }
313 return false;
314}
David Ahern745f43e2011-03-09 22:23:26 -0700315
David Ahern9cbdb702011-04-06 21:54:20 -0600316static const char *output_field2str(enum perf_output_field field)
317{
318 int i, imax = ARRAY_SIZE(all_output_options);
319 const char *str = "";
320
321 for (i = 0; i < imax; ++i) {
322 if (all_output_options[i].field == field) {
323 str = all_output_options[i].str;
324 break;
325 }
326 }
327 return str;
328}
329
Adrian Hunter14057202017-06-21 13:17:19 +0300330#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700331
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300332static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
333 u64 sample_type, const char *sample_msg,
334 enum perf_output_field field,
335 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700336{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300337 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300338 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600339 const char *evname;
340
341 if (attr->sample_type & sample_type)
342 return 0;
343
344 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300345 if (allow_user_set)
346 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300347 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600348 pr_err("Samples for '%s' event do not have %s attribute set. "
349 "Cannot print '%s' field.\n",
350 evname, sample_msg, output_field2str(field));
351 return -1;
352 }
353
354 /* user did not ask for it explicitly so remove from the default list */
355 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300356 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600357 pr_debug("Samples for '%s' event do not have %s attribute set. "
358 "Skipping '%s' field.\n",
359 evname, sample_msg, output_field2str(field));
360
361 return 0;
362}
363
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300364static int perf_evsel__check_stype(struct perf_evsel *evsel,
365 u64 sample_type, const char *sample_msg,
366 enum perf_output_field field)
367{
368 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
369 false);
370}
371
David Ahern9cbdb702011-04-06 21:54:20 -0600372static int perf_evsel__check_attr(struct perf_evsel *evsel,
373 struct perf_session *session)
374{
375 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300376 bool allow_user_set;
377
Jiri Olsae099eba2016-01-05 22:09:09 +0100378 if (perf_header__has_feat(&session->header, HEADER_STAT))
379 return 0;
380
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300381 allow_user_set = perf_header__has_feat(&session->header,
382 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600383
David Ahern1424dc92011-03-09 22:23:28 -0700384 if (PRINT_FIELD(TRACE) &&
385 !perf_session__has_traces(session, "record -R"))
386 return -EINVAL;
387
David Ahern787bef12011-05-27 14:28:43 -0600388 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300389 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
390 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700391 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700392 }
David Ahern7cec0922011-05-30 13:08:23 -0600393
394 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300395 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
396 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600397 return -EINVAL;
398
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100399 if (PRINT_FIELD(DATA_SRC) &&
400 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
401 PERF_OUTPUT_DATA_SRC))
402 return -EINVAL;
403
404 if (PRINT_FIELD(WEIGHT) &&
405 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
406 PERF_OUTPUT_WEIGHT))
407 return -EINVAL;
408
Andi Kleen37fed3d2018-09-18 05:32:09 -0700409 if (PRINT_FIELD(SYM) &&
410 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600411 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700412 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600413 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600414 return -EINVAL;
415 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900416 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
417 pr_err("Display of offsets requested but symbol is not"
418 "selected.\n");
419 return -EINVAL;
420 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700421 if (PRINT_FIELD(DSO) &&
422 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
423 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600424 return -EINVAL;
425 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200426 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
427 pr_err("Display of source line number requested but sample IP is not\n"
428 "selected. Hence, no address to lookup the source line number.\n");
429 return -EINVAL;
430 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800431 if (PRINT_FIELD(BRSTACKINSN) &&
432 !(perf_evlist__combined_branch_type(session->evlist) &
433 PERF_SAMPLE_BRANCH_ANY)) {
434 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
435 "Hint: run 'perf record -b ...'\n");
436 return -EINVAL;
437 }
David Ahern1424dc92011-03-09 22:23:28 -0700438 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300439 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
440 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700441 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700442
443 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300444 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
445 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700446 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700447
448 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300449 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
450 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700451 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600452
Stephane Eranianfc36f942015-08-31 18:41:10 +0200453 if (PRINT_FIELD(IREGS) &&
454 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
455 PERF_OUTPUT_IREGS))
456 return -EINVAL;
457
Andi Kleenb1491ac2017-09-05 11:40:57 -0700458 if (PRINT_FIELD(UREGS) &&
459 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
460 PERF_OUTPUT_UREGS))
461 return -EINVAL;
462
Kan Liang49d58f02017-08-29 13:11:11 -0400463 if (PRINT_FIELD(PHYS_ADDR) &&
464 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
465 PERF_OUTPUT_PHYS_ADDR))
466 return -EINVAL;
467
David Ahern9cbdb702011-04-06 21:54:20 -0600468 return 0;
469}
470
Adrian Hunter7ea95722013-11-01 15:51:30 +0200471static void set_print_ip_opts(struct perf_event_attr *attr)
472{
Adrian Hunter14057202017-06-21 13:17:19 +0300473 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200474
475 output[type].print_ip_opts = 0;
476 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300477 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200478
479 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300480 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200481
482 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300483 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200484
485 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300486 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200487
488 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300489 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200490}
491
David Ahern9cbdb702011-04-06 21:54:20 -0600492/*
493 * verify all user requested events exist and the samples
494 * have the expected data
495 */
496static int perf_session__check_output_opt(struct perf_session *session)
497{
He Kuang40f20e52016-05-16 04:51:19 +0000498 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600499 struct perf_evsel *evsel;
500
Adrian Hunter14057202017-06-21 13:17:19 +0300501 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
502 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600503
504 /*
505 * even if fields is set to 0 (ie., show nothing) event must
506 * exist if user explicitly includes it on the command line
507 */
Adrian Hunter14057202017-06-21 13:17:19 +0300508 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
509 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600510 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300511 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600512 event_type(j));
513 return -1;
514 }
515
516 if (evsel && output[j].fields &&
517 perf_evsel__check_attr(evsel, session))
518 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400519
520 if (evsel == NULL)
521 continue;
522
Adrian Hunter7ea95722013-11-01 15:51:30 +0200523 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700524 }
525
Adrian Hunter98526ee2014-07-31 09:00:59 +0300526 if (!no_callchain) {
527 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000528 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300529
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300530 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000531 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300532 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300533 use_callchain = true;
534 break;
535 }
536 }
He Kuang71ac8992016-08-04 11:25:43 +0000537 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300538 symbol_conf.use_callchain = false;
539 }
540
David Ahern80b8b492013-11-19 21:07:37 -0700541 /*
542 * set default for tracepoints to print symbols only
543 * if callchains are present
544 */
545 if (symbol_conf.use_callchain &&
546 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700547 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700548
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300549 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000550 if (evsel->attr.type != j)
551 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700552
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300553 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000554 output[j].fields |= PERF_OUTPUT_IP;
555 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530556 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000557 output[j].fields |= PERF_OUTPUT_DSO;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300558 set_print_ip_opts(&evsel->attr);
He Kuang40f20e52016-05-16 04:51:19 +0000559 goto out;
560 }
David Ahern80b8b492013-11-19 21:07:37 -0700561 }
562 }
563
564out:
David Ahern1424dc92011-03-09 22:23:28 -0700565 return 0;
566}
David Ahern745f43e2011-03-09 22:23:26 -0700567
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300568static int perf_sample__fprintf_iregs(struct perf_sample *sample,
569 struct perf_event_attr *attr, FILE *fp)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200570{
571 struct regs_dump *regs = &sample->intr_regs;
572 uint64_t mask = attr->sample_regs_intr;
573 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300574 int printed = 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200575
576 if (!regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300577 return 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200578
579 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
580 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300581 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Stephane Eranianfc36f942015-08-31 18:41:10 +0200582 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300583
584 return printed;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200585}
586
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300587static int perf_sample__fprintf_uregs(struct perf_sample *sample,
588 struct perf_event_attr *attr, FILE *fp)
Andi Kleenb1491ac2017-09-05 11:40:57 -0700589{
590 struct regs_dump *regs = &sample->user_regs;
591 uint64_t mask = attr->sample_regs_user;
592 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300593 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700594
595 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300596 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700597
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300598 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700599
600 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
601 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300602 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700603 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300604
605 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700606}
607
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300608static int perf_sample__fprintf_start(struct perf_sample *sample,
609 struct thread *thread,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100610 struct perf_evsel *evsel,
611 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700612{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300613 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700614 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700615 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300616 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700617
David Ahern745f43e2011-03-09 22:23:26 -0700618 if (PRINT_FIELD(COMM)) {
619 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300620 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300621 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300622 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700623 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300624 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700625 }
David Ahernc70c94b2011-03-09 22:23:25 -0700626
David Ahern745f43e2011-03-09 22:23:26 -0700627 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300628 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700629 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300630 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700631 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300632 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700633
David Ahern745f43e2011-03-09 22:23:26 -0700634 if (PRINT_FIELD(CPU)) {
635 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300636 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700637 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300638 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700639 }
David Ahernc70c94b2011-03-09 22:23:25 -0700640
Jiri Olsa28a0b392018-01-07 17:03:52 +0100641 if (PRINT_FIELD(MISC)) {
642 int ret = 0;
643
644 #define has(m) \
645 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
646
647 if (has(KERNEL))
648 ret += fprintf(fp, "K");
649 if (has(USER))
650 ret += fprintf(fp, "U");
651 if (has(HYPERVISOR))
652 ret += fprintf(fp, "H");
653 if (has(GUEST_KERNEL))
654 ret += fprintf(fp, "G");
655 if (has(GUEST_USER))
656 ret += fprintf(fp, "g");
657
658 switch (type) {
659 case PERF_RECORD_MMAP:
660 case PERF_RECORD_MMAP2:
661 if (has(MMAP_DATA))
662 ret += fprintf(fp, "M");
663 break;
664 case PERF_RECORD_COMM:
665 if (has(COMM_EXEC))
666 ret += fprintf(fp, "E");
667 break;
668 case PERF_RECORD_SWITCH:
669 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300670 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100671 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300672 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
673 ret += fprintf(fp, "p");
674 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100675 default:
676 break;
677 }
678
679 #undef has
680
681 ret += fprintf(fp, "%*s", 6 - ret, " ");
682 printed += ret;
683 }
684
David Ahern745f43e2011-03-09 22:23:26 -0700685 if (PRINT_FIELD(TIME)) {
686 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300687 secs = nsecs / NSEC_PER_SEC;
688 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900689
Adrian Hunter83e19862015-09-25 16:15:36 +0300690 if (nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300691 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900692 else {
693 char sample_time[32];
694 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300695 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900696 }
David Ahern745f43e2011-03-09 22:23:26 -0700697 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300698
699 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700700}
701
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200702static inline char
703mispred_str(struct branch_entry *br)
704{
705 if (!(br->flags.mispred || br->flags.predicted))
706 return '-';
707
708 return br->flags.predicted ? 'P' : 'M';
709}
710
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300711static int perf_sample__fprintf_brstack(struct perf_sample *sample,
712 struct thread *thread,
713 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200714{
715 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700716 struct addr_location alf, alt;
717 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300718 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200719
720 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300721 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200722
723 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700724 from = br->entries[i].from;
725 to = br->entries[i].to;
726
727 if (PRINT_FIELD(DSO)) {
728 memset(&alf, 0, sizeof(alf));
729 memset(&alt, 0, sizeof(alt));
Arnaldo Carvalho de Melof07a2d32018-04-24 10:49:50 -0300730 thread__find_map(thread, sample->cpumode, from, &alf);
731 thread__find_map(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700732 }
733
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300734 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700735 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300736 printed += fprintf(fp, "(");
737 printed += map__fprintf_dsoname(alf.map, fp);
738 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700739 }
740
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300741 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700742 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300743 printed += fprintf(fp, "(");
744 printed += map__fprintf_dsoname(alt.map, fp);
745 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700746 }
747
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300748 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200749 mispred_str( br->entries + i),
750 br->entries[i].flags.in_tx? 'X' : '-',
751 br->entries[i].flags.abort? 'A' : '-',
752 br->entries[i].flags.cycles);
753 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300754
755 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200756}
757
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300758static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
759 struct thread *thread,
760 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200761{
762 struct branch_stack *br = sample->branch_stack;
763 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200764 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300765 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200766
767 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300768 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200769
770 for (i = 0; i < br->nr; i++) {
771
772 memset(&alf, 0, sizeof(alf));
773 memset(&alt, 0, sizeof(alt));
774 from = br->entries[i].from;
775 to = br->entries[i].to;
776
Arnaldo Carvalho de Melocc5f02f2018-04-24 11:32:30 -0300777 thread__find_symbol(thread, sample->cpumode, from, &alf);
778 thread__find_symbol(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200779
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300780 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700781 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300782 printed += fprintf(fp, "(");
783 printed += map__fprintf_dsoname(alf.map, fp);
784 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700785 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300786 printed += fprintf(fp, "%c", '/');
787 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700788 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300789 printed += fprintf(fp, "(");
790 printed += map__fprintf_dsoname(alt.map, fp);
791 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700792 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300793 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200794 mispred_str( br->entries + i),
795 br->entries[i].flags.in_tx? 'X' : '-',
796 br->entries[i].flags.abort? 'A' : '-',
797 br->entries[i].flags.cycles);
798 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300799
800 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200801}
802
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300803static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
804 struct thread *thread,
805 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700806{
807 struct branch_stack *br = sample->branch_stack;
808 struct addr_location alf, alt;
809 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300810 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700811
812 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300813 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700814
815 for (i = 0; i < br->nr; i++) {
816
817 memset(&alf, 0, sizeof(alf));
818 memset(&alt, 0, sizeof(alt));
819 from = br->entries[i].from;
820 to = br->entries[i].to;
821
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300822 if (thread__find_map(thread, sample->cpumode, from, &alf) &&
823 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700824 from = map__map_ip(alf.map, from);
825
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300826 if (thread__find_map(thread, sample->cpumode, to, &alt) &&
827 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700828 to = map__map_ip(alt.map, to);
829
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300830 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700831 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300832 printed += fprintf(fp, "(");
833 printed += map__fprintf_dsoname(alf.map, fp);
834 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700835 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300836 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700837 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300838 printed += fprintf(fp, "(");
839 printed += map__fprintf_dsoname(alt.map, fp);
840 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700841 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300842 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700843 mispred_str(br->entries + i),
844 br->entries[i].flags.in_tx ? 'X' : '-',
845 br->entries[i].flags.abort ? 'A' : '-',
846 br->entries[i].flags.cycles);
847 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300848
849 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700850}
Andi Kleen48d02a12017-02-23 15:46:34 -0800851#define MAXBB 16384UL
852
853static int grab_bb(u8 *buffer, u64 start, u64 end,
854 struct machine *machine, struct thread *thread,
855 bool *is64bit, u8 *cpumode, bool last)
856{
857 long offset, len;
858 struct addr_location al;
859 bool kernel;
860
861 if (!start || !end)
862 return 0;
863
864 kernel = machine__kernel_ip(machine, start);
865 if (kernel)
866 *cpumode = PERF_RECORD_MISC_KERNEL;
867 else
868 *cpumode = PERF_RECORD_MISC_USER;
869
870 /*
871 * Block overlaps between kernel and user.
872 * This can happen due to ring filtering
873 * On Intel CPUs the entry into the kernel is filtered,
874 * but the exit is not. Let the caller patch it up.
875 */
876 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300877 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800878 return -ENXIO;
879 }
880
881 memset(&al, 0, sizeof(al));
882 if (end - start > MAXBB - MAXINSN) {
883 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300884 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800885 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300886 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800887 return 0;
888 }
889
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300890 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300891 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800892 return 0;
893 }
894 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300895 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800896 return 0;
897 }
898
899 /* Load maps to ensure dso->is_64_bit has been updated */
900 map__load(al.map);
901
902 offset = al.map->map_ip(al.map, start);
903 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
904 end - start + MAXINSN);
905
906 *is64bit = al.map->dso->is_64_bit;
907 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300908 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800909 start, end);
910 return len;
911}
912
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300913static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
914 struct perf_insn *x, u8 *inbuf, int len,
915 int insn, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800916{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300917 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
918 dump_insn(x, ip, inbuf, len, NULL),
919 en->flags.predicted ? " PRED" : "",
920 en->flags.mispred ? " MISPRED" : "",
921 en->flags.in_tx ? " INTX" : "",
922 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800923 if (en->flags.cycles) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300924 printed += fprintf(fp, " %d cycles", en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800925 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300926 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800927 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300928 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800929}
930
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300931static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
932 u8 cpumode, int cpu, struct symbol **lastsym,
933 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800934{
935 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300936 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800937
938 memset(&al, 0, sizeof(al));
939
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300940 thread__find_map(thread, cpumode, addr, &al);
941
Andi Kleen48d02a12017-02-23 15:46:34 -0800942 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300943 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800944
945 al.cpu = cpu;
946 al.sym = NULL;
947 if (al.map)
948 al.sym = map__find_symbol(al.map, al.addr);
949
950 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300951 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800952
953 if (al.addr < al.sym->end)
954 off = al.addr - al.sym->start;
955 else
956 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300957 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800958 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300959 printed += fprintf(fp, "%+d", off);
960 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800961 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300962 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
963 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800964 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300965
966 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800967}
968
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300969static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
970 struct thread *thread,
971 struct perf_event_attr *attr,
972 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800973{
974 struct branch_stack *br = sample->branch_stack;
975 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300976 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800977 struct perf_insn x;
978 u8 buffer[MAXBB];
979 unsigned off;
980 struct symbol *lastsym = NULL;
981
982 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300983 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800984 nr = br->nr;
985 if (max_blocks && nr > max_blocks + 1)
986 nr = max_blocks + 1;
987
988 x.thread = thread;
989 x.cpu = sample->cpu;
990
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300991 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -0800992
993 /* Handle first from jump, of which we don't know the entry. */
994 len = grab_bb(buffer, br->entries[nr-1].from,
995 br->entries[nr-1].from,
996 machine, thread, &x.is64bit, &x.cpumode, false);
997 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300998 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
999 x.cpumode, x.cpu, &lastsym, attr, fp);
1000 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
1001 &x, buffer, len, 0, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001002 }
1003
1004 /* Print all blocks */
1005 for (i = nr - 2; i >= 0; i--) {
1006 if (br->entries[i].from || br->entries[i].to)
1007 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1008 br->entries[i].from,
1009 br->entries[i].to);
1010 start = br->entries[i + 1].to;
1011 end = br->entries[i].from;
1012
1013 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1014 /* Patch up missing kernel transfers due to ring filters */
1015 if (len == -ENXIO && i > 0) {
1016 end = br->entries[--i].from;
1017 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1018 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1019 }
1020 if (len <= 0)
1021 continue;
1022
1023 insn = 0;
1024 for (off = 0;; off += ilen) {
1025 uint64_t ip = start + off;
1026
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001027 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001028 if (ip == end) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001029 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001030 break;
1031 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001032 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1033 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001034 if (ilen == 0)
1035 break;
1036 insn++;
1037 }
1038 }
1039 }
1040
1041 /*
1042 * Hit the branch? In this case we are already done, and the target
1043 * has not been executed yet.
1044 */
1045 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001046 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001047 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001048 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001049
1050 /*
1051 * Print final block upto sample
1052 */
1053 start = br->entries[0].to;
1054 end = sample->ip;
1055 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001056 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001057 if (len <= 0) {
1058 /* Print at least last IP if basic block did not work */
1059 len = grab_bb(buffer, sample->ip, sample->ip,
1060 machine, thread, &x.is64bit, &x.cpumode, false);
1061 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001062 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001063
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001064 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001065 dump_insn(&x, sample->ip, buffer, len, NULL));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001066 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001067 }
1068 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001069 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1070 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001071 if (ilen == 0)
1072 break;
1073 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001074out:
1075 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001076}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001077
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001078static int perf_sample__fprintf_addr(struct perf_sample *sample,
1079 struct thread *thread,
1080 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001081{
1082 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001083 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001084
1085 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001086 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001087
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001088 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001089
1090 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001091 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001092 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001093 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001094 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001095 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001096 }
1097
1098 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001099 printed += fprintf(fp, " (");
1100 printed += map__fprintf_dsoname(al.map, fp);
1101 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001102 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001103out:
1104 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001105}
1106
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001107static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1108 struct perf_evsel *evsel,
1109 struct thread *thread,
1110 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001111{
1112 struct perf_event_attr *attr = &evsel->attr;
1113 size_t depth = thread_stack__depth(thread);
1114 struct addr_location addr_al;
1115 const char *name = NULL;
1116 static int spacing;
1117 int len = 0;
1118 u64 ip = 0;
1119
1120 /*
1121 * The 'return' has already been popped off the stack so the depth has
1122 * to be adjusted to match the 'call'.
1123 */
1124 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1125 depth += 1;
1126
1127 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1128 if (sample_addr_correlates_sym(attr)) {
1129 thread__resolve(thread, &addr_al, sample);
1130 if (addr_al.sym)
1131 name = addr_al.sym->name;
1132 else
1133 ip = sample->addr;
1134 } else {
1135 ip = sample->addr;
1136 }
1137 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1138 if (al->sym)
1139 name = al->sym->name;
1140 else
1141 ip = sample->ip;
1142 }
1143
1144 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001145 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001146 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001147 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001148
1149 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001150 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001151
1152 /*
1153 * Try to keep the output length from changing frequently so that the
1154 * output lines up more nicely.
1155 */
1156 if (len > spacing || (len && len < spacing - 52))
1157 spacing = round_up(len + 4, 32);
1158
1159 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001160 len += fprintf(fp, "%*s", spacing - len, "");
1161
1162 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001163}
1164
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001165static int perf_sample__fprintf_insn(struct perf_sample *sample,
1166 struct perf_event_attr *attr,
1167 struct thread *thread,
1168 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001169{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001170 int printed = 0;
1171
Andi Kleen224e2c92016-10-07 16:42:27 +03001172 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001173 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen224e2c92016-10-07 16:42:27 +03001174 if (PRINT_FIELD(INSN)) {
1175 int i;
1176
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001177 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001178 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001179 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001180 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001181 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001182 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1183
1184 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001185}
1186
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001187static int perf_sample__fprintf_bts(struct perf_sample *sample,
1188 struct perf_evsel *evsel,
1189 struct thread *thread,
1190 struct addr_location *al,
1191 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001192{
1193 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001194 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001195 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001196 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001197
Adrian Huntere2167082016-06-23 16:40:58 +03001198 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001199 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001200
Akihiro Nagai95582592012-01-30 13:43:09 +09001201 /* print branch_from information */
1202 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001203 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001204 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001205
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001206 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001207 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001208 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001209 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001210
1211 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001212 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001213 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001214 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001215 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001216 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001217 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001218 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001219
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001220 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001221 }
1222
Akihiro Nagai95582592012-01-30 13:43:09 +09001223 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001224 if (PRINT_FIELD(ADDR) ||
1225 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001226 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001227 printed += fprintf(fp, " => ");
1228 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001229 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001230
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001231 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001232 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001233
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001234 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1235 return printed + fprintf(fp, "\n");
Akihiro Nagai95582592012-01-30 13:43:09 +09001236}
1237
Adrian Hunter055cd332016-06-23 16:40:56 +03001238static struct {
1239 u32 flags;
1240 const char *name;
1241} sample_flags[] = {
1242 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1243 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1244 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1245 {PERF_IP_FLAG_BRANCH, "jmp"},
1246 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1247 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1248 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1249 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1250 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1251 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1252 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1253 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1254 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1255 {0, NULL}
1256};
1257
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001258static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001259{
1260 const char *chars = PERF_IP_FLAG_CHARS;
1261 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001262 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1263 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001264 char str[33];
1265 int i, pos = 0;
1266
Adrian Hunter055cd332016-06-23 16:40:56 +03001267 for (i = 0; sample_flags[i].name ; i++) {
1268 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1269 name = sample_flags[i].name;
1270 break;
1271 }
1272 }
1273
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001274 for (i = 0; i < n; i++, flags >>= 1) {
1275 if (flags & 1)
1276 str[pos++] = chars[i];
1277 }
1278 for (; i < 32; i++, flags >>= 1) {
1279 if (flags & 1)
1280 str[pos++] = '?';
1281 }
1282 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001283
1284 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001285 return fprintf(fp, " %-7s%4s ", name, in_tx ? "(x)" : "");
1286
1287 return fprintf(fp, " %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001288}
1289
Wang Nan30372f02016-02-24 11:20:45 +00001290struct printer_data {
1291 int line_no;
1292 bool hit_nul;
1293 bool is_printable;
1294};
1295
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001296static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1297 unsigned int val,
1298 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001299{
1300 unsigned char ch = (unsigned char)val;
1301 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001302 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001303
1304 switch (op) {
1305 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001306 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001307 break;
1308 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001309 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001310 " ");
1311 break;
1312 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001313 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001314 break;
1315 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001316 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001317 break;
1318 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001319 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001320 break;
1321 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001322 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001323 break;
1324 case BINARY_PRINT_CHAR_DATA:
1325 if (printer_data->hit_nul && ch)
1326 printer_data->is_printable = false;
1327
1328 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001329 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001330
1331 if (!printer_data->is_printable)
1332 break;
1333
1334 if (ch == '\0')
1335 printer_data->hit_nul = true;
1336 else
1337 printer_data->is_printable = false;
1338 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001339 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001340 }
1341 break;
1342 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001343 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001344 break;
1345 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001346 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001347 printer_data->line_no++;
1348 break;
1349 case BINARY_PRINT_DATA_END:
1350 default:
1351 break;
1352 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001353
1354 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001355}
1356
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001357static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001358{
1359 unsigned int nr_bytes = sample->raw_size;
1360 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001361 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1362 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001363
1364 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001365 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1366
1367 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001368}
1369
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001370static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001371{
1372 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001373 return fprintf(fp, "%*s", spacing - len, "");
1374
1375 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001376}
1377
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001378static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001379{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001380 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001381}
1382
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001383static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001384{
1385 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1386 int len;
1387
1388 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001389 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001390
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001391 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001392 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001393 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001394}
1395
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001396static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001397{
1398 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1399 int len;
1400
1401 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001402 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001403
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001404 len = fprintf(fp, " hints: %#x extensions: %#x ",
1405 data->hints, data->extensions);
1406 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001407}
1408
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001409static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001410{
1411 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1412 int len;
1413
1414 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001415 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001416
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001417 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1418 data->hw, data->cstate, data->subcstate);
1419 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001420}
1421
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001422static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001423{
1424 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1425 int len;
1426
1427 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001428 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001429
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001430 len = fprintf(fp, " IP: %u ", data->ip);
1431 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001432}
1433
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001434static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001435{
1436 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1437 int len;
1438
1439 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001440 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001441
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001442 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001443 data->deepest_cstate, data->last_cstate,
1444 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001445 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001446}
1447
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001448static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001449{
1450 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1451 unsigned int percent, freq;
1452 int len;
1453
1454 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001455 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001456
1457 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001458 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001459 if (data->max_nonturbo) {
1460 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001461 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001462 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001463 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001464}
1465
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001466static int perf_sample__fprintf_synth(struct perf_sample *sample,
1467 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001468{
1469 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001470 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001471 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001472 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001473 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001474 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001475 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001476 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001477 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001478 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001479 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001480 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001481 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001482 default:
1483 break;
1484 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001485
1486 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001487}
1488
Jiri Olsa809e9422015-11-26 18:55:21 +01001489struct perf_script {
1490 struct perf_tool tool;
1491 struct perf_session *session;
1492 bool show_task_events;
1493 bool show_mmap_events;
1494 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301495 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001496 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001497 bool show_round_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001498 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001499 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001500 struct cpu_map *cpus;
1501 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001502 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001503 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001504 struct perf_time_interval *ptime_range;
1505 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001506 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001507};
1508
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001509static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1510{
1511 struct perf_evsel *evsel;
1512 int max = 0;
1513
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001514 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001515 int len = strlen(perf_evsel__name(evsel));
1516
1517 max = MAX(len, max);
1518 }
1519
1520 return max;
1521}
1522
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001523static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001524{
1525 struct mem_info mi = { .data_src.val = data_src };
1526 char decode[100];
1527 char out[100];
1528 static int maxlen;
1529 int len;
1530
1531 perf_script__meminfo_scnprintf(decode, 100, &mi);
1532
1533 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1534 if (maxlen < len)
1535 maxlen = len;
1536
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001537 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001538}
1539
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001540struct metric_ctx {
1541 struct perf_sample *sample;
1542 struct thread *thread;
1543 struct perf_evsel *evsel;
1544 FILE *fp;
1545};
1546
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001547static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1548 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001549 const char *fmt,
1550 const char *unit, double val)
1551{
1552 struct metric_ctx *mctx = ctx;
1553
1554 if (!fmt)
1555 return;
1556 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001557 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001558 fputs("\tmetric: ", mctx->fp);
1559 if (color)
1560 color_fprintf(mctx->fp, color, fmt, val);
1561 else
1562 printf(fmt, val);
1563 fprintf(mctx->fp, " %s\n", unit);
1564}
1565
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001566static void script_new_line(struct perf_stat_config *config __maybe_unused,
1567 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001568{
1569 struct metric_ctx *mctx = ctx;
1570
1571 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001572 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001573 fputs("\tmetric: ", mctx->fp);
1574}
1575
1576static void perf_sample__fprint_metric(struct perf_script *script,
1577 struct thread *thread,
1578 struct perf_evsel *evsel,
1579 struct perf_sample *sample,
1580 FILE *fp)
1581{
1582 struct perf_stat_output_ctx ctx = {
1583 .print_metric = script_print_metric,
1584 .new_line = script_new_line,
1585 .ctx = &(struct metric_ctx) {
1586 .sample = sample,
1587 .thread = thread,
1588 .evsel = evsel,
1589 .fp = fp,
1590 },
1591 .force_header = false,
1592 };
1593 struct perf_evsel *ev2;
1594 static bool init;
1595 u64 val;
1596
1597 if (!init) {
1598 perf_stat__init_shadow_stats();
1599 init = true;
1600 }
1601 if (!evsel->stats)
1602 perf_evlist__alloc_stats(script->session->evlist, false);
1603 if (evsel_script(evsel->leader)->gnum++ == 0)
1604 perf_stat__reset_shadow_stats();
1605 val = sample->period * evsel->scale;
1606 perf_stat__update_shadow_stats(evsel,
1607 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001608 sample->cpu,
1609 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001610 evsel_script(evsel)->val = val;
1611 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1612 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001613 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001614 evsel_script(ev2)->val,
1615 sample->cpu,
1616 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001617 NULL,
1618 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001619 }
1620 evsel_script(evsel->leader)->gnum = 0;
1621 }
1622}
1623
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001624static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001625 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001626 struct addr_location *al,
1627 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001628{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001629 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001630 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001631 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001632 struct perf_evsel_script *es = evsel->priv;
1633 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001634
Adrian Hunter14057202017-06-21 13:17:19 +03001635 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001636 return;
1637
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001638 ++es->samples;
1639
Jiri Olsa28a0b392018-01-07 17:03:52 +01001640 perf_sample__fprintf_start(sample, thread, evsel,
1641 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001642
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001643 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001644 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001645
Namhyung Kime944d3d2013-11-18 14:34:52 +09001646 if (PRINT_FIELD(EVNAME)) {
1647 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001648
1649 if (!script->name_width)
1650 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1651
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001652 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001653 }
1654
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001655 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001656 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001657
Akihiro Nagai95582592012-01-30 13:43:09 +09001658 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001659 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001660 return;
1661 }
1662
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001663 if (PRINT_FIELD(TRACE)) {
1664 event_format__fprintf(evsel->tp_format, sample->cpu,
1665 sample->raw_data, sample->raw_size, fp);
1666 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001667
1668 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001669 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001670
David Ahern7cec0922011-05-30 13:08:23 -06001671 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001672 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001673
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001674 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001675 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001676
1677 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001678 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001679
David Ahern787bef12011-05-27 14:28:43 -06001680 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001681 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001682
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001683 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001684 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001685 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001686 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001687
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001688 fputc(cursor ? '\n' : ' ', fp);
1689 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001690 }
1691
Stephane Eranianfc36f942015-08-31 18:41:10 +02001692 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001693 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001694
Andi Kleenb1491ac2017-09-05 11:40:57 -07001695 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001696 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001697
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001698 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001699 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001700 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001701 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001702 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001703 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001704
Wang Nan30372f02016-02-24 11:20:45 +00001705 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001706 perf_sample__fprintf_bpf_output(sample, fp);
1707 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001708
1709 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001710 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1711 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001712
1713 if (PRINT_FIELD(METRIC))
1714 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001715}
1716
Tom Zanussi956ffd02009-11-25 01:15:46 -06001717static struct scripting_ops *scripting_ops;
1718
Jiri Olsa36e33c52016-01-06 11:49:56 +01001719static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1720{
1721 int nthreads = thread_map__nr(counter->threads);
1722 int ncpus = perf_evsel__nr_cpus(counter);
1723 int cpu, thread;
1724 static int header_printed;
1725
1726 if (counter->system_wide)
1727 nthreads = 1;
1728
1729 if (!header_printed) {
1730 printf("%3s %8s %15s %15s %15s %15s %s\n",
1731 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1732 header_printed = 1;
1733 }
1734
1735 for (thread = 0; thread < nthreads; thread++) {
1736 for (cpu = 0; cpu < ncpus; cpu++) {
1737 struct perf_counts_values *counts;
1738
1739 counts = perf_counts(counter->counts, cpu, thread);
1740
1741 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1742 counter->cpus->map[cpu],
1743 thread_map__pid(counter->threads, thread),
1744 counts->val,
1745 counts->ena,
1746 counts->run,
1747 tstamp,
1748 perf_evsel__name(counter));
1749 }
1750 }
1751}
1752
Jiri Olsae099eba2016-01-05 22:09:09 +01001753static void process_stat(struct perf_evsel *counter, u64 tstamp)
1754{
1755 if (scripting_ops && scripting_ops->process_stat)
1756 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001757 else
1758 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001759}
1760
1761static void process_stat_interval(u64 tstamp)
1762{
1763 if (scripting_ops && scripting_ops->process_stat_interval)
1764 scripting_ops->process_stat_interval(tstamp);
1765}
1766
Tom Zanussi956ffd02009-11-25 01:15:46 -06001767static void setup_scripting(void)
1768{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001769 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001770 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001771}
1772
Adrian Hunterd445dd22014-08-15 22:08:37 +03001773static int flush_scripting(void)
1774{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001775 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001776}
1777
Tom Zanussi956ffd02009-11-25 01:15:46 -06001778static int cleanup_scripting(void)
1779{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001780 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001781
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001782 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001783}
1784
Jiri Olsa809e9422015-11-26 18:55:21 +01001785static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001786 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001787 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001788 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001789 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001790{
Jiri Olsa809e9422015-11-26 18:55:21 +01001791 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001792 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001793
Jin Yao2ab046c2017-12-08 21:13:46 +08001794 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1795 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001796 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001797 }
David Aherna91f4c42016-11-29 10:15:43 -07001798
David Ahern1424dc92011-03-09 22:23:28 -07001799 if (debug_mode) {
1800 if (sample->time < last_timestamp) {
1801 pr_err("Samples misordered, previous: %" PRIu64
1802 " this: %" PRIu64 "\n", last_timestamp,
1803 sample->time);
1804 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001805 }
David Ahern1424dc92011-03-09 22:23:28 -07001806 last_timestamp = sample->time;
1807 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001808 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001809
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001810 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001811 pr_err("problem processing %d event, skipping it.\n",
1812 event->header.type);
1813 return -1;
1814 }
1815
1816 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001817 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001818
Anton Blanchard5d67be92011-07-04 21:57:50 +10001819 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001820 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001821
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001822 if (scripting_ops)
1823 scripting_ops->process_event(event, sample, evsel, &al);
1824 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001825 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001826
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001827out_put:
1828 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001829 return 0;
1830}
1831
Adrian Hunter7ea95722013-11-01 15:51:30 +02001832static int process_attr(struct perf_tool *tool, union perf_event *event,
1833 struct perf_evlist **pevlist)
1834{
1835 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1836 struct perf_evlist *evlist;
1837 struct perf_evsel *evsel, *pos;
1838 int err;
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301839 static struct perf_evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001840
1841 err = perf_event__process_attr(tool, event, pevlist);
1842 if (err)
1843 return err;
1844
1845 evlist = *pevlist;
1846 evsel = perf_evlist__last(*pevlist);
1847
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301848 if (!evsel->priv) {
1849 if (scr->per_event_dump) {
1850 evsel->priv = perf_evsel_script__new(evsel,
1851 scr->session->data);
1852 } else {
1853 es = zalloc(sizeof(*es));
1854 if (!es)
1855 return -ENOMEM;
1856 es->fp = stdout;
1857 evsel->priv = es;
1858 }
1859 }
1860
Adrian Hunter14057202017-06-21 13:17:19 +03001861 if (evsel->attr.type >= PERF_TYPE_MAX &&
1862 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001863 return 0;
1864
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001865 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001866 if (pos->attr.type == evsel->attr.type && pos != evsel)
1867 return 0;
1868 }
1869
1870 set_print_ip_opts(&evsel->attr);
1871
Jiri Olsad2b5a312015-10-16 12:41:25 +02001872 if (evsel->attr.sample_type)
1873 err = perf_evsel__check_attr(evsel, scr->session);
1874
1875 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001876}
1877
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001878static int process_comm_event(struct perf_tool *tool,
1879 union perf_event *event,
1880 struct perf_sample *sample,
1881 struct machine *machine)
1882{
1883 struct thread *thread;
1884 struct perf_script *script = container_of(tool, struct perf_script, tool);
1885 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001886 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001887 int ret = -1;
1888
1889 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1890 if (thread == NULL) {
1891 pr_debug("problem processing COMM event, skipping it.\n");
1892 return -1;
1893 }
1894
1895 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1896 goto out;
1897
1898 if (!evsel->attr.sample_id_all) {
1899 sample->cpu = 0;
1900 sample->time = 0;
1901 sample->tid = event->comm.tid;
1902 sample->pid = event->comm.pid;
1903 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01001904 perf_sample__fprintf_start(sample, thread, evsel,
1905 PERF_RECORD_COMM, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001906 perf_event__fprintf(event, stdout);
1907 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001908out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001909 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001910 return ret;
1911}
1912
Hari Bathini96a44bb2017-03-08 02:12:06 +05301913static int process_namespaces_event(struct perf_tool *tool,
1914 union perf_event *event,
1915 struct perf_sample *sample,
1916 struct machine *machine)
1917{
1918 struct thread *thread;
1919 struct perf_script *script = container_of(tool, struct perf_script, tool);
1920 struct perf_session *session = script->session;
1921 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1922 int ret = -1;
1923
1924 thread = machine__findnew_thread(machine, event->namespaces.pid,
1925 event->namespaces.tid);
1926 if (thread == NULL) {
1927 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1928 return -1;
1929 }
1930
1931 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1932 goto out;
1933
1934 if (!evsel->attr.sample_id_all) {
1935 sample->cpu = 0;
1936 sample->time = 0;
1937 sample->tid = event->namespaces.tid;
1938 sample->pid = event->namespaces.pid;
1939 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01001940 perf_sample__fprintf_start(sample, thread, evsel,
1941 PERF_RECORD_NAMESPACES, stdout);
Hari Bathini96a44bb2017-03-08 02:12:06 +05301942 perf_event__fprintf(event, stdout);
1943 ret = 0;
1944out:
1945 thread__put(thread);
1946 return ret;
1947}
1948
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001949static int process_fork_event(struct perf_tool *tool,
1950 union perf_event *event,
1951 struct perf_sample *sample,
1952 struct machine *machine)
1953{
1954 struct thread *thread;
1955 struct perf_script *script = container_of(tool, struct perf_script, tool);
1956 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001957 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001958
1959 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1960 return -1;
1961
1962 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1963 if (thread == NULL) {
1964 pr_debug("problem processing FORK event, skipping it.\n");
1965 return -1;
1966 }
1967
1968 if (!evsel->attr.sample_id_all) {
1969 sample->cpu = 0;
1970 sample->time = event->fork.time;
1971 sample->tid = event->fork.tid;
1972 sample->pid = event->fork.pid;
1973 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01001974 perf_sample__fprintf_start(sample, thread, evsel,
1975 PERF_RECORD_FORK, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001976 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001977 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001978
1979 return 0;
1980}
1981static int process_exit_event(struct perf_tool *tool,
1982 union perf_event *event,
1983 struct perf_sample *sample,
1984 struct machine *machine)
1985{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001986 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001987 struct thread *thread;
1988 struct perf_script *script = container_of(tool, struct perf_script, tool);
1989 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001990 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001991
1992 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1993 if (thread == NULL) {
1994 pr_debug("problem processing EXIT event, skipping it.\n");
1995 return -1;
1996 }
1997
1998 if (!evsel->attr.sample_id_all) {
1999 sample->cpu = 0;
2000 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002001 sample->tid = event->fork.tid;
2002 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002003 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002004 perf_sample__fprintf_start(sample, thread, evsel,
2005 PERF_RECORD_EXIT, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002006 perf_event__fprintf(event, stdout);
2007
2008 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002009 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002010
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002011 thread__put(thread);
2012 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002013}
2014
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002015static int process_mmap_event(struct perf_tool *tool,
2016 union perf_event *event,
2017 struct perf_sample *sample,
2018 struct machine *machine)
2019{
2020 struct thread *thread;
2021 struct perf_script *script = container_of(tool, struct perf_script, tool);
2022 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002023 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002024
2025 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2026 return -1;
2027
2028 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2029 if (thread == NULL) {
2030 pr_debug("problem processing MMAP event, skipping it.\n");
2031 return -1;
2032 }
2033
2034 if (!evsel->attr.sample_id_all) {
2035 sample->cpu = 0;
2036 sample->time = 0;
2037 sample->tid = event->mmap.tid;
2038 sample->pid = event->mmap.pid;
2039 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002040 perf_sample__fprintf_start(sample, thread, evsel,
2041 PERF_RECORD_MMAP, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002042 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002043 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002044 return 0;
2045}
2046
2047static int process_mmap2_event(struct perf_tool *tool,
2048 union perf_event *event,
2049 struct perf_sample *sample,
2050 struct machine *machine)
2051{
2052 struct thread *thread;
2053 struct perf_script *script = container_of(tool, struct perf_script, tool);
2054 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002055 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002056
2057 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2058 return -1;
2059
2060 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2061 if (thread == NULL) {
2062 pr_debug("problem processing MMAP2 event, skipping it.\n");
2063 return -1;
2064 }
2065
2066 if (!evsel->attr.sample_id_all) {
2067 sample->cpu = 0;
2068 sample->time = 0;
2069 sample->tid = event->mmap2.tid;
2070 sample->pid = event->mmap2.pid;
2071 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002072 perf_sample__fprintf_start(sample, thread, evsel,
2073 PERF_RECORD_MMAP2, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002074 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002075 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002076 return 0;
2077}
2078
Adrian Hunter7c148982015-07-21 12:44:06 +03002079static int process_switch_event(struct perf_tool *tool,
2080 union perf_event *event,
2081 struct perf_sample *sample,
2082 struct machine *machine)
2083{
2084 struct thread *thread;
2085 struct perf_script *script = container_of(tool, struct perf_script, tool);
2086 struct perf_session *session = script->session;
2087 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2088
2089 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2090 return -1;
2091
2092 thread = machine__findnew_thread(machine, sample->pid,
2093 sample->tid);
2094 if (thread == NULL) {
2095 pr_debug("problem processing SWITCH event, skipping it.\n");
2096 return -1;
2097 }
2098
Jiri Olsa28a0b392018-01-07 17:03:52 +01002099 perf_sample__fprintf_start(sample, thread, evsel,
2100 PERF_RECORD_SWITCH, stdout);
Adrian Hunter7c148982015-07-21 12:44:06 +03002101 perf_event__fprintf(event, stdout);
2102 thread__put(thread);
2103 return 0;
2104}
2105
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002106static int
2107process_lost_event(struct perf_tool *tool,
2108 union perf_event *event,
2109 struct perf_sample *sample,
2110 struct machine *machine)
2111{
2112 struct perf_script *script = container_of(tool, struct perf_script, tool);
2113 struct perf_session *session = script->session;
2114 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2115 struct thread *thread;
2116
2117 thread = machine__findnew_thread(machine, sample->pid,
2118 sample->tid);
2119 if (thread == NULL)
2120 return -1;
2121
2122 perf_sample__fprintf_start(sample, thread, evsel,
2123 PERF_RECORD_LOST, stdout);
2124 perf_event__fprintf(event, stdout);
2125 thread__put(thread);
2126 return 0;
2127}
2128
Jiri Olsa3233b372018-02-06 19:17:59 +01002129static int
2130process_finished_round_event(struct perf_tool *tool __maybe_unused,
2131 union perf_event *event,
2132 struct ordered_events *oe __maybe_unused)
2133
2134{
2135 perf_event__fprintf(event, stdout);
2136 return 0;
2137}
2138
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002139static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002140{
2141 session_done = 1;
2142}
2143
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002144static void perf_script__fclose_per_event_dump(struct perf_script *script)
2145{
2146 struct perf_evlist *evlist = script->session->evlist;
2147 struct perf_evsel *evsel;
2148
2149 evlist__for_each_entry(evlist, evsel) {
2150 if (!evsel->priv)
2151 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002152 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002153 evsel->priv = NULL;
2154 }
2155}
2156
2157static int perf_script__fopen_per_event_dump(struct perf_script *script)
2158{
2159 struct perf_evsel *evsel;
2160
2161 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002162 /*
2163 * Already setup? I.e. we may be called twice in cases like
2164 * Intel PT, one for the intel_pt// and dummy events, then
2165 * for the evsels syntheized from the auxtrace info.
2166 *
2167 * Ses perf_script__process_auxtrace_info.
2168 */
2169 if (evsel->priv != NULL)
2170 continue;
2171
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002172 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002173 if (evsel->priv == NULL)
2174 goto out_err_fclose;
2175 }
2176
2177 return 0;
2178
2179out_err_fclose:
2180 perf_script__fclose_per_event_dump(script);
2181 return -1;
2182}
2183
2184static int perf_script__setup_per_event_dump(struct perf_script *script)
2185{
2186 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002187 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002188
2189 if (script->per_event_dump)
2190 return perf_script__fopen_per_event_dump(script);
2191
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002192 es_stdout.fp = stdout;
2193
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002194 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002195 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002196
2197 return 0;
2198}
2199
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002200static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2201{
2202 struct perf_evsel *evsel;
2203
2204 evlist__for_each_entry(script->session->evlist, evsel) {
2205 struct perf_evsel_script *es = evsel->priv;
2206
2207 perf_evsel_script__fprintf(es, stdout);
2208 perf_evsel_script__delete(es);
2209 evsel->priv = NULL;
2210 }
2211}
2212
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002213static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002214{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002215 int ret;
2216
Tom Zanussic239da32010-04-01 23:59:18 -05002217 signal(SIGINT, sig_handler);
2218
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002219 /* override event processing functions */
2220 if (script->show_task_events) {
2221 script->tool.comm = process_comm_event;
2222 script->tool.fork = process_fork_event;
2223 script->tool.exit = process_exit_event;
2224 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002225 if (script->show_mmap_events) {
2226 script->tool.mmap = process_mmap_event;
2227 script->tool.mmap2 = process_mmap2_event;
2228 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002229 if (script->show_switch_events)
2230 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302231 if (script->show_namespace_events)
2232 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002233 if (script->show_lost_events)
2234 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002235 if (script->show_round_events) {
2236 script->tool.ordered_events = false;
2237 script->tool.finished_round = process_finished_round_event;
2238 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002239
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002240 if (perf_script__setup_per_event_dump(script)) {
2241 pr_err("Couldn't create the per event dump files\n");
2242 return -1;
2243 }
2244
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002245 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002246
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002247 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002248 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002249
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002250 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002251 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002252
2253 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002254}
2255
Tom Zanussi956ffd02009-11-25 01:15:46 -06002256struct script_spec {
2257 struct list_head node;
2258 struct scripting_ops *ops;
2259 char spec[0];
2260};
2261
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002262static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002263
2264static struct script_spec *script_spec__new(const char *spec,
2265 struct scripting_ops *ops)
2266{
2267 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2268
2269 if (s != NULL) {
2270 strcpy(s->spec, spec);
2271 s->ops = ops;
2272 }
2273
2274 return s;
2275}
2276
Tom Zanussi956ffd02009-11-25 01:15:46 -06002277static void script_spec__add(struct script_spec *s)
2278{
2279 list_add_tail(&s->node, &script_specs);
2280}
2281
2282static struct script_spec *script_spec__find(const char *spec)
2283{
2284 struct script_spec *s;
2285
2286 list_for_each_entry(s, &script_specs, node)
2287 if (strcasecmp(s->spec, spec) == 0)
2288 return s;
2289 return NULL;
2290}
2291
Tom Zanussi956ffd02009-11-25 01:15:46 -06002292int script_spec_register(const char *spec, struct scripting_ops *ops)
2293{
2294 struct script_spec *s;
2295
2296 s = script_spec__find(spec);
2297 if (s)
2298 return -1;
2299
Taeung Song8560bae2016-02-26 00:13:10 +09002300 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002301 if (!s)
2302 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002303 else
2304 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002305
2306 return 0;
2307}
2308
2309static struct scripting_ops *script_spec__lookup(const char *spec)
2310{
2311 struct script_spec *s = script_spec__find(spec);
2312 if (!s)
2313 return NULL;
2314
2315 return s->ops;
2316}
2317
2318static void list_available_languages(void)
2319{
2320 struct script_spec *s;
2321
2322 fprintf(stderr, "\n");
2323 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002324 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002325
2326 list_for_each_entry(s, &script_specs, node)
2327 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2328
2329 fprintf(stderr, "\n");
2330}
2331
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002332static int parse_scriptname(const struct option *opt __maybe_unused,
2333 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002334{
2335 char spec[PATH_MAX];
2336 const char *script, *ext;
2337 int len;
2338
Tom Zanussif526d682010-01-27 02:27:52 -06002339 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002340 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002341 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002342 }
2343
2344 script = strchr(str, ':');
2345 if (script) {
2346 len = script - str;
2347 if (len >= PATH_MAX) {
2348 fprintf(stderr, "invalid language specifier");
2349 return -1;
2350 }
2351 strncpy(spec, str, len);
2352 spec[len] = '\0';
2353 scripting_ops = script_spec__lookup(spec);
2354 if (!scripting_ops) {
2355 fprintf(stderr, "invalid language specifier");
2356 return -1;
2357 }
2358 script++;
2359 } else {
2360 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002361 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002362 if (!ext) {
2363 fprintf(stderr, "invalid script extension");
2364 return -1;
2365 }
2366 scripting_ops = script_spec__lookup(++ext);
2367 if (!scripting_ops) {
2368 fprintf(stderr, "invalid script extension");
2369 return -1;
2370 }
2371 }
2372
2373 script_name = strdup(script);
2374
2375 return 0;
2376}
2377
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002378static int parse_output_fields(const struct option *opt __maybe_unused,
2379 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002380{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002381 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002382 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002383 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002384 int rc = 0;
2385 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002386 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002387 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002388
2389 if (!str)
2390 return -ENOMEM;
2391
David Ahern2c9e45f72011-03-17 10:03:21 -06002392 /* first word can state for which event type the user is specifying
2393 * the fields. If no type exists, the specified fields apply to all
2394 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002395 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002396 tok = strchr(str, ':');
2397 if (tok) {
2398 *tok = '\0';
2399 tok++;
2400 if (!strcmp(str, "hw"))
2401 type = PERF_TYPE_HARDWARE;
2402 else if (!strcmp(str, "sw"))
2403 type = PERF_TYPE_SOFTWARE;
2404 else if (!strcmp(str, "trace"))
2405 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002406 else if (!strcmp(str, "raw"))
2407 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002408 else if (!strcmp(str, "break"))
2409 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002410 else if (!strcmp(str, "synth"))
2411 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002412 else {
2413 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002414 rc = -EINVAL;
2415 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002416 }
2417
2418 if (output[type].user_set)
2419 pr_warning("Overriding previous field request for %s events.\n",
2420 event_type(type));
2421
2422 output[type].fields = 0;
2423 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002424 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002425
2426 } else {
2427 tok = str;
2428 if (strlen(str) == 0) {
2429 fprintf(stderr,
2430 "Cannot set fields to 'none' for all event types.\n");
2431 rc = -EINVAL;
2432 goto out;
2433 }
2434
Andi Kleen36ce5652017-06-02 08:48:10 -07002435 /* Don't override defaults for +- */
2436 if (strchr(str, '+') || strchr(str, '-'))
2437 goto parse;
2438
David Ahern2c9e45f72011-03-17 10:03:21 -06002439 if (output_set_by_user())
2440 pr_warning("Overriding previous field request for all events.\n");
2441
Adrian Hunter14057202017-06-21 13:17:19 +03002442 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002443 output[j].fields = 0;
2444 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002445 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002446 }
David Ahern1424dc92011-03-09 22:23:28 -07002447 }
2448
Andi Kleen36ce5652017-06-02 08:48:10 -07002449parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002450 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002451 if (*tok == '+') {
2452 if (change == SET)
2453 goto out_badmix;
2454 change = ADD;
2455 tok++;
2456 } else if (*tok == '-') {
2457 if (change == SET)
2458 goto out_badmix;
2459 change = REMOVE;
2460 tok++;
2461 } else {
2462 if (change != SET && change != DEFAULT)
2463 goto out_badmix;
2464 change = SET;
2465 }
2466
David Ahern745f43e2011-03-09 22:23:26 -07002467 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002468 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002469 break;
David Ahern745f43e2011-03-09 22:23:26 -07002470 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002471 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002472 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002473 continue;
2474 }
David Ahern745f43e2011-03-09 22:23:26 -07002475 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002476 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002477 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002478 goto out;
2479 }
2480
2481 if (type == -1) {
2482 /* add user option to all events types for
2483 * which it is valid
2484 */
Adrian Hunter14057202017-06-21 13:17:19 +03002485 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002486 if (output[j].invalid_fields & all_output_options[i].field) {
2487 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2488 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002489 } else {
2490 if (change == REMOVE)
2491 output[j].fields &= ~all_output_options[i].field;
2492 else
2493 output[j].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002494 output[j].user_set = true;
2495 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002496 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002497 }
2498 } else {
2499 if (output[type].invalid_fields & all_output_options[i].field) {
2500 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2501 all_output_options[i].str, event_type(type));
2502
2503 rc = -EINVAL;
2504 goto out;
2505 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002506 output[type].user_set = true;
2507 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002508 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002509 }
2510
2511 if (type >= 0) {
2512 if (output[type].fields == 0) {
2513 pr_debug("No fields requested for %s type. "
2514 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002515 }
David Ahern1424dc92011-03-09 22:23:28 -07002516 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002517 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002518
Andi Kleen36ce5652017-06-02 08:48:10 -07002519out_badmix:
2520 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2521 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002522out:
David Ahern745f43e2011-03-09 22:23:26 -07002523 free(str);
2524 return rc;
2525}
2526
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002527#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2528 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2529 if ((lang_dirent->d_type == DT_DIR || \
2530 (lang_dirent->d_type == DT_UNKNOWN && \
2531 is_directory(scripts_path, lang_dirent))) && \
2532 (strcmp(lang_dirent->d_name, ".")) && \
2533 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002534
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002535#define for_each_script(lang_path, lang_dir, script_dirent) \
2536 while ((script_dirent = readdir(lang_dir)) != NULL) \
2537 if (script_dirent->d_type != DT_DIR && \
2538 (script_dirent->d_type != DT_UNKNOWN || \
2539 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002540
2541
2542#define RECORD_SUFFIX "-record"
2543#define REPORT_SUFFIX "-report"
2544
2545struct script_desc {
2546 struct list_head node;
2547 char *name;
2548 char *half_liner;
2549 char *args;
2550};
2551
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002552static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002553
2554static struct script_desc *script_desc__new(const char *name)
2555{
2556 struct script_desc *s = zalloc(sizeof(*s));
2557
Tom Zanussib5b87312010-11-10 08:16:51 -06002558 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002559 s->name = strdup(name);
2560
2561 return s;
2562}
2563
2564static void script_desc__delete(struct script_desc *s)
2565{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002566 zfree(&s->name);
2567 zfree(&s->half_liner);
2568 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002569 free(s);
2570}
2571
2572static void script_desc__add(struct script_desc *s)
2573{
2574 list_add_tail(&s->node, &script_descs);
2575}
2576
2577static struct script_desc *script_desc__find(const char *name)
2578{
2579 struct script_desc *s;
2580
2581 list_for_each_entry(s, &script_descs, node)
2582 if (strcasecmp(s->name, name) == 0)
2583 return s;
2584 return NULL;
2585}
2586
2587static struct script_desc *script_desc__findnew(const char *name)
2588{
2589 struct script_desc *s = script_desc__find(name);
2590
2591 if (s)
2592 return s;
2593
2594 s = script_desc__new(name);
2595 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002596 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002597
2598 script_desc__add(s);
2599
2600 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002601}
2602
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002603static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002604{
2605 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002606 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002607
2608 if (strlen(str) > suffix_len) {
2609 p = str + strlen(str) - suffix_len;
2610 if (!strncmp(p, suffix, suffix_len))
2611 return p;
2612 }
2613
2614 return NULL;
2615}
2616
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002617static int read_script_info(struct script_desc *desc, const char *filename)
2618{
2619 char line[BUFSIZ], *p;
2620 FILE *fp;
2621
2622 fp = fopen(filename, "r");
2623 if (!fp)
2624 return -1;
2625
2626 while (fgets(line, sizeof(line), fp)) {
2627 p = ltrim(line);
2628 if (strlen(p) == 0)
2629 continue;
2630 if (*p != '#')
2631 continue;
2632 p++;
2633 if (strlen(p) && *p == '!')
2634 continue;
2635
2636 p = ltrim(p);
2637 if (strlen(p) && p[strlen(p) - 1] == '\n')
2638 p[strlen(p) - 1] = '\0';
2639
2640 if (!strncmp(p, "description:", strlen("description:"))) {
2641 p += strlen("description:");
2642 desc->half_liner = strdup(ltrim(p));
2643 continue;
2644 }
2645
2646 if (!strncmp(p, "args:", strlen("args:"))) {
2647 p += strlen("args:");
2648 desc->args = strdup(ltrim(p));
2649 continue;
2650 }
2651 }
2652
2653 fclose(fp);
2654
2655 return 0;
2656}
2657
Robert Richter38efb532011-11-25 11:38:40 +01002658static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2659{
2660 char *script_root, *str;
2661
2662 script_root = strdup(script_dirent->d_name);
2663 if (!script_root)
2664 return NULL;
2665
2666 str = (char *)ends_with(script_root, suffix);
2667 if (!str) {
2668 free(script_root);
2669 return NULL;
2670 }
2671
2672 *str = '\0';
2673 return script_root;
2674}
2675
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002676static int list_available_scripts(const struct option *opt __maybe_unused,
2677 const char *s __maybe_unused,
2678 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002679{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002680 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002681 char scripts_path[MAXPATHLEN];
2682 DIR *scripts_dir, *lang_dir;
2683 char script_path[MAXPATHLEN];
2684 char lang_path[MAXPATHLEN];
2685 struct script_desc *desc;
2686 char first_half[BUFSIZ];
2687 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002688
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002689 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002690
2691 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002692 if (!scripts_dir) {
2693 fprintf(stdout,
2694 "open(%s) failed.\n"
2695 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2696 scripts_path);
2697 exit(-1);
2698 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002699
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002700 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002701 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2702 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002703 lang_dir = opendir(lang_path);
2704 if (!lang_dir)
2705 continue;
2706
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002707 for_each_script(lang_path, lang_dir, script_dirent) {
2708 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002709 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002710 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002711 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2712 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002713 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002714 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002715 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002716 }
2717 }
2718
2719 fprintf(stdout, "List of available trace scripts:\n");
2720 list_for_each_entry(desc, &script_descs, node) {
2721 sprintf(first_half, "%s %s", desc->name,
2722 desc->args ? desc->args : "");
2723 fprintf(stdout, " %-36s %s\n", first_half,
2724 desc->half_liner ? desc->half_liner : "");
2725 }
2726
2727 exit(0);
2728}
2729
Feng Tange5f37052012-09-07 16:42:26 +08002730/*
Feng Tang49e639e2012-10-30 11:56:03 +08002731 * Some scripts specify the required events in their "xxx-record" file,
2732 * this function will check if the events in perf.data match those
2733 * mentioned in the "xxx-record".
2734 *
2735 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2736 * which is covered well now. And new parsing code should be added to
2737 * cover the future complexing formats like event groups etc.
2738 */
2739static int check_ev_match(char *dir_name, char *scriptname,
2740 struct perf_session *session)
2741{
2742 char filename[MAXPATHLEN], evname[128];
2743 char line[BUFSIZ], *p;
2744 struct perf_evsel *pos;
2745 int match, len;
2746 FILE *fp;
2747
Jiri Olsa77f18152018-03-19 09:29:01 +01002748 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08002749
2750 fp = fopen(filename, "r");
2751 if (!fp)
2752 return -1;
2753
2754 while (fgets(line, sizeof(line), fp)) {
2755 p = ltrim(line);
2756 if (*p == '#')
2757 continue;
2758
2759 while (strlen(p)) {
2760 p = strstr(p, "-e");
2761 if (!p)
2762 break;
2763
2764 p += 2;
2765 p = ltrim(p);
2766 len = strcspn(p, " \t");
2767 if (!len)
2768 break;
2769
2770 snprintf(evname, len + 1, "%s", p);
2771
2772 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002773 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002774 if (!strcmp(perf_evsel__name(pos), evname)) {
2775 match = 1;
2776 break;
2777 }
2778 }
2779
2780 if (!match) {
2781 fclose(fp);
2782 return -1;
2783 }
2784 }
2785 }
2786
2787 fclose(fp);
2788 return 0;
2789}
2790
2791/*
Feng Tange5f37052012-09-07 16:42:26 +08002792 * Return -1 if none is found, otherwise the actual scripts number.
2793 *
2794 * Currently the only user of this function is the script browser, which
2795 * will list all statically runnable scripts, select one, execute it and
2796 * show the output in a perf browser.
2797 */
2798int find_scripts(char **scripts_array, char **scripts_path_array)
2799{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002800 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002801 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002802 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002803 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002804 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002805 .file = {
2806 .path = input_name,
2807 },
2808 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002809 };
Feng Tange5f37052012-09-07 16:42:26 +08002810 char *temp;
2811 int i = 0;
2812
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002813 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002814 if (!session)
2815 return -1;
2816
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002817 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002818
2819 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002820 if (!scripts_dir) {
2821 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002822 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002823 }
Feng Tange5f37052012-09-07 16:42:26 +08002824
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002825 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002826 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
2827 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08002828#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08002829 if (strstr(lang_path, "perl"))
2830 continue;
2831#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08002832#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08002833 if (strstr(lang_path, "python"))
2834 continue;
2835#endif
2836
2837 lang_dir = opendir(lang_path);
2838 if (!lang_dir)
2839 continue;
2840
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002841 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002842 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002843 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002844 continue;
2845 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002846 script_dirent->d_name);
2847 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002848 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002849 (temp - script_dirent->d_name) + 1,
2850 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002851
2852 if (check_ev_match(lang_path,
2853 scripts_array[i], session))
2854 continue;
2855
Feng Tange5f37052012-09-07 16:42:26 +08002856 i++;
2857 }
Feng Tang49e639e2012-10-30 11:56:03 +08002858 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002859 }
2860
Feng Tang49e639e2012-10-30 11:56:03 +08002861 closedir(scripts_dir);
2862 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002863 return i;
2864}
2865
Tom Zanussi38752942009-12-15 02:53:39 -06002866static char *get_script_path(const char *script_root, const char *suffix)
2867{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002868 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002869 char scripts_path[MAXPATHLEN];
2870 char script_path[MAXPATHLEN];
2871 DIR *scripts_dir, *lang_dir;
2872 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002873 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002874
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002875 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002876
2877 scripts_dir = opendir(scripts_path);
2878 if (!scripts_dir)
2879 return NULL;
2880
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002881 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002882 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2883 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002884 lang_dir = opendir(lang_path);
2885 if (!lang_dir)
2886 continue;
2887
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002888 for_each_script(lang_path, lang_dir, script_dirent) {
2889 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002890 if (__script_root && !strcmp(script_root, __script_root)) {
2891 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002892 closedir(lang_dir);
2893 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01002894 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2895 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002896 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002897 }
2898 free(__script_root);
2899 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002900 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002901 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002902 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002903
Robert Richter38efb532011-11-25 11:38:40 +01002904 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002905}
2906
Tom Zanussib5b87312010-11-10 08:16:51 -06002907static bool is_top_script(const char *script_path)
2908{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002909 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002910}
2911
2912static int has_required_arg(char *script_path)
2913{
2914 struct script_desc *desc;
2915 int n_args = 0;
2916 char *p;
2917
2918 desc = script_desc__new(NULL);
2919
2920 if (read_script_info(desc, script_path))
2921 goto out;
2922
2923 if (!desc->args)
2924 goto out;
2925
2926 for (p = desc->args; *p; p++)
2927 if (*p == '<')
2928 n_args++;
2929out:
2930 script_desc__delete(desc);
2931
2932 return n_args;
2933}
2934
David Ahernd54b1a92012-08-26 12:24:46 -06002935static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002936{
2937 char **__argv = malloc(sizeof(const char *) * argc);
2938
David Ahernd54b1a92012-08-26 12:24:46 -06002939 if (!__argv) {
2940 pr_err("malloc failed\n");
2941 return -1;
2942 }
2943
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002944 memcpy(__argv, argv, sizeof(const char *) * argc);
2945 argc = parse_options(argc, (const char **)__argv, record_options,
2946 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2947 free(__argv);
2948
David Ahernd54b1a92012-08-26 12:24:46 -06002949 system_wide = (argc == 0);
2950
2951 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002952}
2953
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002954static void script__setup_sample_type(struct perf_script *script)
2955{
2956 struct perf_session *session = script->session;
2957 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2958
2959 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2960 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03002961 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002962 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03002963 dwarf_callchain_users = true;
2964 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002965 callchain_param.record_mode = CALLCHAIN_LBR;
2966 else
2967 callchain_param.record_mode = CALLCHAIN_FP;
2968 }
2969}
2970
Jiri Olsa89f16882018-09-13 14:54:03 +02002971static int process_stat_round_event(struct perf_session *session,
2972 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01002973{
2974 struct stat_round_event *round = &event->stat_round;
2975 struct perf_evsel *counter;
2976
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002977 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002978 perf_stat_process_counter(&stat_config, counter);
2979 process_stat(counter, round->time);
2980 }
2981
2982 process_stat_interval(round->time);
2983 return 0;
2984}
2985
Jiri Olsa89f16882018-09-13 14:54:03 +02002986static int process_stat_config_event(struct perf_session *session __maybe_unused,
2987 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002988{
2989 perf_event__read_stat_config(&stat_config, &event->stat_config);
2990 return 0;
2991}
2992
Jiri Olsacfc88742016-01-05 22:09:06 +01002993static int set_maps(struct perf_script *script)
2994{
2995 struct perf_evlist *evlist = script->session->evlist;
2996
2997 if (!script->cpus || !script->threads)
2998 return 0;
2999
3000 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3001 return -EINVAL;
3002
3003 perf_evlist__set_maps(evlist, script->cpus, script->threads);
3004
3005 if (perf_evlist__alloc_stats(evlist, true))
3006 return -ENOMEM;
3007
3008 script->allocated = true;
3009 return 0;
3010}
3011
3012static
Jiri Olsa89f16882018-09-13 14:54:03 +02003013int process_thread_map_event(struct perf_session *session,
3014 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003015{
Jiri Olsa89f16882018-09-13 14:54:03 +02003016 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003017 struct perf_script *script = container_of(tool, struct perf_script, tool);
3018
3019 if (script->threads) {
3020 pr_warning("Extra thread map event, ignoring.\n");
3021 return 0;
3022 }
3023
3024 script->threads = thread_map__new_event(&event->thread_map);
3025 if (!script->threads)
3026 return -ENOMEM;
3027
3028 return set_maps(script);
3029}
3030
3031static
Jiri Olsa89f16882018-09-13 14:54:03 +02003032int process_cpu_map_event(struct perf_session *session,
3033 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003034{
Jiri Olsa89f16882018-09-13 14:54:03 +02003035 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003036 struct perf_script *script = container_of(tool, struct perf_script, tool);
3037
3038 if (script->cpus) {
3039 pr_warning("Extra cpu map event, ignoring.\n");
3040 return 0;
3041 }
3042
3043 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3044 if (!script->cpus)
3045 return -ENOMEM;
3046
3047 return set_maps(script);
3048}
3049
Jiri Olsa89f16882018-09-13 14:54:03 +02003050static int process_feature_event(struct perf_session *session,
3051 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303052{
3053 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003054 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303055 return 0;
3056}
3057
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003058#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003059static int perf_script__process_auxtrace_info(struct perf_session *session,
3060 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003061{
Jiri Olsa89f16882018-09-13 14:54:03 +02003062 struct perf_tool *tool = session->tool;
3063
3064 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003065
3066 if (ret == 0) {
3067 struct perf_script *script = container_of(tool, struct perf_script, tool);
3068
3069 ret = perf_script__setup_per_event_dump(script);
3070 }
3071
3072 return ret;
3073}
3074#else
3075#define perf_script__process_auxtrace_info 0
3076#endif
3077
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003078int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003079{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003080 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003081 bool header = false;
3082 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003083 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003084 char *rec_script_path = NULL;
3085 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003086 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003087 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06003088 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003089 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003090 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003091 struct perf_script script = {
3092 .tool = {
3093 .sample = process_sample_event,
3094 .mmap = perf_event__process_mmap,
3095 .mmap2 = perf_event__process_mmap2,
3096 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303097 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003098 .exit = perf_event__process_exit,
3099 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003100 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003101 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003102 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303103 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003104 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003105 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003106 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003107 .auxtrace = perf_event__process_auxtrace,
3108 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003109 .stat = perf_event__process_stat_event,
3110 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003111 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003112 .thread_map = process_thread_map_event,
3113 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003114 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003115 .ordering_requires_timestamps = true,
3116 },
3117 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003118 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003119 .mode = PERF_DATA_MODE_READ,
3120 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003121 const struct option options[] = {
3122 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3123 "dump raw trace in ASCII"),
3124 OPT_INCR('v', "verbose", &verbose,
3125 "be more verbose (show symbol address, etc)"),
3126 OPT_BOOLEAN('L', "Latency", &latency_format,
3127 "show latency attributes (irqs/preemption disabled, etc)"),
3128 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3129 list_available_scripts),
3130 OPT_CALLBACK('s', "script", NULL, "name",
3131 "script file name (lang:script name, script name, or *)",
3132 parse_scriptname),
3133 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3134 "generate perf-script.xx script in specified language"),
3135 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3136 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3137 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003138 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3139 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003140 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3141 "file", "vmlinux pathname"),
3142 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3143 "file", "kallsyms pathname"),
3144 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3145 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003146 OPT_CALLBACK(0, "symfs", NULL, "directory",
3147 "Look for files with symbols relative to this directory",
3148 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003149 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003150 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003151 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003152 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003153 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303154 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3155 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
3156 "callindent,insn,insnlen,synth,phys_addr,metric,misc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003157 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003158 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3159 "system-wide collection from all CPUs"),
3160 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3161 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07003162 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3163 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003164 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3165 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3166 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003167 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3168 "only consider symbols in these pids"),
3169 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3170 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003171 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3172 "Set the maximum stack depth when parsing the callchain, "
3173 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003174 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003175 OPT_BOOLEAN('I', "show-info", &show_full_info,
3176 "display extended information from perf.data file"),
3177 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3178 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003179 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3180 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003181 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3182 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003183 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3184 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303185 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3186 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003187 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3188 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003189 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3190 "Show round events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003191 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3192 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003193 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003194 OPT_INTEGER(0, "max-blocks", &max_blocks,
3195 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03003196 OPT_BOOLEAN(0, "ns", &nanosecs,
3197 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003198 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003199 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003200 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003201 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3202 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003203 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3204 "Enable symbol demangling"),
3205 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3206 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003207 OPT_STRING(0, "time", &script.time_str, "str",
3208 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003209 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3210 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003211 OPT_END()
3212 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003213 const char * const script_subcommands[] = { "record", "report", NULL };
3214 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003215 "perf script [<options>]",
3216 "perf script [<options>] record <script> [<record-options>] <command>",
3217 "perf script [<options>] report <script> [script-args]",
3218 "perf script [<options>] <script> [<record-options>] <command>",
3219 "perf script [<options>] <top-script> [script-args]",
3220 NULL
3221 };
Tom Zanussi38752942009-12-15 02:53:39 -06003222
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003223 perf_set_singlethreaded();
3224
Tom Zanussib5b87312010-11-10 08:16:51 -06003225 setup_scripting();
3226
Yunlong Song40cae2b2015-03-18 21:35:54 +08003227 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003228 PARSE_OPT_STOP_AT_NON_OPTION);
3229
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003230 data.file.path = input_name;
3231 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003232
Tom Zanussib5b87312010-11-10 08:16:51 -06003233 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3234 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3235 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003236 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003237 }
3238
Tom Zanussib5b87312010-11-10 08:16:51 -06003239 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3240 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3241 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003242 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003243 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003244 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003245 return -1;
3246 }
Tom Zanussi38752942009-12-15 02:53:39 -06003247 }
3248
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003249 if (itrace_synth_opts.callchain &&
3250 itrace_synth_opts.callchain_sz > scripting_max_stack)
3251 scripting_max_stack = itrace_synth_opts.callchain_sz;
3252
Ben Hutchings44e668c2010-10-10 16:10:03 +01003253 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003254 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003255
Tom Zanussib5b87312010-11-10 08:16:51 -06003256 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003257 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003258 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003259 pid_t pid;
3260
Tom Zanussib5b87312010-11-10 08:16:51 -06003261 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3262 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3263
3264 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003265 usage_with_options_msg(script_usage, options,
3266 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003267 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003268 }
3269
Tom Zanussib5b87312010-11-10 08:16:51 -06003270 if (is_top_script(argv[0])) {
3271 rep_args = argc - 1;
3272 } else {
3273 int rec_args;
3274
3275 rep_args = has_required_arg(rep_script_path);
3276 rec_args = (argc - 1) - rep_args;
3277 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003278 usage_with_options_msg(script_usage, options,
3279 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003280 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003281 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003282 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003283 }
3284
3285 if (pipe(live_pipe) < 0) {
3286 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003287 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003288 }
3289
3290 pid = fork();
3291 if (pid < 0) {
3292 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003293 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003294 }
3295
3296 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003297 j = 0;
3298
Tom Zanussia0cccc22010-04-01 23:59:25 -05003299 dup2(live_pipe[1], 1);
3300 close(live_pipe[0]);
3301
Robert Richter317df652011-11-25 15:05:25 +01003302 if (is_top_script(argv[0])) {
3303 system_wide = true;
3304 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003305 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3306 err = -1;
3307 goto out;
3308 }
Robert Richter317df652011-11-25 15:05:25 +01003309 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003310
3311 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003312 if (!__argv) {
3313 pr_err("malloc failed\n");
3314 err = -ENOMEM;
3315 goto out;
3316 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003317
Tom Zanussib5b87312010-11-10 08:16:51 -06003318 __argv[j++] = "/bin/sh";
3319 __argv[j++] = rec_script_path;
3320 if (system_wide)
3321 __argv[j++] = "-a";
3322 __argv[j++] = "-q";
3323 __argv[j++] = "-o";
3324 __argv[j++] = "-";
3325 for (i = rep_args + 1; i < argc; i++)
3326 __argv[j++] = argv[i];
3327 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003328
3329 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003330 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003331 exit(-1);
3332 }
3333
3334 dup2(live_pipe[0], 0);
3335 close(live_pipe[1]);
3336
Tom Zanussib5b87312010-11-10 08:16:51 -06003337 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003338 if (!__argv) {
3339 pr_err("malloc failed\n");
3340 err = -ENOMEM;
3341 goto out;
3342 }
3343
Tom Zanussib5b87312010-11-10 08:16:51 -06003344 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003345 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003346 __argv[j++] = rep_script_path;
3347 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003348 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003349 __argv[j++] = "-i";
3350 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003351 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003352
3353 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003354 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003355 exit(-1);
3356 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003357
Tom Zanussib5b87312010-11-10 08:16:51 -06003358 if (rec_script_path)
3359 script_path = rec_script_path;
3360 if (rep_script_path)
3361 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003362
Tom Zanussib5b87312010-11-10 08:16:51 -06003363 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003364 j = 0;
3365
Robert Richter317df652011-11-25 15:05:25 +01003366 if (!rec_script_path)
3367 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003368 else if (!system_wide) {
3369 if (have_cmd(argc - 1, &argv[1]) != 0) {
3370 err = -1;
3371 goto out;
3372 }
3373 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003374
3375 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003376 if (!__argv) {
3377 pr_err("malloc failed\n");
3378 err = -ENOMEM;
3379 goto out;
3380 }
3381
Tom Zanussib5b87312010-11-10 08:16:51 -06003382 __argv[j++] = "/bin/sh";
3383 __argv[j++] = script_path;
3384 if (system_wide)
3385 __argv[j++] = "-a";
3386 for (i = 2; i < argc; i++)
3387 __argv[j++] = argv[i];
3388 __argv[j++] = NULL;
3389
3390 execvp("/bin/sh", (char **)__argv);
3391 free(__argv);
3392 exit(-1);
3393 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003394
Tom Zanussicf4fee52010-03-03 01:04:33 -06003395 if (!script_name)
3396 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003397
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003398 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003399 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003400 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003401
Jiri Olsae90debd2013-12-09 11:02:50 +01003402 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003403 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003404 perf_session__fprintf_info(session, stdout, show_full_info);
3405 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003406 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003407 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003408 if (show_full_info)
3409 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003410
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003411 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003412 goto out_delete;
3413
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003414 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003415 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003416
Adrian Huntere2167082016-06-23 16:40:58 +03003417 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3418 itrace_synth_opts.thread_stack = true;
3419
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003420 session->itrace_synth_opts = &itrace_synth_opts;
3421
Anton Blanchard5d67be92011-07-04 21:57:50 +10003422 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003423 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3424 if (err < 0)
3425 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003426 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003427 }
3428
David Ahern1424dc92011-03-09 22:23:28 -07003429 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003430 symbol_conf.use_callchain = true;
3431 else
3432 symbol_conf.use_callchain = false;
3433
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003434 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003435 tep_set_function_resolver(session->tevent.pevent,
3436 machine__resolve_kernel_addr,
3437 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003438 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003439 err = -1;
3440 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003441 }
3442
Tom Zanussi956ffd02009-11-25 01:15:46 -06003443 if (generate_script_lang) {
3444 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003445 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003446
David Ahern2c9e45f72011-03-17 10:03:21 -06003447 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003448 fprintf(stderr,
3449 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003450 err = -EINVAL;
3451 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003452 }
3453
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003454 input = open(data.file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003455 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003456 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003457 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003458 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003459 }
3460
3461 err = fstat(input, &perf_stat);
3462 if (err < 0) {
3463 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003464 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003465 }
3466
3467 if (!perf_stat.st_size) {
3468 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003469 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003470 }
3471
3472 scripting_ops = script_spec__lookup(generate_script_lang);
3473 if (!scripting_ops) {
3474 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003475 err = -ENOENT;
3476 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003477 }
3478
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003479 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003480 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003481 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003482 }
3483
3484 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003485 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003486 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003487 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003488 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003489 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003490 }
3491
David Ahern9cbdb702011-04-06 21:54:20 -06003492
3493 err = perf_session__check_output_opt(session);
3494 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003495 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003496
Jin Yaocc2ef582018-01-10 23:00:33 +08003497 script.ptime_range = perf_time__range_alloc(script.time_str,
3498 &script.range_size);
3499 if (!script.ptime_range) {
3500 err = -ENOMEM;
3501 goto out_delete;
3502 }
3503
David Aherna91f4c42016-11-29 10:15:43 -07003504 /* needs to be parsed after looking up reference time */
Jin Yao2ab046c2017-12-08 21:13:46 +08003505 if (perf_time__parse_str(script.ptime_range, script.time_str) != 0) {
3506 if (session->evlist->first_sample_time == 0 &&
3507 session->evlist->last_sample_time == 0) {
Jin Yao1e2778e2018-01-10 23:00:27 +08003508 pr_err("HINT: no first/last sample time found in perf data.\n"
3509 "Please use latest perf binary to execute 'perf record'\n"
3510 "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
Jin Yao2ab046c2017-12-08 21:13:46 +08003511 err = -EINVAL;
3512 goto out_delete;
3513 }
3514
3515 script.range_num = perf_time__percent_parse_str(
Jin Yaocc2ef582018-01-10 23:00:33 +08003516 script.ptime_range, script.range_size,
Jin Yao2ab046c2017-12-08 21:13:46 +08003517 script.time_str,
3518 session->evlist->first_sample_time,
3519 session->evlist->last_sample_time);
3520
3521 if (script.range_num < 0) {
3522 pr_err("Invalid time string\n");
3523 err = -EINVAL;
3524 goto out_delete;
3525 }
3526 } else {
3527 script.range_num = 1;
David Aherna91f4c42016-11-29 10:15:43 -07003528 }
3529
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003530 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003531
Adrian Hunterd445dd22014-08-15 22:08:37 +03003532 flush_scripting();
3533
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003534out_delete:
Jin Yaocc2ef582018-01-10 23:00:33 +08003535 zfree(&script.ptime_range);
3536
Jiri Olsacfc88742016-01-05 22:09:06 +01003537 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003538 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003539
3540 if (script_started)
3541 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003542out:
3543 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003544}