blob: 2f93d60c5a17d4f79d7dcd4ce653b722c53f5388 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002#include "builtin.h"
3
Andrea Gelminib7eead82010-08-05 15:51:38 +02004#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02005#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02006#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06007#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02008#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06009#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +020010#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010013#include "util/map.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020014#include "util/symbol.h"
15#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010016#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020017#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070018#include "util/evlist.h"
19#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080020#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020021#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030022#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010023#include "util/cpumap.h"
24#include "util/thread_map.h"
25#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080026#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030027#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030028#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070029#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010030#include "util/path.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030031#include "print_binary.h"
Andi Kleen3ab481a2019-03-05 06:47:45 -080032#include "archinsn.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100033#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030034#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030035#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030036#include <linux/time64.h>
Andi Kleen3ab481a2019-03-05 06:47:45 -080037#include <sys/utsname.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010038#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010039#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080040#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030041#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030042#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030043#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030044#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030045#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030046#include <sys/types.h>
47#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030048#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030049#include <unistd.h>
Andi Kleenb585ebd2018-09-20 11:05:36 -070050#include <subcmd/pager.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020051
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030052#include "sane_ctype.h"
53
Tom Zanussi956ffd02009-11-25 01:15:46 -060054static char const *script_name;
55static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020056static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020057static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020058static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070059static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090060static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010061static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030062static bool print_flags;
Anton Blanchard5d67be92011-07-04 21:57:50 +100063static const char *cpu_list;
64static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010065static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080066static int max_blocks;
Andi Kleen3ab481a2019-03-05 06:47:45 -080067static bool native_arch;
Tom Zanussi956ffd02009-11-25 01:15:46 -060068
Adrian Hunter44cbe722015-09-25 16:15:50 +030069unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030070
David Ahern745f43e2011-03-09 22:23:26 -070071enum perf_output_field {
72 PERF_OUTPUT_COMM = 1U << 0,
73 PERF_OUTPUT_TID = 1U << 1,
74 PERF_OUTPUT_PID = 1U << 2,
75 PERF_OUTPUT_TIME = 1U << 3,
76 PERF_OUTPUT_CPU = 1U << 4,
77 PERF_OUTPUT_EVNAME = 1U << 5,
78 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060079 PERF_OUTPUT_IP = 1U << 7,
80 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060081 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060082 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090083 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020084 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020085 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020086 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020087 PERF_OUTPUT_BRSTACK = 1U << 15,
88 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010089 PERF_OUTPUT_DATA_SRC = 1U << 17,
90 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000091 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030092 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030093 PERF_OUTPUT_INSN = 1U << 21,
94 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080095 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070096 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030097 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -040098 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -070099 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800100 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100101 PERF_OUTPUT_MISC = 1U << 29,
Andi Kleendd2e18e2018-12-03 16:18:48 -0800102 PERF_OUTPUT_SRCCODE = 1U << 30,
David Ahern745f43e2011-03-09 22:23:26 -0700103};
104
105struct output_option {
106 const char *str;
107 enum perf_output_field field;
108} all_output_options[] = {
109 {.str = "comm", .field = PERF_OUTPUT_COMM},
110 {.str = "tid", .field = PERF_OUTPUT_TID},
111 {.str = "pid", .field = PERF_OUTPUT_PID},
112 {.str = "time", .field = PERF_OUTPUT_TIME},
113 {.str = "cpu", .field = PERF_OUTPUT_CPU},
114 {.str = "event", .field = PERF_OUTPUT_EVNAME},
115 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600116 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700117 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600118 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600119 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900120 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200121 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200122 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200123 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700124 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200125 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
126 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100127 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
128 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000129 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300130 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300131 {.str = "insn", .field = PERF_OUTPUT_INSN},
132 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800133 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700134 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300135 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400136 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800137 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100138 {.str = "misc", .field = PERF_OUTPUT_MISC},
Andi Kleendd2e18e2018-12-03 16:18:48 -0800139 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
David Ahern745f43e2011-03-09 22:23:26 -0700140};
141
Adrian Hunter14057202017-06-21 13:17:19 +0300142enum {
143 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
144 OUTPUT_TYPE_MAX
145};
146
David Ahern745f43e2011-03-09 22:23:26 -0700147/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600148static struct {
149 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600150 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400151 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600152 u64 fields;
153 u64 invalid_fields;
Andi Kleen4b6ac812019-02-24 07:37:12 -0800154 u64 user_set_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300155} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700156
David Ahern2c9e45f72011-03-17 10:03:21 -0600157 [PERF_TYPE_HARDWARE] = {
158 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700159
David Ahern2c9e45f72011-03-17 10:03:21 -0600160 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
161 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600162 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530163 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
164 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600165
Wang Nan30372f02016-02-24 11:20:45 +0000166 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600167 },
168
169 [PERF_TYPE_SOFTWARE] = {
170 .user_set = false,
171
172 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
173 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600174 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530175 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
176 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
177 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600178
179 .invalid_fields = PERF_OUTPUT_TRACE,
180 },
181
182 [PERF_TYPE_TRACEPOINT] = {
183 .user_set = false,
184
185 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
186 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000187 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600188 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700189
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530190 [PERF_TYPE_HW_CACHE] = {
191 .user_set = false,
192
193 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
194 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
195 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
196 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
197 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
198
199 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
200 },
201
Arun Sharma0817a6a2011-04-14 10:38:18 -0700202 [PERF_TYPE_RAW] = {
203 .user_set = false,
204
205 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
206 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600207 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530208 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
209 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
210 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
211 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700212
Wang Nan30372f02016-02-24 11:20:45 +0000213 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700214 },
Wang Nan27cfef02015-12-08 02:25:43 +0000215
216 [PERF_TYPE_BREAKPOINT] = {
217 .user_set = false,
218
219 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
220 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
221 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530222 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
223 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000224
Wang Nan30372f02016-02-24 11:20:45 +0000225 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000226 },
Adrian Hunter14057202017-06-21 13:17:19 +0300227
228 [OUTPUT_TYPE_SYNTH] = {
229 .user_set = false,
230
231 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
232 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
233 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530234 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
235 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300236
237 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
238 },
David Ahern1424dc92011-03-09 22:23:28 -0700239};
David Ahern745f43e2011-03-09 22:23:26 -0700240
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300241struct perf_evsel_script {
242 char *filename;
243 FILE *fp;
244 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800245 /* For metric output */
246 u64 val;
247 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300248};
249
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800250static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
251{
252 return (struct perf_evsel_script *)evsel->priv;
253}
254
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300255static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100256 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300257{
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800258 struct perf_evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300259
260 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100261 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300262 goto out_free;
263 es->fp = fopen(es->filename, "w");
264 if (es->fp == NULL)
265 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300266 }
267
268 return es;
269out_free_filename:
270 zfree(&es->filename);
271out_free:
272 free(es);
273 return NULL;
274}
275
276static void perf_evsel_script__delete(struct perf_evsel_script *es)
277{
278 zfree(&es->filename);
279 fclose(es->fp);
280 es->fp = NULL;
281 free(es);
282}
283
284static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
285{
286 struct stat st;
287
288 fstat(fileno(es->fp), &st);
289 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
290 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
291}
292
Adrian Hunter14057202017-06-21 13:17:19 +0300293static inline int output_type(unsigned int type)
294{
295 switch (type) {
296 case PERF_TYPE_SYNTH:
297 return OUTPUT_TYPE_SYNTH;
298 default:
299 return type;
300 }
301}
302
303static inline unsigned int attr_type(unsigned int type)
304{
305 switch (type) {
306 case OUTPUT_TYPE_SYNTH:
307 return PERF_TYPE_SYNTH;
308 default:
309 return type;
310 }
311}
312
David Ahern2c9e45f72011-03-17 10:03:21 -0600313static bool output_set_by_user(void)
314{
315 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300316 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600317 if (output[j].user_set)
318 return true;
319 }
320 return false;
321}
David Ahern745f43e2011-03-09 22:23:26 -0700322
David Ahern9cbdb702011-04-06 21:54:20 -0600323static const char *output_field2str(enum perf_output_field field)
324{
325 int i, imax = ARRAY_SIZE(all_output_options);
326 const char *str = "";
327
328 for (i = 0; i < imax; ++i) {
329 if (all_output_options[i].field == field) {
330 str = all_output_options[i].str;
331 break;
332 }
333 }
334 return str;
335}
336
Adrian Hunter14057202017-06-21 13:17:19 +0300337#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700338
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300339static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
340 u64 sample_type, const char *sample_msg,
341 enum perf_output_field field,
342 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700343{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300344 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300345 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600346 const char *evname;
347
348 if (attr->sample_type & sample_type)
349 return 0;
350
Andi Kleen4b6ac812019-02-24 07:37:12 -0800351 if (output[type].user_set_fields & field) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300352 if (allow_user_set)
353 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300354 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600355 pr_err("Samples for '%s' event do not have %s attribute set. "
356 "Cannot print '%s' field.\n",
357 evname, sample_msg, output_field2str(field));
358 return -1;
359 }
360
361 /* user did not ask for it explicitly so remove from the default list */
362 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300363 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600364 pr_debug("Samples for '%s' event do not have %s attribute set. "
365 "Skipping '%s' field.\n",
366 evname, sample_msg, output_field2str(field));
367
368 return 0;
369}
370
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300371static int perf_evsel__check_stype(struct perf_evsel *evsel,
372 u64 sample_type, const char *sample_msg,
373 enum perf_output_field field)
374{
375 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
376 false);
377}
378
David Ahern9cbdb702011-04-06 21:54:20 -0600379static int perf_evsel__check_attr(struct perf_evsel *evsel,
380 struct perf_session *session)
381{
382 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300383 bool allow_user_set;
384
Jiri Olsae099eba2016-01-05 22:09:09 +0100385 if (perf_header__has_feat(&session->header, HEADER_STAT))
386 return 0;
387
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300388 allow_user_set = perf_header__has_feat(&session->header,
389 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600390
David Ahern1424dc92011-03-09 22:23:28 -0700391 if (PRINT_FIELD(TRACE) &&
392 !perf_session__has_traces(session, "record -R"))
393 return -EINVAL;
394
David Ahern787bef12011-05-27 14:28:43 -0600395 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300396 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
397 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700398 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700399 }
David Ahern7cec0922011-05-30 13:08:23 -0600400
401 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300402 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
403 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600404 return -EINVAL;
405
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100406 if (PRINT_FIELD(DATA_SRC) &&
407 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
408 PERF_OUTPUT_DATA_SRC))
409 return -EINVAL;
410
411 if (PRINT_FIELD(WEIGHT) &&
412 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
413 PERF_OUTPUT_WEIGHT))
414 return -EINVAL;
415
Andi Kleen37fed3d2018-09-18 05:32:09 -0700416 if (PRINT_FIELD(SYM) &&
417 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600418 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700419 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600420 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600421 return -EINVAL;
422 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900423 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
424 pr_err("Display of offsets requested but symbol is not"
425 "selected.\n");
426 return -EINVAL;
427 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700428 if (PRINT_FIELD(DSO) &&
429 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
430 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600431 return -EINVAL;
432 }
Andi Kleendd2e18e2018-12-03 16:18:48 -0800433 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
Adrian Huntercc8fae12013-12-06 09:42:57 +0200434 pr_err("Display of source line number requested but sample IP is not\n"
435 "selected. Hence, no address to lookup the source line number.\n");
436 return -EINVAL;
437 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800438 if (PRINT_FIELD(BRSTACKINSN) &&
439 !(perf_evlist__combined_branch_type(session->evlist) &
440 PERF_SAMPLE_BRANCH_ANY)) {
441 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
442 "Hint: run 'perf record -b ...'\n");
443 return -EINVAL;
444 }
David Ahern1424dc92011-03-09 22:23:28 -0700445 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300446 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
447 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700448 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700449
450 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300451 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
452 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700453 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700454
455 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300456 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
457 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700458 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600459
Stephane Eranianfc36f942015-08-31 18:41:10 +0200460 if (PRINT_FIELD(IREGS) &&
461 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
462 PERF_OUTPUT_IREGS))
463 return -EINVAL;
464
Andi Kleenb1491ac2017-09-05 11:40:57 -0700465 if (PRINT_FIELD(UREGS) &&
466 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
467 PERF_OUTPUT_UREGS))
468 return -EINVAL;
469
Kan Liang49d58f02017-08-29 13:11:11 -0400470 if (PRINT_FIELD(PHYS_ADDR) &&
471 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
472 PERF_OUTPUT_PHYS_ADDR))
473 return -EINVAL;
474
David Ahern9cbdb702011-04-06 21:54:20 -0600475 return 0;
476}
477
Adrian Hunter7ea95722013-11-01 15:51:30 +0200478static void set_print_ip_opts(struct perf_event_attr *attr)
479{
Adrian Hunter14057202017-06-21 13:17:19 +0300480 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200481
482 output[type].print_ip_opts = 0;
483 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300484 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200485
486 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300487 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200488
489 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300490 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200491
492 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300493 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200494
495 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300496 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200497}
498
David Ahern9cbdb702011-04-06 21:54:20 -0600499/*
500 * verify all user requested events exist and the samples
501 * have the expected data
502 */
503static int perf_session__check_output_opt(struct perf_session *session)
504{
He Kuang40f20e52016-05-16 04:51:19 +0000505 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600506 struct perf_evsel *evsel;
507
Adrian Hunter14057202017-06-21 13:17:19 +0300508 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
509 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600510
511 /*
512 * even if fields is set to 0 (ie., show nothing) event must
513 * exist if user explicitly includes it on the command line
514 */
Adrian Hunter14057202017-06-21 13:17:19 +0300515 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
516 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600517 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300518 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600519 event_type(j));
520 return -1;
521 }
522
523 if (evsel && output[j].fields &&
524 perf_evsel__check_attr(evsel, session))
525 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400526
527 if (evsel == NULL)
528 continue;
529
Adrian Hunter7ea95722013-11-01 15:51:30 +0200530 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700531 }
532
Adrian Hunter98526ee2014-07-31 09:00:59 +0300533 if (!no_callchain) {
534 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000535 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300536
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300537 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000538 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300539 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300540 use_callchain = true;
541 break;
542 }
543 }
He Kuang71ac8992016-08-04 11:25:43 +0000544 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300545 symbol_conf.use_callchain = false;
546 }
547
David Ahern80b8b492013-11-19 21:07:37 -0700548 /*
549 * set default for tracepoints to print symbols only
550 * if callchains are present
551 */
552 if (symbol_conf.use_callchain &&
553 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700554 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700555
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300556 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000557 if (evsel->attr.type != j)
558 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700559
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300560 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000561 output[j].fields |= PERF_OUTPUT_IP;
562 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530563 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000564 output[j].fields |= PERF_OUTPUT_DSO;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300565 set_print_ip_opts(&evsel->attr);
He Kuang40f20e52016-05-16 04:51:19 +0000566 goto out;
567 }
David Ahern80b8b492013-11-19 21:07:37 -0700568 }
569 }
570
571out:
David Ahern1424dc92011-03-09 22:23:28 -0700572 return 0;
573}
David Ahern745f43e2011-03-09 22:23:26 -0700574
Milian Wolff9add8fe2018-11-07 23:34:37 +0100575static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
576 FILE *fp
577)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200578{
Andi Kleenb1491ac2017-09-05 11:40:57 -0700579 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300580 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700581
582 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300583 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700584
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300585 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700586
587 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
588 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300589 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700590 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300591
Milian Wolffb07d16f2018-11-07 10:37:05 +0100592 fprintf(fp, "\n");
593
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300594 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700595}
596
Milian Wolff9add8fe2018-11-07 23:34:37 +0100597static int perf_sample__fprintf_iregs(struct perf_sample *sample,
598 struct perf_event_attr *attr, FILE *fp)
599{
600 return perf_sample__fprintf_regs(&sample->intr_regs,
601 attr->sample_regs_intr, fp);
602}
603
604static int perf_sample__fprintf_uregs(struct perf_sample *sample,
605 struct perf_event_attr *attr, FILE *fp)
606{
607 return perf_sample__fprintf_regs(&sample->user_regs,
608 attr->sample_regs_user, fp);
609}
610
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300611static int perf_sample__fprintf_start(struct perf_sample *sample,
612 struct thread *thread,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100613 struct perf_evsel *evsel,
614 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700615{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300616 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700617 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700618 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300619 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700620
David Ahern745f43e2011-03-09 22:23:26 -0700621 if (PRINT_FIELD(COMM)) {
622 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300623 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300624 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300625 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700626 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300627 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700628 }
David Ahernc70c94b2011-03-09 22:23:25 -0700629
David Ahern745f43e2011-03-09 22:23:26 -0700630 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300631 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700632 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300633 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700634 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300635 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700636
David Ahern745f43e2011-03-09 22:23:26 -0700637 if (PRINT_FIELD(CPU)) {
638 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300639 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700640 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300641 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700642 }
David Ahernc70c94b2011-03-09 22:23:25 -0700643
Jiri Olsa28a0b392018-01-07 17:03:52 +0100644 if (PRINT_FIELD(MISC)) {
645 int ret = 0;
646
647 #define has(m) \
648 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
649
650 if (has(KERNEL))
651 ret += fprintf(fp, "K");
652 if (has(USER))
653 ret += fprintf(fp, "U");
654 if (has(HYPERVISOR))
655 ret += fprintf(fp, "H");
656 if (has(GUEST_KERNEL))
657 ret += fprintf(fp, "G");
658 if (has(GUEST_USER))
659 ret += fprintf(fp, "g");
660
661 switch (type) {
662 case PERF_RECORD_MMAP:
663 case PERF_RECORD_MMAP2:
664 if (has(MMAP_DATA))
665 ret += fprintf(fp, "M");
666 break;
667 case PERF_RECORD_COMM:
668 if (has(COMM_EXEC))
669 ret += fprintf(fp, "E");
670 break;
671 case PERF_RECORD_SWITCH:
672 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300673 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100674 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300675 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
676 ret += fprintf(fp, "p");
677 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100678 default:
679 break;
680 }
681
682 #undef has
683
684 ret += fprintf(fp, "%*s", 6 - ret, " ");
685 printed += ret;
686 }
687
David Ahern745f43e2011-03-09 22:23:26 -0700688 if (PRINT_FIELD(TIME)) {
689 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300690 secs = nsecs / NSEC_PER_SEC;
691 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900692
Andi Kleen52bab882019-03-05 06:47:47 -0800693 if (symbol_conf.nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300694 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900695 else {
696 char sample_time[32];
697 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300698 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900699 }
David Ahern745f43e2011-03-09 22:23:26 -0700700 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300701
702 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700703}
704
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200705static inline char
706mispred_str(struct branch_entry *br)
707{
708 if (!(br->flags.mispred || br->flags.predicted))
709 return '-';
710
711 return br->flags.predicted ? 'P' : 'M';
712}
713
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300714static int perf_sample__fprintf_brstack(struct perf_sample *sample,
715 struct thread *thread,
716 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200717{
718 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700719 struct addr_location alf, alt;
720 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300721 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200722
723 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300724 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200725
726 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700727 from = br->entries[i].from;
728 to = br->entries[i].to;
729
730 if (PRINT_FIELD(DSO)) {
731 memset(&alf, 0, sizeof(alf));
732 memset(&alt, 0, sizeof(alt));
Adrian Hunter692d0e62018-11-06 23:07:12 +0200733 thread__find_map_fb(thread, sample->cpumode, from, &alf);
734 thread__find_map_fb(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700735 }
736
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300737 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700738 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300739 printed += fprintf(fp, "(");
740 printed += map__fprintf_dsoname(alf.map, fp);
741 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700742 }
743
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300744 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700745 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300746 printed += fprintf(fp, "(");
747 printed += map__fprintf_dsoname(alt.map, fp);
748 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700749 }
750
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300751 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200752 mispred_str( br->entries + i),
753 br->entries[i].flags.in_tx? 'X' : '-',
754 br->entries[i].flags.abort? 'A' : '-',
755 br->entries[i].flags.cycles);
756 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300757
758 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200759}
760
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300761static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
762 struct thread *thread,
763 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200764{
765 struct branch_stack *br = sample->branch_stack;
766 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200767 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300768 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200769
770 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300771 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200772
773 for (i = 0; i < br->nr; i++) {
774
775 memset(&alf, 0, sizeof(alf));
776 memset(&alt, 0, sizeof(alt));
777 from = br->entries[i].from;
778 to = br->entries[i].to;
779
Adrian Hunter692d0e62018-11-06 23:07:12 +0200780 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
781 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200782
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300783 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700784 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300785 printed += fprintf(fp, "(");
786 printed += map__fprintf_dsoname(alf.map, fp);
787 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700788 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300789 printed += fprintf(fp, "%c", '/');
790 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700791 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300792 printed += fprintf(fp, "(");
793 printed += map__fprintf_dsoname(alt.map, fp);
794 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700795 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300796 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200797 mispred_str( br->entries + i),
798 br->entries[i].flags.in_tx? 'X' : '-',
799 br->entries[i].flags.abort? 'A' : '-',
800 br->entries[i].flags.cycles);
801 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300802
803 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200804}
805
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300806static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
807 struct thread *thread,
808 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700809{
810 struct branch_stack *br = sample->branch_stack;
811 struct addr_location alf, alt;
812 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300813 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700814
815 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300816 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700817
818 for (i = 0; i < br->nr; i++) {
819
820 memset(&alf, 0, sizeof(alf));
821 memset(&alt, 0, sizeof(alt));
822 from = br->entries[i].from;
823 to = br->entries[i].to;
824
Adrian Hunter692d0e62018-11-06 23:07:12 +0200825 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300826 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700827 from = map__map_ip(alf.map, from);
828
Adrian Hunter692d0e62018-11-06 23:07:12 +0200829 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300830 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700831 to = map__map_ip(alt.map, to);
832
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300833 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700834 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300835 printed += fprintf(fp, "(");
836 printed += map__fprintf_dsoname(alf.map, fp);
837 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700838 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300839 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700840 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300841 printed += fprintf(fp, "(");
842 printed += map__fprintf_dsoname(alt.map, fp);
843 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700844 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300845 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700846 mispred_str(br->entries + i),
847 br->entries[i].flags.in_tx ? 'X' : '-',
848 br->entries[i].flags.abort ? 'A' : '-',
849 br->entries[i].flags.cycles);
850 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300851
852 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700853}
Andi Kleen48d02a12017-02-23 15:46:34 -0800854#define MAXBB 16384UL
855
856static int grab_bb(u8 *buffer, u64 start, u64 end,
857 struct machine *machine, struct thread *thread,
858 bool *is64bit, u8 *cpumode, bool last)
859{
860 long offset, len;
861 struct addr_location al;
862 bool kernel;
863
864 if (!start || !end)
865 return 0;
866
867 kernel = machine__kernel_ip(machine, start);
868 if (kernel)
869 *cpumode = PERF_RECORD_MISC_KERNEL;
870 else
871 *cpumode = PERF_RECORD_MISC_USER;
872
873 /*
874 * Block overlaps between kernel and user.
875 * This can happen due to ring filtering
876 * On Intel CPUs the entry into the kernel is filtered,
877 * but the exit is not. Let the caller patch it up.
878 */
879 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300880 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800881 return -ENXIO;
882 }
883
884 memset(&al, 0, sizeof(al));
885 if (end - start > MAXBB - MAXINSN) {
886 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300887 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800888 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300889 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800890 return 0;
891 }
892
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300893 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
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 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300898 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800899 return 0;
900 }
901
902 /* Load maps to ensure dso->is_64_bit has been updated */
903 map__load(al.map);
904
905 offset = al.map->map_ip(al.map, start);
906 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
907 end - start + MAXINSN);
908
909 *is64bit = al.map->dso->is_64_bit;
910 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300911 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800912 start, end);
913 return len;
914}
915
Andi Kleendd2e18e2018-12-03 16:18:48 -0800916static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
917{
918 struct addr_location al;
919 int ret = 0;
920
921 memset(&al, 0, sizeof(al));
922 thread__find_map(thread, cpumode, addr, &al);
923 if (!al.map)
924 return 0;
925 ret = map__fprintf_srccode(al.map, al.addr, stdout,
926 &thread->srccode_state);
927 if (ret)
928 ret += printf("\n");
929 return ret;
930}
931
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300932static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
933 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -0700934 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -0800935{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300936 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
937 dump_insn(x, ip, inbuf, len, NULL),
938 en->flags.predicted ? " PRED" : "",
939 en->flags.mispred ? " MISPRED" : "",
940 en->flags.in_tx ? " INTX" : "",
941 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800942 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -0700943 *total_cycles += en->flags.cycles;
944 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800945 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300946 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800947 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300948 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800949}
950
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300951static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
952 u8 cpumode, int cpu, struct symbol **lastsym,
953 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800954{
955 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300956 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800957
958 memset(&al, 0, sizeof(al));
959
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300960 thread__find_map(thread, cpumode, addr, &al);
961
Andi Kleen48d02a12017-02-23 15:46:34 -0800962 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300963 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800964
965 al.cpu = cpu;
966 al.sym = NULL;
967 if (al.map)
968 al.sym = map__find_symbol(al.map, al.addr);
969
970 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300971 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800972
973 if (al.addr < al.sym->end)
974 off = al.addr - al.sym->start;
975 else
976 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300977 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800978 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300979 printed += fprintf(fp, "%+d", off);
980 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800981 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300982 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
983 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800984 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300985
986 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800987}
988
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300989static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
990 struct thread *thread,
991 struct perf_event_attr *attr,
992 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800993{
994 struct branch_stack *br = sample->branch_stack;
995 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300996 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800997 struct perf_insn x;
998 u8 buffer[MAXBB];
999 unsigned off;
1000 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -07001001 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001002
1003 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001004 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001005 nr = br->nr;
1006 if (max_blocks && nr > max_blocks + 1)
1007 nr = max_blocks + 1;
1008
1009 x.thread = thread;
1010 x.cpu = sample->cpu;
1011
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001012 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -08001013
1014 /* Handle first from jump, of which we don't know the entry. */
1015 len = grab_bb(buffer, br->entries[nr-1].from,
1016 br->entries[nr-1].from,
1017 machine, thread, &x.is64bit, &x.cpumode, false);
1018 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001019 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1020 x.cpumode, x.cpu, &lastsym, attr, fp);
1021 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001022 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001023 if (PRINT_FIELD(SRCCODE))
1024 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
Andi Kleen48d02a12017-02-23 15:46:34 -08001025 }
1026
1027 /* Print all blocks */
1028 for (i = nr - 2; i >= 0; i--) {
1029 if (br->entries[i].from || br->entries[i].to)
1030 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1031 br->entries[i].from,
1032 br->entries[i].to);
1033 start = br->entries[i + 1].to;
1034 end = br->entries[i].from;
1035
1036 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1037 /* Patch up missing kernel transfers due to ring filters */
1038 if (len == -ENXIO && i > 0) {
1039 end = br->entries[--i].from;
1040 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1041 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1042 }
1043 if (len <= 0)
1044 continue;
1045
1046 insn = 0;
1047 for (off = 0;; off += ilen) {
1048 uint64_t ip = start + off;
1049
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001050 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001051 if (ip == end) {
Andi Kleenfe571202018-09-24 10:07:32 -07001052 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp,
1053 &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001054 if (PRINT_FIELD(SRCCODE))
1055 printed += print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001056 break;
1057 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001058 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1059 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001060 if (ilen == 0)
1061 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001062 if (PRINT_FIELD(SRCCODE))
1063 print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001064 insn++;
1065 }
1066 }
1067 }
1068
1069 /*
1070 * Hit the branch? In this case we are already done, and the target
1071 * has not been executed yet.
1072 */
1073 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001074 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001075 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001076 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001077
1078 /*
1079 * Print final block upto sample
Andi Kleen61f61152018-11-19 21:06:17 -08001080 *
1081 * Due to pipeline delays the LBRs might be missing a branch
1082 * or two, which can result in very large or negative blocks
1083 * between final branch and sample. When this happens just
1084 * continue walking after the last TO until we hit a branch.
Andi Kleen48d02a12017-02-23 15:46:34 -08001085 */
1086 start = br->entries[0].to;
1087 end = sample->ip;
Andi Kleen61f61152018-11-19 21:06:17 -08001088 if (end < start) {
1089 /* Missing jump. Scan 128 bytes for the next branch */
1090 end = start + 128;
1091 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001092 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001093 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001094 if (len <= 0) {
1095 /* Print at least last IP if basic block did not work */
1096 len = grab_bb(buffer, sample->ip, sample->ip,
1097 machine, thread, &x.is64bit, &x.cpumode, false);
1098 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001099 goto out;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001100 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001101 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001102 if (PRINT_FIELD(SRCCODE))
1103 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001104 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001105 }
1106 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001107 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1108 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001109 if (ilen == 0)
1110 break;
Andi Kleen61f61152018-11-19 21:06:17 -08001111 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1112 /*
1113 * Hit a missing branch. Just stop.
1114 */
1115 printed += fprintf(fp, "\t... not reaching sample ...\n");
1116 break;
1117 }
Andi Kleendd2e18e2018-12-03 16:18:48 -08001118 if (PRINT_FIELD(SRCCODE))
1119 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001120 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001121out:
1122 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001123}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001124
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001125static int perf_sample__fprintf_addr(struct perf_sample *sample,
1126 struct thread *thread,
1127 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001128{
1129 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001130 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001131
1132 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001133 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001134
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001135 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001136
1137 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001138 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001139 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001140 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001141 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001142 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001143 }
1144
1145 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001146 printed += fprintf(fp, " (");
1147 printed += map__fprintf_dsoname(al.map, fp);
1148 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001149 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001150out:
1151 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001152}
1153
Andi Kleen99f753f2018-09-20 11:05:39 -07001154static const char *resolve_branch_sym(struct perf_sample *sample,
1155 struct perf_evsel *evsel,
1156 struct thread *thread,
1157 struct addr_location *al,
1158 u64 *ip)
1159{
1160 struct addr_location addr_al;
1161 struct perf_event_attr *attr = &evsel->attr;
1162 const char *name = NULL;
1163
1164 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1165 if (sample_addr_correlates_sym(attr)) {
1166 thread__resolve(thread, &addr_al, sample);
1167 if (addr_al.sym)
1168 name = addr_al.sym->name;
1169 else
1170 *ip = sample->addr;
1171 } else {
1172 *ip = sample->addr;
1173 }
1174 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1175 if (al->sym)
1176 name = al->sym->name;
1177 else
1178 *ip = sample->ip;
1179 }
1180 return name;
1181}
1182
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001183static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1184 struct perf_evsel *evsel,
1185 struct thread *thread,
1186 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001187{
1188 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter256d92b2018-12-21 14:06:19 +02001189 size_t depth = thread_stack__depth(thread, sample->cpu);
Adrian Huntere2167082016-06-23 16:40:58 +03001190 const char *name = NULL;
1191 static int spacing;
1192 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001193 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001194 u64 ip = 0;
1195
1196 /*
1197 * The 'return' has already been popped off the stack so the depth has
1198 * to be adjusted to match the 'call'.
1199 */
1200 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1201 depth += 1;
1202
Andi Kleen99f753f2018-09-20 11:05:39 -07001203 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001204
Andi Kleena78cdee2018-09-18 05:32:10 -07001205 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1206 dlen += fprintf(fp, "(");
1207 dlen += map__fprintf_dsoname(al->map, fp);
1208 dlen += fprintf(fp, ")\t");
1209 }
1210
Adrian Huntere2167082016-06-23 16:40:58 +03001211 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001212 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001213 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001214 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001215
1216 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001217 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001218
1219 /*
1220 * Try to keep the output length from changing frequently so that the
1221 * output lines up more nicely.
1222 */
1223 if (len > spacing || (len && len < spacing - 52))
1224 spacing = round_up(len + 4, 32);
1225
1226 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001227 len += fprintf(fp, "%*s", spacing - len, "");
1228
Andi Kleena78cdee2018-09-18 05:32:10 -07001229 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001230}
1231
Andi Kleen3ab481a2019-03-05 06:47:45 -08001232__weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1233 struct thread *thread __maybe_unused,
1234 struct machine *machine __maybe_unused)
1235{
1236}
1237
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001238static int perf_sample__fprintf_insn(struct perf_sample *sample,
1239 struct perf_event_attr *attr,
1240 struct thread *thread,
1241 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001242{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001243 int printed = 0;
1244
Andi Kleen3ab481a2019-03-05 06:47:45 -08001245 if (sample->insn_len == 0 && native_arch)
1246 arch_fetch_insn(sample, thread, machine);
1247
Andi Kleen224e2c92016-10-07 16:42:27 +03001248 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001249 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen3ab481a2019-03-05 06:47:45 -08001250 if (PRINT_FIELD(INSN) && sample->insn_len) {
Andi Kleen224e2c92016-10-07 16:42:27 +03001251 int i;
1252
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001253 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001254 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001255 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001256 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001257 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001258 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1259
1260 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001261}
1262
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001263static int perf_sample__fprintf_bts(struct perf_sample *sample,
1264 struct perf_evsel *evsel,
1265 struct thread *thread,
1266 struct addr_location *al,
1267 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001268{
1269 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001270 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001271 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001272 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001273
Adrian Huntere2167082016-06-23 16:40:58 +03001274 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001275 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001276
Akihiro Nagai95582592012-01-30 13:43:09 +09001277 /* print branch_from information */
1278 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001279 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001280 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001281
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001282 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001283 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001284 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001285 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001286
1287 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001288 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001289 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001290 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001291 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001292 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001293 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001294 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001295
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001296 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001297 }
1298
Akihiro Nagai95582592012-01-30 13:43:09 +09001299 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001300 if (PRINT_FIELD(ADDR) ||
1301 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001302 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001303 printed += fprintf(fp, " => ");
1304 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001305 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001306
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001307 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001308 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001309
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001310 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001311 printed += fprintf(fp, "\n");
1312 if (PRINT_FIELD(SRCCODE)) {
1313 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1314 &thread->srccode_state);
1315 if (ret) {
1316 printed += ret;
1317 printed += printf("\n");
1318 }
1319 }
1320 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001321}
1322
Adrian Hunter055cd332016-06-23 16:40:56 +03001323static struct {
1324 u32 flags;
1325 const char *name;
1326} sample_flags[] = {
1327 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1328 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1329 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1330 {PERF_IP_FLAG_BRANCH, "jmp"},
1331 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1332 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1333 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1334 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1335 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1336 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1337 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1338 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1339 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1340 {0, NULL}
1341};
1342
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001343static const char *sample_flags_to_name(u32 flags)
1344{
1345 int i;
1346
1347 for (i = 0; sample_flags[i].name ; i++) {
1348 if (sample_flags[i].flags == flags)
1349 return sample_flags[i].name;
1350 }
1351
1352 return NULL;
1353}
1354
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001355static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001356{
1357 const char *chars = PERF_IP_FLAG_CHARS;
1358 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001359 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1360 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001361 char str[33];
1362 int i, pos = 0;
1363
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001364 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1365 if (name)
1366 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1367
1368 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1369 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1370 if (name)
1371 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1372 }
1373
1374 if (flags & PERF_IP_FLAG_TRACE_END) {
1375 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1376 if (name)
1377 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001378 }
1379
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001380 for (i = 0; i < n; i++, flags >>= 1) {
1381 if (flags & 1)
1382 str[pos++] = chars[i];
1383 }
1384 for (; i < 32; i++, flags >>= 1) {
1385 if (flags & 1)
1386 str[pos++] = '?';
1387 }
1388 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001389
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001390 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001391}
1392
Wang Nan30372f02016-02-24 11:20:45 +00001393struct printer_data {
1394 int line_no;
1395 bool hit_nul;
1396 bool is_printable;
1397};
1398
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001399static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1400 unsigned int val,
1401 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001402{
1403 unsigned char ch = (unsigned char)val;
1404 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001405 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001406
1407 switch (op) {
1408 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001409 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001410 break;
1411 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001412 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001413 " ");
1414 break;
1415 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001416 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001417 break;
1418 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001419 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001420 break;
1421 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001422 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001423 break;
1424 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001425 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001426 break;
1427 case BINARY_PRINT_CHAR_DATA:
1428 if (printer_data->hit_nul && ch)
1429 printer_data->is_printable = false;
1430
1431 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001432 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001433
1434 if (!printer_data->is_printable)
1435 break;
1436
1437 if (ch == '\0')
1438 printer_data->hit_nul = true;
1439 else
1440 printer_data->is_printable = false;
1441 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001442 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001443 }
1444 break;
1445 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001446 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001447 break;
1448 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001449 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001450 printer_data->line_no++;
1451 break;
1452 case BINARY_PRINT_DATA_END:
1453 default:
1454 break;
1455 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001456
1457 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001458}
1459
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001460static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001461{
1462 unsigned int nr_bytes = sample->raw_size;
1463 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001464 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1465 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001466
1467 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001468 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1469
1470 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001471}
1472
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001473static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001474{
1475 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001476 return fprintf(fp, "%*s", spacing - len, "");
1477
1478 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001479}
1480
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001481static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001482{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001483 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001484}
1485
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001486static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001487{
1488 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1489 int len;
1490
1491 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001492 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001493
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001494 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001495 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001496 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001497}
1498
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001499static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001500{
1501 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1502 int len;
1503
1504 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001505 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001506
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001507 len = fprintf(fp, " hints: %#x extensions: %#x ",
1508 data->hints, data->extensions);
1509 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001510}
1511
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001512static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001513{
1514 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1515 int len;
1516
1517 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001518 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001519
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001520 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1521 data->hw, data->cstate, data->subcstate);
1522 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001523}
1524
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001525static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001526{
1527 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1528 int len;
1529
1530 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001531 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001532
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001533 len = fprintf(fp, " IP: %u ", data->ip);
1534 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001535}
1536
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001537static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001538{
1539 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1540 int len;
1541
1542 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001543 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001544
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001545 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001546 data->deepest_cstate, data->last_cstate,
1547 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001548 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001549}
1550
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001551static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001552{
1553 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1554 unsigned int percent, freq;
1555 int len;
1556
1557 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001558 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001559
1560 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001561 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001562 if (data->max_nonturbo) {
1563 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001564 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001565 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001566 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001567}
1568
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001569static int perf_sample__fprintf_synth(struct perf_sample *sample,
1570 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001571{
1572 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001573 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001574 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001575 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001576 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001577 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001578 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001579 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001580 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001581 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001582 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001583 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001584 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001585 default:
1586 break;
1587 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001588
1589 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001590}
1591
Jiri Olsa809e9422015-11-26 18:55:21 +01001592struct perf_script {
1593 struct perf_tool tool;
1594 struct perf_session *session;
1595 bool show_task_events;
1596 bool show_mmap_events;
1597 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301598 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001599 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001600 bool show_round_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001601 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001602 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001603 struct cpu_map *cpus;
1604 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001605 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001606 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001607 struct perf_time_interval *ptime_range;
1608 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001609 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001610};
1611
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001612static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1613{
1614 struct perf_evsel *evsel;
1615 int max = 0;
1616
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001617 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001618 int len = strlen(perf_evsel__name(evsel));
1619
1620 max = MAX(len, max);
1621 }
1622
1623 return max;
1624}
1625
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001626static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001627{
1628 struct mem_info mi = { .data_src.val = data_src };
1629 char decode[100];
1630 char out[100];
1631 static int maxlen;
1632 int len;
1633
1634 perf_script__meminfo_scnprintf(decode, 100, &mi);
1635
1636 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1637 if (maxlen < len)
1638 maxlen = len;
1639
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001640 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001641}
1642
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001643struct metric_ctx {
1644 struct perf_sample *sample;
1645 struct thread *thread;
1646 struct perf_evsel *evsel;
1647 FILE *fp;
1648};
1649
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001650static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1651 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001652 const char *fmt,
1653 const char *unit, double val)
1654{
1655 struct metric_ctx *mctx = ctx;
1656
1657 if (!fmt)
1658 return;
1659 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001660 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001661 fputs("\tmetric: ", mctx->fp);
1662 if (color)
1663 color_fprintf(mctx->fp, color, fmt, val);
1664 else
1665 printf(fmt, val);
1666 fprintf(mctx->fp, " %s\n", unit);
1667}
1668
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001669static void script_new_line(struct perf_stat_config *config __maybe_unused,
1670 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001671{
1672 struct metric_ctx *mctx = ctx;
1673
1674 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001675 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001676 fputs("\tmetric: ", mctx->fp);
1677}
1678
1679static void perf_sample__fprint_metric(struct perf_script *script,
1680 struct thread *thread,
1681 struct perf_evsel *evsel,
1682 struct perf_sample *sample,
1683 FILE *fp)
1684{
1685 struct perf_stat_output_ctx ctx = {
1686 .print_metric = script_print_metric,
1687 .new_line = script_new_line,
1688 .ctx = &(struct metric_ctx) {
1689 .sample = sample,
1690 .thread = thread,
1691 .evsel = evsel,
1692 .fp = fp,
1693 },
1694 .force_header = false,
1695 };
1696 struct perf_evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001697 u64 val;
1698
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001699 if (!evsel->stats)
1700 perf_evlist__alloc_stats(script->session->evlist, false);
1701 if (evsel_script(evsel->leader)->gnum++ == 0)
1702 perf_stat__reset_shadow_stats();
1703 val = sample->period * evsel->scale;
1704 perf_stat__update_shadow_stats(evsel,
1705 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001706 sample->cpu,
1707 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001708 evsel_script(evsel)->val = val;
1709 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1710 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001711 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001712 evsel_script(ev2)->val,
1713 sample->cpu,
1714 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001715 NULL,
1716 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001717 }
1718 evsel_script(evsel->leader)->gnum = 0;
1719 }
1720}
1721
Andi Kleen99f753f2018-09-20 11:05:39 -07001722static bool show_event(struct perf_sample *sample,
1723 struct perf_evsel *evsel,
1724 struct thread *thread,
1725 struct addr_location *al)
1726{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001727 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001728
1729 if (!symbol_conf.graph_function)
1730 return true;
1731
1732 if (thread->filter) {
1733 if (depth <= thread->filter_entry_depth) {
1734 thread->filter = false;
1735 return false;
1736 }
1737 return true;
1738 } else {
1739 const char *s = symbol_conf.graph_function;
1740 u64 ip;
1741 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1742 &ip);
1743 unsigned nlen;
1744
1745 if (!name)
1746 return false;
1747 nlen = strlen(name);
1748 while (*s) {
1749 unsigned len = strcspn(s, ",");
1750 if (nlen == len && !strncmp(name, s, len)) {
1751 thread->filter = true;
1752 thread->filter_entry_depth = depth;
1753 return true;
1754 }
1755 s += len;
1756 if (*s == ',')
1757 s++;
1758 }
1759 return false;
1760 }
1761}
1762
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001763static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001764 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001765 struct addr_location *al,
1766 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001767{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001768 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001769 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001770 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001771 struct perf_evsel_script *es = evsel->priv;
1772 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001773
Adrian Hunter14057202017-06-21 13:17:19 +03001774 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001775 return;
1776
Andi Kleen99f753f2018-09-20 11:05:39 -07001777 if (!show_event(sample, evsel, thread, al))
1778 return;
1779
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001780 ++es->samples;
1781
Jiri Olsa28a0b392018-01-07 17:03:52 +01001782 perf_sample__fprintf_start(sample, thread, evsel,
1783 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001784
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001785 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001786 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001787
Namhyung Kime944d3d2013-11-18 14:34:52 +09001788 if (PRINT_FIELD(EVNAME)) {
1789 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001790
1791 if (!script->name_width)
1792 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1793
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001794 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001795 }
1796
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001797 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001798 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001799
Akihiro Nagai95582592012-01-30 13:43:09 +09001800 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001801 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001802 return;
1803 }
1804
Andi Kleen96167162019-01-17 11:48:34 -08001805 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001806 event_format__fprintf(evsel->tp_format, sample->cpu,
1807 sample->raw_data, sample->raw_size, fp);
1808 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001809
1810 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001811 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001812
David Ahern7cec0922011-05-30 13:08:23 -06001813 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001814 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001815
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001816 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001817 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001818
1819 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001820 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001821
David Ahern787bef12011-05-27 14:28:43 -06001822 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001823 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001824
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001825 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001826 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001827 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001828 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001829
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001830 fputc(cursor ? '\n' : ' ', fp);
1831 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001832 }
1833
Stephane Eranianfc36f942015-08-31 18:41:10 +02001834 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001835 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001836
Andi Kleenb1491ac2017-09-05 11:40:57 -07001837 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001838 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001839
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001840 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001841 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001842 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001843 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001844 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001845 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001846
Wang Nan30372f02016-02-24 11:20:45 +00001847 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001848 perf_sample__fprintf_bpf_output(sample, fp);
1849 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001850
1851 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001852 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1853 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001854
Andi Kleendd2e18e2018-12-03 16:18:48 -08001855 if (PRINT_FIELD(SRCCODE)) {
1856 if (map__fprintf_srccode(al->map, al->addr, stdout,
1857 &thread->srccode_state))
1858 printf("\n");
1859 }
1860
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001861 if (PRINT_FIELD(METRIC))
1862 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001863
1864 if (verbose)
1865 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001866}
1867
Tom Zanussi956ffd02009-11-25 01:15:46 -06001868static struct scripting_ops *scripting_ops;
1869
Jiri Olsa36e33c52016-01-06 11:49:56 +01001870static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1871{
1872 int nthreads = thread_map__nr(counter->threads);
1873 int ncpus = perf_evsel__nr_cpus(counter);
1874 int cpu, thread;
1875 static int header_printed;
1876
1877 if (counter->system_wide)
1878 nthreads = 1;
1879
1880 if (!header_printed) {
1881 printf("%3s %8s %15s %15s %15s %15s %s\n",
1882 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1883 header_printed = 1;
1884 }
1885
1886 for (thread = 0; thread < nthreads; thread++) {
1887 for (cpu = 0; cpu < ncpus; cpu++) {
1888 struct perf_counts_values *counts;
1889
1890 counts = perf_counts(counter->counts, cpu, thread);
1891
1892 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1893 counter->cpus->map[cpu],
1894 thread_map__pid(counter->threads, thread),
1895 counts->val,
1896 counts->ena,
1897 counts->run,
1898 tstamp,
1899 perf_evsel__name(counter));
1900 }
1901 }
1902}
1903
Jiri Olsae099eba2016-01-05 22:09:09 +01001904static void process_stat(struct perf_evsel *counter, u64 tstamp)
1905{
1906 if (scripting_ops && scripting_ops->process_stat)
1907 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001908 else
1909 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001910}
1911
1912static void process_stat_interval(u64 tstamp)
1913{
1914 if (scripting_ops && scripting_ops->process_stat_interval)
1915 scripting_ops->process_stat_interval(tstamp);
1916}
1917
Tom Zanussi956ffd02009-11-25 01:15:46 -06001918static void setup_scripting(void)
1919{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001920 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001921 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001922}
1923
Adrian Hunterd445dd22014-08-15 22:08:37 +03001924static int flush_scripting(void)
1925{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001926 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001927}
1928
Tom Zanussi956ffd02009-11-25 01:15:46 -06001929static int cleanup_scripting(void)
1930{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001931 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001932
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001933 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001934}
1935
Andi Kleene87e5482019-03-11 07:44:52 -07001936static bool filter_cpu(struct perf_sample *sample)
1937{
1938 if (cpu_list)
1939 return !test_bit(sample->cpu, cpu_bitmap);
1940 return false;
1941}
1942
Jiri Olsa809e9422015-11-26 18:55:21 +01001943static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001944 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001945 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001946 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001947 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001948{
Jiri Olsa809e9422015-11-26 18:55:21 +01001949 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001950 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001951
Jin Yao2ab046c2017-12-08 21:13:46 +08001952 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1953 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001954 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001955 }
David Aherna91f4c42016-11-29 10:15:43 -07001956
David Ahern1424dc92011-03-09 22:23:28 -07001957 if (debug_mode) {
1958 if (sample->time < last_timestamp) {
1959 pr_err("Samples misordered, previous: %" PRIu64
1960 " this: %" PRIu64 "\n", last_timestamp,
1961 sample->time);
1962 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001963 }
David Ahern1424dc92011-03-09 22:23:28 -07001964 last_timestamp = sample->time;
1965 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001966 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001967
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001968 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001969 pr_err("problem processing %d event, skipping it.\n",
1970 event->header.type);
1971 return -1;
1972 }
1973
1974 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001975 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001976
Andi Kleene87e5482019-03-11 07:44:52 -07001977 if (filter_cpu(sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001978 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001979
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001980 if (scripting_ops)
1981 scripting_ops->process_event(event, sample, evsel, &al);
1982 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001983 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001984
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001985out_put:
1986 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001987 return 0;
1988}
1989
Adrian Hunter7ea95722013-11-01 15:51:30 +02001990static int process_attr(struct perf_tool *tool, union perf_event *event,
1991 struct perf_evlist **pevlist)
1992{
1993 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1994 struct perf_evlist *evlist;
1995 struct perf_evsel *evsel, *pos;
1996 int err;
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05301997 static struct perf_evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001998
1999 err = perf_event__process_attr(tool, event, pevlist);
2000 if (err)
2001 return err;
2002
2003 evlist = *pevlist;
2004 evsel = perf_evlist__last(*pevlist);
2005
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302006 if (!evsel->priv) {
2007 if (scr->per_event_dump) {
2008 evsel->priv = perf_evsel_script__new(evsel,
2009 scr->session->data);
2010 } else {
2011 es = zalloc(sizeof(*es));
2012 if (!es)
2013 return -ENOMEM;
2014 es->fp = stdout;
2015 evsel->priv = es;
2016 }
2017 }
2018
Adrian Hunter14057202017-06-21 13:17:19 +03002019 if (evsel->attr.type >= PERF_TYPE_MAX &&
2020 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002021 return 0;
2022
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002023 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02002024 if (pos->attr.type == evsel->attr.type && pos != evsel)
2025 return 0;
2026 }
2027
2028 set_print_ip_opts(&evsel->attr);
2029
Jiri Olsad2b5a312015-10-16 12:41:25 +02002030 if (evsel->attr.sample_type)
2031 err = perf_evsel__check_attr(evsel, scr->session);
2032
2033 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002034}
2035
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002036static int process_comm_event(struct perf_tool *tool,
2037 union perf_event *event,
2038 struct perf_sample *sample,
2039 struct machine *machine)
2040{
2041 struct thread *thread;
2042 struct perf_script *script = container_of(tool, struct perf_script, tool);
2043 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002044 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002045 int ret = -1;
2046
2047 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2048 if (thread == NULL) {
2049 pr_debug("problem processing COMM event, skipping it.\n");
2050 return -1;
2051 }
2052
2053 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2054 goto out;
2055
2056 if (!evsel->attr.sample_id_all) {
2057 sample->cpu = 0;
2058 sample->time = 0;
2059 sample->tid = event->comm.tid;
2060 sample->pid = event->comm.pid;
2061 }
Andi Kleene87e5482019-03-11 07:44:52 -07002062 if (!filter_cpu(sample)) {
2063 perf_sample__fprintf_start(sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01002064 PERF_RECORD_COMM, stdout);
Andi Kleene87e5482019-03-11 07:44:52 -07002065 perf_event__fprintf(event, stdout);
2066 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002067 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002068out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002069 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002070 return ret;
2071}
2072
Hari Bathini96a44bb2017-03-08 02:12:06 +05302073static int process_namespaces_event(struct perf_tool *tool,
2074 union perf_event *event,
2075 struct perf_sample *sample,
2076 struct machine *machine)
2077{
2078 struct thread *thread;
2079 struct perf_script *script = container_of(tool, struct perf_script, tool);
2080 struct perf_session *session = script->session;
2081 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2082 int ret = -1;
2083
2084 thread = machine__findnew_thread(machine, event->namespaces.pid,
2085 event->namespaces.tid);
2086 if (thread == NULL) {
2087 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2088 return -1;
2089 }
2090
2091 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2092 goto out;
2093
2094 if (!evsel->attr.sample_id_all) {
2095 sample->cpu = 0;
2096 sample->time = 0;
2097 sample->tid = event->namespaces.tid;
2098 sample->pid = event->namespaces.pid;
2099 }
Andi Kleene87e5482019-03-11 07:44:52 -07002100 if (!filter_cpu(sample)) {
2101 perf_sample__fprintf_start(sample, thread, evsel,
2102 PERF_RECORD_NAMESPACES, stdout);
2103 perf_event__fprintf(event, stdout);
2104 }
Hari Bathini96a44bb2017-03-08 02:12:06 +05302105 ret = 0;
2106out:
2107 thread__put(thread);
2108 return ret;
2109}
2110
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002111static int process_fork_event(struct perf_tool *tool,
2112 union perf_event *event,
2113 struct perf_sample *sample,
2114 struct machine *machine)
2115{
2116 struct thread *thread;
2117 struct perf_script *script = container_of(tool, struct perf_script, tool);
2118 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002119 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002120
2121 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2122 return -1;
2123
2124 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2125 if (thread == NULL) {
2126 pr_debug("problem processing FORK event, skipping it.\n");
2127 return -1;
2128 }
2129
2130 if (!evsel->attr.sample_id_all) {
2131 sample->cpu = 0;
2132 sample->time = event->fork.time;
2133 sample->tid = event->fork.tid;
2134 sample->pid = event->fork.pid;
2135 }
Andi Kleene87e5482019-03-11 07:44:52 -07002136 if (!filter_cpu(sample)) {
2137 perf_sample__fprintf_start(sample, thread, evsel,
2138 PERF_RECORD_FORK, stdout);
2139 perf_event__fprintf(event, stdout);
2140 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002141 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002142
2143 return 0;
2144}
2145static int process_exit_event(struct perf_tool *tool,
2146 union perf_event *event,
2147 struct perf_sample *sample,
2148 struct machine *machine)
2149{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002150 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002151 struct thread *thread;
2152 struct perf_script *script = container_of(tool, struct perf_script, tool);
2153 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002154 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002155
2156 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2157 if (thread == NULL) {
2158 pr_debug("problem processing EXIT event, skipping it.\n");
2159 return -1;
2160 }
2161
2162 if (!evsel->attr.sample_id_all) {
2163 sample->cpu = 0;
2164 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002165 sample->tid = event->fork.tid;
2166 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002167 }
Andi Kleene87e5482019-03-11 07:44:52 -07002168 if (!filter_cpu(sample)) {
2169 perf_sample__fprintf_start(sample, thread, evsel,
2170 PERF_RECORD_EXIT, stdout);
2171 perf_event__fprintf(event, stdout);
2172 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002173
2174 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002175 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002176
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002177 thread__put(thread);
2178 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002179}
2180
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002181static int process_mmap_event(struct perf_tool *tool,
2182 union perf_event *event,
2183 struct perf_sample *sample,
2184 struct machine *machine)
2185{
2186 struct thread *thread;
2187 struct perf_script *script = container_of(tool, struct perf_script, tool);
2188 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002189 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002190
2191 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2192 return -1;
2193
2194 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2195 if (thread == NULL) {
2196 pr_debug("problem processing MMAP event, skipping it.\n");
2197 return -1;
2198 }
2199
2200 if (!evsel->attr.sample_id_all) {
2201 sample->cpu = 0;
2202 sample->time = 0;
2203 sample->tid = event->mmap.tid;
2204 sample->pid = event->mmap.pid;
2205 }
Andi Kleene87e5482019-03-11 07:44:52 -07002206 if (!filter_cpu(sample)) {
2207 perf_sample__fprintf_start(sample, thread, evsel,
2208 PERF_RECORD_MMAP, stdout);
2209 perf_event__fprintf(event, stdout);
2210 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002211 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002212 return 0;
2213}
2214
2215static int process_mmap2_event(struct perf_tool *tool,
2216 union perf_event *event,
2217 struct perf_sample *sample,
2218 struct machine *machine)
2219{
2220 struct thread *thread;
2221 struct perf_script *script = container_of(tool, struct perf_script, tool);
2222 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03002223 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002224
2225 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2226 return -1;
2227
2228 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2229 if (thread == NULL) {
2230 pr_debug("problem processing MMAP2 event, skipping it.\n");
2231 return -1;
2232 }
2233
2234 if (!evsel->attr.sample_id_all) {
2235 sample->cpu = 0;
2236 sample->time = 0;
2237 sample->tid = event->mmap2.tid;
2238 sample->pid = event->mmap2.pid;
2239 }
Andi Kleene87e5482019-03-11 07:44:52 -07002240 if (!filter_cpu(sample)) {
2241 perf_sample__fprintf_start(sample, thread, evsel,
2242 PERF_RECORD_MMAP2, stdout);
2243 perf_event__fprintf(event, stdout);
2244 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002245 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002246 return 0;
2247}
2248
Adrian Hunter7c148982015-07-21 12:44:06 +03002249static int process_switch_event(struct perf_tool *tool,
2250 union perf_event *event,
2251 struct perf_sample *sample,
2252 struct machine *machine)
2253{
2254 struct thread *thread;
2255 struct perf_script *script = container_of(tool, struct perf_script, tool);
2256 struct perf_session *session = script->session;
2257 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2258
2259 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2260 return -1;
2261
2262 thread = machine__findnew_thread(machine, sample->pid,
2263 sample->tid);
2264 if (thread == NULL) {
2265 pr_debug("problem processing SWITCH event, skipping it.\n");
2266 return -1;
2267 }
2268
Andi Kleene87e5482019-03-11 07:44:52 -07002269 if (!filter_cpu(sample)) {
2270 perf_sample__fprintf_start(sample, thread, evsel,
2271 PERF_RECORD_SWITCH, stdout);
2272 perf_event__fprintf(event, stdout);
2273 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002274 thread__put(thread);
2275 return 0;
2276}
2277
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002278static int
2279process_lost_event(struct perf_tool *tool,
2280 union perf_event *event,
2281 struct perf_sample *sample,
2282 struct machine *machine)
2283{
2284 struct perf_script *script = container_of(tool, struct perf_script, tool);
2285 struct perf_session *session = script->session;
2286 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2287 struct thread *thread;
2288
2289 thread = machine__findnew_thread(machine, sample->pid,
2290 sample->tid);
2291 if (thread == NULL)
2292 return -1;
2293
Andi Kleene87e5482019-03-11 07:44:52 -07002294 if (!filter_cpu(sample)) {
2295 perf_sample__fprintf_start(sample, thread, evsel,
2296 PERF_RECORD_LOST, stdout);
2297 perf_event__fprintf(event, stdout);
2298 }
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002299 thread__put(thread);
2300 return 0;
2301}
2302
Jiri Olsa3233b372018-02-06 19:17:59 +01002303static int
2304process_finished_round_event(struct perf_tool *tool __maybe_unused,
2305 union perf_event *event,
2306 struct ordered_events *oe __maybe_unused)
2307
2308{
2309 perf_event__fprintf(event, stdout);
2310 return 0;
2311}
2312
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002313static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002314{
2315 session_done = 1;
2316}
2317
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002318static void perf_script__fclose_per_event_dump(struct perf_script *script)
2319{
2320 struct perf_evlist *evlist = script->session->evlist;
2321 struct perf_evsel *evsel;
2322
2323 evlist__for_each_entry(evlist, evsel) {
2324 if (!evsel->priv)
2325 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002326 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002327 evsel->priv = NULL;
2328 }
2329}
2330
2331static int perf_script__fopen_per_event_dump(struct perf_script *script)
2332{
2333 struct perf_evsel *evsel;
2334
2335 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002336 /*
2337 * Already setup? I.e. we may be called twice in cases like
2338 * Intel PT, one for the intel_pt// and dummy events, then
2339 * for the evsels syntheized from the auxtrace info.
2340 *
2341 * Ses perf_script__process_auxtrace_info.
2342 */
2343 if (evsel->priv != NULL)
2344 continue;
2345
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002346 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002347 if (evsel->priv == NULL)
2348 goto out_err_fclose;
2349 }
2350
2351 return 0;
2352
2353out_err_fclose:
2354 perf_script__fclose_per_event_dump(script);
2355 return -1;
2356}
2357
2358static int perf_script__setup_per_event_dump(struct perf_script *script)
2359{
2360 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002361 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002362
2363 if (script->per_event_dump)
2364 return perf_script__fopen_per_event_dump(script);
2365
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002366 es_stdout.fp = stdout;
2367
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002368 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002369 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002370
2371 return 0;
2372}
2373
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002374static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2375{
2376 struct perf_evsel *evsel;
2377
2378 evlist__for_each_entry(script->session->evlist, evsel) {
2379 struct perf_evsel_script *es = evsel->priv;
2380
2381 perf_evsel_script__fprintf(es, stdout);
2382 perf_evsel_script__delete(es);
2383 evsel->priv = NULL;
2384 }
2385}
2386
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002387static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002388{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002389 int ret;
2390
Tom Zanussic239da32010-04-01 23:59:18 -05002391 signal(SIGINT, sig_handler);
2392
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002393 perf_stat__init_shadow_stats();
2394
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002395 /* override event processing functions */
2396 if (script->show_task_events) {
2397 script->tool.comm = process_comm_event;
2398 script->tool.fork = process_fork_event;
2399 script->tool.exit = process_exit_event;
2400 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002401 if (script->show_mmap_events) {
2402 script->tool.mmap = process_mmap_event;
2403 script->tool.mmap2 = process_mmap2_event;
2404 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002405 if (script->show_switch_events)
2406 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302407 if (script->show_namespace_events)
2408 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002409 if (script->show_lost_events)
2410 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002411 if (script->show_round_events) {
2412 script->tool.ordered_events = false;
2413 script->tool.finished_round = process_finished_round_event;
2414 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002415
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002416 if (perf_script__setup_per_event_dump(script)) {
2417 pr_err("Couldn't create the per event dump files\n");
2418 return -1;
2419 }
2420
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002421 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002422
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002423 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002424 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002425
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002426 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002427 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002428
2429 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002430}
2431
Tom Zanussi956ffd02009-11-25 01:15:46 -06002432struct script_spec {
2433 struct list_head node;
2434 struct scripting_ops *ops;
2435 char spec[0];
2436};
2437
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002438static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002439
2440static struct script_spec *script_spec__new(const char *spec,
2441 struct scripting_ops *ops)
2442{
2443 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2444
2445 if (s != NULL) {
2446 strcpy(s->spec, spec);
2447 s->ops = ops;
2448 }
2449
2450 return s;
2451}
2452
Tom Zanussi956ffd02009-11-25 01:15:46 -06002453static void script_spec__add(struct script_spec *s)
2454{
2455 list_add_tail(&s->node, &script_specs);
2456}
2457
2458static struct script_spec *script_spec__find(const char *spec)
2459{
2460 struct script_spec *s;
2461
2462 list_for_each_entry(s, &script_specs, node)
2463 if (strcasecmp(s->spec, spec) == 0)
2464 return s;
2465 return NULL;
2466}
2467
Tom Zanussi956ffd02009-11-25 01:15:46 -06002468int script_spec_register(const char *spec, struct scripting_ops *ops)
2469{
2470 struct script_spec *s;
2471
2472 s = script_spec__find(spec);
2473 if (s)
2474 return -1;
2475
Taeung Song8560bae2016-02-26 00:13:10 +09002476 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002477 if (!s)
2478 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002479 else
2480 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002481
2482 return 0;
2483}
2484
2485static struct scripting_ops *script_spec__lookup(const char *spec)
2486{
2487 struct script_spec *s = script_spec__find(spec);
2488 if (!s)
2489 return NULL;
2490
2491 return s->ops;
2492}
2493
2494static void list_available_languages(void)
2495{
2496 struct script_spec *s;
2497
2498 fprintf(stderr, "\n");
2499 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002500 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002501
2502 list_for_each_entry(s, &script_specs, node)
2503 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2504
2505 fprintf(stderr, "\n");
2506}
2507
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002508static int parse_scriptname(const struct option *opt __maybe_unused,
2509 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002510{
2511 char spec[PATH_MAX];
2512 const char *script, *ext;
2513 int len;
2514
Tom Zanussif526d682010-01-27 02:27:52 -06002515 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002516 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002517 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002518 }
2519
2520 script = strchr(str, ':');
2521 if (script) {
2522 len = script - str;
2523 if (len >= PATH_MAX) {
2524 fprintf(stderr, "invalid language specifier");
2525 return -1;
2526 }
2527 strncpy(spec, str, len);
2528 spec[len] = '\0';
2529 scripting_ops = script_spec__lookup(spec);
2530 if (!scripting_ops) {
2531 fprintf(stderr, "invalid language specifier");
2532 return -1;
2533 }
2534 script++;
2535 } else {
2536 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002537 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002538 if (!ext) {
2539 fprintf(stderr, "invalid script extension");
2540 return -1;
2541 }
2542 scripting_ops = script_spec__lookup(++ext);
2543 if (!scripting_ops) {
2544 fprintf(stderr, "invalid script extension");
2545 return -1;
2546 }
2547 }
2548
2549 script_name = strdup(script);
2550
2551 return 0;
2552}
2553
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002554static int parse_output_fields(const struct option *opt __maybe_unused,
2555 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002556{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002557 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002558 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002559 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002560 int rc = 0;
2561 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002562 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002563 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002564
2565 if (!str)
2566 return -ENOMEM;
2567
David Ahern2c9e45f72011-03-17 10:03:21 -06002568 /* first word can state for which event type the user is specifying
2569 * the fields. If no type exists, the specified fields apply to all
2570 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002571 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002572 tok = strchr(str, ':');
2573 if (tok) {
2574 *tok = '\0';
2575 tok++;
2576 if (!strcmp(str, "hw"))
2577 type = PERF_TYPE_HARDWARE;
2578 else if (!strcmp(str, "sw"))
2579 type = PERF_TYPE_SOFTWARE;
2580 else if (!strcmp(str, "trace"))
2581 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002582 else if (!strcmp(str, "raw"))
2583 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002584 else if (!strcmp(str, "break"))
2585 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002586 else if (!strcmp(str, "synth"))
2587 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002588 else {
2589 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002590 rc = -EINVAL;
2591 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002592 }
2593
2594 if (output[type].user_set)
2595 pr_warning("Overriding previous field request for %s events.\n",
2596 event_type(type));
2597
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002598 /* Don't override defaults for +- */
2599 if (strchr(tok, '+') || strchr(tok, '-'))
2600 goto parse;
2601
David Ahern2c9e45f72011-03-17 10:03:21 -06002602 output[type].fields = 0;
2603 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002604 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002605
2606 } else {
2607 tok = str;
2608 if (strlen(str) == 0) {
2609 fprintf(stderr,
2610 "Cannot set fields to 'none' for all event types.\n");
2611 rc = -EINVAL;
2612 goto out;
2613 }
2614
Andi Kleen36ce5652017-06-02 08:48:10 -07002615 /* Don't override defaults for +- */
2616 if (strchr(str, '+') || strchr(str, '-'))
2617 goto parse;
2618
David Ahern2c9e45f72011-03-17 10:03:21 -06002619 if (output_set_by_user())
2620 pr_warning("Overriding previous field request for all events.\n");
2621
Adrian Hunter14057202017-06-21 13:17:19 +03002622 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002623 output[j].fields = 0;
2624 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002625 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002626 }
David Ahern1424dc92011-03-09 22:23:28 -07002627 }
2628
Andi Kleen36ce5652017-06-02 08:48:10 -07002629parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002630 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002631 if (*tok == '+') {
2632 if (change == SET)
2633 goto out_badmix;
2634 change = ADD;
2635 tok++;
2636 } else if (*tok == '-') {
2637 if (change == SET)
2638 goto out_badmix;
2639 change = REMOVE;
2640 tok++;
2641 } else {
2642 if (change != SET && change != DEFAULT)
2643 goto out_badmix;
2644 change = SET;
2645 }
2646
David Ahern745f43e2011-03-09 22:23:26 -07002647 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002648 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002649 break;
David Ahern745f43e2011-03-09 22:23:26 -07002650 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002651 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002652 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002653 continue;
2654 }
David Ahern745f43e2011-03-09 22:23:26 -07002655 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002656 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002657 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002658 goto out;
2659 }
2660
2661 if (type == -1) {
2662 /* add user option to all events types for
2663 * which it is valid
2664 */
Adrian Hunter14057202017-06-21 13:17:19 +03002665 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002666 if (output[j].invalid_fields & all_output_options[i].field) {
2667 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2668 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002669 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002670 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002671 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002672 output[j].user_set_fields &= ~all_output_options[i].field;
2673 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002674 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002675 output[j].user_set_fields |= all_output_options[i].field;
2676 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002677 output[j].user_set = true;
2678 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002679 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002680 }
2681 } else {
2682 if (output[type].invalid_fields & all_output_options[i].field) {
2683 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2684 all_output_options[i].str, event_type(type));
2685
2686 rc = -EINVAL;
2687 goto out;
2688 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002689 if (change == REMOVE)
2690 output[type].fields &= ~all_output_options[i].field;
2691 else
2692 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002693 output[type].user_set = true;
2694 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002695 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002696 }
2697
2698 if (type >= 0) {
2699 if (output[type].fields == 0) {
2700 pr_debug("No fields requested for %s type. "
2701 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002702 }
David Ahern1424dc92011-03-09 22:23:28 -07002703 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002704 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002705
Andi Kleen36ce5652017-06-02 08:48:10 -07002706out_badmix:
2707 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2708 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002709out:
David Ahern745f43e2011-03-09 22:23:26 -07002710 free(str);
2711 return rc;
2712}
2713
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002714#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2715 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2716 if ((lang_dirent->d_type == DT_DIR || \
2717 (lang_dirent->d_type == DT_UNKNOWN && \
2718 is_directory(scripts_path, lang_dirent))) && \
2719 (strcmp(lang_dirent->d_name, ".")) && \
2720 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002721
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002722#define for_each_script(lang_path, lang_dir, script_dirent) \
2723 while ((script_dirent = readdir(lang_dir)) != NULL) \
2724 if (script_dirent->d_type != DT_DIR && \
2725 (script_dirent->d_type != DT_UNKNOWN || \
2726 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002727
2728
2729#define RECORD_SUFFIX "-record"
2730#define REPORT_SUFFIX "-report"
2731
2732struct script_desc {
2733 struct list_head node;
2734 char *name;
2735 char *half_liner;
2736 char *args;
2737};
2738
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002739static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002740
2741static struct script_desc *script_desc__new(const char *name)
2742{
2743 struct script_desc *s = zalloc(sizeof(*s));
2744
Tom Zanussib5b87312010-11-10 08:16:51 -06002745 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002746 s->name = strdup(name);
2747
2748 return s;
2749}
2750
2751static void script_desc__delete(struct script_desc *s)
2752{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002753 zfree(&s->name);
2754 zfree(&s->half_liner);
2755 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002756 free(s);
2757}
2758
2759static void script_desc__add(struct script_desc *s)
2760{
2761 list_add_tail(&s->node, &script_descs);
2762}
2763
2764static struct script_desc *script_desc__find(const char *name)
2765{
2766 struct script_desc *s;
2767
2768 list_for_each_entry(s, &script_descs, node)
2769 if (strcasecmp(s->name, name) == 0)
2770 return s;
2771 return NULL;
2772}
2773
2774static struct script_desc *script_desc__findnew(const char *name)
2775{
2776 struct script_desc *s = script_desc__find(name);
2777
2778 if (s)
2779 return s;
2780
2781 s = script_desc__new(name);
2782 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002783 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002784
2785 script_desc__add(s);
2786
2787 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002788}
2789
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002790static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002791{
2792 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002793 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002794
2795 if (strlen(str) > suffix_len) {
2796 p = str + strlen(str) - suffix_len;
2797 if (!strncmp(p, suffix, suffix_len))
2798 return p;
2799 }
2800
2801 return NULL;
2802}
2803
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002804static int read_script_info(struct script_desc *desc, const char *filename)
2805{
2806 char line[BUFSIZ], *p;
2807 FILE *fp;
2808
2809 fp = fopen(filename, "r");
2810 if (!fp)
2811 return -1;
2812
2813 while (fgets(line, sizeof(line), fp)) {
2814 p = ltrim(line);
2815 if (strlen(p) == 0)
2816 continue;
2817 if (*p != '#')
2818 continue;
2819 p++;
2820 if (strlen(p) && *p == '!')
2821 continue;
2822
2823 p = ltrim(p);
2824 if (strlen(p) && p[strlen(p) - 1] == '\n')
2825 p[strlen(p) - 1] = '\0';
2826
2827 if (!strncmp(p, "description:", strlen("description:"))) {
2828 p += strlen("description:");
2829 desc->half_liner = strdup(ltrim(p));
2830 continue;
2831 }
2832
2833 if (!strncmp(p, "args:", strlen("args:"))) {
2834 p += strlen("args:");
2835 desc->args = strdup(ltrim(p));
2836 continue;
2837 }
2838 }
2839
2840 fclose(fp);
2841
2842 return 0;
2843}
2844
Robert Richter38efb532011-11-25 11:38:40 +01002845static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2846{
2847 char *script_root, *str;
2848
2849 script_root = strdup(script_dirent->d_name);
2850 if (!script_root)
2851 return NULL;
2852
2853 str = (char *)ends_with(script_root, suffix);
2854 if (!str) {
2855 free(script_root);
2856 return NULL;
2857 }
2858
2859 *str = '\0';
2860 return script_root;
2861}
2862
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002863static int list_available_scripts(const struct option *opt __maybe_unused,
2864 const char *s __maybe_unused,
2865 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002866{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002867 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002868 char scripts_path[MAXPATHLEN];
2869 DIR *scripts_dir, *lang_dir;
2870 char script_path[MAXPATHLEN];
2871 char lang_path[MAXPATHLEN];
2872 struct script_desc *desc;
2873 char first_half[BUFSIZ];
2874 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002875
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002876 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002877
2878 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002879 if (!scripts_dir) {
2880 fprintf(stdout,
2881 "open(%s) failed.\n"
2882 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2883 scripts_path);
2884 exit(-1);
2885 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002886
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002887 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002888 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2889 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002890 lang_dir = opendir(lang_path);
2891 if (!lang_dir)
2892 continue;
2893
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002894 for_each_script(lang_path, lang_dir, script_dirent) {
2895 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002896 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002897 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002898 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2899 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002900 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002901 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002902 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002903 }
2904 }
2905
2906 fprintf(stdout, "List of available trace scripts:\n");
2907 list_for_each_entry(desc, &script_descs, node) {
2908 sprintf(first_half, "%s %s", desc->name,
2909 desc->args ? desc->args : "");
2910 fprintf(stdout, " %-36s %s\n", first_half,
2911 desc->half_liner ? desc->half_liner : "");
2912 }
2913
2914 exit(0);
2915}
2916
Feng Tange5f37052012-09-07 16:42:26 +08002917/*
Feng Tang49e639e2012-10-30 11:56:03 +08002918 * Some scripts specify the required events in their "xxx-record" file,
2919 * this function will check if the events in perf.data match those
2920 * mentioned in the "xxx-record".
2921 *
2922 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2923 * which is covered well now. And new parsing code should be added to
2924 * cover the future complexing formats like event groups etc.
2925 */
2926static int check_ev_match(char *dir_name, char *scriptname,
2927 struct perf_session *session)
2928{
2929 char filename[MAXPATHLEN], evname[128];
2930 char line[BUFSIZ], *p;
2931 struct perf_evsel *pos;
2932 int match, len;
2933 FILE *fp;
2934
Jiri Olsa77f18152018-03-19 09:29:01 +01002935 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08002936
2937 fp = fopen(filename, "r");
2938 if (!fp)
2939 return -1;
2940
2941 while (fgets(line, sizeof(line), fp)) {
2942 p = ltrim(line);
2943 if (*p == '#')
2944 continue;
2945
2946 while (strlen(p)) {
2947 p = strstr(p, "-e");
2948 if (!p)
2949 break;
2950
2951 p += 2;
2952 p = ltrim(p);
2953 len = strcspn(p, " \t");
2954 if (!len)
2955 break;
2956
2957 snprintf(evname, len + 1, "%s", p);
2958
2959 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002960 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002961 if (!strcmp(perf_evsel__name(pos), evname)) {
2962 match = 1;
2963 break;
2964 }
2965 }
2966
2967 if (!match) {
2968 fclose(fp);
2969 return -1;
2970 }
2971 }
2972 }
2973
2974 fclose(fp);
2975 return 0;
2976}
2977
2978/*
Feng Tange5f37052012-09-07 16:42:26 +08002979 * Return -1 if none is found, otherwise the actual scripts number.
2980 *
2981 * Currently the only user of this function is the script browser, which
2982 * will list all statically runnable scripts, select one, execute it and
2983 * show the output in a perf browser.
2984 */
Andi Kleen905e4af2019-03-11 07:45:01 -07002985int find_scripts(char **scripts_array, char **scripts_path_array, int num,
2986 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08002987{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002988 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002989 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002990 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002991 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002992 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01002993 .path = input_name,
2994 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002995 };
Feng Tange5f37052012-09-07 16:42:26 +08002996 char *temp;
2997 int i = 0;
2998
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002999 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08003000 if (!session)
3001 return -1;
3002
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003003 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003004
3005 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003006 if (!scripts_dir) {
3007 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003008 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003009 }
Feng Tange5f37052012-09-07 16:42:26 +08003010
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003011 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003012 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3013 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003014#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003015 if (strstr(lang_path, "perl"))
3016 continue;
3017#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003018#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003019 if (strstr(lang_path, "python"))
3020 continue;
3021#endif
3022
3023 lang_dir = opendir(lang_path);
3024 if (!lang_dir)
3025 continue;
3026
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003027 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003028 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003029 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003030 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003031 if (i >= num)
3032 break;
3033 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3034 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003035 script_dirent->d_name);
3036 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003037 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003038 (temp - script_dirent->d_name) + 1,
3039 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003040
3041 if (check_ev_match(lang_path,
3042 scripts_array[i], session))
3043 continue;
3044
Feng Tange5f37052012-09-07 16:42:26 +08003045 i++;
3046 }
Feng Tang49e639e2012-10-30 11:56:03 +08003047 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003048 }
3049
Feng Tang49e639e2012-10-30 11:56:03 +08003050 closedir(scripts_dir);
3051 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003052 return i;
3053}
3054
Tom Zanussi38752942009-12-15 02:53:39 -06003055static char *get_script_path(const char *script_root, const char *suffix)
3056{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003057 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003058 char scripts_path[MAXPATHLEN];
3059 char script_path[MAXPATHLEN];
3060 DIR *scripts_dir, *lang_dir;
3061 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003062 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003063
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003064 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003065
3066 scripts_dir = opendir(scripts_path);
3067 if (!scripts_dir)
3068 return NULL;
3069
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003070 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003071 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3072 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003073 lang_dir = opendir(lang_path);
3074 if (!lang_dir)
3075 continue;
3076
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003077 for_each_script(lang_path, lang_dir, script_dirent) {
3078 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003079 if (__script_root && !strcmp(script_root, __script_root)) {
3080 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003081 closedir(lang_dir);
3082 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003083 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3084 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003085 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003086 }
3087 free(__script_root);
3088 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003089 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003090 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003091 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003092
Robert Richter38efb532011-11-25 11:38:40 +01003093 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003094}
3095
Tom Zanussib5b87312010-11-10 08:16:51 -06003096static bool is_top_script(const char *script_path)
3097{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003098 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003099}
3100
3101static int has_required_arg(char *script_path)
3102{
3103 struct script_desc *desc;
3104 int n_args = 0;
3105 char *p;
3106
3107 desc = script_desc__new(NULL);
3108
3109 if (read_script_info(desc, script_path))
3110 goto out;
3111
3112 if (!desc->args)
3113 goto out;
3114
3115 for (p = desc->args; *p; p++)
3116 if (*p == '<')
3117 n_args++;
3118out:
3119 script_desc__delete(desc);
3120
3121 return n_args;
3122}
3123
David Ahernd54b1a92012-08-26 12:24:46 -06003124static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003125{
3126 char **__argv = malloc(sizeof(const char *) * argc);
3127
David Ahernd54b1a92012-08-26 12:24:46 -06003128 if (!__argv) {
3129 pr_err("malloc failed\n");
3130 return -1;
3131 }
3132
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003133 memcpy(__argv, argv, sizeof(const char *) * argc);
3134 argc = parse_options(argc, (const char **)__argv, record_options,
3135 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3136 free(__argv);
3137
David Ahernd54b1a92012-08-26 12:24:46 -06003138 system_wide = (argc == 0);
3139
3140 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003141}
3142
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003143static void script__setup_sample_type(struct perf_script *script)
3144{
3145 struct perf_session *session = script->session;
3146 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3147
3148 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3149 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003150 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003151 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003152 dwarf_callchain_users = true;
3153 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003154 callchain_param.record_mode = CALLCHAIN_LBR;
3155 else
3156 callchain_param.record_mode = CALLCHAIN_FP;
3157 }
3158}
3159
Jiri Olsa89f16882018-09-13 14:54:03 +02003160static int process_stat_round_event(struct perf_session *session,
3161 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003162{
3163 struct stat_round_event *round = &event->stat_round;
3164 struct perf_evsel *counter;
3165
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003166 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003167 perf_stat_process_counter(&stat_config, counter);
3168 process_stat(counter, round->time);
3169 }
3170
3171 process_stat_interval(round->time);
3172 return 0;
3173}
3174
Jiri Olsa89f16882018-09-13 14:54:03 +02003175static int process_stat_config_event(struct perf_session *session __maybe_unused,
3176 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003177{
3178 perf_event__read_stat_config(&stat_config, &event->stat_config);
3179 return 0;
3180}
3181
Jiri Olsacfc88742016-01-05 22:09:06 +01003182static int set_maps(struct perf_script *script)
3183{
3184 struct perf_evlist *evlist = script->session->evlist;
3185
3186 if (!script->cpus || !script->threads)
3187 return 0;
3188
3189 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3190 return -EINVAL;
3191
3192 perf_evlist__set_maps(evlist, script->cpus, script->threads);
3193
3194 if (perf_evlist__alloc_stats(evlist, true))
3195 return -ENOMEM;
3196
3197 script->allocated = true;
3198 return 0;
3199}
3200
3201static
Jiri Olsa89f16882018-09-13 14:54:03 +02003202int process_thread_map_event(struct perf_session *session,
3203 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003204{
Jiri Olsa89f16882018-09-13 14:54:03 +02003205 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003206 struct perf_script *script = container_of(tool, struct perf_script, tool);
3207
3208 if (script->threads) {
3209 pr_warning("Extra thread map event, ignoring.\n");
3210 return 0;
3211 }
3212
3213 script->threads = thread_map__new_event(&event->thread_map);
3214 if (!script->threads)
3215 return -ENOMEM;
3216
3217 return set_maps(script);
3218}
3219
3220static
Jiri Olsa89f16882018-09-13 14:54:03 +02003221int process_cpu_map_event(struct perf_session *session,
3222 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003223{
Jiri Olsa89f16882018-09-13 14:54:03 +02003224 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003225 struct perf_script *script = container_of(tool, struct perf_script, tool);
3226
3227 if (script->cpus) {
3228 pr_warning("Extra cpu map event, ignoring.\n");
3229 return 0;
3230 }
3231
3232 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3233 if (!script->cpus)
3234 return -ENOMEM;
3235
3236 return set_maps(script);
3237}
3238
Jiri Olsa89f16882018-09-13 14:54:03 +02003239static int process_feature_event(struct perf_session *session,
3240 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303241{
3242 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003243 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303244 return 0;
3245}
3246
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003247#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003248static int perf_script__process_auxtrace_info(struct perf_session *session,
3249 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003250{
Jiri Olsa89f16882018-09-13 14:54:03 +02003251 struct perf_tool *tool = session->tool;
3252
3253 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003254
3255 if (ret == 0) {
3256 struct perf_script *script = container_of(tool, struct perf_script, tool);
3257
3258 ret = perf_script__setup_per_event_dump(script);
3259 }
3260
3261 return ret;
3262}
3263#else
3264#define perf_script__process_auxtrace_info 0
3265#endif
3266
Andi Kleenb585ebd2018-09-20 11:05:36 -07003267static int parse_insn_trace(const struct option *opt __maybe_unused,
3268 const char *str __maybe_unused,
3269 int unset __maybe_unused)
3270{
3271 parse_output_fields(NULL, "+insn,-event,-period", 0);
3272 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003273 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003274 return 0;
3275}
3276
3277static int parse_xed(const struct option *opt __maybe_unused,
3278 const char *str __maybe_unused,
3279 int unset __maybe_unused)
3280{
3281 force_pager("xed -F insn: -A -64 | less");
3282 return 0;
3283}
3284
Andi Kleend1b15522018-09-20 11:05:38 -07003285static int parse_call_trace(const struct option *opt __maybe_unused,
3286 const char *str __maybe_unused,
3287 int unset __maybe_unused)
3288{
3289 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3290 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003291 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003292 return 0;
3293}
3294
3295static int parse_callret_trace(const struct option *opt __maybe_unused,
3296 const char *str __maybe_unused,
3297 int unset __maybe_unused)
3298{
3299 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3300 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003301 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003302 return 0;
3303}
3304
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003305int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003306{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003307 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003308 bool header = false;
3309 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003310 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003311 char *rec_script_path = NULL;
3312 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003313 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003314 struct itrace_synth_opts itrace_synth_opts = {
3315 .set = false,
3316 .default_no_sample = true,
3317 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003318 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003319 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003320 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003321 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003322 struct perf_script script = {
3323 .tool = {
3324 .sample = process_sample_event,
3325 .mmap = perf_event__process_mmap,
3326 .mmap2 = perf_event__process_mmap2,
3327 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303328 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003329 .exit = perf_event__process_exit,
3330 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003331 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003332 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003333 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303334 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003335 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003336 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003337 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003338 .auxtrace = perf_event__process_auxtrace,
3339 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003340 .stat = perf_event__process_stat_event,
3341 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003342 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003343 .thread_map = process_thread_map_event,
3344 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003345 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003346 .ordering_requires_timestamps = true,
3347 },
3348 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003349 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003350 .mode = PERF_DATA_MODE_READ,
3351 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003352 const struct option options[] = {
3353 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3354 "dump raw trace in ASCII"),
3355 OPT_INCR('v', "verbose", &verbose,
3356 "be more verbose (show symbol address, etc)"),
3357 OPT_BOOLEAN('L', "Latency", &latency_format,
3358 "show latency attributes (irqs/preemption disabled, etc)"),
3359 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3360 list_available_scripts),
3361 OPT_CALLBACK('s', "script", NULL, "name",
3362 "script file name (lang:script name, script name, or *)",
3363 parse_scriptname),
3364 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3365 "generate perf-script.xx script in specified language"),
3366 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3367 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3368 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003369 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3370 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003371 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3372 "file", "vmlinux pathname"),
3373 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3374 "file", "kallsyms pathname"),
3375 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3376 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003377 OPT_CALLBACK(0, "symfs", NULL, "directory",
3378 "Look for files with symbols relative to this directory",
3379 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003380 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003381 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003382 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003383 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003384 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303385 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3386 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
3387 "callindent,insn,insnlen,synth,phys_addr,metric,misc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003388 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003389 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3390 "system-wide collection from all CPUs"),
3391 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3392 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003393 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3394 "Decode instructions from itrace", parse_insn_trace),
3395 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3396 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003397 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3398 "Decode calls from from itrace", parse_call_trace),
3399 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3400 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003401 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3402 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003403 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3404 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003405 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3406 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3407 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003408 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3409 "only consider symbols in these pids"),
3410 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3411 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003412 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3413 "Set the maximum stack depth when parsing the callchain, "
3414 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003415 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003416 OPT_BOOLEAN('I', "show-info", &show_full_info,
3417 "display extended information from perf.data file"),
3418 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3419 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003420 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3421 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003422 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3423 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003424 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3425 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303426 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3427 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003428 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3429 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003430 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3431 "Show round events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003432 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3433 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003434 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003435 OPT_INTEGER(0, "max-blocks", &max_blocks,
3436 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003437 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003438 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003439 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003440 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003441 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003442 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3443 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003444 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3445 "Enable symbol demangling"),
3446 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3447 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003448 OPT_STRING(0, "time", &script.time_str, "str",
3449 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003450 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3451 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003452 OPT_END()
3453 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003454 const char * const script_subcommands[] = { "record", "report", NULL };
3455 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003456 "perf script [<options>]",
3457 "perf script [<options>] record <script> [<record-options>] <command>",
3458 "perf script [<options>] report <script> [script-args]",
3459 "perf script [<options>] <script> [<record-options>] <command>",
3460 "perf script [<options>] <top-script> [script-args]",
3461 NULL
3462 };
Tom Zanussi38752942009-12-15 02:53:39 -06003463
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003464 perf_set_singlethreaded();
3465
Tom Zanussib5b87312010-11-10 08:16:51 -06003466 setup_scripting();
3467
Yunlong Song40cae2b2015-03-18 21:35:54 +08003468 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003469 PARSE_OPT_STOP_AT_NON_OPTION);
3470
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003471 data.path = input_name;
3472 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003473
Tom Zanussib5b87312010-11-10 08:16:51 -06003474 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3475 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3476 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003477 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003478 }
3479
Tom Zanussib5b87312010-11-10 08:16:51 -06003480 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3481 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3482 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003483 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003484 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003485 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003486 return -1;
3487 }
Tom Zanussi38752942009-12-15 02:53:39 -06003488 }
3489
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003490 if (itrace_synth_opts.callchain &&
3491 itrace_synth_opts.callchain_sz > scripting_max_stack)
3492 scripting_max_stack = itrace_synth_opts.callchain_sz;
3493
Ben Hutchings44e668c2010-10-10 16:10:03 +01003494 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003495 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003496
Tom Zanussib5b87312010-11-10 08:16:51 -06003497 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003498 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003499 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003500 pid_t pid;
3501
Tom Zanussib5b87312010-11-10 08:16:51 -06003502 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3503 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3504
3505 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003506 usage_with_options_msg(script_usage, options,
3507 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003508 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003509 }
3510
Tom Zanussib5b87312010-11-10 08:16:51 -06003511 if (is_top_script(argv[0])) {
3512 rep_args = argc - 1;
3513 } else {
3514 int rec_args;
3515
3516 rep_args = has_required_arg(rep_script_path);
3517 rec_args = (argc - 1) - rep_args;
3518 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003519 usage_with_options_msg(script_usage, options,
3520 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003521 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003522 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003523 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003524 }
3525
3526 if (pipe(live_pipe) < 0) {
3527 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003528 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003529 }
3530
3531 pid = fork();
3532 if (pid < 0) {
3533 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003534 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003535 }
3536
3537 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003538 j = 0;
3539
Tom Zanussia0cccc22010-04-01 23:59:25 -05003540 dup2(live_pipe[1], 1);
3541 close(live_pipe[0]);
3542
Robert Richter317df652011-11-25 15:05:25 +01003543 if (is_top_script(argv[0])) {
3544 system_wide = true;
3545 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003546 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3547 err = -1;
3548 goto out;
3549 }
Robert Richter317df652011-11-25 15:05:25 +01003550 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003551
3552 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003553 if (!__argv) {
3554 pr_err("malloc failed\n");
3555 err = -ENOMEM;
3556 goto out;
3557 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003558
Tom Zanussib5b87312010-11-10 08:16:51 -06003559 __argv[j++] = "/bin/sh";
3560 __argv[j++] = rec_script_path;
3561 if (system_wide)
3562 __argv[j++] = "-a";
3563 __argv[j++] = "-q";
3564 __argv[j++] = "-o";
3565 __argv[j++] = "-";
3566 for (i = rep_args + 1; i < argc; i++)
3567 __argv[j++] = argv[i];
3568 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003569
3570 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003571 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003572 exit(-1);
3573 }
3574
3575 dup2(live_pipe[0], 0);
3576 close(live_pipe[1]);
3577
Tom Zanussib5b87312010-11-10 08:16:51 -06003578 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003579 if (!__argv) {
3580 pr_err("malloc failed\n");
3581 err = -ENOMEM;
3582 goto out;
3583 }
3584
Tom Zanussib5b87312010-11-10 08:16:51 -06003585 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003586 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003587 __argv[j++] = rep_script_path;
3588 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003589 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003590 __argv[j++] = "-i";
3591 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003592 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003593
3594 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003595 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003596 exit(-1);
3597 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003598
Tom Zanussib5b87312010-11-10 08:16:51 -06003599 if (rec_script_path)
3600 script_path = rec_script_path;
3601 if (rep_script_path)
3602 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003603
Tom Zanussib5b87312010-11-10 08:16:51 -06003604 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003605 j = 0;
3606
Robert Richter317df652011-11-25 15:05:25 +01003607 if (!rec_script_path)
3608 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003609 else if (!system_wide) {
3610 if (have_cmd(argc - 1, &argv[1]) != 0) {
3611 err = -1;
3612 goto out;
3613 }
3614 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003615
3616 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003617 if (!__argv) {
3618 pr_err("malloc failed\n");
3619 err = -ENOMEM;
3620 goto out;
3621 }
3622
Tom Zanussib5b87312010-11-10 08:16:51 -06003623 __argv[j++] = "/bin/sh";
3624 __argv[j++] = script_path;
3625 if (system_wide)
3626 __argv[j++] = "-a";
3627 for (i = 2; i < argc; i++)
3628 __argv[j++] = argv[i];
3629 __argv[j++] = NULL;
3630
3631 execvp("/bin/sh", (char **)__argv);
3632 free(__argv);
3633 exit(-1);
3634 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003635
Milian Wolffc1c9b962018-10-21 21:14:23 +02003636 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003637 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003638 use_browser = 0;
3639 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003640
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003641 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003642 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003643 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003644
Jiri Olsae90debd2013-12-09 11:02:50 +01003645 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003646 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003647 perf_session__fprintf_info(session, stdout, show_full_info);
3648 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003649 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003650 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003651 if (show_full_info)
3652 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003653
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003654 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003655 goto out_delete;
3656
Andi Kleen3ab481a2019-03-05 06:47:45 -08003657 uname(&uts);
3658 if (!strcmp(uts.machine, session->header.env.arch) ||
3659 (!strcmp(uts.machine, "x86_64") &&
3660 !strcmp(session->header.env.arch, "i386")))
3661 native_arch = true;
3662
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003663 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003664 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003665
Andi Kleen99f753f2018-09-20 11:05:39 -07003666 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3667 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003668 itrace_synth_opts.thread_stack = true;
3669
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003670 session->itrace_synth_opts = &itrace_synth_opts;
3671
Anton Blanchard5d67be92011-07-04 21:57:50 +10003672 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003673 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3674 if (err < 0)
3675 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003676 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003677 }
3678
David Ahern1424dc92011-03-09 22:23:28 -07003679 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003680 symbol_conf.use_callchain = true;
3681 else
3682 symbol_conf.use_callchain = false;
3683
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003684 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003685 tep_set_function_resolver(session->tevent.pevent,
3686 machine__resolve_kernel_addr,
3687 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003688 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003689 err = -1;
3690 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003691 }
3692
Tom Zanussi956ffd02009-11-25 01:15:46 -06003693 if (generate_script_lang) {
3694 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003695 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003696
David Ahern2c9e45f72011-03-17 10:03:21 -06003697 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003698 fprintf(stderr,
3699 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003700 err = -EINVAL;
3701 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003702 }
3703
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003704 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003705 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003706 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003707 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003708 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003709 }
3710
3711 err = fstat(input, &perf_stat);
3712 if (err < 0) {
3713 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003714 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003715 }
3716
3717 if (!perf_stat.st_size) {
3718 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003719 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003720 }
3721
3722 scripting_ops = script_spec__lookup(generate_script_lang);
3723 if (!scripting_ops) {
3724 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003725 err = -ENOENT;
3726 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003727 }
3728
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003729 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003730 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003731 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003732 }
3733
3734 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003735 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003736 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003737 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003738 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003739 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003740 }
3741
David Ahern9cbdb702011-04-06 21:54:20 -06003742
3743 err = perf_session__check_output_opt(session);
3744 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003745 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003746
Jin Yao284c4e12019-03-01 18:13:06 +08003747 if (script.time_str) {
3748 err = perf_time__parse_for_ranges(script.time_str, session,
3749 &script.ptime_range,
3750 &script.range_size,
3751 &script.range_num);
3752 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08003753 goto out_delete;
David Aherna91f4c42016-11-29 10:15:43 -07003754 }
3755
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003756 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003757
Adrian Hunterd445dd22014-08-15 22:08:37 +03003758 flush_scripting();
3759
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003760out_delete:
Jin Yao284c4e12019-03-01 18:13:06 +08003761 if (script.ptime_range)
3762 zfree(&script.ptime_range);
Jin Yaocc2ef582018-01-10 23:00:33 +08003763
Jiri Olsacfc88742016-01-05 22:09:06 +01003764 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003765 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003766
3767 if (script_started)
3768 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003769out:
3770 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003771}