blob: 3728b50e52e20d0eb43780ec354d0c2242d6c414 [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
1076 */
1077 start = br->entries[0].to;
1078 end = sample->ip;
1079 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001080 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001081 if (len <= 0) {
1082 /* Print at least last IP if basic block did not work */
1083 len = grab_bb(buffer, sample->ip, sample->ip,
1084 machine, thread, &x.is64bit, &x.cpumode, false);
1085 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001086 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001087
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001088 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001089 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001090 if (PRINT_FIELD(SRCCODE))
1091 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001092 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001093 }
1094 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001095 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1096 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001097 if (ilen == 0)
1098 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001099 if (PRINT_FIELD(SRCCODE))
1100 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001101 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001102out:
1103 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001104}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001105
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001106static int perf_sample__fprintf_addr(struct perf_sample *sample,
1107 struct thread *thread,
1108 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001109{
1110 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001111 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001112
1113 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001114 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001115
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001116 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001117
1118 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001119 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001120 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001121 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001122 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001123 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001124 }
1125
1126 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001127 printed += fprintf(fp, " (");
1128 printed += map__fprintf_dsoname(al.map, fp);
1129 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001130 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001131out:
1132 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001133}
1134
Andi Kleen99f753f2018-09-20 11:05:39 -07001135static const char *resolve_branch_sym(struct perf_sample *sample,
1136 struct perf_evsel *evsel,
1137 struct thread *thread,
1138 struct addr_location *al,
1139 u64 *ip)
1140{
1141 struct addr_location addr_al;
1142 struct perf_event_attr *attr = &evsel->attr;
1143 const char *name = NULL;
1144
1145 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1146 if (sample_addr_correlates_sym(attr)) {
1147 thread__resolve(thread, &addr_al, sample);
1148 if (addr_al.sym)
1149 name = addr_al.sym->name;
1150 else
1151 *ip = sample->addr;
1152 } else {
1153 *ip = sample->addr;
1154 }
1155 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1156 if (al->sym)
1157 name = al->sym->name;
1158 else
1159 *ip = sample->ip;
1160 }
1161 return name;
1162}
1163
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001164static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1165 struct perf_evsel *evsel,
1166 struct thread *thread,
1167 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001168{
1169 struct perf_event_attr *attr = &evsel->attr;
1170 size_t depth = thread_stack__depth(thread);
Adrian Huntere2167082016-06-23 16:40:58 +03001171 const char *name = NULL;
1172 static int spacing;
1173 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001174 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001175 u64 ip = 0;
1176
1177 /*
1178 * The 'return' has already been popped off the stack so the depth has
1179 * to be adjusted to match the 'call'.
1180 */
1181 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1182 depth += 1;
1183
Andi Kleen99f753f2018-09-20 11:05:39 -07001184 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001185
Andi Kleena78cdee2018-09-18 05:32:10 -07001186 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1187 dlen += fprintf(fp, "(");
1188 dlen += map__fprintf_dsoname(al->map, fp);
1189 dlen += fprintf(fp, ")\t");
1190 }
1191
Adrian Huntere2167082016-06-23 16:40:58 +03001192 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001193 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001194 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001195 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001196
1197 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001198 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001199
1200 /*
1201 * Try to keep the output length from changing frequently so that the
1202 * output lines up more nicely.
1203 */
1204 if (len > spacing || (len && len < spacing - 52))
1205 spacing = round_up(len + 4, 32);
1206
1207 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001208 len += fprintf(fp, "%*s", spacing - len, "");
1209
Andi Kleena78cdee2018-09-18 05:32:10 -07001210 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001211}
1212
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001213static int perf_sample__fprintf_insn(struct perf_sample *sample,
1214 struct perf_event_attr *attr,
1215 struct thread *thread,
1216 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001217{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001218 int printed = 0;
1219
Andi Kleen224e2c92016-10-07 16:42:27 +03001220 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001221 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen224e2c92016-10-07 16:42:27 +03001222 if (PRINT_FIELD(INSN)) {
1223 int i;
1224
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001225 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001226 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001227 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001228 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001229 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001230 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1231
1232 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001233}
1234
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001235static int perf_sample__fprintf_bts(struct perf_sample *sample,
1236 struct perf_evsel *evsel,
1237 struct thread *thread,
1238 struct addr_location *al,
1239 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001240{
1241 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001242 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001243 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001244 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001245
Adrian Huntere2167082016-06-23 16:40:58 +03001246 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001247 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001248
Akihiro Nagai95582592012-01-30 13:43:09 +09001249 /* print branch_from information */
1250 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001251 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001252 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001253
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001254 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001255 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001256 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001257 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001258
1259 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001260 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001261 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001262 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001263 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001264 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001265 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001266 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001267
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001268 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001269 }
1270
Akihiro Nagai95582592012-01-30 13:43:09 +09001271 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001272 if (PRINT_FIELD(ADDR) ||
1273 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001274 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001275 printed += fprintf(fp, " => ");
1276 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001277 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001278
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001279 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001280 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001281
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001282 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001283 printed += fprintf(fp, "\n");
1284 if (PRINT_FIELD(SRCCODE)) {
1285 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1286 &thread->srccode_state);
1287 if (ret) {
1288 printed += ret;
1289 printed += printf("\n");
1290 }
1291 }
1292 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001293}
1294
Adrian Hunter055cd332016-06-23 16:40:56 +03001295static struct {
1296 u32 flags;
1297 const char *name;
1298} sample_flags[] = {
1299 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1300 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1301 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1302 {PERF_IP_FLAG_BRANCH, "jmp"},
1303 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1304 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1305 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1306 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1307 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1308 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1309 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1310 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1311 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1312 {0, NULL}
1313};
1314
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001315static const char *sample_flags_to_name(u32 flags)
1316{
1317 int i;
1318
1319 for (i = 0; sample_flags[i].name ; i++) {
1320 if (sample_flags[i].flags == flags)
1321 return sample_flags[i].name;
1322 }
1323
1324 return NULL;
1325}
1326
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001327static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001328{
1329 const char *chars = PERF_IP_FLAG_CHARS;
1330 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001331 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1332 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001333 char str[33];
1334 int i, pos = 0;
1335
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001336 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1337 if (name)
1338 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1339
1340 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1341 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1342 if (name)
1343 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1344 }
1345
1346 if (flags & PERF_IP_FLAG_TRACE_END) {
1347 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1348 if (name)
1349 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001350 }
1351
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001352 for (i = 0; i < n; i++, flags >>= 1) {
1353 if (flags & 1)
1354 str[pos++] = chars[i];
1355 }
1356 for (; i < 32; i++, flags >>= 1) {
1357 if (flags & 1)
1358 str[pos++] = '?';
1359 }
1360 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001361
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001362 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001363}
1364
Wang Nan30372f02016-02-24 11:20:45 +00001365struct printer_data {
1366 int line_no;
1367 bool hit_nul;
1368 bool is_printable;
1369};
1370
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001371static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1372 unsigned int val,
1373 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001374{
1375 unsigned char ch = (unsigned char)val;
1376 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001377 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001378
1379 switch (op) {
1380 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001381 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001382 break;
1383 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001384 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001385 " ");
1386 break;
1387 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001388 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001389 break;
1390 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001391 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001392 break;
1393 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001394 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001395 break;
1396 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001397 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001398 break;
1399 case BINARY_PRINT_CHAR_DATA:
1400 if (printer_data->hit_nul && ch)
1401 printer_data->is_printable = false;
1402
1403 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001404 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001405
1406 if (!printer_data->is_printable)
1407 break;
1408
1409 if (ch == '\0')
1410 printer_data->hit_nul = true;
1411 else
1412 printer_data->is_printable = false;
1413 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001414 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001415 }
1416 break;
1417 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001418 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001419 break;
1420 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001421 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001422 printer_data->line_no++;
1423 break;
1424 case BINARY_PRINT_DATA_END:
1425 default:
1426 break;
1427 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001428
1429 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001430}
1431
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001432static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001433{
1434 unsigned int nr_bytes = sample->raw_size;
1435 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001436 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1437 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001438
1439 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001440 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1441
1442 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001443}
1444
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001445static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001446{
1447 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001448 return fprintf(fp, "%*s", spacing - len, "");
1449
1450 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001451}
1452
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001453static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001454{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001455 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001456}
1457
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001458static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001459{
1460 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1461 int len;
1462
1463 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001464 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001465
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001466 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001467 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001468 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001469}
1470
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001471static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001472{
1473 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1474 int len;
1475
1476 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001477 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001478
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001479 len = fprintf(fp, " hints: %#x extensions: %#x ",
1480 data->hints, data->extensions);
1481 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001482}
1483
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001484static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001485{
1486 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1487 int len;
1488
1489 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001490 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001491
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001492 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1493 data->hw, data->cstate, data->subcstate);
1494 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001495}
1496
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001497static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001498{
1499 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1500 int len;
1501
1502 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001503 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001504
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001505 len = fprintf(fp, " IP: %u ", data->ip);
1506 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001507}
1508
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001509static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001510{
1511 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1512 int len;
1513
1514 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001515 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001516
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001517 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001518 data->deepest_cstate, data->last_cstate,
1519 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001520 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001521}
1522
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001523static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001524{
1525 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1526 unsigned int percent, freq;
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
1532 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001533 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001534 if (data->max_nonturbo) {
1535 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001536 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001537 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001538 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001539}
1540
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001541static int perf_sample__fprintf_synth(struct perf_sample *sample,
1542 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001543{
1544 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001545 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001546 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001547 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001548 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001549 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001550 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001551 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001552 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001553 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001554 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001555 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001556 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001557 default:
1558 break;
1559 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001560
1561 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001562}
1563
Jiri Olsa809e9422015-11-26 18:55:21 +01001564struct perf_script {
1565 struct perf_tool tool;
1566 struct perf_session *session;
1567 bool show_task_events;
1568 bool show_mmap_events;
1569 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301570 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001571 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001572 bool show_round_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001573 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001574 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001575 struct cpu_map *cpus;
1576 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001577 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001578 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001579 struct perf_time_interval *ptime_range;
1580 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001581 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001582};
1583
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001584static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1585{
1586 struct perf_evsel *evsel;
1587 int max = 0;
1588
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001589 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001590 int len = strlen(perf_evsel__name(evsel));
1591
1592 max = MAX(len, max);
1593 }
1594
1595 return max;
1596}
1597
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001598static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001599{
1600 struct mem_info mi = { .data_src.val = data_src };
1601 char decode[100];
1602 char out[100];
1603 static int maxlen;
1604 int len;
1605
1606 perf_script__meminfo_scnprintf(decode, 100, &mi);
1607
1608 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1609 if (maxlen < len)
1610 maxlen = len;
1611
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001612 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001613}
1614
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001615struct metric_ctx {
1616 struct perf_sample *sample;
1617 struct thread *thread;
1618 struct perf_evsel *evsel;
1619 FILE *fp;
1620};
1621
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001622static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1623 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001624 const char *fmt,
1625 const char *unit, double val)
1626{
1627 struct metric_ctx *mctx = ctx;
1628
1629 if (!fmt)
1630 return;
1631 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001632 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001633 fputs("\tmetric: ", mctx->fp);
1634 if (color)
1635 color_fprintf(mctx->fp, color, fmt, val);
1636 else
1637 printf(fmt, val);
1638 fprintf(mctx->fp, " %s\n", unit);
1639}
1640
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001641static void script_new_line(struct perf_stat_config *config __maybe_unused,
1642 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001643{
1644 struct metric_ctx *mctx = ctx;
1645
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}
1650
1651static void perf_sample__fprint_metric(struct perf_script *script,
1652 struct thread *thread,
1653 struct perf_evsel *evsel,
1654 struct perf_sample *sample,
1655 FILE *fp)
1656{
1657 struct perf_stat_output_ctx ctx = {
1658 .print_metric = script_print_metric,
1659 .new_line = script_new_line,
1660 .ctx = &(struct metric_ctx) {
1661 .sample = sample,
1662 .thread = thread,
1663 .evsel = evsel,
1664 .fp = fp,
1665 },
1666 .force_header = false,
1667 };
1668 struct perf_evsel *ev2;
1669 static bool init;
1670 u64 val;
1671
1672 if (!init) {
1673 perf_stat__init_shadow_stats();
1674 init = true;
1675 }
1676 if (!evsel->stats)
1677 perf_evlist__alloc_stats(script->session->evlist, false);
1678 if (evsel_script(evsel->leader)->gnum++ == 0)
1679 perf_stat__reset_shadow_stats();
1680 val = sample->period * evsel->scale;
1681 perf_stat__update_shadow_stats(evsel,
1682 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001683 sample->cpu,
1684 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001685 evsel_script(evsel)->val = val;
1686 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1687 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001688 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001689 evsel_script(ev2)->val,
1690 sample->cpu,
1691 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001692 NULL,
1693 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001694 }
1695 evsel_script(evsel->leader)->gnum = 0;
1696 }
1697}
1698
Andi Kleen99f753f2018-09-20 11:05:39 -07001699static bool show_event(struct perf_sample *sample,
1700 struct perf_evsel *evsel,
1701 struct thread *thread,
1702 struct addr_location *al)
1703{
1704 int depth = thread_stack__depth(thread);
1705
1706 if (!symbol_conf.graph_function)
1707 return true;
1708
1709 if (thread->filter) {
1710 if (depth <= thread->filter_entry_depth) {
1711 thread->filter = false;
1712 return false;
1713 }
1714 return true;
1715 } else {
1716 const char *s = symbol_conf.graph_function;
1717 u64 ip;
1718 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1719 &ip);
1720 unsigned nlen;
1721
1722 if (!name)
1723 return false;
1724 nlen = strlen(name);
1725 while (*s) {
1726 unsigned len = strcspn(s, ",");
1727 if (nlen == len && !strncmp(name, s, len)) {
1728 thread->filter = true;
1729 thread->filter_entry_depth = depth;
1730 return true;
1731 }
1732 s += len;
1733 if (*s == ',')
1734 s++;
1735 }
1736 return false;
1737 }
1738}
1739
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001740static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001741 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001742 struct addr_location *al,
1743 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001744{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001745 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001746 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001747 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001748 struct perf_evsel_script *es = evsel->priv;
1749 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001750
Adrian Hunter14057202017-06-21 13:17:19 +03001751 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001752 return;
1753
Andi Kleen99f753f2018-09-20 11:05:39 -07001754 if (!show_event(sample, evsel, thread, al))
1755 return;
1756
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001757 ++es->samples;
1758
Jiri Olsa28a0b392018-01-07 17:03:52 +01001759 perf_sample__fprintf_start(sample, thread, evsel,
1760 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001761
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001762 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001763 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001764
Namhyung Kime944d3d2013-11-18 14:34:52 +09001765 if (PRINT_FIELD(EVNAME)) {
1766 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001767
1768 if (!script->name_width)
1769 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1770
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001771 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001772 }
1773
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001774 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001775 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001776
Akihiro Nagai95582592012-01-30 13:43:09 +09001777 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001778 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001779 return;
1780 }
1781
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001782 if (PRINT_FIELD(TRACE)) {
1783 event_format__fprintf(evsel->tp_format, sample->cpu,
1784 sample->raw_data, sample->raw_size, fp);
1785 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001786
1787 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001788 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001789
David Ahern7cec0922011-05-30 13:08:23 -06001790 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001791 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001792
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001793 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001794 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001795
1796 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001797 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001798
David Ahern787bef12011-05-27 14:28:43 -06001799 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001800 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001801
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001802 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001803 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001804 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001805 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001806
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001807 fputc(cursor ? '\n' : ' ', fp);
1808 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001809 }
1810
Stephane Eranianfc36f942015-08-31 18:41:10 +02001811 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001812 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001813
Andi Kleenb1491ac2017-09-05 11:40:57 -07001814 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001815 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001816
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001817 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001818 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001819 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001820 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001821 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001822 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001823
Wang Nan30372f02016-02-24 11:20:45 +00001824 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001825 perf_sample__fprintf_bpf_output(sample, fp);
1826 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001827
1828 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001829 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1830 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001831
Andi Kleendd2e18e2018-12-03 16:18:48 -08001832 if (PRINT_FIELD(SRCCODE)) {
1833 if (map__fprintf_srccode(al->map, al->addr, stdout,
1834 &thread->srccode_state))
1835 printf("\n");
1836 }
1837
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001838 if (PRINT_FIELD(METRIC))
1839 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001840
1841 if (verbose)
1842 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001843}
1844
Tom Zanussi956ffd02009-11-25 01:15:46 -06001845static struct scripting_ops *scripting_ops;
1846
Jiri Olsa36e33c52016-01-06 11:49:56 +01001847static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1848{
1849 int nthreads = thread_map__nr(counter->threads);
1850 int ncpus = perf_evsel__nr_cpus(counter);
1851 int cpu, thread;
1852 static int header_printed;
1853
1854 if (counter->system_wide)
1855 nthreads = 1;
1856
1857 if (!header_printed) {
1858 printf("%3s %8s %15s %15s %15s %15s %s\n",
1859 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1860 header_printed = 1;
1861 }
1862
1863 for (thread = 0; thread < nthreads; thread++) {
1864 for (cpu = 0; cpu < ncpus; cpu++) {
1865 struct perf_counts_values *counts;
1866
1867 counts = perf_counts(counter->counts, cpu, thread);
1868
1869 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1870 counter->cpus->map[cpu],
1871 thread_map__pid(counter->threads, thread),
1872 counts->val,
1873 counts->ena,
1874 counts->run,
1875 tstamp,
1876 perf_evsel__name(counter));
1877 }
1878 }
1879}
1880
Jiri Olsae099eba2016-01-05 22:09:09 +01001881static void process_stat(struct perf_evsel *counter, u64 tstamp)
1882{
1883 if (scripting_ops && scripting_ops->process_stat)
1884 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001885 else
1886 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001887}
1888
1889static void process_stat_interval(u64 tstamp)
1890{
1891 if (scripting_ops && scripting_ops->process_stat_interval)
1892 scripting_ops->process_stat_interval(tstamp);
1893}
1894
Tom Zanussi956ffd02009-11-25 01:15:46 -06001895static void setup_scripting(void)
1896{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001897 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001898 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001899}
1900
Adrian Hunterd445dd22014-08-15 22:08:37 +03001901static int flush_scripting(void)
1902{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001903 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001904}
1905
Tom Zanussi956ffd02009-11-25 01:15:46 -06001906static int cleanup_scripting(void)
1907{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001908 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001909
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001910 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001911}
1912
Jiri Olsa809e9422015-11-26 18:55:21 +01001913static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001914 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001915 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001916 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001917 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001918{
Jiri Olsa809e9422015-11-26 18:55:21 +01001919 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001920 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001921
Jin Yao2ab046c2017-12-08 21:13:46 +08001922 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1923 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001924 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001925 }
David Aherna91f4c42016-11-29 10:15:43 -07001926
David Ahern1424dc92011-03-09 22:23:28 -07001927 if (debug_mode) {
1928 if (sample->time < last_timestamp) {
1929 pr_err("Samples misordered, previous: %" PRIu64
1930 " this: %" PRIu64 "\n", last_timestamp,
1931 sample->time);
1932 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001933 }
David Ahern1424dc92011-03-09 22:23:28 -07001934 last_timestamp = sample->time;
1935 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001936 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001937
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001938 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001939 pr_err("problem processing %d event, skipping it.\n",
1940 event->header.type);
1941 return -1;
1942 }
1943
1944 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001945 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001946
Anton Blanchard5d67be92011-07-04 21:57:50 +10001947 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001948 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001949
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001950 if (scripting_ops)
1951 scripting_ops->process_event(event, sample, evsel, &al);
1952 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001953 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001954
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001955out_put:
1956 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001957 return 0;
1958}
1959
Adrian Hunter7ea95722013-11-01 15:51:30 +02001960static int process_attr(struct perf_tool *tool, union perf_event *event,
1961 struct perf_evlist **pevlist)
1962{
1963 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1964 struct perf_evlist *evlist;
1965 struct perf_evsel *evsel, *pos;
1966 int err;
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301967 static struct perf_evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001968
1969 err = perf_event__process_attr(tool, event, pevlist);
1970 if (err)
1971 return err;
1972
1973 evlist = *pevlist;
1974 evsel = perf_evlist__last(*pevlist);
1975
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301976 if (!evsel->priv) {
1977 if (scr->per_event_dump) {
1978 evsel->priv = perf_evsel_script__new(evsel,
1979 scr->session->data);
1980 } else {
1981 es = zalloc(sizeof(*es));
1982 if (!es)
1983 return -ENOMEM;
1984 es->fp = stdout;
1985 evsel->priv = es;
1986 }
1987 }
1988
Adrian Hunter14057202017-06-21 13:17:19 +03001989 if (evsel->attr.type >= PERF_TYPE_MAX &&
1990 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001991 return 0;
1992
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001993 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001994 if (pos->attr.type == evsel->attr.type && pos != evsel)
1995 return 0;
1996 }
1997
1998 set_print_ip_opts(&evsel->attr);
1999
Jiri Olsad2b5a312015-10-16 12:41:25 +02002000 if (evsel->attr.sample_type)
2001 err = perf_evsel__check_attr(evsel, scr->session);
2002
2003 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002004}
2005
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002006static int process_comm_event(struct perf_tool *tool,
2007 union perf_event *event,
2008 struct perf_sample *sample,
2009 struct machine *machine)
2010{
2011 struct thread *thread;
2012 struct perf_script *script = container_of(tool, struct perf_script, tool);
2013 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002014 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002015 int ret = -1;
2016
2017 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2018 if (thread == NULL) {
2019 pr_debug("problem processing COMM event, skipping it.\n");
2020 return -1;
2021 }
2022
2023 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2024 goto out;
2025
2026 if (!evsel->attr.sample_id_all) {
2027 sample->cpu = 0;
2028 sample->time = 0;
2029 sample->tid = event->comm.tid;
2030 sample->pid = event->comm.pid;
2031 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002032 perf_sample__fprintf_start(sample, thread, evsel,
2033 PERF_RECORD_COMM, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002034 perf_event__fprintf(event, stdout);
2035 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002036out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002037 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002038 return ret;
2039}
2040
Hari Bathini96a44bb2017-03-08 02:12:06 +05302041static int process_namespaces_event(struct perf_tool *tool,
2042 union perf_event *event,
2043 struct perf_sample *sample,
2044 struct machine *machine)
2045{
2046 struct thread *thread;
2047 struct perf_script *script = container_of(tool, struct perf_script, tool);
2048 struct perf_session *session = script->session;
2049 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2050 int ret = -1;
2051
2052 thread = machine__findnew_thread(machine, event->namespaces.pid,
2053 event->namespaces.tid);
2054 if (thread == NULL) {
2055 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2056 return -1;
2057 }
2058
2059 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2060 goto out;
2061
2062 if (!evsel->attr.sample_id_all) {
2063 sample->cpu = 0;
2064 sample->time = 0;
2065 sample->tid = event->namespaces.tid;
2066 sample->pid = event->namespaces.pid;
2067 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002068 perf_sample__fprintf_start(sample, thread, evsel,
2069 PERF_RECORD_NAMESPACES, stdout);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302070 perf_event__fprintf(event, stdout);
2071 ret = 0;
2072out:
2073 thread__put(thread);
2074 return ret;
2075}
2076
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002077static int process_fork_event(struct perf_tool *tool,
2078 union perf_event *event,
2079 struct perf_sample *sample,
2080 struct machine *machine)
2081{
2082 struct thread *thread;
2083 struct perf_script *script = container_of(tool, struct perf_script, tool);
2084 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002085 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002086
2087 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2088 return -1;
2089
2090 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2091 if (thread == NULL) {
2092 pr_debug("problem processing FORK event, skipping it.\n");
2093 return -1;
2094 }
2095
2096 if (!evsel->attr.sample_id_all) {
2097 sample->cpu = 0;
2098 sample->time = event->fork.time;
2099 sample->tid = event->fork.tid;
2100 sample->pid = event->fork.pid;
2101 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002102 perf_sample__fprintf_start(sample, thread, evsel,
2103 PERF_RECORD_FORK, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002104 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002105 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002106
2107 return 0;
2108}
2109static int process_exit_event(struct perf_tool *tool,
2110 union perf_event *event,
2111 struct perf_sample *sample,
2112 struct machine *machine)
2113{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002114 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002115 struct thread *thread;
2116 struct perf_script *script = container_of(tool, struct perf_script, tool);
2117 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002118 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002119
2120 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2121 if (thread == NULL) {
2122 pr_debug("problem processing EXIT event, skipping it.\n");
2123 return -1;
2124 }
2125
2126 if (!evsel->attr.sample_id_all) {
2127 sample->cpu = 0;
2128 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002129 sample->tid = event->fork.tid;
2130 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002131 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002132 perf_sample__fprintf_start(sample, thread, evsel,
2133 PERF_RECORD_EXIT, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002134 perf_event__fprintf(event, stdout);
2135
2136 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002137 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002138
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002139 thread__put(thread);
2140 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002141}
2142
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002143static int process_mmap_event(struct perf_tool *tool,
2144 union perf_event *event,
2145 struct perf_sample *sample,
2146 struct machine *machine)
2147{
2148 struct thread *thread;
2149 struct perf_script *script = container_of(tool, struct perf_script, tool);
2150 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002151 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002152
2153 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2154 return -1;
2155
2156 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2157 if (thread == NULL) {
2158 pr_debug("problem processing MMAP event, skipping it.\n");
2159 return -1;
2160 }
2161
2162 if (!evsel->attr.sample_id_all) {
2163 sample->cpu = 0;
2164 sample->time = 0;
2165 sample->tid = event->mmap.tid;
2166 sample->pid = event->mmap.pid;
2167 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002168 perf_sample__fprintf_start(sample, thread, evsel,
2169 PERF_RECORD_MMAP, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002170 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002171 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002172 return 0;
2173}
2174
2175static int process_mmap2_event(struct perf_tool *tool,
2176 union perf_event *event,
2177 struct perf_sample *sample,
2178 struct machine *machine)
2179{
2180 struct thread *thread;
2181 struct perf_script *script = container_of(tool, struct perf_script, tool);
2182 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002183 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002184
2185 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2186 return -1;
2187
2188 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2189 if (thread == NULL) {
2190 pr_debug("problem processing MMAP2 event, skipping it.\n");
2191 return -1;
2192 }
2193
2194 if (!evsel->attr.sample_id_all) {
2195 sample->cpu = 0;
2196 sample->time = 0;
2197 sample->tid = event->mmap2.tid;
2198 sample->pid = event->mmap2.pid;
2199 }
Jiri Olsa28a0b392018-01-07 17:03:52 +01002200 perf_sample__fprintf_start(sample, thread, evsel,
2201 PERF_RECORD_MMAP2, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002202 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002203 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002204 return 0;
2205}
2206
Adrian Hunter7c148982015-07-21 12:44:06 +03002207static int process_switch_event(struct perf_tool *tool,
2208 union perf_event *event,
2209 struct perf_sample *sample,
2210 struct machine *machine)
2211{
2212 struct thread *thread;
2213 struct perf_script *script = container_of(tool, struct perf_script, tool);
2214 struct perf_session *session = script->session;
2215 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2216
2217 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2218 return -1;
2219
2220 thread = machine__findnew_thread(machine, sample->pid,
2221 sample->tid);
2222 if (thread == NULL) {
2223 pr_debug("problem processing SWITCH event, skipping it.\n");
2224 return -1;
2225 }
2226
Jiri Olsa28a0b392018-01-07 17:03:52 +01002227 perf_sample__fprintf_start(sample, thread, evsel,
2228 PERF_RECORD_SWITCH, stdout);
Adrian Hunter7c148982015-07-21 12:44:06 +03002229 perf_event__fprintf(event, stdout);
2230 thread__put(thread);
2231 return 0;
2232}
2233
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002234static int
2235process_lost_event(struct perf_tool *tool,
2236 union perf_event *event,
2237 struct perf_sample *sample,
2238 struct machine *machine)
2239{
2240 struct perf_script *script = container_of(tool, struct perf_script, tool);
2241 struct perf_session *session = script->session;
2242 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2243 struct thread *thread;
2244
2245 thread = machine__findnew_thread(machine, sample->pid,
2246 sample->tid);
2247 if (thread == NULL)
2248 return -1;
2249
2250 perf_sample__fprintf_start(sample, thread, evsel,
2251 PERF_RECORD_LOST, stdout);
2252 perf_event__fprintf(event, stdout);
2253 thread__put(thread);
2254 return 0;
2255}
2256
Jiri Olsa3233b372018-02-06 19:17:59 +01002257static int
2258process_finished_round_event(struct perf_tool *tool __maybe_unused,
2259 union perf_event *event,
2260 struct ordered_events *oe __maybe_unused)
2261
2262{
2263 perf_event__fprintf(event, stdout);
2264 return 0;
2265}
2266
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002267static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002268{
2269 session_done = 1;
2270}
2271
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002272static void perf_script__fclose_per_event_dump(struct perf_script *script)
2273{
2274 struct perf_evlist *evlist = script->session->evlist;
2275 struct perf_evsel *evsel;
2276
2277 evlist__for_each_entry(evlist, evsel) {
2278 if (!evsel->priv)
2279 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002280 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002281 evsel->priv = NULL;
2282 }
2283}
2284
2285static int perf_script__fopen_per_event_dump(struct perf_script *script)
2286{
2287 struct perf_evsel *evsel;
2288
2289 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002290 /*
2291 * Already setup? I.e. we may be called twice in cases like
2292 * Intel PT, one for the intel_pt// and dummy events, then
2293 * for the evsels syntheized from the auxtrace info.
2294 *
2295 * Ses perf_script__process_auxtrace_info.
2296 */
2297 if (evsel->priv != NULL)
2298 continue;
2299
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002300 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002301 if (evsel->priv == NULL)
2302 goto out_err_fclose;
2303 }
2304
2305 return 0;
2306
2307out_err_fclose:
2308 perf_script__fclose_per_event_dump(script);
2309 return -1;
2310}
2311
2312static int perf_script__setup_per_event_dump(struct perf_script *script)
2313{
2314 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002315 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002316
2317 if (script->per_event_dump)
2318 return perf_script__fopen_per_event_dump(script);
2319
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002320 es_stdout.fp = stdout;
2321
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002322 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002323 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002324
2325 return 0;
2326}
2327
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002328static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2329{
2330 struct perf_evsel *evsel;
2331
2332 evlist__for_each_entry(script->session->evlist, evsel) {
2333 struct perf_evsel_script *es = evsel->priv;
2334
2335 perf_evsel_script__fprintf(es, stdout);
2336 perf_evsel_script__delete(es);
2337 evsel->priv = NULL;
2338 }
2339}
2340
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002341static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002342{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002343 int ret;
2344
Tom Zanussic239da32010-04-01 23:59:18 -05002345 signal(SIGINT, sig_handler);
2346
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002347 /* override event processing functions */
2348 if (script->show_task_events) {
2349 script->tool.comm = process_comm_event;
2350 script->tool.fork = process_fork_event;
2351 script->tool.exit = process_exit_event;
2352 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002353 if (script->show_mmap_events) {
2354 script->tool.mmap = process_mmap_event;
2355 script->tool.mmap2 = process_mmap2_event;
2356 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002357 if (script->show_switch_events)
2358 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302359 if (script->show_namespace_events)
2360 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002361 if (script->show_lost_events)
2362 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002363 if (script->show_round_events) {
2364 script->tool.ordered_events = false;
2365 script->tool.finished_round = process_finished_round_event;
2366 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002367
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002368 if (perf_script__setup_per_event_dump(script)) {
2369 pr_err("Couldn't create the per event dump files\n");
2370 return -1;
2371 }
2372
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002373 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002374
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002375 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002376 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002377
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002378 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002379 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002380
2381 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002382}
2383
Tom Zanussi956ffd02009-11-25 01:15:46 -06002384struct script_spec {
2385 struct list_head node;
2386 struct scripting_ops *ops;
2387 char spec[0];
2388};
2389
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002390static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002391
2392static struct script_spec *script_spec__new(const char *spec,
2393 struct scripting_ops *ops)
2394{
2395 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2396
2397 if (s != NULL) {
2398 strcpy(s->spec, spec);
2399 s->ops = ops;
2400 }
2401
2402 return s;
2403}
2404
Tom Zanussi956ffd02009-11-25 01:15:46 -06002405static void script_spec__add(struct script_spec *s)
2406{
2407 list_add_tail(&s->node, &script_specs);
2408}
2409
2410static struct script_spec *script_spec__find(const char *spec)
2411{
2412 struct script_spec *s;
2413
2414 list_for_each_entry(s, &script_specs, node)
2415 if (strcasecmp(s->spec, spec) == 0)
2416 return s;
2417 return NULL;
2418}
2419
Tom Zanussi956ffd02009-11-25 01:15:46 -06002420int script_spec_register(const char *spec, struct scripting_ops *ops)
2421{
2422 struct script_spec *s;
2423
2424 s = script_spec__find(spec);
2425 if (s)
2426 return -1;
2427
Taeung Song8560bae2016-02-26 00:13:10 +09002428 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002429 if (!s)
2430 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002431 else
2432 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002433
2434 return 0;
2435}
2436
2437static struct scripting_ops *script_spec__lookup(const char *spec)
2438{
2439 struct script_spec *s = script_spec__find(spec);
2440 if (!s)
2441 return NULL;
2442
2443 return s->ops;
2444}
2445
2446static void list_available_languages(void)
2447{
2448 struct script_spec *s;
2449
2450 fprintf(stderr, "\n");
2451 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002452 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002453
2454 list_for_each_entry(s, &script_specs, node)
2455 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2456
2457 fprintf(stderr, "\n");
2458}
2459
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002460static int parse_scriptname(const struct option *opt __maybe_unused,
2461 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002462{
2463 char spec[PATH_MAX];
2464 const char *script, *ext;
2465 int len;
2466
Tom Zanussif526d682010-01-27 02:27:52 -06002467 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002468 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002469 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002470 }
2471
2472 script = strchr(str, ':');
2473 if (script) {
2474 len = script - str;
2475 if (len >= PATH_MAX) {
2476 fprintf(stderr, "invalid language specifier");
2477 return -1;
2478 }
2479 strncpy(spec, str, len);
2480 spec[len] = '\0';
2481 scripting_ops = script_spec__lookup(spec);
2482 if (!scripting_ops) {
2483 fprintf(stderr, "invalid language specifier");
2484 return -1;
2485 }
2486 script++;
2487 } else {
2488 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002489 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002490 if (!ext) {
2491 fprintf(stderr, "invalid script extension");
2492 return -1;
2493 }
2494 scripting_ops = script_spec__lookup(++ext);
2495 if (!scripting_ops) {
2496 fprintf(stderr, "invalid script extension");
2497 return -1;
2498 }
2499 }
2500
2501 script_name = strdup(script);
2502
2503 return 0;
2504}
2505
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002506static int parse_output_fields(const struct option *opt __maybe_unused,
2507 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002508{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002509 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002510 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002511 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002512 int rc = 0;
2513 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002514 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002515 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002516
2517 if (!str)
2518 return -ENOMEM;
2519
David Ahern2c9e45f72011-03-17 10:03:21 -06002520 /* first word can state for which event type the user is specifying
2521 * the fields. If no type exists, the specified fields apply to all
2522 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002523 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002524 tok = strchr(str, ':');
2525 if (tok) {
2526 *tok = '\0';
2527 tok++;
2528 if (!strcmp(str, "hw"))
2529 type = PERF_TYPE_HARDWARE;
2530 else if (!strcmp(str, "sw"))
2531 type = PERF_TYPE_SOFTWARE;
2532 else if (!strcmp(str, "trace"))
2533 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002534 else if (!strcmp(str, "raw"))
2535 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002536 else if (!strcmp(str, "break"))
2537 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002538 else if (!strcmp(str, "synth"))
2539 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002540 else {
2541 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002542 rc = -EINVAL;
2543 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002544 }
2545
2546 if (output[type].user_set)
2547 pr_warning("Overriding previous field request for %s events.\n",
2548 event_type(type));
2549
2550 output[type].fields = 0;
2551 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002552 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002553
2554 } else {
2555 tok = str;
2556 if (strlen(str) == 0) {
2557 fprintf(stderr,
2558 "Cannot set fields to 'none' for all event types.\n");
2559 rc = -EINVAL;
2560 goto out;
2561 }
2562
Andi Kleen36ce5652017-06-02 08:48:10 -07002563 /* Don't override defaults for +- */
2564 if (strchr(str, '+') || strchr(str, '-'))
2565 goto parse;
2566
David Ahern2c9e45f72011-03-17 10:03:21 -06002567 if (output_set_by_user())
2568 pr_warning("Overriding previous field request for all events.\n");
2569
Adrian Hunter14057202017-06-21 13:17:19 +03002570 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002571 output[j].fields = 0;
2572 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002573 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002574 }
David Ahern1424dc92011-03-09 22:23:28 -07002575 }
2576
Andi Kleen36ce5652017-06-02 08:48:10 -07002577parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002578 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002579 if (*tok == '+') {
2580 if (change == SET)
2581 goto out_badmix;
2582 change = ADD;
2583 tok++;
2584 } else if (*tok == '-') {
2585 if (change == SET)
2586 goto out_badmix;
2587 change = REMOVE;
2588 tok++;
2589 } else {
2590 if (change != SET && change != DEFAULT)
2591 goto out_badmix;
2592 change = SET;
2593 }
2594
David Ahern745f43e2011-03-09 22:23:26 -07002595 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002596 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002597 break;
David Ahern745f43e2011-03-09 22:23:26 -07002598 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002599 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002600 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002601 continue;
2602 }
David Ahern745f43e2011-03-09 22:23:26 -07002603 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002604 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002605 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002606 goto out;
2607 }
2608
2609 if (type == -1) {
2610 /* add user option to all events types for
2611 * which it is valid
2612 */
Adrian Hunter14057202017-06-21 13:17:19 +03002613 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002614 if (output[j].invalid_fields & all_output_options[i].field) {
2615 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2616 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002617 } else {
2618 if (change == REMOVE)
2619 output[j].fields &= ~all_output_options[i].field;
2620 else
2621 output[j].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002622 output[j].user_set = true;
2623 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002624 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002625 }
2626 } else {
2627 if (output[type].invalid_fields & all_output_options[i].field) {
2628 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2629 all_output_options[i].str, event_type(type));
2630
2631 rc = -EINVAL;
2632 goto out;
2633 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002634 output[type].user_set = true;
2635 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002636 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002637 }
2638
2639 if (type >= 0) {
2640 if (output[type].fields == 0) {
2641 pr_debug("No fields requested for %s type. "
2642 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002643 }
David Ahern1424dc92011-03-09 22:23:28 -07002644 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002645 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002646
Andi Kleen36ce5652017-06-02 08:48:10 -07002647out_badmix:
2648 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2649 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002650out:
David Ahern745f43e2011-03-09 22:23:26 -07002651 free(str);
2652 return rc;
2653}
2654
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002655#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2656 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2657 if ((lang_dirent->d_type == DT_DIR || \
2658 (lang_dirent->d_type == DT_UNKNOWN && \
2659 is_directory(scripts_path, lang_dirent))) && \
2660 (strcmp(lang_dirent->d_name, ".")) && \
2661 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002662
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002663#define for_each_script(lang_path, lang_dir, script_dirent) \
2664 while ((script_dirent = readdir(lang_dir)) != NULL) \
2665 if (script_dirent->d_type != DT_DIR && \
2666 (script_dirent->d_type != DT_UNKNOWN || \
2667 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002668
2669
2670#define RECORD_SUFFIX "-record"
2671#define REPORT_SUFFIX "-report"
2672
2673struct script_desc {
2674 struct list_head node;
2675 char *name;
2676 char *half_liner;
2677 char *args;
2678};
2679
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002680static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002681
2682static struct script_desc *script_desc__new(const char *name)
2683{
2684 struct script_desc *s = zalloc(sizeof(*s));
2685
Tom Zanussib5b87312010-11-10 08:16:51 -06002686 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002687 s->name = strdup(name);
2688
2689 return s;
2690}
2691
2692static void script_desc__delete(struct script_desc *s)
2693{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002694 zfree(&s->name);
2695 zfree(&s->half_liner);
2696 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002697 free(s);
2698}
2699
2700static void script_desc__add(struct script_desc *s)
2701{
2702 list_add_tail(&s->node, &script_descs);
2703}
2704
2705static struct script_desc *script_desc__find(const char *name)
2706{
2707 struct script_desc *s;
2708
2709 list_for_each_entry(s, &script_descs, node)
2710 if (strcasecmp(s->name, name) == 0)
2711 return s;
2712 return NULL;
2713}
2714
2715static struct script_desc *script_desc__findnew(const char *name)
2716{
2717 struct script_desc *s = script_desc__find(name);
2718
2719 if (s)
2720 return s;
2721
2722 s = script_desc__new(name);
2723 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002724 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002725
2726 script_desc__add(s);
2727
2728 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002729}
2730
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002731static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002732{
2733 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002734 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002735
2736 if (strlen(str) > suffix_len) {
2737 p = str + strlen(str) - suffix_len;
2738 if (!strncmp(p, suffix, suffix_len))
2739 return p;
2740 }
2741
2742 return NULL;
2743}
2744
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002745static int read_script_info(struct script_desc *desc, const char *filename)
2746{
2747 char line[BUFSIZ], *p;
2748 FILE *fp;
2749
2750 fp = fopen(filename, "r");
2751 if (!fp)
2752 return -1;
2753
2754 while (fgets(line, sizeof(line), fp)) {
2755 p = ltrim(line);
2756 if (strlen(p) == 0)
2757 continue;
2758 if (*p != '#')
2759 continue;
2760 p++;
2761 if (strlen(p) && *p == '!')
2762 continue;
2763
2764 p = ltrim(p);
2765 if (strlen(p) && p[strlen(p) - 1] == '\n')
2766 p[strlen(p) - 1] = '\0';
2767
2768 if (!strncmp(p, "description:", strlen("description:"))) {
2769 p += strlen("description:");
2770 desc->half_liner = strdup(ltrim(p));
2771 continue;
2772 }
2773
2774 if (!strncmp(p, "args:", strlen("args:"))) {
2775 p += strlen("args:");
2776 desc->args = strdup(ltrim(p));
2777 continue;
2778 }
2779 }
2780
2781 fclose(fp);
2782
2783 return 0;
2784}
2785
Robert Richter38efb532011-11-25 11:38:40 +01002786static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2787{
2788 char *script_root, *str;
2789
2790 script_root = strdup(script_dirent->d_name);
2791 if (!script_root)
2792 return NULL;
2793
2794 str = (char *)ends_with(script_root, suffix);
2795 if (!str) {
2796 free(script_root);
2797 return NULL;
2798 }
2799
2800 *str = '\0';
2801 return script_root;
2802}
2803
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002804static int list_available_scripts(const struct option *opt __maybe_unused,
2805 const char *s __maybe_unused,
2806 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002807{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002808 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002809 char scripts_path[MAXPATHLEN];
2810 DIR *scripts_dir, *lang_dir;
2811 char script_path[MAXPATHLEN];
2812 char lang_path[MAXPATHLEN];
2813 struct script_desc *desc;
2814 char first_half[BUFSIZ];
2815 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002816
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002817 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002818
2819 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002820 if (!scripts_dir) {
2821 fprintf(stdout,
2822 "open(%s) failed.\n"
2823 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2824 scripts_path);
2825 exit(-1);
2826 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002827
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002828 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002829 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2830 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002831 lang_dir = opendir(lang_path);
2832 if (!lang_dir)
2833 continue;
2834
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002835 for_each_script(lang_path, lang_dir, script_dirent) {
2836 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002837 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002838 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002839 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2840 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002841 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002842 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002843 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002844 }
2845 }
2846
2847 fprintf(stdout, "List of available trace scripts:\n");
2848 list_for_each_entry(desc, &script_descs, node) {
2849 sprintf(first_half, "%s %s", desc->name,
2850 desc->args ? desc->args : "");
2851 fprintf(stdout, " %-36s %s\n", first_half,
2852 desc->half_liner ? desc->half_liner : "");
2853 }
2854
2855 exit(0);
2856}
2857
Feng Tange5f37052012-09-07 16:42:26 +08002858/*
Feng Tang49e639e2012-10-30 11:56:03 +08002859 * Some scripts specify the required events in their "xxx-record" file,
2860 * this function will check if the events in perf.data match those
2861 * mentioned in the "xxx-record".
2862 *
2863 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2864 * which is covered well now. And new parsing code should be added to
2865 * cover the future complexing formats like event groups etc.
2866 */
2867static int check_ev_match(char *dir_name, char *scriptname,
2868 struct perf_session *session)
2869{
2870 char filename[MAXPATHLEN], evname[128];
2871 char line[BUFSIZ], *p;
2872 struct perf_evsel *pos;
2873 int match, len;
2874 FILE *fp;
2875
Jiri Olsa77f18152018-03-19 09:29:01 +01002876 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08002877
2878 fp = fopen(filename, "r");
2879 if (!fp)
2880 return -1;
2881
2882 while (fgets(line, sizeof(line), fp)) {
2883 p = ltrim(line);
2884 if (*p == '#')
2885 continue;
2886
2887 while (strlen(p)) {
2888 p = strstr(p, "-e");
2889 if (!p)
2890 break;
2891
2892 p += 2;
2893 p = ltrim(p);
2894 len = strcspn(p, " \t");
2895 if (!len)
2896 break;
2897
2898 snprintf(evname, len + 1, "%s", p);
2899
2900 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002901 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002902 if (!strcmp(perf_evsel__name(pos), evname)) {
2903 match = 1;
2904 break;
2905 }
2906 }
2907
2908 if (!match) {
2909 fclose(fp);
2910 return -1;
2911 }
2912 }
2913 }
2914
2915 fclose(fp);
2916 return 0;
2917}
2918
2919/*
Feng Tange5f37052012-09-07 16:42:26 +08002920 * Return -1 if none is found, otherwise the actual scripts number.
2921 *
2922 * Currently the only user of this function is the script browser, which
2923 * will list all statically runnable scripts, select one, execute it and
2924 * show the output in a perf browser.
2925 */
2926int find_scripts(char **scripts_array, char **scripts_path_array)
2927{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002928 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002929 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002930 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002931 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002932 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002933 .file = {
2934 .path = input_name,
2935 },
2936 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002937 };
Feng Tange5f37052012-09-07 16:42:26 +08002938 char *temp;
2939 int i = 0;
2940
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002941 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002942 if (!session)
2943 return -1;
2944
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002945 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002946
2947 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002948 if (!scripts_dir) {
2949 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002950 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002951 }
Feng Tange5f37052012-09-07 16:42:26 +08002952
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002953 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002954 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
2955 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08002956#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08002957 if (strstr(lang_path, "perl"))
2958 continue;
2959#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08002960#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08002961 if (strstr(lang_path, "python"))
2962 continue;
2963#endif
2964
2965 lang_dir = opendir(lang_path);
2966 if (!lang_dir)
2967 continue;
2968
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002969 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002970 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002971 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002972 continue;
2973 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002974 script_dirent->d_name);
2975 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002976 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002977 (temp - script_dirent->d_name) + 1,
2978 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002979
2980 if (check_ev_match(lang_path,
2981 scripts_array[i], session))
2982 continue;
2983
Feng Tange5f37052012-09-07 16:42:26 +08002984 i++;
2985 }
Feng Tang49e639e2012-10-30 11:56:03 +08002986 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002987 }
2988
Feng Tang49e639e2012-10-30 11:56:03 +08002989 closedir(scripts_dir);
2990 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002991 return i;
2992}
2993
Tom Zanussi38752942009-12-15 02:53:39 -06002994static char *get_script_path(const char *script_root, const char *suffix)
2995{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002996 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002997 char scripts_path[MAXPATHLEN];
2998 char script_path[MAXPATHLEN];
2999 DIR *scripts_dir, *lang_dir;
3000 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003001 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003002
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003003 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003004
3005 scripts_dir = opendir(scripts_path);
3006 if (!scripts_dir)
3007 return NULL;
3008
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003009 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003010 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3011 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003012 lang_dir = opendir(lang_path);
3013 if (!lang_dir)
3014 continue;
3015
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003016 for_each_script(lang_path, lang_dir, script_dirent) {
3017 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003018 if (__script_root && !strcmp(script_root, __script_root)) {
3019 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003020 closedir(lang_dir);
3021 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003022 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3023 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003024 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003025 }
3026 free(__script_root);
3027 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003028 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003029 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003030 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003031
Robert Richter38efb532011-11-25 11:38:40 +01003032 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003033}
3034
Tom Zanussib5b87312010-11-10 08:16:51 -06003035static bool is_top_script(const char *script_path)
3036{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003037 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003038}
3039
3040static int has_required_arg(char *script_path)
3041{
3042 struct script_desc *desc;
3043 int n_args = 0;
3044 char *p;
3045
3046 desc = script_desc__new(NULL);
3047
3048 if (read_script_info(desc, script_path))
3049 goto out;
3050
3051 if (!desc->args)
3052 goto out;
3053
3054 for (p = desc->args; *p; p++)
3055 if (*p == '<')
3056 n_args++;
3057out:
3058 script_desc__delete(desc);
3059
3060 return n_args;
3061}
3062
David Ahernd54b1a92012-08-26 12:24:46 -06003063static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003064{
3065 char **__argv = malloc(sizeof(const char *) * argc);
3066
David Ahernd54b1a92012-08-26 12:24:46 -06003067 if (!__argv) {
3068 pr_err("malloc failed\n");
3069 return -1;
3070 }
3071
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003072 memcpy(__argv, argv, sizeof(const char *) * argc);
3073 argc = parse_options(argc, (const char **)__argv, record_options,
3074 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3075 free(__argv);
3076
David Ahernd54b1a92012-08-26 12:24:46 -06003077 system_wide = (argc == 0);
3078
3079 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003080}
3081
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003082static void script__setup_sample_type(struct perf_script *script)
3083{
3084 struct perf_session *session = script->session;
3085 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3086
3087 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3088 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003089 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003090 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003091 dwarf_callchain_users = true;
3092 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003093 callchain_param.record_mode = CALLCHAIN_LBR;
3094 else
3095 callchain_param.record_mode = CALLCHAIN_FP;
3096 }
3097}
3098
Jiri Olsa89f16882018-09-13 14:54:03 +02003099static int process_stat_round_event(struct perf_session *session,
3100 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003101{
3102 struct stat_round_event *round = &event->stat_round;
3103 struct perf_evsel *counter;
3104
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003105 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003106 perf_stat_process_counter(&stat_config, counter);
3107 process_stat(counter, round->time);
3108 }
3109
3110 process_stat_interval(round->time);
3111 return 0;
3112}
3113
Jiri Olsa89f16882018-09-13 14:54:03 +02003114static int process_stat_config_event(struct perf_session *session __maybe_unused,
3115 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003116{
3117 perf_event__read_stat_config(&stat_config, &event->stat_config);
3118 return 0;
3119}
3120
Jiri Olsacfc88742016-01-05 22:09:06 +01003121static int set_maps(struct perf_script *script)
3122{
3123 struct perf_evlist *evlist = script->session->evlist;
3124
3125 if (!script->cpus || !script->threads)
3126 return 0;
3127
3128 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3129 return -EINVAL;
3130
3131 perf_evlist__set_maps(evlist, script->cpus, script->threads);
3132
3133 if (perf_evlist__alloc_stats(evlist, true))
3134 return -ENOMEM;
3135
3136 script->allocated = true;
3137 return 0;
3138}
3139
3140static
Jiri Olsa89f16882018-09-13 14:54:03 +02003141int process_thread_map_event(struct perf_session *session,
3142 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003143{
Jiri Olsa89f16882018-09-13 14:54:03 +02003144 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003145 struct perf_script *script = container_of(tool, struct perf_script, tool);
3146
3147 if (script->threads) {
3148 pr_warning("Extra thread map event, ignoring.\n");
3149 return 0;
3150 }
3151
3152 script->threads = thread_map__new_event(&event->thread_map);
3153 if (!script->threads)
3154 return -ENOMEM;
3155
3156 return set_maps(script);
3157}
3158
3159static
Jiri Olsa89f16882018-09-13 14:54:03 +02003160int process_cpu_map_event(struct perf_session *session,
3161 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003162{
Jiri Olsa89f16882018-09-13 14:54:03 +02003163 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003164 struct perf_script *script = container_of(tool, struct perf_script, tool);
3165
3166 if (script->cpus) {
3167 pr_warning("Extra cpu map event, ignoring.\n");
3168 return 0;
3169 }
3170
3171 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3172 if (!script->cpus)
3173 return -ENOMEM;
3174
3175 return set_maps(script);
3176}
3177
Jiri Olsa89f16882018-09-13 14:54:03 +02003178static int process_feature_event(struct perf_session *session,
3179 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303180{
3181 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003182 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303183 return 0;
3184}
3185
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003186#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003187static int perf_script__process_auxtrace_info(struct perf_session *session,
3188 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003189{
Jiri Olsa89f16882018-09-13 14:54:03 +02003190 struct perf_tool *tool = session->tool;
3191
3192 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003193
3194 if (ret == 0) {
3195 struct perf_script *script = container_of(tool, struct perf_script, tool);
3196
3197 ret = perf_script__setup_per_event_dump(script);
3198 }
3199
3200 return ret;
3201}
3202#else
3203#define perf_script__process_auxtrace_info 0
3204#endif
3205
Andi Kleenb585ebd2018-09-20 11:05:36 -07003206static int parse_insn_trace(const struct option *opt __maybe_unused,
3207 const char *str __maybe_unused,
3208 int unset __maybe_unused)
3209{
3210 parse_output_fields(NULL, "+insn,-event,-period", 0);
3211 itrace_parse_synth_opts(opt, "i0ns", 0);
3212 nanosecs = true;
3213 return 0;
3214}
3215
3216static int parse_xed(const struct option *opt __maybe_unused,
3217 const char *str __maybe_unused,
3218 int unset __maybe_unused)
3219{
3220 force_pager("xed -F insn: -A -64 | less");
3221 return 0;
3222}
3223
Andi Kleend1b15522018-09-20 11:05:38 -07003224static int parse_call_trace(const struct option *opt __maybe_unused,
3225 const char *str __maybe_unused,
3226 int unset __maybe_unused)
3227{
3228 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3229 itrace_parse_synth_opts(opt, "cewp", 0);
3230 nanosecs = true;
3231 return 0;
3232}
3233
3234static int parse_callret_trace(const struct option *opt __maybe_unused,
3235 const char *str __maybe_unused,
3236 int unset __maybe_unused)
3237{
3238 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3239 itrace_parse_synth_opts(opt, "crewp", 0);
3240 nanosecs = true;
3241 return 0;
3242}
3243
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003244int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003245{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003246 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003247 bool header = false;
3248 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003249 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003250 char *rec_script_path = NULL;
3251 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003252 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003253 struct itrace_synth_opts itrace_synth_opts = {
3254 .set = false,
3255 .default_no_sample = true,
3256 };
Tom Zanussib5b87312010-11-10 08:16:51 -06003257 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003258 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003259 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003260 struct perf_script script = {
3261 .tool = {
3262 .sample = process_sample_event,
3263 .mmap = perf_event__process_mmap,
3264 .mmap2 = perf_event__process_mmap2,
3265 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303266 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003267 .exit = perf_event__process_exit,
3268 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003269 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003270 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003271 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303272 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003273 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003274 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003275 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003276 .auxtrace = perf_event__process_auxtrace,
3277 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003278 .stat = perf_event__process_stat_event,
3279 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003280 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003281 .thread_map = process_thread_map_event,
3282 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003283 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003284 .ordering_requires_timestamps = true,
3285 },
3286 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003287 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003288 .mode = PERF_DATA_MODE_READ,
3289 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003290 const struct option options[] = {
3291 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3292 "dump raw trace in ASCII"),
3293 OPT_INCR('v', "verbose", &verbose,
3294 "be more verbose (show symbol address, etc)"),
3295 OPT_BOOLEAN('L', "Latency", &latency_format,
3296 "show latency attributes (irqs/preemption disabled, etc)"),
3297 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3298 list_available_scripts),
3299 OPT_CALLBACK('s', "script", NULL, "name",
3300 "script file name (lang:script name, script name, or *)",
3301 parse_scriptname),
3302 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3303 "generate perf-script.xx script in specified language"),
3304 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3305 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3306 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003307 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3308 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003309 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3310 "file", "vmlinux pathname"),
3311 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3312 "file", "kallsyms pathname"),
3313 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3314 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003315 OPT_CALLBACK(0, "symfs", NULL, "directory",
3316 "Look for files with symbols relative to this directory",
3317 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003318 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003319 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003320 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003321 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003322 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303323 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3324 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
3325 "callindent,insn,insnlen,synth,phys_addr,metric,misc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003326 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003327 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3328 "system-wide collection from all CPUs"),
3329 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3330 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003331 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3332 "Decode instructions from itrace", parse_insn_trace),
3333 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3334 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003335 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3336 "Decode calls from from itrace", parse_call_trace),
3337 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3338 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003339 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3340 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003341 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3342 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003343 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3344 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3345 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003346 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3347 "only consider symbols in these pids"),
3348 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3349 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003350 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3351 "Set the maximum stack depth when parsing the callchain, "
3352 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003353 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003354 OPT_BOOLEAN('I', "show-info", &show_full_info,
3355 "display extended information from perf.data file"),
3356 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3357 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003358 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3359 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003360 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3361 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003362 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3363 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303364 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3365 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003366 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3367 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003368 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3369 "Show round events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003370 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3371 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003372 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003373 OPT_INTEGER(0, "max-blocks", &max_blocks,
3374 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03003375 OPT_BOOLEAN(0, "ns", &nanosecs,
3376 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003377 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003378 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003379 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003380 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3381 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003382 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3383 "Enable symbol demangling"),
3384 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3385 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003386 OPT_STRING(0, "time", &script.time_str, "str",
3387 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003388 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3389 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003390 OPT_END()
3391 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003392 const char * const script_subcommands[] = { "record", "report", NULL };
3393 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003394 "perf script [<options>]",
3395 "perf script [<options>] record <script> [<record-options>] <command>",
3396 "perf script [<options>] report <script> [script-args]",
3397 "perf script [<options>] <script> [<record-options>] <command>",
3398 "perf script [<options>] <top-script> [script-args]",
3399 NULL
3400 };
Tom Zanussi38752942009-12-15 02:53:39 -06003401
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003402 perf_set_singlethreaded();
3403
Tom Zanussib5b87312010-11-10 08:16:51 -06003404 setup_scripting();
3405
Yunlong Song40cae2b2015-03-18 21:35:54 +08003406 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003407 PARSE_OPT_STOP_AT_NON_OPTION);
3408
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003409 data.file.path = input_name;
3410 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003411
Tom Zanussib5b87312010-11-10 08:16:51 -06003412 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3413 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3414 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003415 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003416 }
3417
Tom Zanussib5b87312010-11-10 08:16:51 -06003418 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3419 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3420 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003421 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003422 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003423 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003424 return -1;
3425 }
Tom Zanussi38752942009-12-15 02:53:39 -06003426 }
3427
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003428 if (itrace_synth_opts.callchain &&
3429 itrace_synth_opts.callchain_sz > scripting_max_stack)
3430 scripting_max_stack = itrace_synth_opts.callchain_sz;
3431
Ben Hutchings44e668c2010-10-10 16:10:03 +01003432 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003433 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003434
Tom Zanussib5b87312010-11-10 08:16:51 -06003435 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003436 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003437 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003438 pid_t pid;
3439
Tom Zanussib5b87312010-11-10 08:16:51 -06003440 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3441 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3442
3443 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003444 usage_with_options_msg(script_usage, options,
3445 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003446 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003447 }
3448
Tom Zanussib5b87312010-11-10 08:16:51 -06003449 if (is_top_script(argv[0])) {
3450 rep_args = argc - 1;
3451 } else {
3452 int rec_args;
3453
3454 rep_args = has_required_arg(rep_script_path);
3455 rec_args = (argc - 1) - rep_args;
3456 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003457 usage_with_options_msg(script_usage, options,
3458 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003459 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003460 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003461 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003462 }
3463
3464 if (pipe(live_pipe) < 0) {
3465 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003466 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003467 }
3468
3469 pid = fork();
3470 if (pid < 0) {
3471 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003472 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003473 }
3474
3475 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003476 j = 0;
3477
Tom Zanussia0cccc22010-04-01 23:59:25 -05003478 dup2(live_pipe[1], 1);
3479 close(live_pipe[0]);
3480
Robert Richter317df652011-11-25 15:05:25 +01003481 if (is_top_script(argv[0])) {
3482 system_wide = true;
3483 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003484 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3485 err = -1;
3486 goto out;
3487 }
Robert Richter317df652011-11-25 15:05:25 +01003488 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003489
3490 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003491 if (!__argv) {
3492 pr_err("malloc failed\n");
3493 err = -ENOMEM;
3494 goto out;
3495 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003496
Tom Zanussib5b87312010-11-10 08:16:51 -06003497 __argv[j++] = "/bin/sh";
3498 __argv[j++] = rec_script_path;
3499 if (system_wide)
3500 __argv[j++] = "-a";
3501 __argv[j++] = "-q";
3502 __argv[j++] = "-o";
3503 __argv[j++] = "-";
3504 for (i = rep_args + 1; i < argc; i++)
3505 __argv[j++] = argv[i];
3506 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003507
3508 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003509 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003510 exit(-1);
3511 }
3512
3513 dup2(live_pipe[0], 0);
3514 close(live_pipe[1]);
3515
Tom Zanussib5b87312010-11-10 08:16:51 -06003516 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003517 if (!__argv) {
3518 pr_err("malloc failed\n");
3519 err = -ENOMEM;
3520 goto out;
3521 }
3522
Tom Zanussib5b87312010-11-10 08:16:51 -06003523 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003524 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003525 __argv[j++] = rep_script_path;
3526 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003527 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003528 __argv[j++] = "-i";
3529 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003530 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003531
3532 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003533 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003534 exit(-1);
3535 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003536
Tom Zanussib5b87312010-11-10 08:16:51 -06003537 if (rec_script_path)
3538 script_path = rec_script_path;
3539 if (rep_script_path)
3540 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003541
Tom Zanussib5b87312010-11-10 08:16:51 -06003542 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003543 j = 0;
3544
Robert Richter317df652011-11-25 15:05:25 +01003545 if (!rec_script_path)
3546 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003547 else if (!system_wide) {
3548 if (have_cmd(argc - 1, &argv[1]) != 0) {
3549 err = -1;
3550 goto out;
3551 }
3552 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003553
3554 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003555 if (!__argv) {
3556 pr_err("malloc failed\n");
3557 err = -ENOMEM;
3558 goto out;
3559 }
3560
Tom Zanussib5b87312010-11-10 08:16:51 -06003561 __argv[j++] = "/bin/sh";
3562 __argv[j++] = script_path;
3563 if (system_wide)
3564 __argv[j++] = "-a";
3565 for (i = 2; i < argc; i++)
3566 __argv[j++] = argv[i];
3567 __argv[j++] = NULL;
3568
3569 execvp("/bin/sh", (char **)__argv);
3570 free(__argv);
3571 exit(-1);
3572 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003573
Milian Wolffc1c9b962018-10-21 21:14:23 +02003574 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003575 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003576 use_browser = 0;
3577 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003578
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003579 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003580 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003581 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003582
Jiri Olsae90debd2013-12-09 11:02:50 +01003583 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003584 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003585 perf_session__fprintf_info(session, stdout, show_full_info);
3586 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003587 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003588 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003589 if (show_full_info)
3590 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003591
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003592 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003593 goto out_delete;
3594
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003595 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003596 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003597
Andi Kleen99f753f2018-09-20 11:05:39 -07003598 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3599 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003600 itrace_synth_opts.thread_stack = true;
3601
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003602 session->itrace_synth_opts = &itrace_synth_opts;
3603
Anton Blanchard5d67be92011-07-04 21:57:50 +10003604 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003605 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3606 if (err < 0)
3607 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003608 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003609 }
3610
David Ahern1424dc92011-03-09 22:23:28 -07003611 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003612 symbol_conf.use_callchain = true;
3613 else
3614 symbol_conf.use_callchain = false;
3615
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003616 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003617 tep_set_function_resolver(session->tevent.pevent,
3618 machine__resolve_kernel_addr,
3619 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003620 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003621 err = -1;
3622 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003623 }
3624
Tom Zanussi956ffd02009-11-25 01:15:46 -06003625 if (generate_script_lang) {
3626 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003627 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003628
David Ahern2c9e45f72011-03-17 10:03:21 -06003629 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003630 fprintf(stderr,
3631 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003632 err = -EINVAL;
3633 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003634 }
3635
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003636 input = open(data.file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003637 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003638 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003639 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003640 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003641 }
3642
3643 err = fstat(input, &perf_stat);
3644 if (err < 0) {
3645 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003646 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003647 }
3648
3649 if (!perf_stat.st_size) {
3650 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003651 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003652 }
3653
3654 scripting_ops = script_spec__lookup(generate_script_lang);
3655 if (!scripting_ops) {
3656 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003657 err = -ENOENT;
3658 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003659 }
3660
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003661 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003662 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003663 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003664 }
3665
3666 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003667 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003668 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003669 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003670 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003671 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003672 }
3673
David Ahern9cbdb702011-04-06 21:54:20 -06003674
3675 err = perf_session__check_output_opt(session);
3676 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003677 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003678
Jin Yaocc2ef582018-01-10 23:00:33 +08003679 script.ptime_range = perf_time__range_alloc(script.time_str,
3680 &script.range_size);
3681 if (!script.ptime_range) {
3682 err = -ENOMEM;
3683 goto out_delete;
3684 }
3685
David Aherna91f4c42016-11-29 10:15:43 -07003686 /* needs to be parsed after looking up reference time */
Jin Yao2ab046c2017-12-08 21:13:46 +08003687 if (perf_time__parse_str(script.ptime_range, script.time_str) != 0) {
3688 if (session->evlist->first_sample_time == 0 &&
3689 session->evlist->last_sample_time == 0) {
Jin Yao1e2778e2018-01-10 23:00:27 +08003690 pr_err("HINT: no first/last sample time found in perf data.\n"
3691 "Please use latest perf binary to execute 'perf record'\n"
3692 "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
Jin Yao2ab046c2017-12-08 21:13:46 +08003693 err = -EINVAL;
3694 goto out_delete;
3695 }
3696
3697 script.range_num = perf_time__percent_parse_str(
Jin Yaocc2ef582018-01-10 23:00:33 +08003698 script.ptime_range, script.range_size,
Jin Yao2ab046c2017-12-08 21:13:46 +08003699 script.time_str,
3700 session->evlist->first_sample_time,
3701 session->evlist->last_sample_time);
3702
3703 if (script.range_num < 0) {
3704 pr_err("Invalid time string\n");
3705 err = -EINVAL;
3706 goto out_delete;
3707 }
3708 } else {
3709 script.range_num = 1;
David Aherna91f4c42016-11-29 10:15:43 -07003710 }
3711
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003712 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003713
Adrian Hunterd445dd22014-08-15 22:08:37 +03003714 flush_scripting();
3715
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003716out_delete:
Jin Yaocc2ef582018-01-10 23:00:33 +08003717 zfree(&script.ptime_range);
3718
Jiri Olsacfc88742016-01-05 22:09:06 +01003719 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003720 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003721
3722 if (script_started)
3723 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003724out:
3725 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003726}