blob: 330dcd9b9b8ff450f375971d9901ba2386d89578 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002#include "builtin.h"
3
Andrea Gelminib7eead82010-08-05 15:51:38 +02004#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02005#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02006#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06007#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02008#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06009#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +020010#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020013#include "util/symbol.h"
14#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010015#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020016#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070017#include "util/evlist.h"
18#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080019#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020020#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030021#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010022#include "util/cpumap.h"
23#include "util/thread_map.h"
24#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080025#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030026#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030027#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070028#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010029#include "util/path.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030030#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100031#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030032#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030033#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030034#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010035#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010036#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080037#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030038#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030039#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030040#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030041#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030042#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030043#include <sys/types.h>
44#include <sys/stat.h>
45#include <unistd.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020046
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030047#include "sane_ctype.h"
48
Tom Zanussi956ffd02009-11-25 01:15:46 -060049static char const *script_name;
50static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020051static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020052static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020053static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070054static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090055static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010056static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030057static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030058static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100059static const char *cpu_list;
60static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010061static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080062static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060063
Adrian Hunter44cbe722015-09-25 16:15:50 +030064unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030065
David Ahern745f43e2011-03-09 22:23:26 -070066enum perf_output_field {
67 PERF_OUTPUT_COMM = 1U << 0,
68 PERF_OUTPUT_TID = 1U << 1,
69 PERF_OUTPUT_PID = 1U << 2,
70 PERF_OUTPUT_TIME = 1U << 3,
71 PERF_OUTPUT_CPU = 1U << 4,
72 PERF_OUTPUT_EVNAME = 1U << 5,
73 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060074 PERF_OUTPUT_IP = 1U << 7,
75 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060076 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060077 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090078 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020079 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020080 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020081 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020082 PERF_OUTPUT_BRSTACK = 1U << 15,
83 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010084 PERF_OUTPUT_DATA_SRC = 1U << 17,
85 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000086 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030087 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030088 PERF_OUTPUT_INSN = 1U << 21,
89 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080090 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070091 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030092 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -040093 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -070094 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -080095 PERF_OUTPUT_METRIC = 1U << 28,
David Ahern745f43e2011-03-09 22:23:26 -070096};
97
98struct output_option {
99 const char *str;
100 enum perf_output_field field;
101} all_output_options[] = {
102 {.str = "comm", .field = PERF_OUTPUT_COMM},
103 {.str = "tid", .field = PERF_OUTPUT_TID},
104 {.str = "pid", .field = PERF_OUTPUT_PID},
105 {.str = "time", .field = PERF_OUTPUT_TIME},
106 {.str = "cpu", .field = PERF_OUTPUT_CPU},
107 {.str = "event", .field = PERF_OUTPUT_EVNAME},
108 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600109 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700110 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600111 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600112 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900113 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200114 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200115 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200116 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700117 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200118 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
119 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100120 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
121 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000122 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300123 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300124 {.str = "insn", .field = PERF_OUTPUT_INSN},
125 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800126 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700127 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300128 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400129 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800130 {.str = "metric", .field = PERF_OUTPUT_METRIC},
David Ahern745f43e2011-03-09 22:23:26 -0700131};
132
Adrian Hunter14057202017-06-21 13:17:19 +0300133enum {
134 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
135 OUTPUT_TYPE_MAX
136};
137
David Ahern745f43e2011-03-09 22:23:26 -0700138/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600139static struct {
140 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600141 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400142 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600143 u64 fields;
144 u64 invalid_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300145} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700146
David Ahern2c9e45f72011-03-17 10:03:21 -0600147 [PERF_TYPE_HARDWARE] = {
148 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700149
David Ahern2c9e45f72011-03-17 10:03:21 -0600150 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
151 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600152 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200153 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
154 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600155
Wang Nan30372f02016-02-24 11:20:45 +0000156 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600157 },
158
159 [PERF_TYPE_SOFTWARE] = {
160 .user_set = false,
161
162 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
163 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600164 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200165 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000166 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600167
168 .invalid_fields = PERF_OUTPUT_TRACE,
169 },
170
171 [PERF_TYPE_TRACEPOINT] = {
172 .user_set = false,
173
174 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
175 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000176 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600177 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700178
179 [PERF_TYPE_RAW] = {
180 .user_set = false,
181
182 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
183 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600184 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200185 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100186 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
Kan Liang49d58f02017-08-29 13:11:11 -0400187 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT |
188 PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700189
Wang Nan30372f02016-02-24 11:20:45 +0000190 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700191 },
Wang Nan27cfef02015-12-08 02:25:43 +0000192
193 [PERF_TYPE_BREAKPOINT] = {
194 .user_set = false,
195
196 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
197 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
198 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
199 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
200 PERF_OUTPUT_PERIOD,
201
Wang Nan30372f02016-02-24 11:20:45 +0000202 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000203 },
Adrian Hunter14057202017-06-21 13:17:19 +0300204
205 [OUTPUT_TYPE_SYNTH] = {
206 .user_set = false,
207
208 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
209 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
210 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Adrian Hunter47e78082017-05-26 11:17:22 +0300211 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
212 PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300213
214 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
215 },
David Ahern1424dc92011-03-09 22:23:28 -0700216};
David Ahern745f43e2011-03-09 22:23:26 -0700217
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300218struct perf_evsel_script {
219 char *filename;
220 FILE *fp;
221 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800222 /* For metric output */
223 u64 val;
224 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300225};
226
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800227static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
228{
229 return (struct perf_evsel_script *)evsel->priv;
230}
231
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300232static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100233 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300234{
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800235 struct perf_evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300236
237 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100238 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300239 goto out_free;
240 es->fp = fopen(es->filename, "w");
241 if (es->fp == NULL)
242 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300243 }
244
245 return es;
246out_free_filename:
247 zfree(&es->filename);
248out_free:
249 free(es);
250 return NULL;
251}
252
253static void perf_evsel_script__delete(struct perf_evsel_script *es)
254{
255 zfree(&es->filename);
256 fclose(es->fp);
257 es->fp = NULL;
258 free(es);
259}
260
261static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
262{
263 struct stat st;
264
265 fstat(fileno(es->fp), &st);
266 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
267 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
268}
269
Adrian Hunter14057202017-06-21 13:17:19 +0300270static inline int output_type(unsigned int type)
271{
272 switch (type) {
273 case PERF_TYPE_SYNTH:
274 return OUTPUT_TYPE_SYNTH;
275 default:
276 return type;
277 }
278}
279
280static inline unsigned int attr_type(unsigned int type)
281{
282 switch (type) {
283 case OUTPUT_TYPE_SYNTH:
284 return PERF_TYPE_SYNTH;
285 default:
286 return type;
287 }
288}
289
David Ahern2c9e45f72011-03-17 10:03:21 -0600290static bool output_set_by_user(void)
291{
292 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300293 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600294 if (output[j].user_set)
295 return true;
296 }
297 return false;
298}
David Ahern745f43e2011-03-09 22:23:26 -0700299
David Ahern9cbdb702011-04-06 21:54:20 -0600300static const char *output_field2str(enum perf_output_field field)
301{
302 int i, imax = ARRAY_SIZE(all_output_options);
303 const char *str = "";
304
305 for (i = 0; i < imax; ++i) {
306 if (all_output_options[i].field == field) {
307 str = all_output_options[i].str;
308 break;
309 }
310 }
311 return str;
312}
313
Adrian Hunter14057202017-06-21 13:17:19 +0300314#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700315
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300316static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
317 u64 sample_type, const char *sample_msg,
318 enum perf_output_field field,
319 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700320{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300321 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300322 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600323 const char *evname;
324
325 if (attr->sample_type & sample_type)
326 return 0;
327
328 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300329 if (allow_user_set)
330 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300331 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600332 pr_err("Samples for '%s' event do not have %s attribute set. "
333 "Cannot print '%s' field.\n",
334 evname, sample_msg, output_field2str(field));
335 return -1;
336 }
337
338 /* user did not ask for it explicitly so remove from the default list */
339 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300340 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600341 pr_debug("Samples for '%s' event do not have %s attribute set. "
342 "Skipping '%s' field.\n",
343 evname, sample_msg, output_field2str(field));
344
345 return 0;
346}
347
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300348static int perf_evsel__check_stype(struct perf_evsel *evsel,
349 u64 sample_type, const char *sample_msg,
350 enum perf_output_field field)
351{
352 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
353 false);
354}
355
David Ahern9cbdb702011-04-06 21:54:20 -0600356static int perf_evsel__check_attr(struct perf_evsel *evsel,
357 struct perf_session *session)
358{
359 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300360 bool allow_user_set;
361
Jiri Olsae099eba2016-01-05 22:09:09 +0100362 if (perf_header__has_feat(&session->header, HEADER_STAT))
363 return 0;
364
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300365 allow_user_set = perf_header__has_feat(&session->header,
366 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600367
David Ahern1424dc92011-03-09 22:23:28 -0700368 if (PRINT_FIELD(TRACE) &&
369 !perf_session__has_traces(session, "record -R"))
370 return -EINVAL;
371
David Ahern787bef12011-05-27 14:28:43 -0600372 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300373 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
374 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700375 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700376 }
David Ahern7cec0922011-05-30 13:08:23 -0600377
378 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300379 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
380 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600381 return -EINVAL;
382
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100383 if (PRINT_FIELD(DATA_SRC) &&
384 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
385 PERF_OUTPUT_DATA_SRC))
386 return -EINVAL;
387
388 if (PRINT_FIELD(WEIGHT) &&
389 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
390 PERF_OUTPUT_WEIGHT))
391 return -EINVAL;
392
David Ahern7cec0922011-05-30 13:08:23 -0600393 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
394 pr_err("Display of symbols requested but neither sample IP nor "
395 "sample address\nis selected. Hence, no addresses to convert "
396 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600397 return -EINVAL;
398 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900399 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
400 pr_err("Display of offsets requested but symbol is not"
401 "selected.\n");
402 return -EINVAL;
403 }
Mark Santaniello55b9b502017-06-19 09:38:24 -0700404 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR) &&
Mark Santaniello106dacd2017-06-19 09:38:25 -0700405 !PRINT_FIELD(BRSTACK) && !PRINT_FIELD(BRSTACKSYM) && !PRINT_FIELD(BRSTACKOFF)) {
406 pr_err("Display of DSO requested but no address to convert. Select\n"
407 "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
David Ahern610723f2011-05-27 14:28:44 -0600408 return -EINVAL;
409 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200410 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
411 pr_err("Display of source line number requested but sample IP is not\n"
412 "selected. Hence, no address to lookup the source line number.\n");
413 return -EINVAL;
414 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800415 if (PRINT_FIELD(BRSTACKINSN) &&
416 !(perf_evlist__combined_branch_type(session->evlist) &
417 PERF_SAMPLE_BRANCH_ANY)) {
418 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
419 "Hint: run 'perf record -b ...'\n");
420 return -EINVAL;
421 }
David Ahern1424dc92011-03-09 22:23:28 -0700422 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300423 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
424 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700425 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700426
427 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300428 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
429 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700430 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700431
432 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300433 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
434 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700435 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600436
Stephane Eranianfc36f942015-08-31 18:41:10 +0200437 if (PRINT_FIELD(IREGS) &&
438 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
439 PERF_OUTPUT_IREGS))
440 return -EINVAL;
441
Andi Kleenb1491ac2017-09-05 11:40:57 -0700442 if (PRINT_FIELD(UREGS) &&
443 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
444 PERF_OUTPUT_UREGS))
445 return -EINVAL;
446
Kan Liang49d58f02017-08-29 13:11:11 -0400447 if (PRINT_FIELD(PHYS_ADDR) &&
448 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
449 PERF_OUTPUT_PHYS_ADDR))
450 return -EINVAL;
451
David Ahern9cbdb702011-04-06 21:54:20 -0600452 return 0;
453}
454
Adrian Hunter7ea95722013-11-01 15:51:30 +0200455static void set_print_ip_opts(struct perf_event_attr *attr)
456{
Adrian Hunter14057202017-06-21 13:17:19 +0300457 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200458
459 output[type].print_ip_opts = 0;
460 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300461 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200462
463 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300464 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200465
466 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300467 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200468
469 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300470 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200471
472 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300473 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200474}
475
David Ahern9cbdb702011-04-06 21:54:20 -0600476/*
477 * verify all user requested events exist and the samples
478 * have the expected data
479 */
480static int perf_session__check_output_opt(struct perf_session *session)
481{
He Kuang40f20e52016-05-16 04:51:19 +0000482 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600483 struct perf_evsel *evsel;
484
Adrian Hunter14057202017-06-21 13:17:19 +0300485 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
486 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600487
488 /*
489 * even if fields is set to 0 (ie., show nothing) event must
490 * exist if user explicitly includes it on the command line
491 */
Adrian Hunter14057202017-06-21 13:17:19 +0300492 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
493 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600494 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300495 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600496 event_type(j));
497 return -1;
498 }
499
500 if (evsel && output[j].fields &&
501 perf_evsel__check_attr(evsel, session))
502 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400503
504 if (evsel == NULL)
505 continue;
506
Adrian Hunter7ea95722013-11-01 15:51:30 +0200507 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700508 }
509
Adrian Hunter98526ee2014-07-31 09:00:59 +0300510 if (!no_callchain) {
511 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000512 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300513
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300514 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000515 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300516 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
517 use_callchain = true;
518 break;
519 }
520 }
He Kuang71ac8992016-08-04 11:25:43 +0000521 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300522 symbol_conf.use_callchain = false;
523 }
524
David Ahern80b8b492013-11-19 21:07:37 -0700525 /*
526 * set default for tracepoints to print symbols only
527 * if callchains are present
528 */
529 if (symbol_conf.use_callchain &&
530 !output[PERF_TYPE_TRACEPOINT].user_set) {
531 struct perf_event_attr *attr;
532
533 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700534
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300535 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000536 if (evsel->attr.type != j)
537 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700538
He Kuang40f20e52016-05-16 04:51:19 +0000539 attr = &evsel->attr;
540
541 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
542 output[j].fields |= PERF_OUTPUT_IP;
543 output[j].fields |= PERF_OUTPUT_SYM;
544 output[j].fields |= PERF_OUTPUT_DSO;
545 set_print_ip_opts(attr);
546 goto out;
547 }
David Ahern80b8b492013-11-19 21:07:37 -0700548 }
549 }
550
551out:
David Ahern1424dc92011-03-09 22:23:28 -0700552 return 0;
553}
David Ahern745f43e2011-03-09 22:23:26 -0700554
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300555static int perf_sample__fprintf_iregs(struct perf_sample *sample,
556 struct perf_event_attr *attr, FILE *fp)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200557{
558 struct regs_dump *regs = &sample->intr_regs;
559 uint64_t mask = attr->sample_regs_intr;
560 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300561 int printed = 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200562
563 if (!regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300564 return 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200565
566 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
567 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300568 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Stephane Eranianfc36f942015-08-31 18:41:10 +0200569 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300570
571 return printed;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200572}
573
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300574static int perf_sample__fprintf_uregs(struct perf_sample *sample,
575 struct perf_event_attr *attr, FILE *fp)
Andi Kleenb1491ac2017-09-05 11:40:57 -0700576{
577 struct regs_dump *regs = &sample->user_regs;
578 uint64_t mask = attr->sample_regs_user;
579 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
592 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700593}
594
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300595static int perf_sample__fprintf_start(struct perf_sample *sample,
596 struct thread *thread,
597 struct perf_evsel *evsel, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700598{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300599 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700600 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700601 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300602 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700603
David Ahern745f43e2011-03-09 22:23:26 -0700604 if (PRINT_FIELD(COMM)) {
605 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300606 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600607 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300608 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700609 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300610 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700611 }
David Ahernc70c94b2011-03-09 22:23:25 -0700612
David Ahern745f43e2011-03-09 22:23:26 -0700613 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300614 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700615 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300616 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700617 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300618 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700619
David Ahern745f43e2011-03-09 22:23:26 -0700620 if (PRINT_FIELD(CPU)) {
621 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300622 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700623 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300624 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700625 }
David Ahernc70c94b2011-03-09 22:23:25 -0700626
David Ahern745f43e2011-03-09 22:23:26 -0700627 if (PRINT_FIELD(TIME)) {
628 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300629 secs = nsecs / NSEC_PER_SEC;
630 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900631
Adrian Hunter83e19862015-09-25 16:15:36 +0300632 if (nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300633 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900634 else {
635 char sample_time[32];
636 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300637 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900638 }
David Ahern745f43e2011-03-09 22:23:26 -0700639 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300640
641 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700642}
643
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200644static inline char
645mispred_str(struct branch_entry *br)
646{
647 if (!(br->flags.mispred || br->flags.predicted))
648 return '-';
649
650 return br->flags.predicted ? 'P' : 'M';
651}
652
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300653static int perf_sample__fprintf_brstack(struct perf_sample *sample,
654 struct thread *thread,
655 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200656{
657 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700658 struct addr_location alf, alt;
659 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300660 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200661
662 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300663 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200664
665 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700666 from = br->entries[i].from;
667 to = br->entries[i].to;
668
669 if (PRINT_FIELD(DSO)) {
670 memset(&alf, 0, sizeof(alf));
671 memset(&alt, 0, sizeof(alt));
672 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
673 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
674 }
675
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300676 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700677 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300678 printed += fprintf(fp, "(");
679 printed += map__fprintf_dsoname(alf.map, fp);
680 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700681 }
682
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300683 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700684 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300685 printed += fprintf(fp, "(");
686 printed += map__fprintf_dsoname(alt.map, fp);
687 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700688 }
689
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300690 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200691 mispred_str( br->entries + i),
692 br->entries[i].flags.in_tx? 'X' : '-',
693 br->entries[i].flags.abort? 'A' : '-',
694 br->entries[i].flags.cycles);
695 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300696
697 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200698}
699
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300700static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
701 struct thread *thread,
702 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200703{
704 struct branch_stack *br = sample->branch_stack;
705 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200706 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300707 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200708
709 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300710 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200711
712 for (i = 0; i < br->nr; i++) {
713
714 memset(&alf, 0, sizeof(alf));
715 memset(&alt, 0, sizeof(alt));
716 from = br->entries[i].from;
717 to = br->entries[i].to;
718
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300719 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200720 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300721 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200722
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300723 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200724 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300725 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200726
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300727 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700728 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300729 printed += fprintf(fp, "(");
730 printed += map__fprintf_dsoname(alf.map, fp);
731 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700732 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300733 printed += fprintf(fp, "%c", '/');
734 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700735 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300736 printed += fprintf(fp, "(");
737 printed += map__fprintf_dsoname(alt.map, fp);
738 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700739 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300740 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200741 mispred_str( br->entries + i),
742 br->entries[i].flags.in_tx? 'X' : '-',
743 br->entries[i].flags.abort? 'A' : '-',
744 br->entries[i].flags.cycles);
745 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300746
747 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200748}
749
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300750static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
751 struct thread *thread,
752 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700753{
754 struct branch_stack *br = sample->branch_stack;
755 struct addr_location alf, alt;
756 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300757 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700758
759 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300760 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700761
762 for (i = 0; i < br->nr; i++) {
763
764 memset(&alf, 0, sizeof(alf));
765 memset(&alt, 0, sizeof(alt));
766 from = br->entries[i].from;
767 to = br->entries[i].to;
768
769 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
770 if (alf.map && !alf.map->dso->adjust_symbols)
771 from = map__map_ip(alf.map, from);
772
773 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
774 if (alt.map && !alt.map->dso->adjust_symbols)
775 to = map__map_ip(alt.map, to);
776
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300777 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700778 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300779 printed += fprintf(fp, "(");
780 printed += map__fprintf_dsoname(alf.map, fp);
781 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700782 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300783 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700784 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300785 printed += fprintf(fp, "(");
786 printed += map__fprintf_dsoname(alt.map, fp);
787 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700788 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300789 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700790 mispred_str(br->entries + i),
791 br->entries[i].flags.in_tx ? 'X' : '-',
792 br->entries[i].flags.abort ? 'A' : '-',
793 br->entries[i].flags.cycles);
794 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300795
796 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700797}
Andi Kleen48d02a12017-02-23 15:46:34 -0800798#define MAXBB 16384UL
799
800static int grab_bb(u8 *buffer, u64 start, u64 end,
801 struct machine *machine, struct thread *thread,
802 bool *is64bit, u8 *cpumode, bool last)
803{
804 long offset, len;
805 struct addr_location al;
806 bool kernel;
807
808 if (!start || !end)
809 return 0;
810
811 kernel = machine__kernel_ip(machine, start);
812 if (kernel)
813 *cpumode = PERF_RECORD_MISC_KERNEL;
814 else
815 *cpumode = PERF_RECORD_MISC_USER;
816
817 /*
818 * Block overlaps between kernel and user.
819 * This can happen due to ring filtering
820 * On Intel CPUs the entry into the kernel is filtered,
821 * but the exit is not. Let the caller patch it up.
822 */
823 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300824 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800825 return -ENXIO;
826 }
827
828 memset(&al, 0, sizeof(al));
829 if (end - start > MAXBB - MAXINSN) {
830 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300831 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800832 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300833 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800834 return 0;
835 }
836
837 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
838 if (!al.map || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300839 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800840 return 0;
841 }
842 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300843 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800844 return 0;
845 }
846
847 /* Load maps to ensure dso->is_64_bit has been updated */
848 map__load(al.map);
849
850 offset = al.map->map_ip(al.map, start);
851 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
852 end - start + MAXINSN);
853
854 *is64bit = al.map->dso->is_64_bit;
855 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300856 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800857 start, end);
858 return len;
859}
860
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300861static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
862 struct perf_insn *x, u8 *inbuf, int len,
863 int insn, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800864{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300865 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
866 dump_insn(x, ip, inbuf, len, NULL),
867 en->flags.predicted ? " PRED" : "",
868 en->flags.mispred ? " MISPRED" : "",
869 en->flags.in_tx ? " INTX" : "",
870 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800871 if (en->flags.cycles) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300872 printed += fprintf(fp, " %d cycles", en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800873 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300874 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800875 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300876 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800877}
878
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300879static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
880 u8 cpumode, int cpu, struct symbol **lastsym,
881 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800882{
883 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300884 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800885
886 memset(&al, 0, sizeof(al));
887
888 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
889 if (!al.map)
890 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
891 addr, &al);
892 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300893 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800894
895 al.cpu = cpu;
896 al.sym = NULL;
897 if (al.map)
898 al.sym = map__find_symbol(al.map, al.addr);
899
900 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300901 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800902
903 if (al.addr < al.sym->end)
904 off = al.addr - al.sym->start;
905 else
906 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300907 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800908 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300909 printed += fprintf(fp, "%+d", off);
910 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800911 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300912 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
913 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800914 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300915
916 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800917}
918
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300919static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
920 struct thread *thread,
921 struct perf_event_attr *attr,
922 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800923{
924 struct branch_stack *br = sample->branch_stack;
925 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300926 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800927 struct perf_insn x;
928 u8 buffer[MAXBB];
929 unsigned off;
930 struct symbol *lastsym = NULL;
931
932 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300933 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800934 nr = br->nr;
935 if (max_blocks && nr > max_blocks + 1)
936 nr = max_blocks + 1;
937
938 x.thread = thread;
939 x.cpu = sample->cpu;
940
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300941 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -0800942
943 /* Handle first from jump, of which we don't know the entry. */
944 len = grab_bb(buffer, br->entries[nr-1].from,
945 br->entries[nr-1].from,
946 machine, thread, &x.is64bit, &x.cpumode, false);
947 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300948 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
949 x.cpumode, x.cpu, &lastsym, attr, fp);
950 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
951 &x, buffer, len, 0, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800952 }
953
954 /* Print all blocks */
955 for (i = nr - 2; i >= 0; i--) {
956 if (br->entries[i].from || br->entries[i].to)
957 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
958 br->entries[i].from,
959 br->entries[i].to);
960 start = br->entries[i + 1].to;
961 end = br->entries[i].from;
962
963 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
964 /* Patch up missing kernel transfers due to ring filters */
965 if (len == -ENXIO && i > 0) {
966 end = br->entries[--i].from;
967 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
968 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
969 }
970 if (len <= 0)
971 continue;
972
973 insn = 0;
974 for (off = 0;; off += ilen) {
975 uint64_t ip = start + off;
976
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300977 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800978 if (ip == end) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300979 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800980 break;
981 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300982 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
983 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -0800984 if (ilen == 0)
985 break;
986 insn++;
987 }
988 }
989 }
990
991 /*
992 * Hit the branch? In this case we are already done, and the target
993 * has not been executed yet.
994 */
995 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300996 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -0800997 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300998 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -0800999
1000 /*
1001 * Print final block upto sample
1002 */
1003 start = br->entries[0].to;
1004 end = sample->ip;
1005 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001006 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001007 if (len <= 0) {
1008 /* Print at least last IP if basic block did not work */
1009 len = grab_bb(buffer, sample->ip, sample->ip,
1010 machine, thread, &x.is64bit, &x.cpumode, false);
1011 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001012 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001013
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001014 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001015 dump_insn(&x, sample->ip, buffer, len, NULL));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001016 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001017 }
1018 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001019 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1020 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001021 if (ilen == 0)
1022 break;
1023 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001024out:
1025 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001026}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001027
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001028static int perf_sample__fprintf_addr(struct perf_sample *sample,
1029 struct thread *thread,
1030 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001031{
1032 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001033 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001034
1035 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001036 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001037
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001038 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001039
1040 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001041 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001042 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001043 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001044 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001045 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001046 }
1047
1048 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001049 printed += fprintf(fp, " (");
1050 printed += map__fprintf_dsoname(al.map, fp);
1051 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001052 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001053out:
1054 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001055}
1056
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001057static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1058 struct perf_evsel *evsel,
1059 struct thread *thread,
1060 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001061{
1062 struct perf_event_attr *attr = &evsel->attr;
1063 size_t depth = thread_stack__depth(thread);
1064 struct addr_location addr_al;
1065 const char *name = NULL;
1066 static int spacing;
1067 int len = 0;
1068 u64 ip = 0;
1069
1070 /*
1071 * The 'return' has already been popped off the stack so the depth has
1072 * to be adjusted to match the 'call'.
1073 */
1074 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1075 depth += 1;
1076
1077 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1078 if (sample_addr_correlates_sym(attr)) {
1079 thread__resolve(thread, &addr_al, sample);
1080 if (addr_al.sym)
1081 name = addr_al.sym->name;
1082 else
1083 ip = sample->addr;
1084 } else {
1085 ip = sample->addr;
1086 }
1087 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1088 if (al->sym)
1089 name = al->sym->name;
1090 else
1091 ip = sample->ip;
1092 }
1093
1094 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001095 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001096 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001097 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001098
1099 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001100 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001101
1102 /*
1103 * Try to keep the output length from changing frequently so that the
1104 * output lines up more nicely.
1105 */
1106 if (len > spacing || (len && len < spacing - 52))
1107 spacing = round_up(len + 4, 32);
1108
1109 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001110 len += fprintf(fp, "%*s", spacing - len, "");
1111
1112 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001113}
1114
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001115static int perf_sample__fprintf_insn(struct perf_sample *sample,
1116 struct perf_event_attr *attr,
1117 struct thread *thread,
1118 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001119{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001120 int printed = 0;
1121
Andi Kleen224e2c92016-10-07 16:42:27 +03001122 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001123 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen224e2c92016-10-07 16:42:27 +03001124 if (PRINT_FIELD(INSN)) {
1125 int i;
1126
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001127 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001128 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001129 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001130 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001131 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001132 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1133
1134 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001135}
1136
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001137static int perf_sample__fprintf_bts(struct perf_sample *sample,
1138 struct perf_evsel *evsel,
1139 struct thread *thread,
1140 struct addr_location *al,
1141 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001142{
1143 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001144 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001145 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001146 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001147
Adrian Huntere2167082016-06-23 16:40:58 +03001148 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001149 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001150
Akihiro Nagai95582592012-01-30 13:43:09 +09001151 /* print branch_from information */
1152 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001153 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001154 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001155
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001156 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001157 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001158 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001159 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001160
1161 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001162 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001163 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001164 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001165 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001166 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001167 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001168 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001169
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001170 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001171 }
1172
Akihiro Nagai95582592012-01-30 13:43:09 +09001173 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001174 if (PRINT_FIELD(ADDR) ||
1175 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001176 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001177 printed += fprintf(fp, " => ");
1178 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001179 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001180
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001181 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001182 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001183
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001184 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1185 return printed + fprintf(fp, "\n");
Akihiro Nagai95582592012-01-30 13:43:09 +09001186}
1187
Adrian Hunter055cd332016-06-23 16:40:56 +03001188static struct {
1189 u32 flags;
1190 const char *name;
1191} sample_flags[] = {
1192 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1193 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1194 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1195 {PERF_IP_FLAG_BRANCH, "jmp"},
1196 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1197 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1198 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1199 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1200 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1201 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1202 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1203 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1204 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1205 {0, NULL}
1206};
1207
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001208static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001209{
1210 const char *chars = PERF_IP_FLAG_CHARS;
1211 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001212 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1213 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001214 char str[33];
1215 int i, pos = 0;
1216
Adrian Hunter055cd332016-06-23 16:40:56 +03001217 for (i = 0; sample_flags[i].name ; i++) {
1218 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1219 name = sample_flags[i].name;
1220 break;
1221 }
1222 }
1223
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001224 for (i = 0; i < n; i++, flags >>= 1) {
1225 if (flags & 1)
1226 str[pos++] = chars[i];
1227 }
1228 for (; i < 32; i++, flags >>= 1) {
1229 if (flags & 1)
1230 str[pos++] = '?';
1231 }
1232 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001233
1234 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001235 return fprintf(fp, " %-7s%4s ", name, in_tx ? "(x)" : "");
1236
1237 return fprintf(fp, " %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001238}
1239
Wang Nan30372f02016-02-24 11:20:45 +00001240struct printer_data {
1241 int line_no;
1242 bool hit_nul;
1243 bool is_printable;
1244};
1245
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001246static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1247 unsigned int val,
1248 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001249{
1250 unsigned char ch = (unsigned char)val;
1251 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001252 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001253
1254 switch (op) {
1255 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001256 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001257 break;
1258 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001259 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001260 " ");
1261 break;
1262 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001263 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001264 break;
1265 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001266 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001267 break;
1268 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001269 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001270 break;
1271 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001272 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001273 break;
1274 case BINARY_PRINT_CHAR_DATA:
1275 if (printer_data->hit_nul && ch)
1276 printer_data->is_printable = false;
1277
1278 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001279 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001280
1281 if (!printer_data->is_printable)
1282 break;
1283
1284 if (ch == '\0')
1285 printer_data->hit_nul = true;
1286 else
1287 printer_data->is_printable = false;
1288 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001289 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001290 }
1291 break;
1292 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001293 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001294 break;
1295 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001296 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001297 printer_data->line_no++;
1298 break;
1299 case BINARY_PRINT_DATA_END:
1300 default:
1301 break;
1302 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001303
1304 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001305}
1306
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001307static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001308{
1309 unsigned int nr_bytes = sample->raw_size;
1310 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001311 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1312 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001313
1314 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001315 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1316
1317 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001318}
1319
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001320static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001321{
1322 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001323 return fprintf(fp, "%*s", spacing - len, "");
1324
1325 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001326}
1327
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001328static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001329{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001330 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001331}
1332
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001333static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001334{
1335 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1336 int len;
1337
1338 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001339 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001340
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001341 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001342 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001343 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001344}
1345
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001346static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001347{
1348 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1349 int len;
1350
1351 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001352 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001353
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001354 len = fprintf(fp, " hints: %#x extensions: %#x ",
1355 data->hints, data->extensions);
1356 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001357}
1358
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001359static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001360{
1361 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1362 int len;
1363
1364 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001365 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001366
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001367 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1368 data->hw, data->cstate, data->subcstate);
1369 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001370}
1371
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001372static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001373{
1374 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1375 int len;
1376
1377 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001378 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001379
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001380 len = fprintf(fp, " IP: %u ", data->ip);
1381 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001382}
1383
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001384static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001385{
1386 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1387 int len;
1388
1389 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001390 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001391
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001392 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001393 data->deepest_cstate, data->last_cstate,
1394 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001395 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001396}
1397
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001398static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001399{
1400 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1401 unsigned int percent, freq;
1402 int len;
1403
1404 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001405 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001406
1407 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001408 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001409 if (data->max_nonturbo) {
1410 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001411 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001412 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001413 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001414}
1415
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001416static int perf_sample__fprintf_synth(struct perf_sample *sample,
1417 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001418{
1419 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001420 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001421 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001422 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001423 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001424 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001425 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001426 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001427 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001428 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001429 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001430 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001431 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001432 default:
1433 break;
1434 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001435
1436 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001437}
1438
Jin Yao2ab046c2017-12-08 21:13:46 +08001439#define PTIME_RANGE_MAX 10
1440
Jiri Olsa809e9422015-11-26 18:55:21 +01001441struct perf_script {
1442 struct perf_tool tool;
1443 struct perf_session *session;
1444 bool show_task_events;
1445 bool show_mmap_events;
1446 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301447 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001448 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001449 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001450 struct cpu_map *cpus;
1451 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001452 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001453 const char *time_str;
Jin Yao2ab046c2017-12-08 21:13:46 +08001454 struct perf_time_interval ptime_range[PTIME_RANGE_MAX];
1455 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001456};
1457
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001458static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1459{
1460 struct perf_evsel *evsel;
1461 int max = 0;
1462
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001463 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001464 int len = strlen(perf_evsel__name(evsel));
1465
1466 max = MAX(len, max);
1467 }
1468
1469 return max;
1470}
1471
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001472static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001473{
1474 struct mem_info mi = { .data_src.val = data_src };
1475 char decode[100];
1476 char out[100];
1477 static int maxlen;
1478 int len;
1479
1480 perf_script__meminfo_scnprintf(decode, 100, &mi);
1481
1482 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1483 if (maxlen < len)
1484 maxlen = len;
1485
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001486 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001487}
1488
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001489struct metric_ctx {
1490 struct perf_sample *sample;
1491 struct thread *thread;
1492 struct perf_evsel *evsel;
1493 FILE *fp;
1494};
1495
1496static void script_print_metric(void *ctx, const char *color,
1497 const char *fmt,
1498 const char *unit, double val)
1499{
1500 struct metric_ctx *mctx = ctx;
1501
1502 if (!fmt)
1503 return;
1504 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
1505 mctx->fp);
1506 fputs("\tmetric: ", mctx->fp);
1507 if (color)
1508 color_fprintf(mctx->fp, color, fmt, val);
1509 else
1510 printf(fmt, val);
1511 fprintf(mctx->fp, " %s\n", unit);
1512}
1513
1514static void script_new_line(void *ctx)
1515{
1516 struct metric_ctx *mctx = ctx;
1517
1518 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
1519 mctx->fp);
1520 fputs("\tmetric: ", mctx->fp);
1521}
1522
1523static void perf_sample__fprint_metric(struct perf_script *script,
1524 struct thread *thread,
1525 struct perf_evsel *evsel,
1526 struct perf_sample *sample,
1527 FILE *fp)
1528{
1529 struct perf_stat_output_ctx ctx = {
1530 .print_metric = script_print_metric,
1531 .new_line = script_new_line,
1532 .ctx = &(struct metric_ctx) {
1533 .sample = sample,
1534 .thread = thread,
1535 .evsel = evsel,
1536 .fp = fp,
1537 },
1538 .force_header = false,
1539 };
1540 struct perf_evsel *ev2;
1541 static bool init;
1542 u64 val;
1543
1544 if (!init) {
1545 perf_stat__init_shadow_stats();
1546 init = true;
1547 }
1548 if (!evsel->stats)
1549 perf_evlist__alloc_stats(script->session->evlist, false);
1550 if (evsel_script(evsel->leader)->gnum++ == 0)
1551 perf_stat__reset_shadow_stats();
1552 val = sample->period * evsel->scale;
1553 perf_stat__update_shadow_stats(evsel,
1554 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001555 sample->cpu,
1556 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001557 evsel_script(evsel)->val = val;
1558 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1559 for_each_group_member (ev2, evsel->leader) {
1560 perf_stat__print_shadow_stats(ev2,
1561 evsel_script(ev2)->val,
1562 sample->cpu,
1563 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001564 NULL,
1565 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001566 }
1567 evsel_script(evsel->leader)->gnum = 0;
1568 }
1569}
1570
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001571static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001572 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001573 struct addr_location *al,
1574 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001575{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001576 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001577 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001578 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001579 struct perf_evsel_script *es = evsel->priv;
1580 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001581
Adrian Hunter14057202017-06-21 13:17:19 +03001582 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001583 return;
1584
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001585 ++es->samples;
1586
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001587 perf_sample__fprintf_start(sample, thread, evsel, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001588
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001589 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001590 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001591
Namhyung Kime944d3d2013-11-18 14:34:52 +09001592 if (PRINT_FIELD(EVNAME)) {
1593 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001594
1595 if (!script->name_width)
1596 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1597
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001598 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001599 }
1600
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001601 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001602 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001603
Akihiro Nagai95582592012-01-30 13:43:09 +09001604 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001605 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001606 return;
1607 }
1608
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001609 if (PRINT_FIELD(TRACE)) {
1610 event_format__fprintf(evsel->tp_format, sample->cpu,
1611 sample->raw_data, sample->raw_size, fp);
1612 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001613
1614 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001615 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001616
David Ahern7cec0922011-05-30 13:08:23 -06001617 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001618 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001619
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001620 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001621 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001622
1623 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001624 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001625
David Ahern787bef12011-05-27 14:28:43 -06001626 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001627 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001628
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001629 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001630 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001631 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001632 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001633
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001634 fputc(cursor ? '\n' : ' ', fp);
1635 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001636 }
1637
Stephane Eranianfc36f942015-08-31 18:41:10 +02001638 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001639 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001640
Andi Kleenb1491ac2017-09-05 11:40:57 -07001641 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001642 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001643
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001644 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001645 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001646 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001647 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001648 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001649 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001650
Wang Nan30372f02016-02-24 11:20:45 +00001651 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001652 perf_sample__fprintf_bpf_output(sample, fp);
1653 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001654
1655 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001656 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1657 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001658
1659 if (PRINT_FIELD(METRIC))
1660 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001661}
1662
Tom Zanussi956ffd02009-11-25 01:15:46 -06001663static struct scripting_ops *scripting_ops;
1664
Jiri Olsa36e33c52016-01-06 11:49:56 +01001665static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1666{
1667 int nthreads = thread_map__nr(counter->threads);
1668 int ncpus = perf_evsel__nr_cpus(counter);
1669 int cpu, thread;
1670 static int header_printed;
1671
1672 if (counter->system_wide)
1673 nthreads = 1;
1674
1675 if (!header_printed) {
1676 printf("%3s %8s %15s %15s %15s %15s %s\n",
1677 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1678 header_printed = 1;
1679 }
1680
1681 for (thread = 0; thread < nthreads; thread++) {
1682 for (cpu = 0; cpu < ncpus; cpu++) {
1683 struct perf_counts_values *counts;
1684
1685 counts = perf_counts(counter->counts, cpu, thread);
1686
1687 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1688 counter->cpus->map[cpu],
1689 thread_map__pid(counter->threads, thread),
1690 counts->val,
1691 counts->ena,
1692 counts->run,
1693 tstamp,
1694 perf_evsel__name(counter));
1695 }
1696 }
1697}
1698
Jiri Olsae099eba2016-01-05 22:09:09 +01001699static void process_stat(struct perf_evsel *counter, u64 tstamp)
1700{
1701 if (scripting_ops && scripting_ops->process_stat)
1702 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001703 else
1704 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001705}
1706
1707static void process_stat_interval(u64 tstamp)
1708{
1709 if (scripting_ops && scripting_ops->process_stat_interval)
1710 scripting_ops->process_stat_interval(tstamp);
1711}
1712
Tom Zanussi956ffd02009-11-25 01:15:46 -06001713static void setup_scripting(void)
1714{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001715 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001716 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001717}
1718
Adrian Hunterd445dd22014-08-15 22:08:37 +03001719static int flush_scripting(void)
1720{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001721 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001722}
1723
Tom Zanussi956ffd02009-11-25 01:15:46 -06001724static int cleanup_scripting(void)
1725{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001726 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001727
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001728 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001729}
1730
Jiri Olsa809e9422015-11-26 18:55:21 +01001731static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001732 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001733 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001734 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001735 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001736{
Jiri Olsa809e9422015-11-26 18:55:21 +01001737 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001738 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001739
Jin Yao2ab046c2017-12-08 21:13:46 +08001740 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1741 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001742 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001743 }
David Aherna91f4c42016-11-29 10:15:43 -07001744
David Ahern1424dc92011-03-09 22:23:28 -07001745 if (debug_mode) {
1746 if (sample->time < last_timestamp) {
1747 pr_err("Samples misordered, previous: %" PRIu64
1748 " this: %" PRIu64 "\n", last_timestamp,
1749 sample->time);
1750 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001751 }
David Ahern1424dc92011-03-09 22:23:28 -07001752 last_timestamp = sample->time;
1753 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001754 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001755
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001756 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001757 pr_err("problem processing %d event, skipping it.\n",
1758 event->header.type);
1759 return -1;
1760 }
1761
1762 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001763 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001764
Anton Blanchard5d67be92011-07-04 21:57:50 +10001765 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001766 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001767
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001768 if (scripting_ops)
1769 scripting_ops->process_event(event, sample, evsel, &al);
1770 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001771 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001772
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001773out_put:
1774 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001775 return 0;
1776}
1777
Adrian Hunter7ea95722013-11-01 15:51:30 +02001778static int process_attr(struct perf_tool *tool, union perf_event *event,
1779 struct perf_evlist **pevlist)
1780{
1781 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1782 struct perf_evlist *evlist;
1783 struct perf_evsel *evsel, *pos;
1784 int err;
1785
1786 err = perf_event__process_attr(tool, event, pevlist);
1787 if (err)
1788 return err;
1789
1790 evlist = *pevlist;
1791 evsel = perf_evlist__last(*pevlist);
1792
Adrian Hunter14057202017-06-21 13:17:19 +03001793 if (evsel->attr.type >= PERF_TYPE_MAX &&
1794 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001795 return 0;
1796
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001797 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001798 if (pos->attr.type == evsel->attr.type && pos != evsel)
1799 return 0;
1800 }
1801
1802 set_print_ip_opts(&evsel->attr);
1803
Jiri Olsad2b5a312015-10-16 12:41:25 +02001804 if (evsel->attr.sample_type)
1805 err = perf_evsel__check_attr(evsel, scr->session);
1806
1807 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001808}
1809
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001810static int process_comm_event(struct perf_tool *tool,
1811 union perf_event *event,
1812 struct perf_sample *sample,
1813 struct machine *machine)
1814{
1815 struct thread *thread;
1816 struct perf_script *script = container_of(tool, struct perf_script, tool);
1817 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001818 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001819 int ret = -1;
1820
1821 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1822 if (thread == NULL) {
1823 pr_debug("problem processing COMM event, skipping it.\n");
1824 return -1;
1825 }
1826
1827 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1828 goto out;
1829
1830 if (!evsel->attr.sample_id_all) {
1831 sample->cpu = 0;
1832 sample->time = 0;
1833 sample->tid = event->comm.tid;
1834 sample->pid = event->comm.pid;
1835 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001836 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001837 perf_event__fprintf(event, stdout);
1838 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001839out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001840 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001841 return ret;
1842}
1843
Hari Bathini96a44bb2017-03-08 02:12:06 +05301844static int process_namespaces_event(struct perf_tool *tool,
1845 union perf_event *event,
1846 struct perf_sample *sample,
1847 struct machine *machine)
1848{
1849 struct thread *thread;
1850 struct perf_script *script = container_of(tool, struct perf_script, tool);
1851 struct perf_session *session = script->session;
1852 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1853 int ret = -1;
1854
1855 thread = machine__findnew_thread(machine, event->namespaces.pid,
1856 event->namespaces.tid);
1857 if (thread == NULL) {
1858 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1859 return -1;
1860 }
1861
1862 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1863 goto out;
1864
1865 if (!evsel->attr.sample_id_all) {
1866 sample->cpu = 0;
1867 sample->time = 0;
1868 sample->tid = event->namespaces.tid;
1869 sample->pid = event->namespaces.pid;
1870 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001871 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Hari Bathini96a44bb2017-03-08 02:12:06 +05301872 perf_event__fprintf(event, stdout);
1873 ret = 0;
1874out:
1875 thread__put(thread);
1876 return ret;
1877}
1878
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001879static int process_fork_event(struct perf_tool *tool,
1880 union perf_event *event,
1881 struct perf_sample *sample,
1882 struct machine *machine)
1883{
1884 struct thread *thread;
1885 struct perf_script *script = container_of(tool, struct perf_script, tool);
1886 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001887 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001888
1889 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1890 return -1;
1891
1892 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1893 if (thread == NULL) {
1894 pr_debug("problem processing FORK event, skipping it.\n");
1895 return -1;
1896 }
1897
1898 if (!evsel->attr.sample_id_all) {
1899 sample->cpu = 0;
1900 sample->time = event->fork.time;
1901 sample->tid = event->fork.tid;
1902 sample->pid = event->fork.pid;
1903 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001904 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001905 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001906 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001907
1908 return 0;
1909}
1910static int process_exit_event(struct perf_tool *tool,
1911 union perf_event *event,
1912 struct perf_sample *sample,
1913 struct machine *machine)
1914{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001915 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001916 struct thread *thread;
1917 struct perf_script *script = container_of(tool, struct perf_script, tool);
1918 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001919 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001920
1921 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1922 if (thread == NULL) {
1923 pr_debug("problem processing EXIT event, skipping it.\n");
1924 return -1;
1925 }
1926
1927 if (!evsel->attr.sample_id_all) {
1928 sample->cpu = 0;
1929 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001930 sample->tid = event->fork.tid;
1931 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001932 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001933 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001934 perf_event__fprintf(event, stdout);
1935
1936 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001937 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001938
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001939 thread__put(thread);
1940 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001941}
1942
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001943static int process_mmap_event(struct perf_tool *tool,
1944 union perf_event *event,
1945 struct perf_sample *sample,
1946 struct machine *machine)
1947{
1948 struct thread *thread;
1949 struct perf_script *script = container_of(tool, struct perf_script, tool);
1950 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001951 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001952
1953 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1954 return -1;
1955
1956 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1957 if (thread == NULL) {
1958 pr_debug("problem processing MMAP event, skipping it.\n");
1959 return -1;
1960 }
1961
1962 if (!evsel->attr.sample_id_all) {
1963 sample->cpu = 0;
1964 sample->time = 0;
1965 sample->tid = event->mmap.tid;
1966 sample->pid = event->mmap.pid;
1967 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001968 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001969 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001970 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001971 return 0;
1972}
1973
1974static int process_mmap2_event(struct perf_tool *tool,
1975 union perf_event *event,
1976 struct perf_sample *sample,
1977 struct machine *machine)
1978{
1979 struct thread *thread;
1980 struct perf_script *script = container_of(tool, struct perf_script, tool);
1981 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001982 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001983
1984 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1985 return -1;
1986
1987 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1988 if (thread == NULL) {
1989 pr_debug("problem processing MMAP2 event, skipping it.\n");
1990 return -1;
1991 }
1992
1993 if (!evsel->attr.sample_id_all) {
1994 sample->cpu = 0;
1995 sample->time = 0;
1996 sample->tid = event->mmap2.tid;
1997 sample->pid = event->mmap2.pid;
1998 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001999 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002000 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002001 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002002 return 0;
2003}
2004
Adrian Hunter7c148982015-07-21 12:44:06 +03002005static int process_switch_event(struct perf_tool *tool,
2006 union perf_event *event,
2007 struct perf_sample *sample,
2008 struct machine *machine)
2009{
2010 struct thread *thread;
2011 struct perf_script *script = container_of(tool, struct perf_script, tool);
2012 struct perf_session *session = script->session;
2013 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2014
2015 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2016 return -1;
2017
2018 thread = machine__findnew_thread(machine, sample->pid,
2019 sample->tid);
2020 if (thread == NULL) {
2021 pr_debug("problem processing SWITCH event, skipping it.\n");
2022 return -1;
2023 }
2024
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03002025 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Adrian Hunter7c148982015-07-21 12:44:06 +03002026 perf_event__fprintf(event, stdout);
2027 thread__put(thread);
2028 return 0;
2029}
2030
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002031static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002032{
2033 session_done = 1;
2034}
2035
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002036static void perf_script__fclose_per_event_dump(struct perf_script *script)
2037{
2038 struct perf_evlist *evlist = script->session->evlist;
2039 struct perf_evsel *evsel;
2040
2041 evlist__for_each_entry(evlist, evsel) {
2042 if (!evsel->priv)
2043 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002044 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002045 evsel->priv = NULL;
2046 }
2047}
2048
2049static int perf_script__fopen_per_event_dump(struct perf_script *script)
2050{
2051 struct perf_evsel *evsel;
2052
2053 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002054 /*
2055 * Already setup? I.e. we may be called twice in cases like
2056 * Intel PT, one for the intel_pt// and dummy events, then
2057 * for the evsels syntheized from the auxtrace info.
2058 *
2059 * Ses perf_script__process_auxtrace_info.
2060 */
2061 if (evsel->priv != NULL)
2062 continue;
2063
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002064 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002065 if (evsel->priv == NULL)
2066 goto out_err_fclose;
2067 }
2068
2069 return 0;
2070
2071out_err_fclose:
2072 perf_script__fclose_per_event_dump(script);
2073 return -1;
2074}
2075
2076static int perf_script__setup_per_event_dump(struct perf_script *script)
2077{
2078 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002079 static struct perf_evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002080
2081 if (script->per_event_dump)
2082 return perf_script__fopen_per_event_dump(script);
2083
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002084 es_stdout.fp = stdout;
2085
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002086 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002087 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002088
2089 return 0;
2090}
2091
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002092static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2093{
2094 struct perf_evsel *evsel;
2095
2096 evlist__for_each_entry(script->session->evlist, evsel) {
2097 struct perf_evsel_script *es = evsel->priv;
2098
2099 perf_evsel_script__fprintf(es, stdout);
2100 perf_evsel_script__delete(es);
2101 evsel->priv = NULL;
2102 }
2103}
2104
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002105static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002106{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002107 int ret;
2108
Tom Zanussic239da32010-04-01 23:59:18 -05002109 signal(SIGINT, sig_handler);
2110
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002111 /* override event processing functions */
2112 if (script->show_task_events) {
2113 script->tool.comm = process_comm_event;
2114 script->tool.fork = process_fork_event;
2115 script->tool.exit = process_exit_event;
2116 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002117 if (script->show_mmap_events) {
2118 script->tool.mmap = process_mmap_event;
2119 script->tool.mmap2 = process_mmap2_event;
2120 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002121 if (script->show_switch_events)
2122 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302123 if (script->show_namespace_events)
2124 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002125
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002126 if (perf_script__setup_per_event_dump(script)) {
2127 pr_err("Couldn't create the per event dump files\n");
2128 return -1;
2129 }
2130
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002131 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002132
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002133 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002134 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002135
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002136 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002137 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002138
2139 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002140}
2141
Tom Zanussi956ffd02009-11-25 01:15:46 -06002142struct script_spec {
2143 struct list_head node;
2144 struct scripting_ops *ops;
2145 char spec[0];
2146};
2147
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002148static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002149
2150static struct script_spec *script_spec__new(const char *spec,
2151 struct scripting_ops *ops)
2152{
2153 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2154
2155 if (s != NULL) {
2156 strcpy(s->spec, spec);
2157 s->ops = ops;
2158 }
2159
2160 return s;
2161}
2162
Tom Zanussi956ffd02009-11-25 01:15:46 -06002163static void script_spec__add(struct script_spec *s)
2164{
2165 list_add_tail(&s->node, &script_specs);
2166}
2167
2168static struct script_spec *script_spec__find(const char *spec)
2169{
2170 struct script_spec *s;
2171
2172 list_for_each_entry(s, &script_specs, node)
2173 if (strcasecmp(s->spec, spec) == 0)
2174 return s;
2175 return NULL;
2176}
2177
Tom Zanussi956ffd02009-11-25 01:15:46 -06002178int script_spec_register(const char *spec, struct scripting_ops *ops)
2179{
2180 struct script_spec *s;
2181
2182 s = script_spec__find(spec);
2183 if (s)
2184 return -1;
2185
Taeung Song8560bae2016-02-26 00:13:10 +09002186 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002187 if (!s)
2188 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002189 else
2190 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002191
2192 return 0;
2193}
2194
2195static struct scripting_ops *script_spec__lookup(const char *spec)
2196{
2197 struct script_spec *s = script_spec__find(spec);
2198 if (!s)
2199 return NULL;
2200
2201 return s->ops;
2202}
2203
2204static void list_available_languages(void)
2205{
2206 struct script_spec *s;
2207
2208 fprintf(stderr, "\n");
2209 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002210 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002211
2212 list_for_each_entry(s, &script_specs, node)
2213 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2214
2215 fprintf(stderr, "\n");
2216}
2217
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002218static int parse_scriptname(const struct option *opt __maybe_unused,
2219 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002220{
2221 char spec[PATH_MAX];
2222 const char *script, *ext;
2223 int len;
2224
Tom Zanussif526d682010-01-27 02:27:52 -06002225 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002226 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002227 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002228 }
2229
2230 script = strchr(str, ':');
2231 if (script) {
2232 len = script - str;
2233 if (len >= PATH_MAX) {
2234 fprintf(stderr, "invalid language specifier");
2235 return -1;
2236 }
2237 strncpy(spec, str, len);
2238 spec[len] = '\0';
2239 scripting_ops = script_spec__lookup(spec);
2240 if (!scripting_ops) {
2241 fprintf(stderr, "invalid language specifier");
2242 return -1;
2243 }
2244 script++;
2245 } else {
2246 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002247 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002248 if (!ext) {
2249 fprintf(stderr, "invalid script extension");
2250 return -1;
2251 }
2252 scripting_ops = script_spec__lookup(++ext);
2253 if (!scripting_ops) {
2254 fprintf(stderr, "invalid script extension");
2255 return -1;
2256 }
2257 }
2258
2259 script_name = strdup(script);
2260
2261 return 0;
2262}
2263
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002264static int parse_output_fields(const struct option *opt __maybe_unused,
2265 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002266{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002267 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002268 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002269 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002270 int rc = 0;
2271 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002272 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002273 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002274
2275 if (!str)
2276 return -ENOMEM;
2277
David Ahern2c9e45f72011-03-17 10:03:21 -06002278 /* first word can state for which event type the user is specifying
2279 * the fields. If no type exists, the specified fields apply to all
2280 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002281 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002282 tok = strchr(str, ':');
2283 if (tok) {
2284 *tok = '\0';
2285 tok++;
2286 if (!strcmp(str, "hw"))
2287 type = PERF_TYPE_HARDWARE;
2288 else if (!strcmp(str, "sw"))
2289 type = PERF_TYPE_SOFTWARE;
2290 else if (!strcmp(str, "trace"))
2291 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002292 else if (!strcmp(str, "raw"))
2293 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002294 else if (!strcmp(str, "break"))
2295 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002296 else if (!strcmp(str, "synth"))
2297 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002298 else {
2299 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002300 rc = -EINVAL;
2301 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002302 }
2303
2304 if (output[type].user_set)
2305 pr_warning("Overriding previous field request for %s events.\n",
2306 event_type(type));
2307
2308 output[type].fields = 0;
2309 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002310 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002311
2312 } else {
2313 tok = str;
2314 if (strlen(str) == 0) {
2315 fprintf(stderr,
2316 "Cannot set fields to 'none' for all event types.\n");
2317 rc = -EINVAL;
2318 goto out;
2319 }
2320
Andi Kleen36ce5652017-06-02 08:48:10 -07002321 /* Don't override defaults for +- */
2322 if (strchr(str, '+') || strchr(str, '-'))
2323 goto parse;
2324
David Ahern2c9e45f72011-03-17 10:03:21 -06002325 if (output_set_by_user())
2326 pr_warning("Overriding previous field request for all events.\n");
2327
Adrian Hunter14057202017-06-21 13:17:19 +03002328 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002329 output[j].fields = 0;
2330 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002331 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002332 }
David Ahern1424dc92011-03-09 22:23:28 -07002333 }
2334
Andi Kleen36ce5652017-06-02 08:48:10 -07002335parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002336 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002337 if (*tok == '+') {
2338 if (change == SET)
2339 goto out_badmix;
2340 change = ADD;
2341 tok++;
2342 } else if (*tok == '-') {
2343 if (change == SET)
2344 goto out_badmix;
2345 change = REMOVE;
2346 tok++;
2347 } else {
2348 if (change != SET && change != DEFAULT)
2349 goto out_badmix;
2350 change = SET;
2351 }
2352
David Ahern745f43e2011-03-09 22:23:26 -07002353 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002354 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002355 break;
David Ahern745f43e2011-03-09 22:23:26 -07002356 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002357 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002358 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002359 continue;
2360 }
David Ahern745f43e2011-03-09 22:23:26 -07002361 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002362 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002363 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002364 goto out;
2365 }
2366
2367 if (type == -1) {
2368 /* add user option to all events types for
2369 * which it is valid
2370 */
Adrian Hunter14057202017-06-21 13:17:19 +03002371 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002372 if (output[j].invalid_fields & all_output_options[i].field) {
2373 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2374 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002375 } else {
2376 if (change == REMOVE)
2377 output[j].fields &= ~all_output_options[i].field;
2378 else
2379 output[j].fields |= all_output_options[i].field;
2380 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002381 }
2382 } else {
2383 if (output[type].invalid_fields & all_output_options[i].field) {
2384 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2385 all_output_options[i].str, event_type(type));
2386
2387 rc = -EINVAL;
2388 goto out;
2389 }
2390 output[type].fields |= all_output_options[i].field;
2391 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002392 }
2393
2394 if (type >= 0) {
2395 if (output[type].fields == 0) {
2396 pr_debug("No fields requested for %s type. "
2397 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002398 }
David Ahern1424dc92011-03-09 22:23:28 -07002399 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002400 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002401
Andi Kleen36ce5652017-06-02 08:48:10 -07002402out_badmix:
2403 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2404 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002405out:
David Ahern745f43e2011-03-09 22:23:26 -07002406 free(str);
2407 return rc;
2408}
2409
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002410#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2411 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2412 if ((lang_dirent->d_type == DT_DIR || \
2413 (lang_dirent->d_type == DT_UNKNOWN && \
2414 is_directory(scripts_path, lang_dirent))) && \
2415 (strcmp(lang_dirent->d_name, ".")) && \
2416 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002417
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002418#define for_each_script(lang_path, lang_dir, script_dirent) \
2419 while ((script_dirent = readdir(lang_dir)) != NULL) \
2420 if (script_dirent->d_type != DT_DIR && \
2421 (script_dirent->d_type != DT_UNKNOWN || \
2422 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002423
2424
2425#define RECORD_SUFFIX "-record"
2426#define REPORT_SUFFIX "-report"
2427
2428struct script_desc {
2429 struct list_head node;
2430 char *name;
2431 char *half_liner;
2432 char *args;
2433};
2434
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002435static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002436
2437static struct script_desc *script_desc__new(const char *name)
2438{
2439 struct script_desc *s = zalloc(sizeof(*s));
2440
Tom Zanussib5b87312010-11-10 08:16:51 -06002441 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002442 s->name = strdup(name);
2443
2444 return s;
2445}
2446
2447static void script_desc__delete(struct script_desc *s)
2448{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002449 zfree(&s->name);
2450 zfree(&s->half_liner);
2451 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002452 free(s);
2453}
2454
2455static void script_desc__add(struct script_desc *s)
2456{
2457 list_add_tail(&s->node, &script_descs);
2458}
2459
2460static struct script_desc *script_desc__find(const char *name)
2461{
2462 struct script_desc *s;
2463
2464 list_for_each_entry(s, &script_descs, node)
2465 if (strcasecmp(s->name, name) == 0)
2466 return s;
2467 return NULL;
2468}
2469
2470static struct script_desc *script_desc__findnew(const char *name)
2471{
2472 struct script_desc *s = script_desc__find(name);
2473
2474 if (s)
2475 return s;
2476
2477 s = script_desc__new(name);
2478 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002479 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002480
2481 script_desc__add(s);
2482
2483 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002484}
2485
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002486static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002487{
2488 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002489 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002490
2491 if (strlen(str) > suffix_len) {
2492 p = str + strlen(str) - suffix_len;
2493 if (!strncmp(p, suffix, suffix_len))
2494 return p;
2495 }
2496
2497 return NULL;
2498}
2499
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002500static int read_script_info(struct script_desc *desc, const char *filename)
2501{
2502 char line[BUFSIZ], *p;
2503 FILE *fp;
2504
2505 fp = fopen(filename, "r");
2506 if (!fp)
2507 return -1;
2508
2509 while (fgets(line, sizeof(line), fp)) {
2510 p = ltrim(line);
2511 if (strlen(p) == 0)
2512 continue;
2513 if (*p != '#')
2514 continue;
2515 p++;
2516 if (strlen(p) && *p == '!')
2517 continue;
2518
2519 p = ltrim(p);
2520 if (strlen(p) && p[strlen(p) - 1] == '\n')
2521 p[strlen(p) - 1] = '\0';
2522
2523 if (!strncmp(p, "description:", strlen("description:"))) {
2524 p += strlen("description:");
2525 desc->half_liner = strdup(ltrim(p));
2526 continue;
2527 }
2528
2529 if (!strncmp(p, "args:", strlen("args:"))) {
2530 p += strlen("args:");
2531 desc->args = strdup(ltrim(p));
2532 continue;
2533 }
2534 }
2535
2536 fclose(fp);
2537
2538 return 0;
2539}
2540
Robert Richter38efb532011-11-25 11:38:40 +01002541static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2542{
2543 char *script_root, *str;
2544
2545 script_root = strdup(script_dirent->d_name);
2546 if (!script_root)
2547 return NULL;
2548
2549 str = (char *)ends_with(script_root, suffix);
2550 if (!str) {
2551 free(script_root);
2552 return NULL;
2553 }
2554
2555 *str = '\0';
2556 return script_root;
2557}
2558
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002559static int list_available_scripts(const struct option *opt __maybe_unused,
2560 const char *s __maybe_unused,
2561 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002562{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002563 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002564 char scripts_path[MAXPATHLEN];
2565 DIR *scripts_dir, *lang_dir;
2566 char script_path[MAXPATHLEN];
2567 char lang_path[MAXPATHLEN];
2568 struct script_desc *desc;
2569 char first_half[BUFSIZ];
2570 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002571
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002572 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002573
2574 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002575 if (!scripts_dir) {
2576 fprintf(stdout,
2577 "open(%s) failed.\n"
2578 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2579 scripts_path);
2580 exit(-1);
2581 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002582
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002583 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002584 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002585 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002586 lang_dir = opendir(lang_path);
2587 if (!lang_dir)
2588 continue;
2589
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002590 for_each_script(lang_path, lang_dir, script_dirent) {
2591 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002592 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002593 desc = script_desc__findnew(script_root);
2594 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002595 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002596 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002597 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002598 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002599 }
2600 }
2601
2602 fprintf(stdout, "List of available trace scripts:\n");
2603 list_for_each_entry(desc, &script_descs, node) {
2604 sprintf(first_half, "%s %s", desc->name,
2605 desc->args ? desc->args : "");
2606 fprintf(stdout, " %-36s %s\n", first_half,
2607 desc->half_liner ? desc->half_liner : "");
2608 }
2609
2610 exit(0);
2611}
2612
Feng Tange5f37052012-09-07 16:42:26 +08002613/*
Feng Tang49e639e2012-10-30 11:56:03 +08002614 * Some scripts specify the required events in their "xxx-record" file,
2615 * this function will check if the events in perf.data match those
2616 * mentioned in the "xxx-record".
2617 *
2618 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2619 * which is covered well now. And new parsing code should be added to
2620 * cover the future complexing formats like event groups etc.
2621 */
2622static int check_ev_match(char *dir_name, char *scriptname,
2623 struct perf_session *session)
2624{
2625 char filename[MAXPATHLEN], evname[128];
2626 char line[BUFSIZ], *p;
2627 struct perf_evsel *pos;
2628 int match, len;
2629 FILE *fp;
2630
2631 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2632
2633 fp = fopen(filename, "r");
2634 if (!fp)
2635 return -1;
2636
2637 while (fgets(line, sizeof(line), fp)) {
2638 p = ltrim(line);
2639 if (*p == '#')
2640 continue;
2641
2642 while (strlen(p)) {
2643 p = strstr(p, "-e");
2644 if (!p)
2645 break;
2646
2647 p += 2;
2648 p = ltrim(p);
2649 len = strcspn(p, " \t");
2650 if (!len)
2651 break;
2652
2653 snprintf(evname, len + 1, "%s", p);
2654
2655 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002656 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002657 if (!strcmp(perf_evsel__name(pos), evname)) {
2658 match = 1;
2659 break;
2660 }
2661 }
2662
2663 if (!match) {
2664 fclose(fp);
2665 return -1;
2666 }
2667 }
2668 }
2669
2670 fclose(fp);
2671 return 0;
2672}
2673
2674/*
Feng Tange5f37052012-09-07 16:42:26 +08002675 * Return -1 if none is found, otherwise the actual scripts number.
2676 *
2677 * Currently the only user of this function is the script browser, which
2678 * will list all statically runnable scripts, select one, execute it and
2679 * show the output in a perf browser.
2680 */
2681int find_scripts(char **scripts_array, char **scripts_path_array)
2682{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002683 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002684 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002685 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002686 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002687 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002688 .file = {
2689 .path = input_name,
2690 },
2691 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002692 };
Feng Tange5f37052012-09-07 16:42:26 +08002693 char *temp;
2694 int i = 0;
2695
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002696 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002697 if (!session)
2698 return -1;
2699
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002700 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002701
2702 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002703 if (!scripts_dir) {
2704 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002705 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002706 }
Feng Tange5f37052012-09-07 16:42:26 +08002707
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002708 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002709 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002710 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002711#ifdef NO_LIBPERL
2712 if (strstr(lang_path, "perl"))
2713 continue;
2714#endif
2715#ifdef NO_LIBPYTHON
2716 if (strstr(lang_path, "python"))
2717 continue;
2718#endif
2719
2720 lang_dir = opendir(lang_path);
2721 if (!lang_dir)
2722 continue;
2723
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002724 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002725 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002726 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002727 continue;
2728 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002729 script_dirent->d_name);
2730 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002731 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002732 (temp - script_dirent->d_name) + 1,
2733 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002734
2735 if (check_ev_match(lang_path,
2736 scripts_array[i], session))
2737 continue;
2738
Feng Tange5f37052012-09-07 16:42:26 +08002739 i++;
2740 }
Feng Tang49e639e2012-10-30 11:56:03 +08002741 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002742 }
2743
Feng Tang49e639e2012-10-30 11:56:03 +08002744 closedir(scripts_dir);
2745 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002746 return i;
2747}
2748
Tom Zanussi38752942009-12-15 02:53:39 -06002749static char *get_script_path(const char *script_root, const char *suffix)
2750{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002751 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002752 char scripts_path[MAXPATHLEN];
2753 char script_path[MAXPATHLEN];
2754 DIR *scripts_dir, *lang_dir;
2755 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002756 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002757
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002758 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002759
2760 scripts_dir = opendir(scripts_path);
2761 if (!scripts_dir)
2762 return NULL;
2763
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002764 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002765 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002766 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002767 lang_dir = opendir(lang_path);
2768 if (!lang_dir)
2769 continue;
2770
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002771 for_each_script(lang_path, lang_dir, script_dirent) {
2772 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002773 if (__script_root && !strcmp(script_root, __script_root)) {
2774 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002775 closedir(lang_dir);
2776 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002777 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002778 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002779 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002780 }
2781 free(__script_root);
2782 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002783 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002784 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002785 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002786
Robert Richter38efb532011-11-25 11:38:40 +01002787 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002788}
2789
Tom Zanussib5b87312010-11-10 08:16:51 -06002790static bool is_top_script(const char *script_path)
2791{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002792 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002793}
2794
2795static int has_required_arg(char *script_path)
2796{
2797 struct script_desc *desc;
2798 int n_args = 0;
2799 char *p;
2800
2801 desc = script_desc__new(NULL);
2802
2803 if (read_script_info(desc, script_path))
2804 goto out;
2805
2806 if (!desc->args)
2807 goto out;
2808
2809 for (p = desc->args; *p; p++)
2810 if (*p == '<')
2811 n_args++;
2812out:
2813 script_desc__delete(desc);
2814
2815 return n_args;
2816}
2817
David Ahernd54b1a92012-08-26 12:24:46 -06002818static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002819{
2820 char **__argv = malloc(sizeof(const char *) * argc);
2821
David Ahernd54b1a92012-08-26 12:24:46 -06002822 if (!__argv) {
2823 pr_err("malloc failed\n");
2824 return -1;
2825 }
2826
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002827 memcpy(__argv, argv, sizeof(const char *) * argc);
2828 argc = parse_options(argc, (const char **)__argv, record_options,
2829 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2830 free(__argv);
2831
David Ahernd54b1a92012-08-26 12:24:46 -06002832 system_wide = (argc == 0);
2833
2834 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002835}
2836
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002837static void script__setup_sample_type(struct perf_script *script)
2838{
2839 struct perf_session *session = script->session;
2840 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2841
2842 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2843 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2844 (sample_type & PERF_SAMPLE_STACK_USER))
2845 callchain_param.record_mode = CALLCHAIN_DWARF;
2846 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2847 callchain_param.record_mode = CALLCHAIN_LBR;
2848 else
2849 callchain_param.record_mode = CALLCHAIN_FP;
2850 }
2851}
2852
Jiri Olsae099eba2016-01-05 22:09:09 +01002853static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2854 union perf_event *event,
2855 struct perf_session *session)
2856{
2857 struct stat_round_event *round = &event->stat_round;
2858 struct perf_evsel *counter;
2859
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002860 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002861 perf_stat_process_counter(&stat_config, counter);
2862 process_stat(counter, round->time);
2863 }
2864
2865 process_stat_interval(round->time);
2866 return 0;
2867}
2868
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002869static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2870 union perf_event *event,
2871 struct perf_session *session __maybe_unused)
2872{
2873 perf_event__read_stat_config(&stat_config, &event->stat_config);
2874 return 0;
2875}
2876
Jiri Olsacfc88742016-01-05 22:09:06 +01002877static int set_maps(struct perf_script *script)
2878{
2879 struct perf_evlist *evlist = script->session->evlist;
2880
2881 if (!script->cpus || !script->threads)
2882 return 0;
2883
2884 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2885 return -EINVAL;
2886
2887 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2888
2889 if (perf_evlist__alloc_stats(evlist, true))
2890 return -ENOMEM;
2891
2892 script->allocated = true;
2893 return 0;
2894}
2895
2896static
2897int process_thread_map_event(struct perf_tool *tool,
2898 union perf_event *event,
2899 struct perf_session *session __maybe_unused)
2900{
2901 struct perf_script *script = container_of(tool, struct perf_script, tool);
2902
2903 if (script->threads) {
2904 pr_warning("Extra thread map event, ignoring.\n");
2905 return 0;
2906 }
2907
2908 script->threads = thread_map__new_event(&event->thread_map);
2909 if (!script->threads)
2910 return -ENOMEM;
2911
2912 return set_maps(script);
2913}
2914
2915static
2916int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2917 union perf_event *event,
2918 struct perf_session *session __maybe_unused)
2919{
2920 struct perf_script *script = container_of(tool, struct perf_script, tool);
2921
2922 if (script->cpus) {
2923 pr_warning("Extra cpu map event, ignoring.\n");
2924 return 0;
2925 }
2926
2927 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2928 if (!script->cpus)
2929 return -ENOMEM;
2930
2931 return set_maps(script);
2932}
2933
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002934#ifdef HAVE_AUXTRACE_SUPPORT
2935static int perf_script__process_auxtrace_info(struct perf_tool *tool,
2936 union perf_event *event,
2937 struct perf_session *session)
2938{
2939 int ret = perf_event__process_auxtrace_info(tool, event, session);
2940
2941 if (ret == 0) {
2942 struct perf_script *script = container_of(tool, struct perf_script, tool);
2943
2944 ret = perf_script__setup_per_event_dump(script);
2945 }
2946
2947 return ret;
2948}
2949#else
2950#define perf_script__process_auxtrace_info 0
2951#endif
2952
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002953int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002954{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002955 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002956 bool header = false;
2957 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002958 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002959 char *rec_script_path = NULL;
2960 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002961 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002962 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002963 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002964 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002965 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002966 struct perf_script script = {
2967 .tool = {
2968 .sample = process_sample_event,
2969 .mmap = perf_event__process_mmap,
2970 .mmap2 = perf_event__process_mmap2,
2971 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302972 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002973 .exit = perf_event__process_exit,
2974 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002975 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002976 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002977 .tracing_data = perf_event__process_tracing_data,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07002978 .feature = perf_event__process_feature,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002979 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002980 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002981 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002982 .auxtrace = perf_event__process_auxtrace,
2983 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002984 .stat = perf_event__process_stat_event,
2985 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002986 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002987 .thread_map = process_thread_map_event,
2988 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002989 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002990 .ordering_requires_timestamps = true,
2991 },
2992 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002993 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08002994 .mode = PERF_DATA_MODE_READ,
2995 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002996 const struct option options[] = {
2997 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2998 "dump raw trace in ASCII"),
2999 OPT_INCR('v', "verbose", &verbose,
3000 "be more verbose (show symbol address, etc)"),
3001 OPT_BOOLEAN('L', "Latency", &latency_format,
3002 "show latency attributes (irqs/preemption disabled, etc)"),
3003 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3004 list_available_scripts),
3005 OPT_CALLBACK('s', "script", NULL, "name",
3006 "script file name (lang:script name, script name, or *)",
3007 parse_scriptname),
3008 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3009 "generate perf-script.xx script in specified language"),
3010 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3011 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3012 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003013 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3014 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003015 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3016 "file", "vmlinux pathname"),
3017 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3018 "file", "kallsyms pathname"),
3019 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3020 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003021 OPT_CALLBACK(0, "symfs", NULL, "directory",
3022 "Look for files with symbols relative to this directory",
3023 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003024 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003025 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003026 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003027 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003028 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Andi Kleenb1491ac2017-09-05 11:40:57 -07003029 "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
Kan Liang49d58f02017-08-29 13:11:11 -04003030 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
Andi Kleen48d02a12017-02-23 15:46:34 -08003031 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003032 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3033 "system-wide collection from all CPUs"),
3034 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3035 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07003036 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3037 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003038 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3039 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3040 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003041 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3042 "only consider symbols in these pids"),
3043 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3044 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003045 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3046 "Set the maximum stack depth when parsing the callchain, "
3047 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003048 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003049 OPT_BOOLEAN('I', "show-info", &show_full_info,
3050 "display extended information from perf.data file"),
3051 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3052 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003053 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3054 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003055 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3056 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003057 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3058 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303059 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3060 "Show namespace events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003061 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3062 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003063 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003064 OPT_INTEGER(0, "max-blocks", &max_blocks,
3065 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03003066 OPT_BOOLEAN(0, "ns", &nanosecs,
3067 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003068 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
3069 "Instruction Tracing options",
3070 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003071 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3072 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003073 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3074 "Enable symbol demangling"),
3075 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3076 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003077 OPT_STRING(0, "time", &script.time_str, "str",
3078 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003079 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3080 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003081 OPT_END()
3082 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003083 const char * const script_subcommands[] = { "record", "report", NULL };
3084 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003085 "perf script [<options>]",
3086 "perf script [<options>] record <script> [<record-options>] <command>",
3087 "perf script [<options>] report <script> [script-args]",
3088 "perf script [<options>] <script> [<record-options>] <command>",
3089 "perf script [<options>] <top-script> [script-args]",
3090 NULL
3091 };
Tom Zanussi38752942009-12-15 02:53:39 -06003092
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003093 perf_set_singlethreaded();
3094
Tom Zanussib5b87312010-11-10 08:16:51 -06003095 setup_scripting();
3096
Yunlong Song40cae2b2015-03-18 21:35:54 +08003097 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003098 PARSE_OPT_STOP_AT_NON_OPTION);
3099
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003100 data.file.path = input_name;
3101 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003102
Tom Zanussib5b87312010-11-10 08:16:51 -06003103 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3104 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3105 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003106 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003107 }
3108
Tom Zanussib5b87312010-11-10 08:16:51 -06003109 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3110 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3111 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003112 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003113 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003114 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003115 return -1;
3116 }
Tom Zanussi38752942009-12-15 02:53:39 -06003117 }
3118
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003119 if (itrace_synth_opts.callchain &&
3120 itrace_synth_opts.callchain_sz > scripting_max_stack)
3121 scripting_max_stack = itrace_synth_opts.callchain_sz;
3122
Ben Hutchings44e668c2010-10-10 16:10:03 +01003123 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003124 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003125
Tom Zanussib5b87312010-11-10 08:16:51 -06003126 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003127 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003128 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003129 pid_t pid;
3130
Tom Zanussib5b87312010-11-10 08:16:51 -06003131 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3132 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3133
3134 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003135 usage_with_options_msg(script_usage, options,
3136 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003137 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003138 }
3139
Tom Zanussib5b87312010-11-10 08:16:51 -06003140 if (is_top_script(argv[0])) {
3141 rep_args = argc - 1;
3142 } else {
3143 int rec_args;
3144
3145 rep_args = has_required_arg(rep_script_path);
3146 rec_args = (argc - 1) - rep_args;
3147 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003148 usage_with_options_msg(script_usage, options,
3149 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003150 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003151 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003152 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003153 }
3154
3155 if (pipe(live_pipe) < 0) {
3156 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003157 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003158 }
3159
3160 pid = fork();
3161 if (pid < 0) {
3162 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003163 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003164 }
3165
3166 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003167 j = 0;
3168
Tom Zanussia0cccc22010-04-01 23:59:25 -05003169 dup2(live_pipe[1], 1);
3170 close(live_pipe[0]);
3171
Robert Richter317df652011-11-25 15:05:25 +01003172 if (is_top_script(argv[0])) {
3173 system_wide = true;
3174 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003175 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3176 err = -1;
3177 goto out;
3178 }
Robert Richter317df652011-11-25 15:05:25 +01003179 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003180
3181 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003182 if (!__argv) {
3183 pr_err("malloc failed\n");
3184 err = -ENOMEM;
3185 goto out;
3186 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003187
Tom Zanussib5b87312010-11-10 08:16:51 -06003188 __argv[j++] = "/bin/sh";
3189 __argv[j++] = rec_script_path;
3190 if (system_wide)
3191 __argv[j++] = "-a";
3192 __argv[j++] = "-q";
3193 __argv[j++] = "-o";
3194 __argv[j++] = "-";
3195 for (i = rep_args + 1; i < argc; i++)
3196 __argv[j++] = argv[i];
3197 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003198
3199 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003200 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003201 exit(-1);
3202 }
3203
3204 dup2(live_pipe[0], 0);
3205 close(live_pipe[1]);
3206
Tom Zanussib5b87312010-11-10 08:16:51 -06003207 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003208 if (!__argv) {
3209 pr_err("malloc failed\n");
3210 err = -ENOMEM;
3211 goto out;
3212 }
3213
Tom Zanussib5b87312010-11-10 08:16:51 -06003214 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003215 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003216 __argv[j++] = rep_script_path;
3217 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003218 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003219 __argv[j++] = "-i";
3220 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003221 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003222
3223 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003224 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003225 exit(-1);
3226 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003227
Tom Zanussib5b87312010-11-10 08:16:51 -06003228 if (rec_script_path)
3229 script_path = rec_script_path;
3230 if (rep_script_path)
3231 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003232
Tom Zanussib5b87312010-11-10 08:16:51 -06003233 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003234 j = 0;
3235
Robert Richter317df652011-11-25 15:05:25 +01003236 if (!rec_script_path)
3237 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003238 else if (!system_wide) {
3239 if (have_cmd(argc - 1, &argv[1]) != 0) {
3240 err = -1;
3241 goto out;
3242 }
3243 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003244
3245 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003246 if (!__argv) {
3247 pr_err("malloc failed\n");
3248 err = -ENOMEM;
3249 goto out;
3250 }
3251
Tom Zanussib5b87312010-11-10 08:16:51 -06003252 __argv[j++] = "/bin/sh";
3253 __argv[j++] = script_path;
3254 if (system_wide)
3255 __argv[j++] = "-a";
3256 for (i = 2; i < argc; i++)
3257 __argv[j++] = argv[i];
3258 __argv[j++] = NULL;
3259
3260 execvp("/bin/sh", (char **)__argv);
3261 free(__argv);
3262 exit(-1);
3263 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003264
Tom Zanussicf4fee52010-03-03 01:04:33 -06003265 if (!script_name)
3266 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003267
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003268 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003269 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003270 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003271
Jiri Olsae90debd2013-12-09 11:02:50 +01003272 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003273 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003274 perf_session__fprintf_info(session, stdout, show_full_info);
3275 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003276 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003277 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003278 if (show_full_info)
3279 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003280
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003281 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003282 goto out_delete;
3283
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003284 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003285 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003286
Adrian Huntere2167082016-06-23 16:40:58 +03003287 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3288 itrace_synth_opts.thread_stack = true;
3289
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003290 session->itrace_synth_opts = &itrace_synth_opts;
3291
Anton Blanchard5d67be92011-07-04 21:57:50 +10003292 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003293 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3294 if (err < 0)
3295 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003296 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003297 }
3298
David Ahern1424dc92011-03-09 22:23:28 -07003299 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003300 symbol_conf.use_callchain = true;
3301 else
3302 symbol_conf.use_callchain = false;
3303
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003304 if (session->tevent.pevent &&
3305 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003306 machine__resolve_kernel_addr,
3307 &session->machines.host) < 0) {
3308 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003309 err = -1;
3310 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003311 }
3312
Tom Zanussi956ffd02009-11-25 01:15:46 -06003313 if (generate_script_lang) {
3314 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003315 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003316
David Ahern2c9e45f72011-03-17 10:03:21 -06003317 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003318 fprintf(stderr,
3319 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003320 err = -EINVAL;
3321 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003322 }
3323
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003324 input = open(data.file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003325 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003326 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003327 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003328 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003329 }
3330
3331 err = fstat(input, &perf_stat);
3332 if (err < 0) {
3333 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003334 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003335 }
3336
3337 if (!perf_stat.st_size) {
3338 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003339 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003340 }
3341
3342 scripting_ops = script_spec__lookup(generate_script_lang);
3343 if (!scripting_ops) {
3344 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003345 err = -ENOENT;
3346 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003347 }
3348
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003349 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003350 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003351 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003352 }
3353
3354 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003355 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003356 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003357 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003358 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003359 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003360 }
3361
David Ahern9cbdb702011-04-06 21:54:20 -06003362
3363 err = perf_session__check_output_opt(session);
3364 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003365 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003366
David Aherna91f4c42016-11-29 10:15:43 -07003367 /* needs to be parsed after looking up reference time */
Jin Yao2ab046c2017-12-08 21:13:46 +08003368 if (perf_time__parse_str(script.ptime_range, script.time_str) != 0) {
3369 if (session->evlist->first_sample_time == 0 &&
3370 session->evlist->last_sample_time == 0) {
3371 pr_err("No first/last sample time in perf data\n");
3372 err = -EINVAL;
3373 goto out_delete;
3374 }
3375
3376 script.range_num = perf_time__percent_parse_str(
3377 script.ptime_range, PTIME_RANGE_MAX,
3378 script.time_str,
3379 session->evlist->first_sample_time,
3380 session->evlist->last_sample_time);
3381
3382 if (script.range_num < 0) {
3383 pr_err("Invalid time string\n");
3384 err = -EINVAL;
3385 goto out_delete;
3386 }
3387 } else {
3388 script.range_num = 1;
David Aherna91f4c42016-11-29 10:15:43 -07003389 }
3390
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003391 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003392
Adrian Hunterd445dd22014-08-15 22:08:37 +03003393 flush_scripting();
3394
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003395out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01003396 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003397 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003398
3399 if (script_started)
3400 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003401out:
3402 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003403}