blob: 357906ed1898d4c1f92eff43567639010ac1ac68 [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>
Andi Kleenb585ebd2018-09-20 11:05:36 -070047#include <subcmd/pager.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020048
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030049#include "sane_ctype.h"
50
Tom Zanussi956ffd02009-11-25 01:15:46 -060051static char const *script_name;
52static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020053static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020054static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020055static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070056static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090057static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010058static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030059static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030060static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100061static const char *cpu_list;
62static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010063static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080064static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060065
Adrian Hunter44cbe722015-09-25 16:15:50 +030066unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030067
David Ahern745f43e2011-03-09 22:23:26 -070068enum perf_output_field {
69 PERF_OUTPUT_COMM = 1U << 0,
70 PERF_OUTPUT_TID = 1U << 1,
71 PERF_OUTPUT_PID = 1U << 2,
72 PERF_OUTPUT_TIME = 1U << 3,
73 PERF_OUTPUT_CPU = 1U << 4,
74 PERF_OUTPUT_EVNAME = 1U << 5,
75 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060076 PERF_OUTPUT_IP = 1U << 7,
77 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060078 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060079 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090080 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020081 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020082 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020083 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020084 PERF_OUTPUT_BRSTACK = 1U << 15,
85 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010086 PERF_OUTPUT_DATA_SRC = 1U << 17,
87 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000088 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030089 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030090 PERF_OUTPUT_INSN = 1U << 21,
91 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080092 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070093 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030094 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -040095 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -070096 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -080097 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +010098 PERF_OUTPUT_MISC = 1U << 29,
Andi Kleendd2e18e2018-12-03 16:18:48 -080099 PERF_OUTPUT_SRCCODE = 1U << 30,
David Ahern745f43e2011-03-09 22:23:26 -0700100};
101
102struct output_option {
103 const char *str;
104 enum perf_output_field field;
105} all_output_options[] = {
106 {.str = "comm", .field = PERF_OUTPUT_COMM},
107 {.str = "tid", .field = PERF_OUTPUT_TID},
108 {.str = "pid", .field = PERF_OUTPUT_PID},
109 {.str = "time", .field = PERF_OUTPUT_TIME},
110 {.str = "cpu", .field = PERF_OUTPUT_CPU},
111 {.str = "event", .field = PERF_OUTPUT_EVNAME},
112 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600113 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700114 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600115 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600116 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900117 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200118 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200119 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200120 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700121 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200122 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
123 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100124 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
125 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000126 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300127 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300128 {.str = "insn", .field = PERF_OUTPUT_INSN},
129 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800130 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700131 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300132 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400133 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800134 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100135 {.str = "misc", .field = PERF_OUTPUT_MISC},
Andi Kleendd2e18e2018-12-03 16:18:48 -0800136 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
David Ahern745f43e2011-03-09 22:23:26 -0700137};
138
Adrian Hunter14057202017-06-21 13:17:19 +0300139enum {
140 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
141 OUTPUT_TYPE_MAX
142};
143
David Ahern745f43e2011-03-09 22:23:26 -0700144/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600145static struct {
146 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600147 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400148 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600149 u64 fields;
150 u64 invalid_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300151} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700152
David Ahern2c9e45f72011-03-17 10:03:21 -0600153 [PERF_TYPE_HARDWARE] = {
154 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700155
David Ahern2c9e45f72011-03-17 10:03:21 -0600156 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
157 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600158 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530159 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
160 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600161
Wang Nan30372f02016-02-24 11:20:45 +0000162 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600163 },
164
165 [PERF_TYPE_SOFTWARE] = {
166 .user_set = false,
167
168 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
169 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600170 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530171 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
172 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
173 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600174
175 .invalid_fields = PERF_OUTPUT_TRACE,
176 },
177
178 [PERF_TYPE_TRACEPOINT] = {
179 .user_set = false,
180
181 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
182 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000183 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600184 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700185
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530186 [PERF_TYPE_HW_CACHE] = {
187 .user_set = false,
188
189 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
190 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
191 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
192 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
193 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
194
195 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
196 },
197
Arun Sharma0817a6a2011-04-14 10:38:18 -0700198 [PERF_TYPE_RAW] = {
199 .user_set = false,
200
201 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
202 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600203 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530204 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
205 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
206 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
207 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700208
Wang Nan30372f02016-02-24 11:20:45 +0000209 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700210 },
Wang Nan27cfef02015-12-08 02:25:43 +0000211
212 [PERF_TYPE_BREAKPOINT] = {
213 .user_set = false,
214
215 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
216 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
217 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530218 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
219 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000220
Wang Nan30372f02016-02-24 11:20:45 +0000221 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000222 },
Adrian Hunter14057202017-06-21 13:17:19 +0300223
224 [OUTPUT_TYPE_SYNTH] = {
225 .user_set = false,
226
227 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
228 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
229 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530230 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
231 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300232
233 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
234 },
David Ahern1424dc92011-03-09 22:23:28 -0700235};
David Ahern745f43e2011-03-09 22:23:26 -0700236
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300237struct perf_evsel_script {
238 char *filename;
239 FILE *fp;
240 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800241 /* For metric output */
242 u64 val;
243 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300244};
245
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800246static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
247{
248 return (struct perf_evsel_script *)evsel->priv;
249}
250
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300251static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100252 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300253{
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800254 struct perf_evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300255
256 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100257 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300258 goto out_free;
259 es->fp = fopen(es->filename, "w");
260 if (es->fp == NULL)
261 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300262 }
263
264 return es;
265out_free_filename:
266 zfree(&es->filename);
267out_free:
268 free(es);
269 return NULL;
270}
271
272static void perf_evsel_script__delete(struct perf_evsel_script *es)
273{
274 zfree(&es->filename);
275 fclose(es->fp);
276 es->fp = NULL;
277 free(es);
278}
279
280static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
281{
282 struct stat st;
283
284 fstat(fileno(es->fp), &st);
285 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
286 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
287}
288
Adrian Hunter14057202017-06-21 13:17:19 +0300289static inline int output_type(unsigned int type)
290{
291 switch (type) {
292 case PERF_TYPE_SYNTH:
293 return OUTPUT_TYPE_SYNTH;
294 default:
295 return type;
296 }
297}
298
299static inline unsigned int attr_type(unsigned int type)
300{
301 switch (type) {
302 case OUTPUT_TYPE_SYNTH:
303 return PERF_TYPE_SYNTH;
304 default:
305 return type;
306 }
307}
308
David Ahern2c9e45f72011-03-17 10:03:21 -0600309static bool output_set_by_user(void)
310{
311 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300312 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600313 if (output[j].user_set)
314 return true;
315 }
316 return false;
317}
David Ahern745f43e2011-03-09 22:23:26 -0700318
David Ahern9cbdb702011-04-06 21:54:20 -0600319static const char *output_field2str(enum perf_output_field field)
320{
321 int i, imax = ARRAY_SIZE(all_output_options);
322 const char *str = "";
323
324 for (i = 0; i < imax; ++i) {
325 if (all_output_options[i].field == field) {
326 str = all_output_options[i].str;
327 break;
328 }
329 }
330 return str;
331}
332
Adrian Hunter14057202017-06-21 13:17:19 +0300333#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700334
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300335static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
336 u64 sample_type, const char *sample_msg,
337 enum perf_output_field field,
338 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700339{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300340 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300341 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600342 const char *evname;
343
344 if (attr->sample_type & sample_type)
345 return 0;
346
347 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300348 if (allow_user_set)
349 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300350 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600351 pr_err("Samples for '%s' event do not have %s attribute set. "
352 "Cannot print '%s' field.\n",
353 evname, sample_msg, output_field2str(field));
354 return -1;
355 }
356
357 /* user did not ask for it explicitly so remove from the default list */
358 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300359 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600360 pr_debug("Samples for '%s' event do not have %s attribute set. "
361 "Skipping '%s' field.\n",
362 evname, sample_msg, output_field2str(field));
363
364 return 0;
365}
366
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300367static int perf_evsel__check_stype(struct perf_evsel *evsel,
368 u64 sample_type, const char *sample_msg,
369 enum perf_output_field field)
370{
371 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
372 false);
373}
374
David Ahern9cbdb702011-04-06 21:54:20 -0600375static int perf_evsel__check_attr(struct perf_evsel *evsel,
376 struct perf_session *session)
377{
378 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300379 bool allow_user_set;
380
Jiri Olsae099eba2016-01-05 22:09:09 +0100381 if (perf_header__has_feat(&session->header, HEADER_STAT))
382 return 0;
383
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300384 allow_user_set = perf_header__has_feat(&session->header,
385 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600386
David Ahern1424dc92011-03-09 22:23:28 -0700387 if (PRINT_FIELD(TRACE) &&
388 !perf_session__has_traces(session, "record -R"))
389 return -EINVAL;
390
David Ahern787bef12011-05-27 14:28:43 -0600391 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300392 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
393 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700394 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700395 }
David Ahern7cec0922011-05-30 13:08:23 -0600396
397 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300398 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
399 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600400 return -EINVAL;
401
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100402 if (PRINT_FIELD(DATA_SRC) &&
403 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
404 PERF_OUTPUT_DATA_SRC))
405 return -EINVAL;
406
407 if (PRINT_FIELD(WEIGHT) &&
408 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
409 PERF_OUTPUT_WEIGHT))
410 return -EINVAL;
411
Andi Kleen37fed3d2018-09-18 05:32:09 -0700412 if (PRINT_FIELD(SYM) &&
413 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600414 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700415 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600416 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600417 return -EINVAL;
418 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900419 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
420 pr_err("Display of offsets requested but symbol is not"
421 "selected.\n");
422 return -EINVAL;
423 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700424 if (PRINT_FIELD(DSO) &&
425 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
426 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600427 return -EINVAL;
428 }
Andi Kleendd2e18e2018-12-03 16:18:48 -0800429 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
Adrian Huntercc8fae12013-12-06 09:42:57 +0200430 pr_err("Display of source line number requested but sample IP is not\n"
431 "selected. Hence, no address to lookup the source line number.\n");
432 return -EINVAL;
433 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800434 if (PRINT_FIELD(BRSTACKINSN) &&
435 !(perf_evlist__combined_branch_type(session->evlist) &
436 PERF_SAMPLE_BRANCH_ANY)) {
437 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
438 "Hint: run 'perf record -b ...'\n");
439 return -EINVAL;
440 }
David Ahern1424dc92011-03-09 22:23:28 -0700441 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300442 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
443 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700444 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700445
446 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300447 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
448 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700449 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700450
451 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300452 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
453 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700454 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600455
Stephane Eranianfc36f942015-08-31 18:41:10 +0200456 if (PRINT_FIELD(IREGS) &&
457 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
458 PERF_OUTPUT_IREGS))
459 return -EINVAL;
460
Andi Kleenb1491ac2017-09-05 11:40:57 -0700461 if (PRINT_FIELD(UREGS) &&
462 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
463 PERF_OUTPUT_UREGS))
464 return -EINVAL;
465
Kan Liang49d58f02017-08-29 13:11:11 -0400466 if (PRINT_FIELD(PHYS_ADDR) &&
467 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
468 PERF_OUTPUT_PHYS_ADDR))
469 return -EINVAL;
470
David Ahern9cbdb702011-04-06 21:54:20 -0600471 return 0;
472}
473
Adrian Hunter7ea95722013-11-01 15:51:30 +0200474static void set_print_ip_opts(struct perf_event_attr *attr)
475{
Adrian Hunter14057202017-06-21 13:17:19 +0300476 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200477
478 output[type].print_ip_opts = 0;
479 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300480 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200481
482 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300483 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200484
485 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300486 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200487
488 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300489 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200490
491 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300492 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200493}
494
David Ahern9cbdb702011-04-06 21:54:20 -0600495/*
496 * verify all user requested events exist and the samples
497 * have the expected data
498 */
499static int perf_session__check_output_opt(struct perf_session *session)
500{
He Kuang40f20e52016-05-16 04:51:19 +0000501 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600502 struct perf_evsel *evsel;
503
Adrian Hunter14057202017-06-21 13:17:19 +0300504 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
505 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600506
507 /*
508 * even if fields is set to 0 (ie., show nothing) event must
509 * exist if user explicitly includes it on the command line
510 */
Adrian Hunter14057202017-06-21 13:17:19 +0300511 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
512 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600513 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300514 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600515 event_type(j));
516 return -1;
517 }
518
519 if (evsel && output[j].fields &&
520 perf_evsel__check_attr(evsel, session))
521 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400522
523 if (evsel == NULL)
524 continue;
525
Adrian Hunter7ea95722013-11-01 15:51:30 +0200526 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700527 }
528
Adrian Hunter98526ee2014-07-31 09:00:59 +0300529 if (!no_callchain) {
530 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000531 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300532
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300533 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000534 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300535 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300536 use_callchain = true;
537 break;
538 }
539 }
He Kuang71ac8992016-08-04 11:25:43 +0000540 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300541 symbol_conf.use_callchain = false;
542 }
543
David Ahern80b8b492013-11-19 21:07:37 -0700544 /*
545 * set default for tracepoints to print symbols only
546 * if callchains are present
547 */
548 if (symbol_conf.use_callchain &&
549 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700550 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700551
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300552 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000553 if (evsel->attr.type != j)
554 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700555
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300556 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000557 output[j].fields |= PERF_OUTPUT_IP;
558 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530559 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000560 output[j].fields |= PERF_OUTPUT_DSO;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300561 set_print_ip_opts(&evsel->attr);
He Kuang40f20e52016-05-16 04:51:19 +0000562 goto out;
563 }
David Ahern80b8b492013-11-19 21:07:37 -0700564 }
565 }
566
567out:
David Ahern1424dc92011-03-09 22:23:28 -0700568 return 0;
569}
David Ahern745f43e2011-03-09 22:23:26 -0700570
Milian Wolff9add8fe2018-11-07 23:34:37 +0100571static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
572 FILE *fp
573)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200574{
Andi Kleenb1491ac2017-09-05 11:40:57 -0700575 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300576 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700577
578 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300579 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700580
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300581 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700582
583 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
584 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300585 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700586 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300587
Milian Wolffb07d16f2018-11-07 10:37:05 +0100588 fprintf(fp, "\n");
589
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300590 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700591}
592
Milian Wolff9add8fe2018-11-07 23:34:37 +0100593static int perf_sample__fprintf_iregs(struct perf_sample *sample,
594 struct perf_event_attr *attr, FILE *fp)
595{
596 return perf_sample__fprintf_regs(&sample->intr_regs,
597 attr->sample_regs_intr, fp);
598}
599
600static int perf_sample__fprintf_uregs(struct perf_sample *sample,
601 struct perf_event_attr *attr, FILE *fp)
602{
603 return perf_sample__fprintf_regs(&sample->user_regs,
604 attr->sample_regs_user, fp);
605}
606
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300607static int perf_sample__fprintf_start(struct perf_sample *sample,
608 struct thread *thread,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100609 struct perf_evsel *evsel,
610 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700611{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300612 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700613 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700614 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300615 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700616
David Ahern745f43e2011-03-09 22:23:26 -0700617 if (PRINT_FIELD(COMM)) {
618 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300619 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300620 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300621 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700622 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300623 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700624 }
David Ahernc70c94b2011-03-09 22:23:25 -0700625
David Ahern745f43e2011-03-09 22:23:26 -0700626 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300627 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700628 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300629 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700630 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300631 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700632
David Ahern745f43e2011-03-09 22:23:26 -0700633 if (PRINT_FIELD(CPU)) {
634 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300635 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700636 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300637 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700638 }
David Ahernc70c94b2011-03-09 22:23:25 -0700639
Jiri Olsa28a0b392018-01-07 17:03:52 +0100640 if (PRINT_FIELD(MISC)) {
641 int ret = 0;
642
643 #define has(m) \
644 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
645
646 if (has(KERNEL))
647 ret += fprintf(fp, "K");
648 if (has(USER))
649 ret += fprintf(fp, "U");
650 if (has(HYPERVISOR))
651 ret += fprintf(fp, "H");
652 if (has(GUEST_KERNEL))
653 ret += fprintf(fp, "G");
654 if (has(GUEST_USER))
655 ret += fprintf(fp, "g");
656
657 switch (type) {
658 case PERF_RECORD_MMAP:
659 case PERF_RECORD_MMAP2:
660 if (has(MMAP_DATA))
661 ret += fprintf(fp, "M");
662 break;
663 case PERF_RECORD_COMM:
664 if (has(COMM_EXEC))
665 ret += fprintf(fp, "E");
666 break;
667 case PERF_RECORD_SWITCH:
668 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300669 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100670 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300671 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
672 ret += fprintf(fp, "p");
673 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100674 default:
675 break;
676 }
677
678 #undef has
679
680 ret += fprintf(fp, "%*s", 6 - ret, " ");
681 printed += ret;
682 }
683
David Ahern745f43e2011-03-09 22:23:26 -0700684 if (PRINT_FIELD(TIME)) {
685 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300686 secs = nsecs / NSEC_PER_SEC;
687 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900688
Adrian Hunter83e19862015-09-25 16:15:36 +0300689 if (nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300690 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900691 else {
692 char sample_time[32];
693 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300694 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900695 }
David Ahern745f43e2011-03-09 22:23:26 -0700696 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300697
698 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700699}
700
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200701static inline char
702mispred_str(struct branch_entry *br)
703{
704 if (!(br->flags.mispred || br->flags.predicted))
705 return '-';
706
707 return br->flags.predicted ? 'P' : 'M';
708}
709
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300710static int perf_sample__fprintf_brstack(struct perf_sample *sample,
711 struct thread *thread,
712 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200713{
714 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700715 struct addr_location alf, alt;
716 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300717 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200718
719 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300720 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200721
722 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700723 from = br->entries[i].from;
724 to = br->entries[i].to;
725
726 if (PRINT_FIELD(DSO)) {
727 memset(&alf, 0, sizeof(alf));
728 memset(&alt, 0, sizeof(alt));
Adrian Hunter692d0e62018-11-06 23:07:12 +0200729 thread__find_map_fb(thread, sample->cpumode, from, &alf);
730 thread__find_map_fb(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700731 }
732
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300733 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700734 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300735 printed += fprintf(fp, "(");
736 printed += map__fprintf_dsoname(alf.map, fp);
737 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700738 }
739
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300740 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700741 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300742 printed += fprintf(fp, "(");
743 printed += map__fprintf_dsoname(alt.map, fp);
744 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700745 }
746
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300747 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200748 mispred_str( br->entries + i),
749 br->entries[i].flags.in_tx? 'X' : '-',
750 br->entries[i].flags.abort? 'A' : '-',
751 br->entries[i].flags.cycles);
752 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300753
754 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200755}
756
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300757static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
758 struct thread *thread,
759 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200760{
761 struct branch_stack *br = sample->branch_stack;
762 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200763 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300764 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200765
766 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300767 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200768
769 for (i = 0; i < br->nr; i++) {
770
771 memset(&alf, 0, sizeof(alf));
772 memset(&alt, 0, sizeof(alt));
773 from = br->entries[i].from;
774 to = br->entries[i].to;
775
Adrian Hunter692d0e62018-11-06 23:07:12 +0200776 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
777 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200778
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300779 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700780 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300781 printed += fprintf(fp, "(");
782 printed += map__fprintf_dsoname(alf.map, fp);
783 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700784 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300785 printed += fprintf(fp, "%c", '/');
786 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700787 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300788 printed += fprintf(fp, "(");
789 printed += map__fprintf_dsoname(alt.map, fp);
790 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700791 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300792 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200793 mispred_str( br->entries + i),
794 br->entries[i].flags.in_tx? 'X' : '-',
795 br->entries[i].flags.abort? 'A' : '-',
796 br->entries[i].flags.cycles);
797 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300798
799 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200800}
801
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300802static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
803 struct thread *thread,
804 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700805{
806 struct branch_stack *br = sample->branch_stack;
807 struct addr_location alf, alt;
808 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300809 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700810
811 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300812 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700813
814 for (i = 0; i < br->nr; i++) {
815
816 memset(&alf, 0, sizeof(alf));
817 memset(&alt, 0, sizeof(alt));
818 from = br->entries[i].from;
819 to = br->entries[i].to;
820
Adrian Hunter692d0e62018-11-06 23:07:12 +0200821 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300822 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700823 from = map__map_ip(alf.map, from);
824
Adrian Hunter692d0e62018-11-06 23:07:12 +0200825 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300826 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700827 to = map__map_ip(alt.map, to);
828
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300829 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700830 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300831 printed += fprintf(fp, "(");
832 printed += map__fprintf_dsoname(alf.map, fp);
833 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700834 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300835 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700836 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300837 printed += fprintf(fp, "(");
838 printed += map__fprintf_dsoname(alt.map, fp);
839 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700840 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300841 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700842 mispred_str(br->entries + i),
843 br->entries[i].flags.in_tx ? 'X' : '-',
844 br->entries[i].flags.abort ? 'A' : '-',
845 br->entries[i].flags.cycles);
846 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300847
848 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700849}
Andi Kleen48d02a12017-02-23 15:46:34 -0800850#define MAXBB 16384UL
851
852static int grab_bb(u8 *buffer, u64 start, u64 end,
853 struct machine *machine, struct thread *thread,
854 bool *is64bit, u8 *cpumode, bool last)
855{
856 long offset, len;
857 struct addr_location al;
858 bool kernel;
859
860 if (!start || !end)
861 return 0;
862
863 kernel = machine__kernel_ip(machine, start);
864 if (kernel)
865 *cpumode = PERF_RECORD_MISC_KERNEL;
866 else
867 *cpumode = PERF_RECORD_MISC_USER;
868
869 /*
870 * Block overlaps between kernel and user.
871 * This can happen due to ring filtering
872 * On Intel CPUs the entry into the kernel is filtered,
873 * but the exit is not. Let the caller patch it up.
874 */
875 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300876 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800877 return -ENXIO;
878 }
879
880 memset(&al, 0, sizeof(al));
881 if (end - start > MAXBB - MAXINSN) {
882 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300883 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800884 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300885 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800886 return 0;
887 }
888
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300889 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300890 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800891 return 0;
892 }
893 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300894 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800895 return 0;
896 }
897
898 /* Load maps to ensure dso->is_64_bit has been updated */
899 map__load(al.map);
900
901 offset = al.map->map_ip(al.map, start);
902 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
903 end - start + MAXINSN);
904
905 *is64bit = al.map->dso->is_64_bit;
906 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300907 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800908 start, end);
909 return len;
910}
911
Andi Kleendd2e18e2018-12-03 16:18:48 -0800912static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
913{
914 struct addr_location al;
915 int ret = 0;
916
917 memset(&al, 0, sizeof(al));
918 thread__find_map(thread, cpumode, addr, &al);
919 if (!al.map)
920 return 0;
921 ret = map__fprintf_srccode(al.map, al.addr, stdout,
922 &thread->srccode_state);
923 if (ret)
924 ret += printf("\n");
925 return ret;
926}
927
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300928static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
929 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -0700930 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -0800931{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300932 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
933 dump_insn(x, ip, inbuf, len, NULL),
934 en->flags.predicted ? " PRED" : "",
935 en->flags.mispred ? " MISPRED" : "",
936 en->flags.in_tx ? " INTX" : "",
937 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800938 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -0700939 *total_cycles += en->flags.cycles;
940 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800941 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300942 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800943 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300944 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800945}
946
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300947static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
948 u8 cpumode, int cpu, struct symbol **lastsym,
949 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800950{
951 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300952 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800953
954 memset(&al, 0, sizeof(al));
955
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300956 thread__find_map(thread, cpumode, addr, &al);
957
Andi Kleen48d02a12017-02-23 15:46:34 -0800958 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300959 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800960
961 al.cpu = cpu;
962 al.sym = NULL;
963 if (al.map)
964 al.sym = map__find_symbol(al.map, al.addr);
965
966 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300967 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800968
969 if (al.addr < al.sym->end)
970 off = al.addr - al.sym->start;
971 else
972 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300973 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800974 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300975 printed += fprintf(fp, "%+d", off);
976 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800977 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300978 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
979 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800980 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300981
982 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800983}
984
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300985static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
986 struct thread *thread,
987 struct perf_event_attr *attr,
988 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800989{
990 struct branch_stack *br = sample->branch_stack;
991 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300992 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800993 struct perf_insn x;
994 u8 buffer[MAXBB];
995 unsigned off;
996 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -0700997 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800998
999 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001000 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001001 nr = br->nr;
1002 if (max_blocks && nr > max_blocks + 1)
1003 nr = max_blocks + 1;
1004
1005 x.thread = thread;
1006 x.cpu = sample->cpu;
1007
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001008 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -08001009
1010 /* Handle first from jump, of which we don't know the entry. */
1011 len = grab_bb(buffer, br->entries[nr-1].from,
1012 br->entries[nr-1].from,
1013 machine, thread, &x.is64bit, &x.cpumode, false);
1014 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001015 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1016 x.cpumode, x.cpu, &lastsym, attr, fp);
1017 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001018 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001019 if (PRINT_FIELD(SRCCODE))
1020 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
Andi Kleen48d02a12017-02-23 15:46:34 -08001021 }
1022
1023 /* Print all blocks */
1024 for (i = nr - 2; i >= 0; i--) {
1025 if (br->entries[i].from || br->entries[i].to)
1026 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1027 br->entries[i].from,
1028 br->entries[i].to);
1029 start = br->entries[i + 1].to;
1030 end = br->entries[i].from;
1031
1032 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1033 /* Patch up missing kernel transfers due to ring filters */
1034 if (len == -ENXIO && i > 0) {
1035 end = br->entries[--i].from;
1036 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1037 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1038 }
1039 if (len <= 0)
1040 continue;
1041
1042 insn = 0;
1043 for (off = 0;; off += ilen) {
1044 uint64_t ip = start + off;
1045
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001046 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001047 if (ip == end) {
Andi Kleenfe571202018-09-24 10:07:32 -07001048 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp,
1049 &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001050 if (PRINT_FIELD(SRCCODE))
1051 printed += print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001052 break;
1053 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001054 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1055 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001056 if (ilen == 0)
1057 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001058 if (PRINT_FIELD(SRCCODE))
1059 print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001060 insn++;
1061 }
1062 }
1063 }
1064
1065 /*
1066 * Hit the branch? In this case we are already done, and the target
1067 * has not been executed yet.
1068 */
1069 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001070 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001071 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001072 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001073
1074 /*
1075 * Print final block upto sample
Andi Kleen61f61152018-11-19 21:06:17 -08001076 *
1077 * Due to pipeline delays the LBRs might be missing a branch
1078 * or two, which can result in very large or negative blocks
1079 * between final branch and sample. When this happens just
1080 * continue walking after the last TO until we hit a branch.
Andi Kleen48d02a12017-02-23 15:46:34 -08001081 */
1082 start = br->entries[0].to;
1083 end = sample->ip;
Andi Kleen61f61152018-11-19 21:06:17 -08001084 if (end < start) {
1085 /* Missing jump. Scan 128 bytes for the next branch */
1086 end = start + 128;
1087 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001088 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001089 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001090 if (len <= 0) {
1091 /* Print at least last IP if basic block did not work */
1092 len = grab_bb(buffer, sample->ip, sample->ip,
1093 machine, thread, &x.is64bit, &x.cpumode, false);
1094 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001095 goto out;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001096 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001097 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001098 if (PRINT_FIELD(SRCCODE))
1099 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001100 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001101 }
1102 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001103 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1104 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001105 if (ilen == 0)
1106 break;
Andi Kleen61f61152018-11-19 21:06:17 -08001107 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1108 /*
1109 * Hit a missing branch. Just stop.
1110 */
1111 printed += fprintf(fp, "\t... not reaching sample ...\n");
1112 break;
1113 }
Andi Kleendd2e18e2018-12-03 16:18:48 -08001114 if (PRINT_FIELD(SRCCODE))
1115 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001116 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001117out:
1118 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001119}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001120
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001121static int perf_sample__fprintf_addr(struct perf_sample *sample,
1122 struct thread *thread,
1123 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001124{
1125 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001126 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001127
1128 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001129 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001130
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001131 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001132
1133 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001134 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001135 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001136 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001137 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001138 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001139 }
1140
1141 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001142 printed += fprintf(fp, " (");
1143 printed += map__fprintf_dsoname(al.map, fp);
1144 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001145 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001146out:
1147 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001148}
1149
Andi Kleen99f753f2018-09-20 11:05:39 -07001150static const char *resolve_branch_sym(struct perf_sample *sample,
1151 struct perf_evsel *evsel,
1152 struct thread *thread,
1153 struct addr_location *al,
1154 u64 *ip)
1155{
1156 struct addr_location addr_al;
1157 struct perf_event_attr *attr = &evsel->attr;
1158 const char *name = NULL;
1159
1160 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1161 if (sample_addr_correlates_sym(attr)) {
1162 thread__resolve(thread, &addr_al, sample);
1163 if (addr_al.sym)
1164 name = addr_al.sym->name;
1165 else
1166 *ip = sample->addr;
1167 } else {
1168 *ip = sample->addr;
1169 }
1170 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1171 if (al->sym)
1172 name = al->sym->name;
1173 else
1174 *ip = sample->ip;
1175 }
1176 return name;
1177}
1178
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001179static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1180 struct perf_evsel *evsel,
1181 struct thread *thread,
1182 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001183{
1184 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter256d92b2018-12-21 14:06:19 +02001185 size_t depth = thread_stack__depth(thread, sample->cpu);
Adrian Huntere2167082016-06-23 16:40:58 +03001186 const char *name = NULL;
1187 static int spacing;
1188 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001189 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001190 u64 ip = 0;
1191
1192 /*
1193 * The 'return' has already been popped off the stack so the depth has
1194 * to be adjusted to match the 'call'.
1195 */
1196 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1197 depth += 1;
1198
Andi Kleen99f753f2018-09-20 11:05:39 -07001199 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001200
Andi Kleena78cdee2018-09-18 05:32:10 -07001201 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1202 dlen += fprintf(fp, "(");
1203 dlen += map__fprintf_dsoname(al->map, fp);
1204 dlen += fprintf(fp, ")\t");
1205 }
1206
Adrian Huntere2167082016-06-23 16:40:58 +03001207 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001208 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001209 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001210 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001211
1212 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001213 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001214
1215 /*
1216 * Try to keep the output length from changing frequently so that the
1217 * output lines up more nicely.
1218 */
1219 if (len > spacing || (len && len < spacing - 52))
1220 spacing = round_up(len + 4, 32);
1221
1222 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001223 len += fprintf(fp, "%*s", spacing - len, "");
1224
Andi Kleena78cdee2018-09-18 05:32:10 -07001225 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001226}
1227
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001228static int perf_sample__fprintf_insn(struct perf_sample *sample,
1229 struct perf_event_attr *attr,
1230 struct thread *thread,
1231 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001232{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001233 int printed = 0;
1234
Andi Kleen224e2c92016-10-07 16:42:27 +03001235 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001236 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen224e2c92016-10-07 16:42:27 +03001237 if (PRINT_FIELD(INSN)) {
1238 int i;
1239
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001240 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001241 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001242 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001243 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001244 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001245 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1246
1247 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001248}
1249
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001250static int perf_sample__fprintf_bts(struct perf_sample *sample,
1251 struct perf_evsel *evsel,
1252 struct thread *thread,
1253 struct addr_location *al,
1254 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001255{
1256 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001257 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001258 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001259 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001260
Adrian Huntere2167082016-06-23 16:40:58 +03001261 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001262 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001263
Akihiro Nagai95582592012-01-30 13:43:09 +09001264 /* print branch_from information */
1265 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001266 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001267 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001268
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001269 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001270 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001271 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001272 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001273
1274 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001275 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001276 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001277 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001278 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001279 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001280 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001281 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001282
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001283 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001284 }
1285
Akihiro Nagai95582592012-01-30 13:43:09 +09001286 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001287 if (PRINT_FIELD(ADDR) ||
1288 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001289 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001290 printed += fprintf(fp, " => ");
1291 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001292 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001293
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001294 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001295 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001296
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001297 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001298 printed += fprintf(fp, "\n");
1299 if (PRINT_FIELD(SRCCODE)) {
1300 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1301 &thread->srccode_state);
1302 if (ret) {
1303 printed += ret;
1304 printed += printf("\n");
1305 }
1306 }
1307 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001308}
1309
Adrian Hunter055cd332016-06-23 16:40:56 +03001310static struct {
1311 u32 flags;
1312 const char *name;
1313} sample_flags[] = {
1314 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1315 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1316 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1317 {PERF_IP_FLAG_BRANCH, "jmp"},
1318 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1319 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1320 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1321 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1322 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1323 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1324 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1325 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1326 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1327 {0, NULL}
1328};
1329
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001330static const char *sample_flags_to_name(u32 flags)
1331{
1332 int i;
1333
1334 for (i = 0; sample_flags[i].name ; i++) {
1335 if (sample_flags[i].flags == flags)
1336 return sample_flags[i].name;
1337 }
1338
1339 return NULL;
1340}
1341
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001342static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001343{
1344 const char *chars = PERF_IP_FLAG_CHARS;
1345 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001346 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1347 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001348 char str[33];
1349 int i, pos = 0;
1350
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001351 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1352 if (name)
1353 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1354
1355 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1356 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1357 if (name)
1358 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1359 }
1360
1361 if (flags & PERF_IP_FLAG_TRACE_END) {
1362 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1363 if (name)
1364 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001365 }
1366
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001367 for (i = 0; i < n; i++, flags >>= 1) {
1368 if (flags & 1)
1369 str[pos++] = chars[i];
1370 }
1371 for (; i < 32; i++, flags >>= 1) {
1372 if (flags & 1)
1373 str[pos++] = '?';
1374 }
1375 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001376
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001377 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001378}
1379
Wang Nan30372f02016-02-24 11:20:45 +00001380struct printer_data {
1381 int line_no;
1382 bool hit_nul;
1383 bool is_printable;
1384};
1385
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001386static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1387 unsigned int val,
1388 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001389{
1390 unsigned char ch = (unsigned char)val;
1391 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001392 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001393
1394 switch (op) {
1395 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001396 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001397 break;
1398 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001399 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001400 " ");
1401 break;
1402 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001403 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001404 break;
1405 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001406 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001407 break;
1408 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001409 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001410 break;
1411 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001412 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001413 break;
1414 case BINARY_PRINT_CHAR_DATA:
1415 if (printer_data->hit_nul && ch)
1416 printer_data->is_printable = false;
1417
1418 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001419 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001420
1421 if (!printer_data->is_printable)
1422 break;
1423
1424 if (ch == '\0')
1425 printer_data->hit_nul = true;
1426 else
1427 printer_data->is_printable = false;
1428 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001429 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001430 }
1431 break;
1432 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001433 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001434 break;
1435 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001436 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001437 printer_data->line_no++;
1438 break;
1439 case BINARY_PRINT_DATA_END:
1440 default:
1441 break;
1442 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001443
1444 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001445}
1446
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001447static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001448{
1449 unsigned int nr_bytes = sample->raw_size;
1450 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001451 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1452 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001453
1454 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001455 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1456
1457 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001458}
1459
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001460static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001461{
1462 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001463 return fprintf(fp, "%*s", spacing - len, "");
1464
1465 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001466}
1467
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001468static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001469{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001470 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001471}
1472
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001473static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001474{
1475 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1476 int len;
1477
1478 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001479 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001480
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001481 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001482 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001483 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001484}
1485
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001486static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001487{
1488 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1489 int len;
1490
1491 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001492 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001493
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001494 len = fprintf(fp, " hints: %#x extensions: %#x ",
1495 data->hints, data->extensions);
1496 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001497}
1498
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001499static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001500{
1501 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1502 int len;
1503
1504 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001505 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001506
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001507 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1508 data->hw, data->cstate, data->subcstate);
1509 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001510}
1511
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001512static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001513{
1514 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1515 int len;
1516
1517 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001518 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001519
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001520 len = fprintf(fp, " IP: %u ", data->ip);
1521 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001522}
1523
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001524static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001525{
1526 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1527 int len;
1528
1529 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001530 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001531
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001532 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001533 data->deepest_cstate, data->last_cstate,
1534 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001535 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001536}
1537
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001538static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001539{
1540 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1541 unsigned int percent, freq;
1542 int len;
1543
1544 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001545 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001546
1547 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001548 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001549 if (data->max_nonturbo) {
1550 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001551 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001552 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001553 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001554}
1555
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001556static int perf_sample__fprintf_synth(struct perf_sample *sample,
1557 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001558{
1559 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001560 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001561 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001562 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001563 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001564 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001565 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001566 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001567 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001568 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001569 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001570 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001571 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001572 default:
1573 break;
1574 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001575
1576 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001577}
1578
Jiri Olsa809e9422015-11-26 18:55:21 +01001579struct perf_script {
1580 struct perf_tool tool;
1581 struct perf_session *session;
1582 bool show_task_events;
1583 bool show_mmap_events;
1584 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301585 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001586 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001587 bool show_round_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001588 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001589 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001590 struct cpu_map *cpus;
1591 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001592 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001593 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001594 struct perf_time_interval *ptime_range;
1595 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001596 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001597};
1598
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001599static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1600{
1601 struct perf_evsel *evsel;
1602 int max = 0;
1603
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001604 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001605 int len = strlen(perf_evsel__name(evsel));
1606
1607 max = MAX(len, max);
1608 }
1609
1610 return max;
1611}
1612
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001613static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001614{
1615 struct mem_info mi = { .data_src.val = data_src };
1616 char decode[100];
1617 char out[100];
1618 static int maxlen;
1619 int len;
1620
1621 perf_script__meminfo_scnprintf(decode, 100, &mi);
1622
1623 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1624 if (maxlen < len)
1625 maxlen = len;
1626
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001627 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001628}
1629
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001630struct metric_ctx {
1631 struct perf_sample *sample;
1632 struct thread *thread;
1633 struct perf_evsel *evsel;
1634 FILE *fp;
1635};
1636
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001637static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1638 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001639 const char *fmt,
1640 const char *unit, double val)
1641{
1642 struct metric_ctx *mctx = ctx;
1643
1644 if (!fmt)
1645 return;
1646 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001647 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001648 fputs("\tmetric: ", mctx->fp);
1649 if (color)
1650 color_fprintf(mctx->fp, color, fmt, val);
1651 else
1652 printf(fmt, val);
1653 fprintf(mctx->fp, " %s\n", unit);
1654}
1655
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001656static void script_new_line(struct perf_stat_config *config __maybe_unused,
1657 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001658{
1659 struct metric_ctx *mctx = ctx;
1660
1661 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001662 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001663 fputs("\tmetric: ", mctx->fp);
1664}
1665
1666static void perf_sample__fprint_metric(struct perf_script *script,
1667 struct thread *thread,
1668 struct perf_evsel *evsel,
1669 struct perf_sample *sample,
1670 FILE *fp)
1671{
1672 struct perf_stat_output_ctx ctx = {
1673 .print_metric = script_print_metric,
1674 .new_line = script_new_line,
1675 .ctx = &(struct metric_ctx) {
1676 .sample = sample,
1677 .thread = thread,
1678 .evsel = evsel,
1679 .fp = fp,
1680 },
1681 .force_header = false,
1682 };
1683 struct perf_evsel *ev2;
1684 static bool init;
1685 u64 val;
1686
1687 if (!init) {
1688 perf_stat__init_shadow_stats();
1689 init = true;
1690 }
1691 if (!evsel->stats)
1692 perf_evlist__alloc_stats(script->session->evlist, false);
1693 if (evsel_script(evsel->leader)->gnum++ == 0)
1694 perf_stat__reset_shadow_stats();
1695 val = sample->period * evsel->scale;
1696 perf_stat__update_shadow_stats(evsel,
1697 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001698 sample->cpu,
1699 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001700 evsel_script(evsel)->val = val;
1701 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1702 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001703 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001704 evsel_script(ev2)->val,
1705 sample->cpu,
1706 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001707 NULL,
1708 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001709 }
1710 evsel_script(evsel->leader)->gnum = 0;
1711 }
1712}
1713
Andi Kleen99f753f2018-09-20 11:05:39 -07001714static bool show_event(struct perf_sample *sample,
1715 struct perf_evsel *evsel,
1716 struct thread *thread,
1717 struct addr_location *al)
1718{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001719 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001720
1721 if (!symbol_conf.graph_function)
1722 return true;
1723
1724 if (thread->filter) {
1725 if (depth <= thread->filter_entry_depth) {
1726 thread->filter = false;
1727 return false;
1728 }
1729 return true;
1730 } else {
1731 const char *s = symbol_conf.graph_function;
1732 u64 ip;
1733 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1734 &ip);
1735 unsigned nlen;
1736
1737 if (!name)
1738 return false;
1739 nlen = strlen(name);
1740 while (*s) {
1741 unsigned len = strcspn(s, ",");
1742 if (nlen == len && !strncmp(name, s, len)) {
1743 thread->filter = true;
1744 thread->filter_entry_depth = depth;
1745 return true;
1746 }
1747 s += len;
1748 if (*s == ',')
1749 s++;
1750 }
1751 return false;
1752 }
1753}
1754
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001755static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001756 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001757 struct addr_location *al,
1758 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001759{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001760 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001761 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001762 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001763 struct perf_evsel_script *es = evsel->priv;
1764 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001765
Adrian Hunter14057202017-06-21 13:17:19 +03001766 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001767 return;
1768
Andi Kleen99f753f2018-09-20 11:05:39 -07001769 if (!show_event(sample, evsel, thread, al))
1770 return;
1771
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001772 ++es->samples;
1773
Jiri Olsa28a0b392018-01-07 17:03:52 +01001774 perf_sample__fprintf_start(sample, thread, evsel,
1775 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001776
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001777 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001778 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001779
Namhyung Kime944d3d2013-11-18 14:34:52 +09001780 if (PRINT_FIELD(EVNAME)) {
1781 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001782
1783 if (!script->name_width)
1784 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1785
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001786 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001787 }
1788
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001789 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001790 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001791
Akihiro Nagai95582592012-01-30 13:43:09 +09001792 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001793 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001794 return;
1795 }
1796
Andi Kleen96167162019-01-17 11:48:34 -08001797 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001798 event_format__fprintf(evsel->tp_format, sample->cpu,
1799 sample->raw_data, sample->raw_size, fp);
1800 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001801
1802 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001803 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001804
David Ahern7cec0922011-05-30 13:08:23 -06001805 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001806 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001807
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001808 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001809 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001810
1811 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001812 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001813
David Ahern787bef12011-05-27 14:28:43 -06001814 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001815 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001816
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001817 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001818 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001819 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001820 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001821
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001822 fputc(cursor ? '\n' : ' ', fp);
1823 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001824 }
1825
Stephane Eranianfc36f942015-08-31 18:41:10 +02001826 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001827 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001828
Andi Kleenb1491ac2017-09-05 11:40:57 -07001829 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001830 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001831
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001832 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001833 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001834 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001835 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001836 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001837 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001838
Wang Nan30372f02016-02-24 11:20:45 +00001839 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001840 perf_sample__fprintf_bpf_output(sample, fp);
1841 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001842
1843 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001844 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1845 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001846
Andi Kleendd2e18e2018-12-03 16:18:48 -08001847 if (PRINT_FIELD(SRCCODE)) {
1848 if (map__fprintf_srccode(al->map, al->addr, stdout,
1849 &thread->srccode_state))
1850 printf("\n");
1851 }
1852
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001853 if (PRINT_FIELD(METRIC))
1854 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001855
1856 if (verbose)
1857 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001858}
1859
Tom Zanussi956ffd02009-11-25 01:15:46 -06001860static struct scripting_ops *scripting_ops;
1861
Jiri Olsa36e33c52016-01-06 11:49:56 +01001862static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1863{
1864 int nthreads = thread_map__nr(counter->threads);
1865 int ncpus = perf_evsel__nr_cpus(counter);
1866 int cpu, thread;
1867 static int header_printed;
1868
1869 if (counter->system_wide)
1870 nthreads = 1;
1871
1872 if (!header_printed) {
1873 printf("%3s %8s %15s %15s %15s %15s %s\n",
1874 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1875 header_printed = 1;
1876 }
1877
1878 for (thread = 0; thread < nthreads; thread++) {
1879 for (cpu = 0; cpu < ncpus; cpu++) {
1880 struct perf_counts_values *counts;
1881
1882 counts = perf_counts(counter->counts, cpu, thread);
1883
1884 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1885 counter->cpus->map[cpu],
1886 thread_map__pid(counter->threads, thread),
1887 counts->val,
1888 counts->ena,
1889 counts->run,
1890 tstamp,
1891 perf_evsel__name(counter));
1892 }
1893 }
1894}
1895
Jiri Olsae099eba2016-01-05 22:09:09 +01001896static void process_stat(struct perf_evsel *counter, u64 tstamp)
1897{
1898 if (scripting_ops && scripting_ops->process_stat)
1899 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001900 else
1901 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001902}
1903
1904static void process_stat_interval(u64 tstamp)
1905{
1906 if (scripting_ops && scripting_ops->process_stat_interval)
1907 scripting_ops->process_stat_interval(tstamp);
1908}
1909
Tom Zanussi956ffd02009-11-25 01:15:46 -06001910static void setup_scripting(void)
1911{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001912 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001913 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001914}
1915
Adrian Hunterd445dd22014-08-15 22:08:37 +03001916static int flush_scripting(void)
1917{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001918 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001919}
1920
Tom Zanussi956ffd02009-11-25 01:15:46 -06001921static int cleanup_scripting(void)
1922{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001923 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001924
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001925 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001926}
1927
Jiri Olsa809e9422015-11-26 18:55:21 +01001928static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001929 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001930 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001931 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001932 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001933{
Jiri Olsa809e9422015-11-26 18:55:21 +01001934 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001935 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001936
Jin Yao2ab046c2017-12-08 21:13:46 +08001937 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1938 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001939 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001940 }
David Aherna91f4c42016-11-29 10:15:43 -07001941
David Ahern1424dc92011-03-09 22:23:28 -07001942 if (debug_mode) {
1943 if (sample->time < last_timestamp) {
1944 pr_err("Samples misordered, previous: %" PRIu64
1945 " this: %" PRIu64 "\n", last_timestamp,
1946 sample->time);
1947 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001948 }
David Ahern1424dc92011-03-09 22:23:28 -07001949 last_timestamp = sample->time;
1950 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001951 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001952
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001953 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001954 pr_err("problem processing %d event, skipping it.\n",
1955 event->header.type);
1956 return -1;
1957 }
1958
1959 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001960 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001961
Anton Blanchard5d67be92011-07-04 21:57:50 +10001962 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001963 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001964
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001965 if (scripting_ops)
1966 scripting_ops->process_event(event, sample, evsel, &al);
1967 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001968 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001969
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001970out_put:
1971 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001972 return 0;
1973}
1974
Adrian Hunter7ea95722013-11-01 15:51:30 +02001975static int process_attr(struct perf_tool *tool, union perf_event *event,
1976 struct perf_evlist **pevlist)
1977{
1978 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1979 struct perf_evlist *evlist;
1980 struct perf_evsel *evsel, *pos;
1981 int err;
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301982 static struct perf_evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001983
1984 err = perf_event__process_attr(tool, event, pevlist);
1985 if (err)
1986 return err;
1987
1988 evlist = *pevlist;
1989 evsel = perf_evlist__last(*pevlist);
1990
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301991 if (!evsel->priv) {
1992 if (scr->per_event_dump) {
1993 evsel->priv = perf_evsel_script__new(evsel,
1994 scr->session->data);
1995 } else {
1996 es = zalloc(sizeof(*es));
1997 if (!es)
1998 return -ENOMEM;
1999 es->fp = stdout;
2000 evsel->priv = es;
2001 }
2002 }
2003
Adrian Hunter14057202017-06-21 13:17:19 +03002004 if (evsel->attr.type >= PERF_TYPE_MAX &&
2005 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002006 return 0;
2007
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002008 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02002009 if (pos->attr.type == evsel->attr.type && pos != evsel)
2010 return 0;
2011 }
2012
2013 set_print_ip_opts(&evsel->attr);
2014
Jiri Olsad2b5a312015-10-16 12:41:25 +02002015 if (evsel->attr.sample_type)
2016 err = perf_evsel__check_attr(evsel, scr->session);
2017
2018 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002019}
2020
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002021static int process_comm_event(struct perf_tool *tool,
2022 union perf_event *event,
2023 struct perf_sample *sample,
2024 struct machine *machine)
2025{
2026 struct thread *thread;
2027 struct perf_script *script = container_of(tool, struct perf_script, tool);
2028 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002029 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002030 int ret = -1;
2031
2032 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2033 if (thread == NULL) {
2034 pr_debug("problem processing COMM event, skipping it.\n");
2035 return -1;
2036 }
2037
2038 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2039 goto out;
2040
2041 if (!evsel->attr.sample_id_all) {
2042 sample->cpu = 0;
2043 sample->time = 0;
2044 sample->tid = event->comm.tid;
2045 sample->pid = event->comm.pid;
2046 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002047 perf_sample__fprintf_start(sample, thread, evsel,
2048 PERF_RECORD_COMM, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002049 perf_event__fprintf(event, stdout);
2050 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002051out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002052 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002053 return ret;
2054}
2055
Hari Bathini96a44bb2017-03-08 02:12:06 +05302056static int process_namespaces_event(struct perf_tool *tool,
2057 union perf_event *event,
2058 struct perf_sample *sample,
2059 struct machine *machine)
2060{
2061 struct thread *thread;
2062 struct perf_script *script = container_of(tool, struct perf_script, tool);
2063 struct perf_session *session = script->session;
2064 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2065 int ret = -1;
2066
2067 thread = machine__findnew_thread(machine, event->namespaces.pid,
2068 event->namespaces.tid);
2069 if (thread == NULL) {
2070 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2071 return -1;
2072 }
2073
2074 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2075 goto out;
2076
2077 if (!evsel->attr.sample_id_all) {
2078 sample->cpu = 0;
2079 sample->time = 0;
2080 sample->tid = event->namespaces.tid;
2081 sample->pid = event->namespaces.pid;
2082 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002083 perf_sample__fprintf_start(sample, thread, evsel,
2084 PERF_RECORD_NAMESPACES, stdout);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302085 perf_event__fprintf(event, stdout);
2086 ret = 0;
2087out:
2088 thread__put(thread);
2089 return ret;
2090}
2091
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002092static int process_fork_event(struct perf_tool *tool,
2093 union perf_event *event,
2094 struct perf_sample *sample,
2095 struct machine *machine)
2096{
2097 struct thread *thread;
2098 struct perf_script *script = container_of(tool, struct perf_script, tool);
2099 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002100 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002101
2102 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2103 return -1;
2104
2105 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2106 if (thread == NULL) {
2107 pr_debug("problem processing FORK event, skipping it.\n");
2108 return -1;
2109 }
2110
2111 if (!evsel->attr.sample_id_all) {
2112 sample->cpu = 0;
2113 sample->time = event->fork.time;
2114 sample->tid = event->fork.tid;
2115 sample->pid = event->fork.pid;
2116 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002117 perf_sample__fprintf_start(sample, thread, evsel,
2118 PERF_RECORD_FORK, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002119 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002120 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002121
2122 return 0;
2123}
2124static int process_exit_event(struct perf_tool *tool,
2125 union perf_event *event,
2126 struct perf_sample *sample,
2127 struct machine *machine)
2128{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002129 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002130 struct thread *thread;
2131 struct perf_script *script = container_of(tool, struct perf_script, tool);
2132 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002133 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002134
2135 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2136 if (thread == NULL) {
2137 pr_debug("problem processing EXIT event, skipping it.\n");
2138 return -1;
2139 }
2140
2141 if (!evsel->attr.sample_id_all) {
2142 sample->cpu = 0;
2143 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002144 sample->tid = event->fork.tid;
2145 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002146 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002147 perf_sample__fprintf_start(sample, thread, evsel,
2148 PERF_RECORD_EXIT, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002149 perf_event__fprintf(event, stdout);
2150
2151 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002152 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002153
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002154 thread__put(thread);
2155 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002156}
2157
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002158static int process_mmap_event(struct perf_tool *tool,
2159 union perf_event *event,
2160 struct perf_sample *sample,
2161 struct machine *machine)
2162{
2163 struct thread *thread;
2164 struct perf_script *script = container_of(tool, struct perf_script, tool);
2165 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002166 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002167
2168 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2169 return -1;
2170
2171 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2172 if (thread == NULL) {
2173 pr_debug("problem processing MMAP event, skipping it.\n");
2174 return -1;
2175 }
2176
2177 if (!evsel->attr.sample_id_all) {
2178 sample->cpu = 0;
2179 sample->time = 0;
2180 sample->tid = event->mmap.tid;
2181 sample->pid = event->mmap.pid;
2182 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002183 perf_sample__fprintf_start(sample, thread, evsel,
2184 PERF_RECORD_MMAP, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002185 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002186 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002187 return 0;
2188}
2189
2190static int process_mmap2_event(struct perf_tool *tool,
2191 union perf_event *event,
2192 struct perf_sample *sample,
2193 struct machine *machine)
2194{
2195 struct thread *thread;
2196 struct perf_script *script = container_of(tool, struct perf_script, tool);
2197 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002198 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002199
2200 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2201 return -1;
2202
2203 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2204 if (thread == NULL) {
2205 pr_debug("problem processing MMAP2 event, skipping it.\n");
2206 return -1;
2207 }
2208
2209 if (!evsel->attr.sample_id_all) {
2210 sample->cpu = 0;
2211 sample->time = 0;
2212 sample->tid = event->mmap2.tid;
2213 sample->pid = event->mmap2.pid;
2214 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002215 perf_sample__fprintf_start(sample, thread, evsel,
2216 PERF_RECORD_MMAP2, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002217 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002218 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002219 return 0;
2220}
2221
Adrian Hunter7c148982015-07-21 12:44:06 +03002222static int process_switch_event(struct perf_tool *tool,
2223 union perf_event *event,
2224 struct perf_sample *sample,
2225 struct machine *machine)
2226{
2227 struct thread *thread;
2228 struct perf_script *script = container_of(tool, struct perf_script, tool);
2229 struct perf_session *session = script->session;
2230 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2231
2232 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2233 return -1;
2234
2235 thread = machine__findnew_thread(machine, sample->pid,
2236 sample->tid);
2237 if (thread == NULL) {
2238 pr_debug("problem processing SWITCH event, skipping it.\n");
2239 return -1;
2240 }
2241
Jiri Olsa28a0b392018-01-07 17:03:52 +01002242 perf_sample__fprintf_start(sample, thread, evsel,
2243 PERF_RECORD_SWITCH, stdout);
Adrian Hunter7c148982015-07-21 12:44:06 +03002244 perf_event__fprintf(event, stdout);
2245 thread__put(thread);
2246 return 0;
2247}
2248
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002249static int
2250process_lost_event(struct perf_tool *tool,
2251 union perf_event *event,
2252 struct perf_sample *sample,
2253 struct machine *machine)
2254{
2255 struct perf_script *script = container_of(tool, struct perf_script, tool);
2256 struct perf_session *session = script->session;
2257 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2258 struct thread *thread;
2259
2260 thread = machine__findnew_thread(machine, sample->pid,
2261 sample->tid);
2262 if (thread == NULL)
2263 return -1;
2264
2265 perf_sample__fprintf_start(sample, thread, evsel,
2266 PERF_RECORD_LOST, stdout);
2267 perf_event__fprintf(event, stdout);
2268 thread__put(thread);
2269 return 0;
2270}
2271
Jiri Olsa3233b372018-02-06 19:17:59 +01002272static int
2273process_finished_round_event(struct perf_tool *tool __maybe_unused,
2274 union perf_event *event,
2275 struct ordered_events *oe __maybe_unused)
2276
2277{
2278 perf_event__fprintf(event, stdout);
2279 return 0;
2280}
2281
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002282static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002283{
2284 session_done = 1;
2285}
2286
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002287static void perf_script__fclose_per_event_dump(struct perf_script *script)
2288{
2289 struct perf_evlist *evlist = script->session->evlist;
2290 struct perf_evsel *evsel;
2291
2292 evlist__for_each_entry(evlist, evsel) {
2293 if (!evsel->priv)
2294 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002295 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002296 evsel->priv = NULL;
2297 }
2298}
2299
2300static int perf_script__fopen_per_event_dump(struct perf_script *script)
2301{
2302 struct perf_evsel *evsel;
2303
2304 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002305 /*
2306 * Already setup? I.e. we may be called twice in cases like
2307 * Intel PT, one for the intel_pt// and dummy events, then
2308 * for the evsels syntheized from the auxtrace info.
2309 *
2310 * Ses perf_script__process_auxtrace_info.
2311 */
2312 if (evsel->priv != NULL)
2313 continue;
2314
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002315 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002316 if (evsel->priv == NULL)
2317 goto out_err_fclose;
2318 }
2319
2320 return 0;
2321
2322out_err_fclose:
2323 perf_script__fclose_per_event_dump(script);
2324 return -1;
2325}
2326
2327static int perf_script__setup_per_event_dump(struct perf_script *script)
2328{
2329 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002330 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002331
2332 if (script->per_event_dump)
2333 return perf_script__fopen_per_event_dump(script);
2334
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002335 es_stdout.fp = stdout;
2336
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002337 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002338 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002339
2340 return 0;
2341}
2342
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002343static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2344{
2345 struct perf_evsel *evsel;
2346
2347 evlist__for_each_entry(script->session->evlist, evsel) {
2348 struct perf_evsel_script *es = evsel->priv;
2349
2350 perf_evsel_script__fprintf(es, stdout);
2351 perf_evsel_script__delete(es);
2352 evsel->priv = NULL;
2353 }
2354}
2355
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002356static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002357{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002358 int ret;
2359
Tom Zanussic239da32010-04-01 23:59:18 -05002360 signal(SIGINT, sig_handler);
2361
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002362 /* override event processing functions */
2363 if (script->show_task_events) {
2364 script->tool.comm = process_comm_event;
2365 script->tool.fork = process_fork_event;
2366 script->tool.exit = process_exit_event;
2367 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002368 if (script->show_mmap_events) {
2369 script->tool.mmap = process_mmap_event;
2370 script->tool.mmap2 = process_mmap2_event;
2371 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002372 if (script->show_switch_events)
2373 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302374 if (script->show_namespace_events)
2375 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002376 if (script->show_lost_events)
2377 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002378 if (script->show_round_events) {
2379 script->tool.ordered_events = false;
2380 script->tool.finished_round = process_finished_round_event;
2381 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002382
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002383 if (perf_script__setup_per_event_dump(script)) {
2384 pr_err("Couldn't create the per event dump files\n");
2385 return -1;
2386 }
2387
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002388 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002389
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002390 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002391 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002392
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002393 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002394 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002395
2396 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002397}
2398
Tom Zanussi956ffd02009-11-25 01:15:46 -06002399struct script_spec {
2400 struct list_head node;
2401 struct scripting_ops *ops;
2402 char spec[0];
2403};
2404
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002405static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002406
2407static struct script_spec *script_spec__new(const char *spec,
2408 struct scripting_ops *ops)
2409{
2410 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2411
2412 if (s != NULL) {
2413 strcpy(s->spec, spec);
2414 s->ops = ops;
2415 }
2416
2417 return s;
2418}
2419
Tom Zanussi956ffd02009-11-25 01:15:46 -06002420static void script_spec__add(struct script_spec *s)
2421{
2422 list_add_tail(&s->node, &script_specs);
2423}
2424
2425static struct script_spec *script_spec__find(const char *spec)
2426{
2427 struct script_spec *s;
2428
2429 list_for_each_entry(s, &script_specs, node)
2430 if (strcasecmp(s->spec, spec) == 0)
2431 return s;
2432 return NULL;
2433}
2434
Tom Zanussi956ffd02009-11-25 01:15:46 -06002435int script_spec_register(const char *spec, struct scripting_ops *ops)
2436{
2437 struct script_spec *s;
2438
2439 s = script_spec__find(spec);
2440 if (s)
2441 return -1;
2442
Taeung Song8560bae2016-02-26 00:13:10 +09002443 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002444 if (!s)
2445 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002446 else
2447 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002448
2449 return 0;
2450}
2451
2452static struct scripting_ops *script_spec__lookup(const char *spec)
2453{
2454 struct script_spec *s = script_spec__find(spec);
2455 if (!s)
2456 return NULL;
2457
2458 return s->ops;
2459}
2460
2461static void list_available_languages(void)
2462{
2463 struct script_spec *s;
2464
2465 fprintf(stderr, "\n");
2466 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002467 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002468
2469 list_for_each_entry(s, &script_specs, node)
2470 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2471
2472 fprintf(stderr, "\n");
2473}
2474
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002475static int parse_scriptname(const struct option *opt __maybe_unused,
2476 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002477{
2478 char spec[PATH_MAX];
2479 const char *script, *ext;
2480 int len;
2481
Tom Zanussif526d682010-01-27 02:27:52 -06002482 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002483 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002484 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002485 }
2486
2487 script = strchr(str, ':');
2488 if (script) {
2489 len = script - str;
2490 if (len >= PATH_MAX) {
2491 fprintf(stderr, "invalid language specifier");
2492 return -1;
2493 }
2494 strncpy(spec, str, len);
2495 spec[len] = '\0';
2496 scripting_ops = script_spec__lookup(spec);
2497 if (!scripting_ops) {
2498 fprintf(stderr, "invalid language specifier");
2499 return -1;
2500 }
2501 script++;
2502 } else {
2503 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002504 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002505 if (!ext) {
2506 fprintf(stderr, "invalid script extension");
2507 return -1;
2508 }
2509 scripting_ops = script_spec__lookup(++ext);
2510 if (!scripting_ops) {
2511 fprintf(stderr, "invalid script extension");
2512 return -1;
2513 }
2514 }
2515
2516 script_name = strdup(script);
2517
2518 return 0;
2519}
2520
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002521static int parse_output_fields(const struct option *opt __maybe_unused,
2522 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002523{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002524 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002525 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002526 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002527 int rc = 0;
2528 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002529 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002530 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002531
2532 if (!str)
2533 return -ENOMEM;
2534
David Ahern2c9e45f72011-03-17 10:03:21 -06002535 /* first word can state for which event type the user is specifying
2536 * the fields. If no type exists, the specified fields apply to all
2537 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002538 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002539 tok = strchr(str, ':');
2540 if (tok) {
2541 *tok = '\0';
2542 tok++;
2543 if (!strcmp(str, "hw"))
2544 type = PERF_TYPE_HARDWARE;
2545 else if (!strcmp(str, "sw"))
2546 type = PERF_TYPE_SOFTWARE;
2547 else if (!strcmp(str, "trace"))
2548 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002549 else if (!strcmp(str, "raw"))
2550 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002551 else if (!strcmp(str, "break"))
2552 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002553 else if (!strcmp(str, "synth"))
2554 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002555 else {
2556 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002557 rc = -EINVAL;
2558 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002559 }
2560
2561 if (output[type].user_set)
2562 pr_warning("Overriding previous field request for %s events.\n",
2563 event_type(type));
2564
2565 output[type].fields = 0;
2566 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002567 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002568
2569 } else {
2570 tok = str;
2571 if (strlen(str) == 0) {
2572 fprintf(stderr,
2573 "Cannot set fields to 'none' for all event types.\n");
2574 rc = -EINVAL;
2575 goto out;
2576 }
2577
Andi Kleen36ce5652017-06-02 08:48:10 -07002578 /* Don't override defaults for +- */
2579 if (strchr(str, '+') || strchr(str, '-'))
2580 goto parse;
2581
David Ahern2c9e45f72011-03-17 10:03:21 -06002582 if (output_set_by_user())
2583 pr_warning("Overriding previous field request for all events.\n");
2584
Adrian Hunter14057202017-06-21 13:17:19 +03002585 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002586 output[j].fields = 0;
2587 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002588 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002589 }
David Ahern1424dc92011-03-09 22:23:28 -07002590 }
2591
Andi Kleen36ce5652017-06-02 08:48:10 -07002592parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002593 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002594 if (*tok == '+') {
2595 if (change == SET)
2596 goto out_badmix;
2597 change = ADD;
2598 tok++;
2599 } else if (*tok == '-') {
2600 if (change == SET)
2601 goto out_badmix;
2602 change = REMOVE;
2603 tok++;
2604 } else {
2605 if (change != SET && change != DEFAULT)
2606 goto out_badmix;
2607 change = SET;
2608 }
2609
David Ahern745f43e2011-03-09 22:23:26 -07002610 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002611 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002612 break;
David Ahern745f43e2011-03-09 22:23:26 -07002613 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002614 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002615 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002616 continue;
2617 }
David Ahern745f43e2011-03-09 22:23:26 -07002618 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002619 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002620 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002621 goto out;
2622 }
2623
2624 if (type == -1) {
2625 /* add user option to all events types for
2626 * which it is valid
2627 */
Adrian Hunter14057202017-06-21 13:17:19 +03002628 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002629 if (output[j].invalid_fields & all_output_options[i].field) {
2630 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2631 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002632 } else {
2633 if (change == REMOVE)
2634 output[j].fields &= ~all_output_options[i].field;
2635 else
2636 output[j].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002637 output[j].user_set = true;
2638 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002639 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002640 }
2641 } else {
2642 if (output[type].invalid_fields & all_output_options[i].field) {
2643 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2644 all_output_options[i].str, event_type(type));
2645
2646 rc = -EINVAL;
2647 goto out;
2648 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002649 output[type].user_set = true;
2650 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002651 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002652 }
2653
2654 if (type >= 0) {
2655 if (output[type].fields == 0) {
2656 pr_debug("No fields requested for %s type. "
2657 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002658 }
David Ahern1424dc92011-03-09 22:23:28 -07002659 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002660 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002661
Andi Kleen36ce5652017-06-02 08:48:10 -07002662out_badmix:
2663 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2664 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002665out:
David Ahern745f43e2011-03-09 22:23:26 -07002666 free(str);
2667 return rc;
2668}
2669
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002670#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2671 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2672 if ((lang_dirent->d_type == DT_DIR || \
2673 (lang_dirent->d_type == DT_UNKNOWN && \
2674 is_directory(scripts_path, lang_dirent))) && \
2675 (strcmp(lang_dirent->d_name, ".")) && \
2676 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002677
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002678#define for_each_script(lang_path, lang_dir, script_dirent) \
2679 while ((script_dirent = readdir(lang_dir)) != NULL) \
2680 if (script_dirent->d_type != DT_DIR && \
2681 (script_dirent->d_type != DT_UNKNOWN || \
2682 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002683
2684
2685#define RECORD_SUFFIX "-record"
2686#define REPORT_SUFFIX "-report"
2687
2688struct script_desc {
2689 struct list_head node;
2690 char *name;
2691 char *half_liner;
2692 char *args;
2693};
2694
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002695static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002696
2697static struct script_desc *script_desc__new(const char *name)
2698{
2699 struct script_desc *s = zalloc(sizeof(*s));
2700
Tom Zanussib5b87312010-11-10 08:16:51 -06002701 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002702 s->name = strdup(name);
2703
2704 return s;
2705}
2706
2707static void script_desc__delete(struct script_desc *s)
2708{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002709 zfree(&s->name);
2710 zfree(&s->half_liner);
2711 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002712 free(s);
2713}
2714
2715static void script_desc__add(struct script_desc *s)
2716{
2717 list_add_tail(&s->node, &script_descs);
2718}
2719
2720static struct script_desc *script_desc__find(const char *name)
2721{
2722 struct script_desc *s;
2723
2724 list_for_each_entry(s, &script_descs, node)
2725 if (strcasecmp(s->name, name) == 0)
2726 return s;
2727 return NULL;
2728}
2729
2730static struct script_desc *script_desc__findnew(const char *name)
2731{
2732 struct script_desc *s = script_desc__find(name);
2733
2734 if (s)
2735 return s;
2736
2737 s = script_desc__new(name);
2738 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002739 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002740
2741 script_desc__add(s);
2742
2743 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002744}
2745
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002746static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002747{
2748 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002749 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002750
2751 if (strlen(str) > suffix_len) {
2752 p = str + strlen(str) - suffix_len;
2753 if (!strncmp(p, suffix, suffix_len))
2754 return p;
2755 }
2756
2757 return NULL;
2758}
2759
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002760static int read_script_info(struct script_desc *desc, const char *filename)
2761{
2762 char line[BUFSIZ], *p;
2763 FILE *fp;
2764
2765 fp = fopen(filename, "r");
2766 if (!fp)
2767 return -1;
2768
2769 while (fgets(line, sizeof(line), fp)) {
2770 p = ltrim(line);
2771 if (strlen(p) == 0)
2772 continue;
2773 if (*p != '#')
2774 continue;
2775 p++;
2776 if (strlen(p) && *p == '!')
2777 continue;
2778
2779 p = ltrim(p);
2780 if (strlen(p) && p[strlen(p) - 1] == '\n')
2781 p[strlen(p) - 1] = '\0';
2782
2783 if (!strncmp(p, "description:", strlen("description:"))) {
2784 p += strlen("description:");
2785 desc->half_liner = strdup(ltrim(p));
2786 continue;
2787 }
2788
2789 if (!strncmp(p, "args:", strlen("args:"))) {
2790 p += strlen("args:");
2791 desc->args = strdup(ltrim(p));
2792 continue;
2793 }
2794 }
2795
2796 fclose(fp);
2797
2798 return 0;
2799}
2800
Robert Richter38efb532011-11-25 11:38:40 +01002801static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2802{
2803 char *script_root, *str;
2804
2805 script_root = strdup(script_dirent->d_name);
2806 if (!script_root)
2807 return NULL;
2808
2809 str = (char *)ends_with(script_root, suffix);
2810 if (!str) {
2811 free(script_root);
2812 return NULL;
2813 }
2814
2815 *str = '\0';
2816 return script_root;
2817}
2818
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002819static int list_available_scripts(const struct option *opt __maybe_unused,
2820 const char *s __maybe_unused,
2821 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002822{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002823 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002824 char scripts_path[MAXPATHLEN];
2825 DIR *scripts_dir, *lang_dir;
2826 char script_path[MAXPATHLEN];
2827 char lang_path[MAXPATHLEN];
2828 struct script_desc *desc;
2829 char first_half[BUFSIZ];
2830 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002831
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002832 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002833
2834 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002835 if (!scripts_dir) {
2836 fprintf(stdout,
2837 "open(%s) failed.\n"
2838 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2839 scripts_path);
2840 exit(-1);
2841 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002842
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002843 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002844 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2845 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002846 lang_dir = opendir(lang_path);
2847 if (!lang_dir)
2848 continue;
2849
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002850 for_each_script(lang_path, lang_dir, script_dirent) {
2851 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002852 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002853 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002854 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2855 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002856 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002857 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002858 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002859 }
2860 }
2861
2862 fprintf(stdout, "List of available trace scripts:\n");
2863 list_for_each_entry(desc, &script_descs, node) {
2864 sprintf(first_half, "%s %s", desc->name,
2865 desc->args ? desc->args : "");
2866 fprintf(stdout, " %-36s %s\n", first_half,
2867 desc->half_liner ? desc->half_liner : "");
2868 }
2869
2870 exit(0);
2871}
2872
Feng Tange5f37052012-09-07 16:42:26 +08002873/*
Feng Tang49e639e2012-10-30 11:56:03 +08002874 * Some scripts specify the required events in their "xxx-record" file,
2875 * this function will check if the events in perf.data match those
2876 * mentioned in the "xxx-record".
2877 *
2878 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2879 * which is covered well now. And new parsing code should be added to
2880 * cover the future complexing formats like event groups etc.
2881 */
2882static int check_ev_match(char *dir_name, char *scriptname,
2883 struct perf_session *session)
2884{
2885 char filename[MAXPATHLEN], evname[128];
2886 char line[BUFSIZ], *p;
2887 struct perf_evsel *pos;
2888 int match, len;
2889 FILE *fp;
2890
Jiri Olsa77f18152018-03-19 09:29:01 +01002891 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08002892
2893 fp = fopen(filename, "r");
2894 if (!fp)
2895 return -1;
2896
2897 while (fgets(line, sizeof(line), fp)) {
2898 p = ltrim(line);
2899 if (*p == '#')
2900 continue;
2901
2902 while (strlen(p)) {
2903 p = strstr(p, "-e");
2904 if (!p)
2905 break;
2906
2907 p += 2;
2908 p = ltrim(p);
2909 len = strcspn(p, " \t");
2910 if (!len)
2911 break;
2912
2913 snprintf(evname, len + 1, "%s", p);
2914
2915 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002916 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002917 if (!strcmp(perf_evsel__name(pos), evname)) {
2918 match = 1;
2919 break;
2920 }
2921 }
2922
2923 if (!match) {
2924 fclose(fp);
2925 return -1;
2926 }
2927 }
2928 }
2929
2930 fclose(fp);
2931 return 0;
2932}
2933
2934/*
Feng Tange5f37052012-09-07 16:42:26 +08002935 * Return -1 if none is found, otherwise the actual scripts number.
2936 *
2937 * Currently the only user of this function is the script browser, which
2938 * will list all statically runnable scripts, select one, execute it and
2939 * show the output in a perf browser.
2940 */
2941int find_scripts(char **scripts_array, char **scripts_path_array)
2942{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002943 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002944 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002945 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002946 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002947 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002948 .file = {
2949 .path = input_name,
2950 },
2951 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002952 };
Feng Tange5f37052012-09-07 16:42:26 +08002953 char *temp;
2954 int i = 0;
2955
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002956 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002957 if (!session)
2958 return -1;
2959
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002960 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002961
2962 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002963 if (!scripts_dir) {
2964 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002965 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002966 }
Feng Tange5f37052012-09-07 16:42:26 +08002967
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002968 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002969 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
2970 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08002971#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08002972 if (strstr(lang_path, "perl"))
2973 continue;
2974#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08002975#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08002976 if (strstr(lang_path, "python"))
2977 continue;
2978#endif
2979
2980 lang_dir = opendir(lang_path);
2981 if (!lang_dir)
2982 continue;
2983
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002984 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002985 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002986 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002987 continue;
2988 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002989 script_dirent->d_name);
2990 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002991 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002992 (temp - script_dirent->d_name) + 1,
2993 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002994
2995 if (check_ev_match(lang_path,
2996 scripts_array[i], session))
2997 continue;
2998
Feng Tange5f37052012-09-07 16:42:26 +08002999 i++;
3000 }
Feng Tang49e639e2012-10-30 11:56:03 +08003001 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003002 }
3003
Feng Tang49e639e2012-10-30 11:56:03 +08003004 closedir(scripts_dir);
3005 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003006 return i;
3007}
3008
Tom Zanussi38752942009-12-15 02:53:39 -06003009static char *get_script_path(const char *script_root, const char *suffix)
3010{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003011 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003012 char scripts_path[MAXPATHLEN];
3013 char script_path[MAXPATHLEN];
3014 DIR *scripts_dir, *lang_dir;
3015 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003016 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003017
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003018 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003019
3020 scripts_dir = opendir(scripts_path);
3021 if (!scripts_dir)
3022 return NULL;
3023
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003024 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003025 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3026 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003027 lang_dir = opendir(lang_path);
3028 if (!lang_dir)
3029 continue;
3030
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003031 for_each_script(lang_path, lang_dir, script_dirent) {
3032 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003033 if (__script_root && !strcmp(script_root, __script_root)) {
3034 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003035 closedir(lang_dir);
3036 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003037 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3038 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003039 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003040 }
3041 free(__script_root);
3042 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003043 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003044 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003045 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003046
Robert Richter38efb532011-11-25 11:38:40 +01003047 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003048}
3049
Tom Zanussib5b87312010-11-10 08:16:51 -06003050static bool is_top_script(const char *script_path)
3051{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003052 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003053}
3054
3055static int has_required_arg(char *script_path)
3056{
3057 struct script_desc *desc;
3058 int n_args = 0;
3059 char *p;
3060
3061 desc = script_desc__new(NULL);
3062
3063 if (read_script_info(desc, script_path))
3064 goto out;
3065
3066 if (!desc->args)
3067 goto out;
3068
3069 for (p = desc->args; *p; p++)
3070 if (*p == '<')
3071 n_args++;
3072out:
3073 script_desc__delete(desc);
3074
3075 return n_args;
3076}
3077
David Ahernd54b1a92012-08-26 12:24:46 -06003078static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003079{
3080 char **__argv = malloc(sizeof(const char *) * argc);
3081
David Ahernd54b1a92012-08-26 12:24:46 -06003082 if (!__argv) {
3083 pr_err("malloc failed\n");
3084 return -1;
3085 }
3086
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003087 memcpy(__argv, argv, sizeof(const char *) * argc);
3088 argc = parse_options(argc, (const char **)__argv, record_options,
3089 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3090 free(__argv);
3091
David Ahernd54b1a92012-08-26 12:24:46 -06003092 system_wide = (argc == 0);
3093
3094 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003095}
3096
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003097static void script__setup_sample_type(struct perf_script *script)
3098{
3099 struct perf_session *session = script->session;
3100 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3101
3102 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3103 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003104 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003105 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003106 dwarf_callchain_users = true;
3107 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003108 callchain_param.record_mode = CALLCHAIN_LBR;
3109 else
3110 callchain_param.record_mode = CALLCHAIN_FP;
3111 }
3112}
3113
Jiri Olsa89f16882018-09-13 14:54:03 +02003114static int process_stat_round_event(struct perf_session *session,
3115 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003116{
3117 struct stat_round_event *round = &event->stat_round;
3118 struct perf_evsel *counter;
3119
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003120 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003121 perf_stat_process_counter(&stat_config, counter);
3122 process_stat(counter, round->time);
3123 }
3124
3125 process_stat_interval(round->time);
3126 return 0;
3127}
3128
Jiri Olsa89f16882018-09-13 14:54:03 +02003129static int process_stat_config_event(struct perf_session *session __maybe_unused,
3130 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003131{
3132 perf_event__read_stat_config(&stat_config, &event->stat_config);
3133 return 0;
3134}
3135
Jiri Olsacfc88742016-01-05 22:09:06 +01003136static int set_maps(struct perf_script *script)
3137{
3138 struct perf_evlist *evlist = script->session->evlist;
3139
3140 if (!script->cpus || !script->threads)
3141 return 0;
3142
3143 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3144 return -EINVAL;
3145
3146 perf_evlist__set_maps(evlist, script->cpus, script->threads);
3147
3148 if (perf_evlist__alloc_stats(evlist, true))
3149 return -ENOMEM;
3150
3151 script->allocated = true;
3152 return 0;
3153}
3154
3155static
Jiri Olsa89f16882018-09-13 14:54:03 +02003156int process_thread_map_event(struct perf_session *session,
3157 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003158{
Jiri Olsa89f16882018-09-13 14:54:03 +02003159 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003160 struct perf_script *script = container_of(tool, struct perf_script, tool);
3161
3162 if (script->threads) {
3163 pr_warning("Extra thread map event, ignoring.\n");
3164 return 0;
3165 }
3166
3167 script->threads = thread_map__new_event(&event->thread_map);
3168 if (!script->threads)
3169 return -ENOMEM;
3170
3171 return set_maps(script);
3172}
3173
3174static
Jiri Olsa89f16882018-09-13 14:54:03 +02003175int process_cpu_map_event(struct perf_session *session,
3176 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003177{
Jiri Olsa89f16882018-09-13 14:54:03 +02003178 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003179 struct perf_script *script = container_of(tool, struct perf_script, tool);
3180
3181 if (script->cpus) {
3182 pr_warning("Extra cpu map event, ignoring.\n");
3183 return 0;
3184 }
3185
3186 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3187 if (!script->cpus)
3188 return -ENOMEM;
3189
3190 return set_maps(script);
3191}
3192
Jiri Olsa89f16882018-09-13 14:54:03 +02003193static int process_feature_event(struct perf_session *session,
3194 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303195{
3196 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003197 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303198 return 0;
3199}
3200
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003201#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003202static int perf_script__process_auxtrace_info(struct perf_session *session,
3203 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003204{
Jiri Olsa89f16882018-09-13 14:54:03 +02003205 struct perf_tool *tool = session->tool;
3206
3207 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003208
3209 if (ret == 0) {
3210 struct perf_script *script = container_of(tool, struct perf_script, tool);
3211
3212 ret = perf_script__setup_per_event_dump(script);
3213 }
3214
3215 return ret;
3216}
3217#else
3218#define perf_script__process_auxtrace_info 0
3219#endif
3220
Andi Kleenb585ebd2018-09-20 11:05:36 -07003221static int parse_insn_trace(const struct option *opt __maybe_unused,
3222 const char *str __maybe_unused,
3223 int unset __maybe_unused)
3224{
3225 parse_output_fields(NULL, "+insn,-event,-period", 0);
3226 itrace_parse_synth_opts(opt, "i0ns", 0);
3227 nanosecs = true;
3228 return 0;
3229}
3230
3231static int parse_xed(const struct option *opt __maybe_unused,
3232 const char *str __maybe_unused,
3233 int unset __maybe_unused)
3234{
3235 force_pager("xed -F insn: -A -64 | less");
3236 return 0;
3237}
3238
Andi Kleend1b15522018-09-20 11:05:38 -07003239static int parse_call_trace(const struct option *opt __maybe_unused,
3240 const char *str __maybe_unused,
3241 int unset __maybe_unused)
3242{
3243 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3244 itrace_parse_synth_opts(opt, "cewp", 0);
3245 nanosecs = true;
3246 return 0;
3247}
3248
3249static int parse_callret_trace(const struct option *opt __maybe_unused,
3250 const char *str __maybe_unused,
3251 int unset __maybe_unused)
3252{
3253 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3254 itrace_parse_synth_opts(opt, "crewp", 0);
3255 nanosecs = true;
3256 return 0;
3257}
3258
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003259int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003260{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003261 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003262 bool header = false;
3263 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003264 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003265 char *rec_script_path = NULL;
3266 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003267 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003268 struct itrace_synth_opts itrace_synth_opts = {
3269 .set = false,
3270 .default_no_sample = true,
3271 };
Tom Zanussib5b87312010-11-10 08:16:51 -06003272 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003273 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003274 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003275 struct perf_script script = {
3276 .tool = {
3277 .sample = process_sample_event,
3278 .mmap = perf_event__process_mmap,
3279 .mmap2 = perf_event__process_mmap2,
3280 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303281 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003282 .exit = perf_event__process_exit,
3283 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003284 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003285 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003286 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303287 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003288 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003289 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003290 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003291 .auxtrace = perf_event__process_auxtrace,
3292 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003293 .stat = perf_event__process_stat_event,
3294 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003295 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003296 .thread_map = process_thread_map_event,
3297 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003298 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003299 .ordering_requires_timestamps = true,
3300 },
3301 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003302 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003303 .mode = PERF_DATA_MODE_READ,
3304 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003305 const struct option options[] = {
3306 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3307 "dump raw trace in ASCII"),
3308 OPT_INCR('v', "verbose", &verbose,
3309 "be more verbose (show symbol address, etc)"),
3310 OPT_BOOLEAN('L', "Latency", &latency_format,
3311 "show latency attributes (irqs/preemption disabled, etc)"),
3312 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3313 list_available_scripts),
3314 OPT_CALLBACK('s', "script", NULL, "name",
3315 "script file name (lang:script name, script name, or *)",
3316 parse_scriptname),
3317 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3318 "generate perf-script.xx script in specified language"),
3319 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3320 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3321 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003322 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3323 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003324 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3325 "file", "vmlinux pathname"),
3326 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3327 "file", "kallsyms pathname"),
3328 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3329 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003330 OPT_CALLBACK(0, "symfs", NULL, "directory",
3331 "Look for files with symbols relative to this directory",
3332 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003333 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003334 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003335 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003336 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003337 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303338 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3339 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
3340 "callindent,insn,insnlen,synth,phys_addr,metric,misc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003341 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003342 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3343 "system-wide collection from all CPUs"),
3344 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3345 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003346 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3347 "Decode instructions from itrace", parse_insn_trace),
3348 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3349 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003350 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3351 "Decode calls from from itrace", parse_call_trace),
3352 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3353 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003354 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3355 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003356 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3357 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003358 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3359 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3360 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003361 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3362 "only consider symbols in these pids"),
3363 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3364 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003365 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3366 "Set the maximum stack depth when parsing the callchain, "
3367 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003368 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003369 OPT_BOOLEAN('I', "show-info", &show_full_info,
3370 "display extended information from perf.data file"),
3371 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3372 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003373 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3374 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003375 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3376 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003377 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3378 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303379 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3380 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003381 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3382 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003383 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3384 "Show round events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003385 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3386 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003387 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003388 OPT_INTEGER(0, "max-blocks", &max_blocks,
3389 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03003390 OPT_BOOLEAN(0, "ns", &nanosecs,
3391 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003392 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003393 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003394 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003395 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3396 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003397 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3398 "Enable symbol demangling"),
3399 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3400 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003401 OPT_STRING(0, "time", &script.time_str, "str",
3402 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003403 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3404 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003405 OPT_END()
3406 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003407 const char * const script_subcommands[] = { "record", "report", NULL };
3408 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003409 "perf script [<options>]",
3410 "perf script [<options>] record <script> [<record-options>] <command>",
3411 "perf script [<options>] report <script> [script-args]",
3412 "perf script [<options>] <script> [<record-options>] <command>",
3413 "perf script [<options>] <top-script> [script-args]",
3414 NULL
3415 };
Tom Zanussi38752942009-12-15 02:53:39 -06003416
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003417 perf_set_singlethreaded();
3418
Tom Zanussib5b87312010-11-10 08:16:51 -06003419 setup_scripting();
3420
Yunlong Song40cae2b2015-03-18 21:35:54 +08003421 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003422 PARSE_OPT_STOP_AT_NON_OPTION);
3423
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003424 data.file.path = input_name;
3425 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003426
Tom Zanussib5b87312010-11-10 08:16:51 -06003427 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3428 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3429 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003430 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003431 }
3432
Tom Zanussib5b87312010-11-10 08:16:51 -06003433 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3434 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3435 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003436 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003437 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003438 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003439 return -1;
3440 }
Tom Zanussi38752942009-12-15 02:53:39 -06003441 }
3442
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003443 if (itrace_synth_opts.callchain &&
3444 itrace_synth_opts.callchain_sz > scripting_max_stack)
3445 scripting_max_stack = itrace_synth_opts.callchain_sz;
3446
Ben Hutchings44e668c2010-10-10 16:10:03 +01003447 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003448 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003449
Tom Zanussib5b87312010-11-10 08:16:51 -06003450 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003451 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003452 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003453 pid_t pid;
3454
Tom Zanussib5b87312010-11-10 08:16:51 -06003455 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3456 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3457
3458 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003459 usage_with_options_msg(script_usage, options,
3460 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003461 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003462 }
3463
Tom Zanussib5b87312010-11-10 08:16:51 -06003464 if (is_top_script(argv[0])) {
3465 rep_args = argc - 1;
3466 } else {
3467 int rec_args;
3468
3469 rep_args = has_required_arg(rep_script_path);
3470 rec_args = (argc - 1) - rep_args;
3471 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003472 usage_with_options_msg(script_usage, options,
3473 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003474 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003475 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003476 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003477 }
3478
3479 if (pipe(live_pipe) < 0) {
3480 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003481 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003482 }
3483
3484 pid = fork();
3485 if (pid < 0) {
3486 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003487 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003488 }
3489
3490 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003491 j = 0;
3492
Tom Zanussia0cccc22010-04-01 23:59:25 -05003493 dup2(live_pipe[1], 1);
3494 close(live_pipe[0]);
3495
Robert Richter317df652011-11-25 15:05:25 +01003496 if (is_top_script(argv[0])) {
3497 system_wide = true;
3498 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003499 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3500 err = -1;
3501 goto out;
3502 }
Robert Richter317df652011-11-25 15:05:25 +01003503 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003504
3505 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003506 if (!__argv) {
3507 pr_err("malloc failed\n");
3508 err = -ENOMEM;
3509 goto out;
3510 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003511
Tom Zanussib5b87312010-11-10 08:16:51 -06003512 __argv[j++] = "/bin/sh";
3513 __argv[j++] = rec_script_path;
3514 if (system_wide)
3515 __argv[j++] = "-a";
3516 __argv[j++] = "-q";
3517 __argv[j++] = "-o";
3518 __argv[j++] = "-";
3519 for (i = rep_args + 1; i < argc; i++)
3520 __argv[j++] = argv[i];
3521 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003522
3523 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003524 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003525 exit(-1);
3526 }
3527
3528 dup2(live_pipe[0], 0);
3529 close(live_pipe[1]);
3530
Tom Zanussib5b87312010-11-10 08:16:51 -06003531 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003532 if (!__argv) {
3533 pr_err("malloc failed\n");
3534 err = -ENOMEM;
3535 goto out;
3536 }
3537
Tom Zanussib5b87312010-11-10 08:16:51 -06003538 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003539 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003540 __argv[j++] = rep_script_path;
3541 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003542 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003543 __argv[j++] = "-i";
3544 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003545 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003546
3547 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003548 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003549 exit(-1);
3550 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003551
Tom Zanussib5b87312010-11-10 08:16:51 -06003552 if (rec_script_path)
3553 script_path = rec_script_path;
3554 if (rep_script_path)
3555 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003556
Tom Zanussib5b87312010-11-10 08:16:51 -06003557 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003558 j = 0;
3559
Robert Richter317df652011-11-25 15:05:25 +01003560 if (!rec_script_path)
3561 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003562 else if (!system_wide) {
3563 if (have_cmd(argc - 1, &argv[1]) != 0) {
3564 err = -1;
3565 goto out;
3566 }
3567 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003568
3569 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003570 if (!__argv) {
3571 pr_err("malloc failed\n");
3572 err = -ENOMEM;
3573 goto out;
3574 }
3575
Tom Zanussib5b87312010-11-10 08:16:51 -06003576 __argv[j++] = "/bin/sh";
3577 __argv[j++] = script_path;
3578 if (system_wide)
3579 __argv[j++] = "-a";
3580 for (i = 2; i < argc; i++)
3581 __argv[j++] = argv[i];
3582 __argv[j++] = NULL;
3583
3584 execvp("/bin/sh", (char **)__argv);
3585 free(__argv);
3586 exit(-1);
3587 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003588
Milian Wolffc1c9b962018-10-21 21:14:23 +02003589 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003590 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003591 use_browser = 0;
3592 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003593
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003594 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003595 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003596 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003597
Jiri Olsae90debd2013-12-09 11:02:50 +01003598 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003599 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003600 perf_session__fprintf_info(session, stdout, show_full_info);
3601 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003602 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003603 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003604 if (show_full_info)
3605 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003606
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003607 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003608 goto out_delete;
3609
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003610 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003611 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003612
Andi Kleen99f753f2018-09-20 11:05:39 -07003613 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3614 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003615 itrace_synth_opts.thread_stack = true;
3616
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003617 session->itrace_synth_opts = &itrace_synth_opts;
3618
Anton Blanchard5d67be92011-07-04 21:57:50 +10003619 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003620 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3621 if (err < 0)
3622 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003623 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003624 }
3625
David Ahern1424dc92011-03-09 22:23:28 -07003626 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003627 symbol_conf.use_callchain = true;
3628 else
3629 symbol_conf.use_callchain = false;
3630
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003631 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003632 tep_set_function_resolver(session->tevent.pevent,
3633 machine__resolve_kernel_addr,
3634 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003635 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003636 err = -1;
3637 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003638 }
3639
Tom Zanussi956ffd02009-11-25 01:15:46 -06003640 if (generate_script_lang) {
3641 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003642 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003643
David Ahern2c9e45f72011-03-17 10:03:21 -06003644 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003645 fprintf(stderr,
3646 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003647 err = -EINVAL;
3648 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003649 }
3650
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003651 input = open(data.file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003652 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003653 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003654 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003655 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003656 }
3657
3658 err = fstat(input, &perf_stat);
3659 if (err < 0) {
3660 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003661 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003662 }
3663
3664 if (!perf_stat.st_size) {
3665 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003666 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003667 }
3668
3669 scripting_ops = script_spec__lookup(generate_script_lang);
3670 if (!scripting_ops) {
3671 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003672 err = -ENOENT;
3673 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003674 }
3675
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003676 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003677 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003678 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003679 }
3680
3681 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003682 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003683 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003684 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003685 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003686 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003687 }
3688
David Ahern9cbdb702011-04-06 21:54:20 -06003689
3690 err = perf_session__check_output_opt(session);
3691 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003692 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003693
Jin Yaocc2ef582018-01-10 23:00:33 +08003694 script.ptime_range = perf_time__range_alloc(script.time_str,
3695 &script.range_size);
3696 if (!script.ptime_range) {
3697 err = -ENOMEM;
3698 goto out_delete;
3699 }
3700
David Aherna91f4c42016-11-29 10:15:43 -07003701 /* needs to be parsed after looking up reference time */
Jin Yao2ab046c2017-12-08 21:13:46 +08003702 if (perf_time__parse_str(script.ptime_range, script.time_str) != 0) {
3703 if (session->evlist->first_sample_time == 0 &&
3704 session->evlist->last_sample_time == 0) {
Jin Yao1e2778e2018-01-10 23:00:27 +08003705 pr_err("HINT: no first/last sample time found in perf data.\n"
3706 "Please use latest perf binary to execute 'perf record'\n"
3707 "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
Jin Yao2ab046c2017-12-08 21:13:46 +08003708 err = -EINVAL;
3709 goto out_delete;
3710 }
3711
3712 script.range_num = perf_time__percent_parse_str(
Jin Yaocc2ef582018-01-10 23:00:33 +08003713 script.ptime_range, script.range_size,
Jin Yao2ab046c2017-12-08 21:13:46 +08003714 script.time_str,
3715 session->evlist->first_sample_time,
3716 session->evlist->last_sample_time);
3717
3718 if (script.range_num < 0) {
3719 pr_err("Invalid time string\n");
3720 err = -EINVAL;
3721 goto out_delete;
3722 }
3723 } else {
3724 script.range_num = 1;
David Aherna91f4c42016-11-29 10:15:43 -07003725 }
3726
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003727 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003728
Adrian Hunterd445dd22014-08-15 22:08:37 +03003729 flush_scripting();
3730
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003731out_delete:
Jin Yaocc2ef582018-01-10 23:00:33 +08003732 zfree(&script.ptime_range);
3733
Jiri Olsacfc88742016-01-05 22:09:06 +01003734 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003735 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003736
3737 if (script_started)
3738 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003739out:
3740 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003741}