blob: fe1dcd4f2c6d41b18e94a8d13b6efd9c1c9d72c4 [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#include "builtin.h"
2
Andrea Gelminib7eead82010-08-05 15:51:38 +02003#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02004#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02005#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06006#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02007#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06008#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +02009#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020010#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020011#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020012#include "util/symbol.h"
13#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010014#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020015#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070016#include "util/evlist.h"
17#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080018#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030020#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010021#include "util/cpumap.h"
22#include "util/thread_map.h"
23#include "util/stat.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030024#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030025#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070026#include "util/time-utils.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030027#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100028#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030029#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030030#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030031#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010032#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010033#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080034#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030035#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030036#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030037#include <inttypes.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020038
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030039#include "sane_ctype.h"
40
Tom Zanussi956ffd02009-11-25 01:15:46 -060041static char const *script_name;
42static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020043static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020044static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020045static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070046static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090047static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010048static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030049static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030050static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100051static const char *cpu_list;
52static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010053static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080054static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060055
Adrian Hunter44cbe722015-09-25 16:15:50 +030056unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030057
David Ahern745f43e2011-03-09 22:23:26 -070058enum perf_output_field {
59 PERF_OUTPUT_COMM = 1U << 0,
60 PERF_OUTPUT_TID = 1U << 1,
61 PERF_OUTPUT_PID = 1U << 2,
62 PERF_OUTPUT_TIME = 1U << 3,
63 PERF_OUTPUT_CPU = 1U << 4,
64 PERF_OUTPUT_EVNAME = 1U << 5,
65 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060066 PERF_OUTPUT_IP = 1U << 7,
67 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060068 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060069 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090070 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020071 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaa2014-08-25 16:45:42 +020072 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020073 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020074 PERF_OUTPUT_BRSTACK = 1U << 15,
75 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010076 PERF_OUTPUT_DATA_SRC = 1U << 17,
77 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000078 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030079 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030080 PERF_OUTPUT_INSN = 1U << 21,
81 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080082 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
David Ahern745f43e2011-03-09 22:23:26 -070083};
84
85struct output_option {
86 const char *str;
87 enum perf_output_field field;
88} all_output_options[] = {
89 {.str = "comm", .field = PERF_OUTPUT_COMM},
90 {.str = "tid", .field = PERF_OUTPUT_TID},
91 {.str = "pid", .field = PERF_OUTPUT_PID},
92 {.str = "time", .field = PERF_OUTPUT_TIME},
93 {.str = "cpu", .field = PERF_OUTPUT_CPU},
94 {.str = "event", .field = PERF_OUTPUT_EVNAME},
95 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060096 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070097 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060098 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060099 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900100 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200101 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200102 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200103 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200104 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
105 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100106 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
107 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000108 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300109 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300110 {.str = "insn", .field = PERF_OUTPUT_INSN},
111 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800112 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
David Ahern745f43e2011-03-09 22:23:26 -0700113};
114
115/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600116static struct {
117 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600118 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400119 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600120 u64 fields;
121 u64 invalid_fields;
122} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700123
David Ahern2c9e45f72011-03-17 10:03:21 -0600124 [PERF_TYPE_HARDWARE] = {
125 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700126
David Ahern2c9e45f72011-03-17 10:03:21 -0600127 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
128 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600129 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200130 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
131 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600132
Wang Nan30372f02016-02-24 11:20:45 +0000133 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600134 },
135
136 [PERF_TYPE_SOFTWARE] = {
137 .user_set = false,
138
139 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
140 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600141 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200142 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000143 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600144
145 .invalid_fields = PERF_OUTPUT_TRACE,
146 },
147
148 [PERF_TYPE_TRACEPOINT] = {
149 .user_set = false,
150
151 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
152 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000153 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600154 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700155
156 [PERF_TYPE_RAW] = {
157 .user_set = false,
158
159 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
160 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600161 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200162 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100163 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
164 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700165
Wang Nan30372f02016-02-24 11:20:45 +0000166 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700167 },
Wang Nan27cfef02015-12-08 02:25:43 +0000168
169 [PERF_TYPE_BREAKPOINT] = {
170 .user_set = false,
171
172 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
173 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
174 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
175 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
176 PERF_OUTPUT_PERIOD,
177
Wang Nan30372f02016-02-24 11:20:45 +0000178 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000179 },
David Ahern1424dc92011-03-09 22:23:28 -0700180};
David Ahern745f43e2011-03-09 22:23:26 -0700181
David Ahern2c9e45f72011-03-17 10:03:21 -0600182static bool output_set_by_user(void)
183{
184 int j;
185 for (j = 0; j < PERF_TYPE_MAX; ++j) {
186 if (output[j].user_set)
187 return true;
188 }
189 return false;
190}
David Ahern745f43e2011-03-09 22:23:26 -0700191
David Ahern9cbdb702011-04-06 21:54:20 -0600192static const char *output_field2str(enum perf_output_field field)
193{
194 int i, imax = ARRAY_SIZE(all_output_options);
195 const char *str = "";
196
197 for (i = 0; i < imax; ++i) {
198 if (all_output_options[i].field == field) {
199 str = all_output_options[i].str;
200 break;
201 }
202 }
203 return str;
204}
205
David Ahern2c9e45f72011-03-17 10:03:21 -0600206#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700207
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300208static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
209 u64 sample_type, const char *sample_msg,
210 enum perf_output_field field,
211 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700212{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300213 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600214 int type = attr->type;
215 const char *evname;
216
217 if (attr->sample_type & sample_type)
218 return 0;
219
220 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300221 if (allow_user_set)
222 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300223 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600224 pr_err("Samples for '%s' event do not have %s attribute set. "
225 "Cannot print '%s' field.\n",
226 evname, sample_msg, output_field2str(field));
227 return -1;
228 }
229
230 /* user did not ask for it explicitly so remove from the default list */
231 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300232 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600233 pr_debug("Samples for '%s' event do not have %s attribute set. "
234 "Skipping '%s' field.\n",
235 evname, sample_msg, output_field2str(field));
236
237 return 0;
238}
239
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300240static int perf_evsel__check_stype(struct perf_evsel *evsel,
241 u64 sample_type, const char *sample_msg,
242 enum perf_output_field field)
243{
244 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
245 false);
246}
247
David Ahern9cbdb702011-04-06 21:54:20 -0600248static int perf_evsel__check_attr(struct perf_evsel *evsel,
249 struct perf_session *session)
250{
251 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300252 bool allow_user_set;
253
Jiri Olsae099eba2016-01-05 22:09:09 +0100254 if (perf_header__has_feat(&session->header, HEADER_STAT))
255 return 0;
256
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300257 allow_user_set = perf_header__has_feat(&session->header,
258 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600259
David Ahern1424dc92011-03-09 22:23:28 -0700260 if (PRINT_FIELD(TRACE) &&
261 !perf_session__has_traces(session, "record -R"))
262 return -EINVAL;
263
David Ahern787bef12011-05-27 14:28:43 -0600264 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300265 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
266 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700267 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700268 }
David Ahern7cec0922011-05-30 13:08:23 -0600269
270 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300271 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
272 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600273 return -EINVAL;
274
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100275 if (PRINT_FIELD(DATA_SRC) &&
276 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
277 PERF_OUTPUT_DATA_SRC))
278 return -EINVAL;
279
280 if (PRINT_FIELD(WEIGHT) &&
281 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
282 PERF_OUTPUT_WEIGHT))
283 return -EINVAL;
284
David Ahern7cec0922011-05-30 13:08:23 -0600285 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
286 pr_err("Display of symbols requested but neither sample IP nor "
287 "sample address\nis selected. Hence, no addresses to convert "
288 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600289 return -EINVAL;
290 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900291 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
292 pr_err("Display of offsets requested but symbol is not"
293 "selected.\n");
294 return -EINVAL;
295 }
David Ahern7cec0922011-05-30 13:08:23 -0600296 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
297 pr_err("Display of DSO requested but neither sample IP nor "
298 "sample address\nis selected. Hence, no addresses to convert "
299 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600300 return -EINVAL;
301 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200302 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
303 pr_err("Display of source line number requested but sample IP is not\n"
304 "selected. Hence, no address to lookup the source line number.\n");
305 return -EINVAL;
306 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800307 if (PRINT_FIELD(BRSTACKINSN) &&
308 !(perf_evlist__combined_branch_type(session->evlist) &
309 PERF_SAMPLE_BRANCH_ANY)) {
310 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
311 "Hint: run 'perf record -b ...'\n");
312 return -EINVAL;
313 }
David Ahern1424dc92011-03-09 22:23:28 -0700314 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300315 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
316 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700317 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700318
319 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300320 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
321 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700322 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700323
324 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300325 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
326 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700327 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600328
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200329 if (PRINT_FIELD(PERIOD) &&
330 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
331 PERF_OUTPUT_PERIOD))
332 return -EINVAL;
333
Stephane Eranianfc36f942015-08-31 18:41:10 +0200334 if (PRINT_FIELD(IREGS) &&
335 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
336 PERF_OUTPUT_IREGS))
337 return -EINVAL;
338
David Ahern9cbdb702011-04-06 21:54:20 -0600339 return 0;
340}
341
Adrian Hunter7ea95722013-11-01 15:51:30 +0200342static void set_print_ip_opts(struct perf_event_attr *attr)
343{
344 unsigned int type = attr->type;
345
346 output[type].print_ip_opts = 0;
347 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300348 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200349
350 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300351 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200352
353 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300354 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200355
356 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300357 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200358
359 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300360 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200361}
362
David Ahern9cbdb702011-04-06 21:54:20 -0600363/*
364 * verify all user requested events exist and the samples
365 * have the expected data
366 */
367static int perf_session__check_output_opt(struct perf_session *session)
368{
He Kuang40f20e52016-05-16 04:51:19 +0000369 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600370 struct perf_evsel *evsel;
371
372 for (j = 0; j < PERF_TYPE_MAX; ++j) {
373 evsel = perf_session__find_first_evtype(session, j);
374
375 /*
376 * even if fields is set to 0 (ie., show nothing) event must
377 * exist if user explicitly includes it on the command line
378 */
379 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
380 pr_err("%s events do not exist. "
381 "Remove corresponding -f option to proceed.\n",
382 event_type(j));
383 return -1;
384 }
385
386 if (evsel && output[j].fields &&
387 perf_evsel__check_attr(evsel, session))
388 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400389
390 if (evsel == NULL)
391 continue;
392
Adrian Hunter7ea95722013-11-01 15:51:30 +0200393 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700394 }
395
Adrian Hunter98526ee2014-07-31 09:00:59 +0300396 if (!no_callchain) {
397 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000398 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300399
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300400 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000401 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300402 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
403 use_callchain = true;
404 break;
405 }
406 }
He Kuang71ac8992016-08-04 11:25:43 +0000407 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300408 symbol_conf.use_callchain = false;
409 }
410
David Ahern80b8b492013-11-19 21:07:37 -0700411 /*
412 * set default for tracepoints to print symbols only
413 * if callchains are present
414 */
415 if (symbol_conf.use_callchain &&
416 !output[PERF_TYPE_TRACEPOINT].user_set) {
417 struct perf_event_attr *attr;
418
419 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700420
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300421 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000422 if (evsel->attr.type != j)
423 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700424
He Kuang40f20e52016-05-16 04:51:19 +0000425 attr = &evsel->attr;
426
427 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
428 output[j].fields |= PERF_OUTPUT_IP;
429 output[j].fields |= PERF_OUTPUT_SYM;
430 output[j].fields |= PERF_OUTPUT_DSO;
431 set_print_ip_opts(attr);
432 goto out;
433 }
David Ahern80b8b492013-11-19 21:07:37 -0700434 }
435 }
436
437out:
David Ahern1424dc92011-03-09 22:23:28 -0700438 return 0;
439}
David Ahern745f43e2011-03-09 22:23:26 -0700440
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300441static void print_sample_iregs(struct perf_sample *sample,
Stephane Eranianfc36f942015-08-31 18:41:10 +0200442 struct perf_event_attr *attr)
443{
444 struct regs_dump *regs = &sample->intr_regs;
445 uint64_t mask = attr->sample_regs_intr;
446 unsigned i = 0, r;
447
448 if (!regs)
449 return;
450
451 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
452 u64 val = regs->regs[i++];
453 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
454 }
455}
456
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300457static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700458 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300459 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700460{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300461 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700462 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700463 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700464
David Ahern745f43e2011-03-09 22:23:26 -0700465 if (PRINT_FIELD(COMM)) {
466 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200467 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600468 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200469 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700470 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200471 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700472 }
David Ahernc70c94b2011-03-09 22:23:25 -0700473
David Ahern745f43e2011-03-09 22:23:26 -0700474 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
475 printf("%5d/%-5d ", sample->pid, sample->tid);
476 else if (PRINT_FIELD(PID))
477 printf("%5d ", sample->pid);
478 else if (PRINT_FIELD(TID))
479 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700480
David Ahern745f43e2011-03-09 22:23:26 -0700481 if (PRINT_FIELD(CPU)) {
482 if (latency_format)
483 printf("%3d ", sample->cpu);
484 else
485 printf("[%03d] ", sample->cpu);
486 }
David Ahernc70c94b2011-03-09 22:23:25 -0700487
David Ahern745f43e2011-03-09 22:23:26 -0700488 if (PRINT_FIELD(TIME)) {
489 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300490 secs = nsecs / NSEC_PER_SEC;
491 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900492
Adrian Hunter83e19862015-09-25 16:15:36 +0300493 if (nanosecs)
494 printf("%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900495 else {
496 char sample_time[32];
497 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
498 printf("%12s: ", sample_time);
499 }
David Ahern745f43e2011-03-09 22:23:26 -0700500 }
David Ahernc70c94b2011-03-09 22:23:25 -0700501}
502
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200503static inline char
504mispred_str(struct branch_entry *br)
505{
506 if (!(br->flags.mispred || br->flags.predicted))
507 return '-';
508
509 return br->flags.predicted ? 'P' : 'M';
510}
511
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300512static void print_sample_brstack(struct perf_sample *sample)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200513{
514 struct branch_stack *br = sample->branch_stack;
515 u64 i;
516
517 if (!(br && br->nr))
518 return;
519
520 for (i = 0; i < br->nr; i++) {
521 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
522 br->entries[i].from,
523 br->entries[i].to,
524 mispred_str( br->entries + i),
525 br->entries[i].flags.in_tx? 'X' : '-',
526 br->entries[i].flags.abort? 'A' : '-',
527 br->entries[i].flags.cycles);
528 }
529}
530
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300531static void print_sample_brstacksym(struct perf_sample *sample,
532 struct thread *thread)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200533{
534 struct branch_stack *br = sample->branch_stack;
535 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200536 u64 i, from, to;
537
538 if (!(br && br->nr))
539 return;
540
541 for (i = 0; i < br->nr; i++) {
542
543 memset(&alf, 0, sizeof(alf));
544 memset(&alt, 0, sizeof(alt));
545 from = br->entries[i].from;
546 to = br->entries[i].to;
547
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300548 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200549 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300550 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200551
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300552 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200553 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300554 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200555
556 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
557 putchar('/');
558 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
559 printf("/%c/%c/%c/%d ",
560 mispred_str( br->entries + i),
561 br->entries[i].flags.in_tx? 'X' : '-',
562 br->entries[i].flags.abort? 'A' : '-',
563 br->entries[i].flags.cycles);
564 }
565}
566
Andi Kleen48d02a12017-02-23 15:46:34 -0800567#define MAXBB 16384UL
568
569static int grab_bb(u8 *buffer, u64 start, u64 end,
570 struct machine *machine, struct thread *thread,
571 bool *is64bit, u8 *cpumode, bool last)
572{
573 long offset, len;
574 struct addr_location al;
575 bool kernel;
576
577 if (!start || !end)
578 return 0;
579
580 kernel = machine__kernel_ip(machine, start);
581 if (kernel)
582 *cpumode = PERF_RECORD_MISC_KERNEL;
583 else
584 *cpumode = PERF_RECORD_MISC_USER;
585
586 /*
587 * Block overlaps between kernel and user.
588 * This can happen due to ring filtering
589 * On Intel CPUs the entry into the kernel is filtered,
590 * but the exit is not. Let the caller patch it up.
591 */
592 if (kernel != machine__kernel_ip(machine, end)) {
593 printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
594 start, end);
595 return -ENXIO;
596 }
597
598 memset(&al, 0, sizeof(al));
599 if (end - start > MAXBB - MAXINSN) {
600 if (last)
601 printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
602 else
603 printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
604 return 0;
605 }
606
607 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
608 if (!al.map || !al.map->dso) {
609 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
610 return 0;
611 }
612 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
613 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
614 return 0;
615 }
616
617 /* Load maps to ensure dso->is_64_bit has been updated */
618 map__load(al.map);
619
620 offset = al.map->map_ip(al.map, start);
621 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
622 end - start + MAXINSN);
623
624 *is64bit = al.map->dso->is_64_bit;
625 if (len <= 0)
626 printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
627 start, end);
628 return len;
629}
630
631static void print_jump(uint64_t ip, struct branch_entry *en,
632 struct perf_insn *x, u8 *inbuf, int len,
633 int insn)
634{
635 printf("\t%016" PRIx64 "\t%-30s\t#%s%s%s%s",
636 ip,
637 dump_insn(x, ip, inbuf, len, NULL),
638 en->flags.predicted ? " PRED" : "",
639 en->flags.mispred ? " MISPRED" : "",
640 en->flags.in_tx ? " INTX" : "",
641 en->flags.abort ? " ABORT" : "");
642 if (en->flags.cycles) {
643 printf(" %d cycles", en->flags.cycles);
644 if (insn)
645 printf(" %.2f IPC", (float)insn / en->flags.cycles);
646 }
647 putchar('\n');
648}
649
650static void print_ip_sym(struct thread *thread, u8 cpumode, int cpu,
651 uint64_t addr, struct symbol **lastsym,
652 struct perf_event_attr *attr)
653{
654 struct addr_location al;
655 int off;
656
657 memset(&al, 0, sizeof(al));
658
659 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
660 if (!al.map)
661 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
662 addr, &al);
663 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
664 return;
665
666 al.cpu = cpu;
667 al.sym = NULL;
668 if (al.map)
669 al.sym = map__find_symbol(al.map, al.addr);
670
671 if (!al.sym)
672 return;
673
674 if (al.addr < al.sym->end)
675 off = al.addr - al.sym->start;
676 else
677 off = al.addr - al.map->start - al.sym->start;
678 printf("\t%s", al.sym->name);
679 if (off)
680 printf("%+d", off);
681 putchar(':');
682 if (PRINT_FIELD(SRCLINE))
683 map__fprintf_srcline(al.map, al.addr, "\t", stdout);
684 putchar('\n');
685 *lastsym = al.sym;
686}
687
688static void print_sample_brstackinsn(struct perf_sample *sample,
689 struct thread *thread,
690 struct perf_event_attr *attr,
691 struct machine *machine)
692{
693 struct branch_stack *br = sample->branch_stack;
694 u64 start, end;
695 int i, insn, len, nr, ilen;
696 struct perf_insn x;
697 u8 buffer[MAXBB];
698 unsigned off;
699 struct symbol *lastsym = NULL;
700
701 if (!(br && br->nr))
702 return;
703 nr = br->nr;
704 if (max_blocks && nr > max_blocks + 1)
705 nr = max_blocks + 1;
706
707 x.thread = thread;
708 x.cpu = sample->cpu;
709
710 putchar('\n');
711
712 /* Handle first from jump, of which we don't know the entry. */
713 len = grab_bb(buffer, br->entries[nr-1].from,
714 br->entries[nr-1].from,
715 machine, thread, &x.is64bit, &x.cpumode, false);
716 if (len > 0) {
717 print_ip_sym(thread, x.cpumode, x.cpu,
718 br->entries[nr - 1].from, &lastsym, attr);
719 print_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
720 &x, buffer, len, 0);
721 }
722
723 /* Print all blocks */
724 for (i = nr - 2; i >= 0; i--) {
725 if (br->entries[i].from || br->entries[i].to)
726 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
727 br->entries[i].from,
728 br->entries[i].to);
729 start = br->entries[i + 1].to;
730 end = br->entries[i].from;
731
732 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
733 /* Patch up missing kernel transfers due to ring filters */
734 if (len == -ENXIO && i > 0) {
735 end = br->entries[--i].from;
736 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
737 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
738 }
739 if (len <= 0)
740 continue;
741
742 insn = 0;
743 for (off = 0;; off += ilen) {
744 uint64_t ip = start + off;
745
746 print_ip_sym(thread, x.cpumode, x.cpu, ip, &lastsym, attr);
747 if (ip == end) {
748 print_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn);
749 break;
750 } else {
751 printf("\t%016" PRIx64 "\t%s\n", ip,
752 dump_insn(&x, ip, buffer + off, len - off, &ilen));
753 if (ilen == 0)
754 break;
755 insn++;
756 }
757 }
758 }
759
760 /*
761 * Hit the branch? In this case we are already done, and the target
762 * has not been executed yet.
763 */
764 if (br->entries[0].from == sample->ip)
765 return;
766 if (br->entries[0].flags.abort)
767 return;
768
769 /*
770 * Print final block upto sample
771 */
772 start = br->entries[0].to;
773 end = sample->ip;
774 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
775 print_ip_sym(thread, x.cpumode, x.cpu, start, &lastsym, attr);
776 if (len <= 0) {
777 /* Print at least last IP if basic block did not work */
778 len = grab_bb(buffer, sample->ip, sample->ip,
779 machine, thread, &x.is64bit, &x.cpumode, false);
780 if (len <= 0)
781 return;
782
783 printf("\t%016" PRIx64 "\t%s\n", sample->ip,
784 dump_insn(&x, sample->ip, buffer, len, NULL));
785 return;
786 }
787 for (off = 0; off <= end - start; off += ilen) {
788 printf("\t%016" PRIx64 "\t%s\n", start + off,
789 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
790 if (ilen == 0)
791 break;
792 }
793}
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200794
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300795static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600796 struct thread *thread,
797 struct perf_event_attr *attr)
798{
799 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600800
801 printf("%16" PRIx64, sample->addr);
802
803 if (!sample_addr_correlates_sym(attr))
804 return;
805
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300806 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600807
808 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900809 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900810 if (PRINT_FIELD(SYMOFFSET))
811 symbol__fprintf_symname_offs(al.sym, &al, stdout);
812 else
813 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600814 }
815
816 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900817 printf(" (");
818 map__fprintf_dsoname(al.map, stdout);
819 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600820 }
821}
822
Adrian Huntere2167082016-06-23 16:40:58 +0300823static void print_sample_callindent(struct perf_sample *sample,
824 struct perf_evsel *evsel,
825 struct thread *thread,
826 struct addr_location *al)
827{
828 struct perf_event_attr *attr = &evsel->attr;
829 size_t depth = thread_stack__depth(thread);
830 struct addr_location addr_al;
831 const char *name = NULL;
832 static int spacing;
833 int len = 0;
834 u64 ip = 0;
835
836 /*
837 * The 'return' has already been popped off the stack so the depth has
838 * to be adjusted to match the 'call'.
839 */
840 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
841 depth += 1;
842
843 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
844 if (sample_addr_correlates_sym(attr)) {
845 thread__resolve(thread, &addr_al, sample);
846 if (addr_al.sym)
847 name = addr_al.sym->name;
848 else
849 ip = sample->addr;
850 } else {
851 ip = sample->addr;
852 }
853 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
854 if (al->sym)
855 name = al->sym->name;
856 else
857 ip = sample->ip;
858 }
859
860 if (name)
861 len = printf("%*s%s", (int)depth * 4, "", name);
862 else if (ip)
863 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
864
865 if (len < 0)
866 return;
867
868 /*
869 * Try to keep the output length from changing frequently so that the
870 * output lines up more nicely.
871 */
872 if (len > spacing || (len && len < spacing - 52))
873 spacing = round_up(len + 4, 32);
874
875 if (len < spacing)
876 printf("%*s", spacing - len, "");
877}
878
Andi Kleen224e2c92016-10-07 16:42:27 +0300879static void print_insn(struct perf_sample *sample,
Andi Kleen48d02a12017-02-23 15:46:34 -0800880 struct perf_event_attr *attr,
881 struct thread *thread,
882 struct machine *machine)
Andi Kleen224e2c92016-10-07 16:42:27 +0300883{
884 if (PRINT_FIELD(INSNLEN))
885 printf(" ilen: %d", sample->insn_len);
886 if (PRINT_FIELD(INSN)) {
887 int i;
888
889 printf(" insn:");
890 for (i = 0; i < sample->insn_len; i++)
891 printf(" %02x", (unsigned char)sample->insn[i]);
892 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800893 if (PRINT_FIELD(BRSTACKINSN))
894 print_sample_brstackinsn(sample, thread, attr, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300895}
896
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300897static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +0900898 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200899 struct thread *thread,
Andi Kleen48d02a12017-02-23 15:46:34 -0800900 struct addr_location *al,
901 struct machine *machine)
Akihiro Nagai95582592012-01-30 13:43:09 +0900902{
903 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300904 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900905
Adrian Huntere2167082016-06-23 16:40:58 +0300906 if (PRINT_FIELD(CALLINDENT))
907 print_sample_callindent(sample, evsel, thread, al);
908
Akihiro Nagai95582592012-01-30 13:43:09 +0900909 /* print branch_from information */
910 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300911 unsigned int print_opts = output[attr->type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -0700912 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300913
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300914 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700915 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300916 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700917 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300918
919 if (cursor == NULL) {
920 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300921 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300922 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300923 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300924 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300925 } else
926 putchar('\n');
927
928 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +0900929 }
930
Akihiro Nagai95582592012-01-30 13:43:09 +0900931 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300932 if (PRINT_FIELD(ADDR) ||
933 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300934 !output[attr->type].user_set)) {
935 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300936 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300937 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900938
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300939 if (print_srcline_last)
940 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
941
Andi Kleen48d02a12017-02-23 15:46:34 -0800942 print_insn(sample, attr, thread, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300943
Akihiro Nagai95582592012-01-30 13:43:09 +0900944 printf("\n");
945}
946
Adrian Hunter055cd332016-06-23 16:40:56 +0300947static struct {
948 u32 flags;
949 const char *name;
950} sample_flags[] = {
951 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
952 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
953 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
954 {PERF_IP_FLAG_BRANCH, "jmp"},
955 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
956 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
957 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
958 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
959 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
960 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
961 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
962 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
963 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
964 {0, NULL}
965};
966
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300967static void print_sample_flags(u32 flags)
968{
969 const char *chars = PERF_IP_FLAG_CHARS;
970 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +0300971 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
972 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300973 char str[33];
974 int i, pos = 0;
975
Adrian Hunter055cd332016-06-23 16:40:56 +0300976 for (i = 0; sample_flags[i].name ; i++) {
977 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
978 name = sample_flags[i].name;
979 break;
980 }
981 }
982
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300983 for (i = 0; i < n; i++, flags >>= 1) {
984 if (flags & 1)
985 str[pos++] = chars[i];
986 }
987 for (; i < 32; i++, flags >>= 1) {
988 if (flags & 1)
989 str[pos++] = '?';
990 }
991 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +0300992
993 if (name)
994 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
995 else
996 printf(" %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300997}
998
Wang Nan30372f02016-02-24 11:20:45 +0000999struct printer_data {
1000 int line_no;
1001 bool hit_nul;
1002 bool is_printable;
1003};
1004
1005static void
1006print_sample_bpf_output_printer(enum binary_printer_ops op,
1007 unsigned int val,
1008 void *extra)
1009{
1010 unsigned char ch = (unsigned char)val;
1011 struct printer_data *printer_data = extra;
1012
1013 switch (op) {
1014 case BINARY_PRINT_DATA_BEGIN:
1015 printf("\n");
1016 break;
1017 case BINARY_PRINT_LINE_BEGIN:
1018 printf("%17s", !printer_data->line_no ? "BPF output:" :
1019 " ");
1020 break;
1021 case BINARY_PRINT_ADDR:
1022 printf(" %04x:", val);
1023 break;
1024 case BINARY_PRINT_NUM_DATA:
1025 printf(" %02x", val);
1026 break;
1027 case BINARY_PRINT_NUM_PAD:
1028 printf(" ");
1029 break;
1030 case BINARY_PRINT_SEP:
1031 printf(" ");
1032 break;
1033 case BINARY_PRINT_CHAR_DATA:
1034 if (printer_data->hit_nul && ch)
1035 printer_data->is_printable = false;
1036
1037 if (!isprint(ch)) {
1038 printf("%c", '.');
1039
1040 if (!printer_data->is_printable)
1041 break;
1042
1043 if (ch == '\0')
1044 printer_data->hit_nul = true;
1045 else
1046 printer_data->is_printable = false;
1047 } else {
1048 printf("%c", ch);
1049 }
1050 break;
1051 case BINARY_PRINT_CHAR_PAD:
1052 printf(" ");
1053 break;
1054 case BINARY_PRINT_LINE_END:
1055 printf("\n");
1056 printer_data->line_no++;
1057 break;
1058 case BINARY_PRINT_DATA_END:
1059 default:
1060 break;
1061 }
1062}
1063
1064static void print_sample_bpf_output(struct perf_sample *sample)
1065{
1066 unsigned int nr_bytes = sample->raw_size;
1067 struct printer_data printer_data = {0, false, true};
1068
1069 print_binary(sample->raw_data, nr_bytes, 8,
1070 print_sample_bpf_output_printer, &printer_data);
1071
1072 if (printer_data.is_printable && printer_data.hit_nul)
1073 printf("%17s \"%s\"\n", "BPF string:",
1074 (char *)(sample->raw_data));
1075}
1076
Jiri Olsa809e9422015-11-26 18:55:21 +01001077struct perf_script {
1078 struct perf_tool tool;
1079 struct perf_session *session;
1080 bool show_task_events;
1081 bool show_mmap_events;
1082 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301083 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001084 bool allocated;
1085 struct cpu_map *cpus;
1086 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001087 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001088 const char *time_str;
1089 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001090};
1091
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001092static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1093{
1094 struct perf_evsel *evsel;
1095 int max = 0;
1096
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001097 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001098 int len = strlen(perf_evsel__name(evsel));
1099
1100 max = MAX(len, max);
1101 }
1102
1103 return max;
1104}
1105
Jiri Olsac19ac912016-02-24 09:46:54 +01001106static size_t data_src__printf(u64 data_src)
1107{
1108 struct mem_info mi = { .data_src.val = data_src };
1109 char decode[100];
1110 char out[100];
1111 static int maxlen;
1112 int len;
1113
1114 perf_script__meminfo_scnprintf(decode, 100, &mi);
1115
1116 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1117 if (maxlen < len)
1118 maxlen = len;
1119
1120 return printf("%-*s", maxlen, out);
1121}
1122
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001123static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001124 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001125 struct addr_location *al,
1126 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001127{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001128 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001129 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -07001130
David Ahern2c9e45f72011-03-17 10:03:21 -06001131 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001132 return;
1133
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001134 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -07001135
Jiri Olsa535aeaa2014-08-25 16:45:42 +02001136 if (PRINT_FIELD(PERIOD))
1137 printf("%10" PRIu64 " ", sample->period);
1138
Namhyung Kime944d3d2013-11-18 14:34:52 +09001139 if (PRINT_FIELD(EVNAME)) {
1140 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001141
1142 if (!script->name_width)
1143 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1144
1145 printf("%*s: ", script->name_width,
1146 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001147 }
1148
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001149 if (print_flags)
1150 print_sample_flags(sample->flags);
1151
Akihiro Nagai95582592012-01-30 13:43:09 +09001152 if (is_bts_event(attr)) {
Andi Kleen48d02a12017-02-23 15:46:34 -08001153 print_sample_bts(sample, evsel, thread, al, machine);
Akihiro Nagai95582592012-01-30 13:43:09 +09001154 return;
1155 }
1156
David Ahern745f43e2011-03-09 22:23:26 -07001157 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001158 event_format__print(evsel->tp_format, sample->cpu,
1159 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -06001160 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001161 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -06001162
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001163 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +01001164 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001165
1166 if (PRINT_FIELD(WEIGHT))
1167 printf("%16" PRIu64, sample->weight);
1168
David Ahern787bef12011-05-27 14:28:43 -06001169 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001170 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001171
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001172 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001173 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001174 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001175 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001176
1177 putchar(cursor ? '\n' : ' ');
1178 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001179 }
1180
Stephane Eranianfc36f942015-08-31 18:41:10 +02001181 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001182 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001183
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001184 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001185 print_sample_brstack(sample);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001186 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001187 print_sample_brstacksym(sample, thread);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001188
Wang Nan30372f02016-02-24 11:20:45 +00001189 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1190 print_sample_bpf_output(sample);
Andi Kleen48d02a12017-02-23 15:46:34 -08001191 print_insn(sample, attr, thread, machine);
David Ahernc70c94b2011-03-09 22:23:25 -07001192 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001193}
1194
Tom Zanussi956ffd02009-11-25 01:15:46 -06001195static struct scripting_ops *scripting_ops;
1196
Jiri Olsa36e33c52016-01-06 11:49:56 +01001197static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1198{
1199 int nthreads = thread_map__nr(counter->threads);
1200 int ncpus = perf_evsel__nr_cpus(counter);
1201 int cpu, thread;
1202 static int header_printed;
1203
1204 if (counter->system_wide)
1205 nthreads = 1;
1206
1207 if (!header_printed) {
1208 printf("%3s %8s %15s %15s %15s %15s %s\n",
1209 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1210 header_printed = 1;
1211 }
1212
1213 for (thread = 0; thread < nthreads; thread++) {
1214 for (cpu = 0; cpu < ncpus; cpu++) {
1215 struct perf_counts_values *counts;
1216
1217 counts = perf_counts(counter->counts, cpu, thread);
1218
1219 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1220 counter->cpus->map[cpu],
1221 thread_map__pid(counter->threads, thread),
1222 counts->val,
1223 counts->ena,
1224 counts->run,
1225 tstamp,
1226 perf_evsel__name(counter));
1227 }
1228 }
1229}
1230
Jiri Olsae099eba2016-01-05 22:09:09 +01001231static void process_stat(struct perf_evsel *counter, u64 tstamp)
1232{
1233 if (scripting_ops && scripting_ops->process_stat)
1234 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001235 else
1236 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001237}
1238
1239static void process_stat_interval(u64 tstamp)
1240{
1241 if (scripting_ops && scripting_ops->process_stat_interval)
1242 scripting_ops->process_stat_interval(tstamp);
1243}
1244
Tom Zanussi956ffd02009-11-25 01:15:46 -06001245static void setup_scripting(void)
1246{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001247 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001248 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001249}
1250
Adrian Hunterd445dd22014-08-15 22:08:37 +03001251static int flush_scripting(void)
1252{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001253 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001254}
1255
Tom Zanussi956ffd02009-11-25 01:15:46 -06001256static int cleanup_scripting(void)
1257{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001258 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001259
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001260 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001261}
1262
Jiri Olsa809e9422015-11-26 18:55:21 +01001263static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001264 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001265 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001266 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001267 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001268{
Jiri Olsa809e9422015-11-26 18:55:21 +01001269 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001270 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001271
David Aherna91f4c42016-11-29 10:15:43 -07001272 if (perf_time__skip_sample(&scr->ptime, sample->time))
1273 return 0;
1274
David Ahern1424dc92011-03-09 22:23:28 -07001275 if (debug_mode) {
1276 if (sample->time < last_timestamp) {
1277 pr_err("Samples misordered, previous: %" PRIu64
1278 " this: %" PRIu64 "\n", last_timestamp,
1279 sample->time);
1280 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001281 }
David Ahern1424dc92011-03-09 22:23:28 -07001282 last_timestamp = sample->time;
1283 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001284 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001285
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001286 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001287 pr_err("problem processing %d event, skipping it.\n",
1288 event->header.type);
1289 return -1;
1290 }
1291
1292 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001293 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001294
Anton Blanchard5d67be92011-07-04 21:57:50 +10001295 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001296 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001297
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001298 if (scripting_ops)
1299 scripting_ops->process_event(event, sample, evsel, &al);
1300 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001301 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001302
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001303out_put:
1304 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001305 return 0;
1306}
1307
Adrian Hunter7ea95722013-11-01 15:51:30 +02001308static int process_attr(struct perf_tool *tool, union perf_event *event,
1309 struct perf_evlist **pevlist)
1310{
1311 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1312 struct perf_evlist *evlist;
1313 struct perf_evsel *evsel, *pos;
1314 int err;
1315
1316 err = perf_event__process_attr(tool, event, pevlist);
1317 if (err)
1318 return err;
1319
1320 evlist = *pevlist;
1321 evsel = perf_evlist__last(*pevlist);
1322
1323 if (evsel->attr.type >= PERF_TYPE_MAX)
1324 return 0;
1325
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001326 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001327 if (pos->attr.type == evsel->attr.type && pos != evsel)
1328 return 0;
1329 }
1330
1331 set_print_ip_opts(&evsel->attr);
1332
Jiri Olsad2b5a312015-10-16 12:41:25 +02001333 if (evsel->attr.sample_type)
1334 err = perf_evsel__check_attr(evsel, scr->session);
1335
1336 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001337}
1338
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001339static int process_comm_event(struct perf_tool *tool,
1340 union perf_event *event,
1341 struct perf_sample *sample,
1342 struct machine *machine)
1343{
1344 struct thread *thread;
1345 struct perf_script *script = container_of(tool, struct perf_script, tool);
1346 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001347 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001348 int ret = -1;
1349
1350 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1351 if (thread == NULL) {
1352 pr_debug("problem processing COMM event, skipping it.\n");
1353 return -1;
1354 }
1355
1356 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1357 goto out;
1358
1359 if (!evsel->attr.sample_id_all) {
1360 sample->cpu = 0;
1361 sample->time = 0;
1362 sample->tid = event->comm.tid;
1363 sample->pid = event->comm.pid;
1364 }
1365 print_sample_start(sample, thread, evsel);
1366 perf_event__fprintf(event, stdout);
1367 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001368out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001369 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001370 return ret;
1371}
1372
Hari Bathini96a44bb2017-03-08 02:12:06 +05301373static int process_namespaces_event(struct perf_tool *tool,
1374 union perf_event *event,
1375 struct perf_sample *sample,
1376 struct machine *machine)
1377{
1378 struct thread *thread;
1379 struct perf_script *script = container_of(tool, struct perf_script, tool);
1380 struct perf_session *session = script->session;
1381 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1382 int ret = -1;
1383
1384 thread = machine__findnew_thread(machine, event->namespaces.pid,
1385 event->namespaces.tid);
1386 if (thread == NULL) {
1387 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1388 return -1;
1389 }
1390
1391 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1392 goto out;
1393
1394 if (!evsel->attr.sample_id_all) {
1395 sample->cpu = 0;
1396 sample->time = 0;
1397 sample->tid = event->namespaces.tid;
1398 sample->pid = event->namespaces.pid;
1399 }
1400 print_sample_start(sample, thread, evsel);
1401 perf_event__fprintf(event, stdout);
1402 ret = 0;
1403out:
1404 thread__put(thread);
1405 return ret;
1406}
1407
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001408static int process_fork_event(struct perf_tool *tool,
1409 union perf_event *event,
1410 struct perf_sample *sample,
1411 struct machine *machine)
1412{
1413 struct thread *thread;
1414 struct perf_script *script = container_of(tool, struct perf_script, tool);
1415 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001416 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001417
1418 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1419 return -1;
1420
1421 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1422 if (thread == NULL) {
1423 pr_debug("problem processing FORK event, skipping it.\n");
1424 return -1;
1425 }
1426
1427 if (!evsel->attr.sample_id_all) {
1428 sample->cpu = 0;
1429 sample->time = event->fork.time;
1430 sample->tid = event->fork.tid;
1431 sample->pid = event->fork.pid;
1432 }
1433 print_sample_start(sample, thread, evsel);
1434 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001435 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001436
1437 return 0;
1438}
1439static int process_exit_event(struct perf_tool *tool,
1440 union perf_event *event,
1441 struct perf_sample *sample,
1442 struct machine *machine)
1443{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001444 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001445 struct thread *thread;
1446 struct perf_script *script = container_of(tool, struct perf_script, tool);
1447 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001448 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001449
1450 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1451 if (thread == NULL) {
1452 pr_debug("problem processing EXIT event, skipping it.\n");
1453 return -1;
1454 }
1455
1456 if (!evsel->attr.sample_id_all) {
1457 sample->cpu = 0;
1458 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001459 sample->tid = event->fork.tid;
1460 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001461 }
1462 print_sample_start(sample, thread, evsel);
1463 perf_event__fprintf(event, stdout);
1464
1465 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001466 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001467
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001468 thread__put(thread);
1469 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001470}
1471
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001472static int process_mmap_event(struct perf_tool *tool,
1473 union perf_event *event,
1474 struct perf_sample *sample,
1475 struct machine *machine)
1476{
1477 struct thread *thread;
1478 struct perf_script *script = container_of(tool, struct perf_script, tool);
1479 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001480 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001481
1482 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1483 return -1;
1484
1485 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1486 if (thread == NULL) {
1487 pr_debug("problem processing MMAP event, skipping it.\n");
1488 return -1;
1489 }
1490
1491 if (!evsel->attr.sample_id_all) {
1492 sample->cpu = 0;
1493 sample->time = 0;
1494 sample->tid = event->mmap.tid;
1495 sample->pid = event->mmap.pid;
1496 }
1497 print_sample_start(sample, thread, evsel);
1498 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001499 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001500 return 0;
1501}
1502
1503static int process_mmap2_event(struct perf_tool *tool,
1504 union perf_event *event,
1505 struct perf_sample *sample,
1506 struct machine *machine)
1507{
1508 struct thread *thread;
1509 struct perf_script *script = container_of(tool, struct perf_script, tool);
1510 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001511 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001512
1513 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1514 return -1;
1515
1516 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1517 if (thread == NULL) {
1518 pr_debug("problem processing MMAP2 event, skipping it.\n");
1519 return -1;
1520 }
1521
1522 if (!evsel->attr.sample_id_all) {
1523 sample->cpu = 0;
1524 sample->time = 0;
1525 sample->tid = event->mmap2.tid;
1526 sample->pid = event->mmap2.pid;
1527 }
1528 print_sample_start(sample, thread, evsel);
1529 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001530 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001531 return 0;
1532}
1533
Adrian Hunter7c148982015-07-21 12:44:06 +03001534static int process_switch_event(struct perf_tool *tool,
1535 union perf_event *event,
1536 struct perf_sample *sample,
1537 struct machine *machine)
1538{
1539 struct thread *thread;
1540 struct perf_script *script = container_of(tool, struct perf_script, tool);
1541 struct perf_session *session = script->session;
1542 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1543
1544 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1545 return -1;
1546
1547 thread = machine__findnew_thread(machine, sample->pid,
1548 sample->tid);
1549 if (thread == NULL) {
1550 pr_debug("problem processing SWITCH event, skipping it.\n");
1551 return -1;
1552 }
1553
1554 print_sample_start(sample, thread, evsel);
1555 perf_event__fprintf(event, stdout);
1556 thread__put(thread);
1557 return 0;
1558}
1559
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001560static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001561{
1562 session_done = 1;
1563}
1564
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001565static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001566{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001567 int ret;
1568
Tom Zanussic239da32010-04-01 23:59:18 -05001569 signal(SIGINT, sig_handler);
1570
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001571 /* override event processing functions */
1572 if (script->show_task_events) {
1573 script->tool.comm = process_comm_event;
1574 script->tool.fork = process_fork_event;
1575 script->tool.exit = process_exit_event;
1576 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001577 if (script->show_mmap_events) {
1578 script->tool.mmap = process_mmap_event;
1579 script->tool.mmap2 = process_mmap2_event;
1580 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001581 if (script->show_switch_events)
1582 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301583 if (script->show_namespace_events)
1584 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001585
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001586 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001587
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001588 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001589 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001590
1591 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001592}
1593
Tom Zanussi956ffd02009-11-25 01:15:46 -06001594struct script_spec {
1595 struct list_head node;
1596 struct scripting_ops *ops;
1597 char spec[0];
1598};
1599
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001600static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001601
1602static struct script_spec *script_spec__new(const char *spec,
1603 struct scripting_ops *ops)
1604{
1605 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1606
1607 if (s != NULL) {
1608 strcpy(s->spec, spec);
1609 s->ops = ops;
1610 }
1611
1612 return s;
1613}
1614
Tom Zanussi956ffd02009-11-25 01:15:46 -06001615static void script_spec__add(struct script_spec *s)
1616{
1617 list_add_tail(&s->node, &script_specs);
1618}
1619
1620static struct script_spec *script_spec__find(const char *spec)
1621{
1622 struct script_spec *s;
1623
1624 list_for_each_entry(s, &script_specs, node)
1625 if (strcasecmp(s->spec, spec) == 0)
1626 return s;
1627 return NULL;
1628}
1629
Tom Zanussi956ffd02009-11-25 01:15:46 -06001630int script_spec_register(const char *spec, struct scripting_ops *ops)
1631{
1632 struct script_spec *s;
1633
1634 s = script_spec__find(spec);
1635 if (s)
1636 return -1;
1637
Taeung Song8560bae2016-02-26 00:13:10 +09001638 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001639 if (!s)
1640 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001641 else
1642 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001643
1644 return 0;
1645}
1646
1647static struct scripting_ops *script_spec__lookup(const char *spec)
1648{
1649 struct script_spec *s = script_spec__find(spec);
1650 if (!s)
1651 return NULL;
1652
1653 return s->ops;
1654}
1655
1656static void list_available_languages(void)
1657{
1658 struct script_spec *s;
1659
1660 fprintf(stderr, "\n");
1661 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001662 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001663
1664 list_for_each_entry(s, &script_specs, node)
1665 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1666
1667 fprintf(stderr, "\n");
1668}
1669
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001670static int parse_scriptname(const struct option *opt __maybe_unused,
1671 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001672{
1673 char spec[PATH_MAX];
1674 const char *script, *ext;
1675 int len;
1676
Tom Zanussif526d682010-01-27 02:27:52 -06001677 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001678 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001679 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001680 }
1681
1682 script = strchr(str, ':');
1683 if (script) {
1684 len = script - str;
1685 if (len >= PATH_MAX) {
1686 fprintf(stderr, "invalid language specifier");
1687 return -1;
1688 }
1689 strncpy(spec, str, len);
1690 spec[len] = '\0';
1691 scripting_ops = script_spec__lookup(spec);
1692 if (!scripting_ops) {
1693 fprintf(stderr, "invalid language specifier");
1694 return -1;
1695 }
1696 script++;
1697 } else {
1698 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001699 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001700 if (!ext) {
1701 fprintf(stderr, "invalid script extension");
1702 return -1;
1703 }
1704 scripting_ops = script_spec__lookup(++ext);
1705 if (!scripting_ops) {
1706 fprintf(stderr, "invalid script extension");
1707 return -1;
1708 }
1709 }
1710
1711 script_name = strdup(script);
1712
1713 return 0;
1714}
1715
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001716static int parse_output_fields(const struct option *opt __maybe_unused,
1717 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001718{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001719 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001720 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001721 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001722 int rc = 0;
1723 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001724 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001725
1726 if (!str)
1727 return -ENOMEM;
1728
David Ahern2c9e45f72011-03-17 10:03:21 -06001729 /* first word can state for which event type the user is specifying
1730 * the fields. If no type exists, the specified fields apply to all
1731 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001732 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001733 tok = strchr(str, ':');
1734 if (tok) {
1735 *tok = '\0';
1736 tok++;
1737 if (!strcmp(str, "hw"))
1738 type = PERF_TYPE_HARDWARE;
1739 else if (!strcmp(str, "sw"))
1740 type = PERF_TYPE_SOFTWARE;
1741 else if (!strcmp(str, "trace"))
1742 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001743 else if (!strcmp(str, "raw"))
1744 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001745 else if (!strcmp(str, "break"))
1746 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001747 else {
1748 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001749 rc = -EINVAL;
1750 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001751 }
1752
1753 if (output[type].user_set)
1754 pr_warning("Overriding previous field request for %s events.\n",
1755 event_type(type));
1756
1757 output[type].fields = 0;
1758 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001759 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001760
1761 } else {
1762 tok = str;
1763 if (strlen(str) == 0) {
1764 fprintf(stderr,
1765 "Cannot set fields to 'none' for all event types.\n");
1766 rc = -EINVAL;
1767 goto out;
1768 }
1769
1770 if (output_set_by_user())
1771 pr_warning("Overriding previous field request for all events.\n");
1772
1773 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1774 output[j].fields = 0;
1775 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001776 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001777 }
David Ahern1424dc92011-03-09 22:23:28 -07001778 }
1779
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001780 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
David Ahern745f43e2011-03-09 22:23:26 -07001781 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001782 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001783 break;
David Ahern745f43e2011-03-09 22:23:26 -07001784 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001785 if (i == imax && strcmp(tok, "flags") == 0) {
1786 print_flags = true;
1787 continue;
1788 }
David Ahern745f43e2011-03-09 22:23:26 -07001789 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001790 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001791 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001792 goto out;
1793 }
1794
1795 if (type == -1) {
1796 /* add user option to all events types for
1797 * which it is valid
1798 */
1799 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1800 if (output[j].invalid_fields & all_output_options[i].field) {
1801 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1802 all_output_options[i].str, event_type(j));
1803 } else
1804 output[j].fields |= all_output_options[i].field;
1805 }
1806 } else {
1807 if (output[type].invalid_fields & all_output_options[i].field) {
1808 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1809 all_output_options[i].str, event_type(type));
1810
1811 rc = -EINVAL;
1812 goto out;
1813 }
1814 output[type].fields |= all_output_options[i].field;
1815 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001816 }
1817
1818 if (type >= 0) {
1819 if (output[type].fields == 0) {
1820 pr_debug("No fields requested for %s type. "
1821 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001822 }
David Ahern1424dc92011-03-09 22:23:28 -07001823 }
David Ahern745f43e2011-03-09 22:23:26 -07001824
David Ahern2c9e45f72011-03-17 10:03:21 -06001825out:
David Ahern745f43e2011-03-09 22:23:26 -07001826 free(str);
1827 return rc;
1828}
1829
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001830/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1831static int is_directory(const char *base_path, const struct dirent *dent)
1832{
1833 char path[PATH_MAX];
1834 struct stat st;
1835
1836 sprintf(path, "%s/%s", base_path, dent->d_name);
1837 if (stat(path, &st))
1838 return 0;
1839
1840 return S_ISDIR(st.st_mode);
1841}
1842
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001843#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1844 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1845 if ((lang_dirent->d_type == DT_DIR || \
1846 (lang_dirent->d_type == DT_UNKNOWN && \
1847 is_directory(scripts_path, lang_dirent))) && \
1848 (strcmp(lang_dirent->d_name, ".")) && \
1849 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001850
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001851#define for_each_script(lang_path, lang_dir, script_dirent) \
1852 while ((script_dirent = readdir(lang_dir)) != NULL) \
1853 if (script_dirent->d_type != DT_DIR && \
1854 (script_dirent->d_type != DT_UNKNOWN || \
1855 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001856
1857
1858#define RECORD_SUFFIX "-record"
1859#define REPORT_SUFFIX "-report"
1860
1861struct script_desc {
1862 struct list_head node;
1863 char *name;
1864 char *half_liner;
1865 char *args;
1866};
1867
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001868static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001869
1870static struct script_desc *script_desc__new(const char *name)
1871{
1872 struct script_desc *s = zalloc(sizeof(*s));
1873
Tom Zanussib5b87312010-11-10 08:16:51 -06001874 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001875 s->name = strdup(name);
1876
1877 return s;
1878}
1879
1880static void script_desc__delete(struct script_desc *s)
1881{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001882 zfree(&s->name);
1883 zfree(&s->half_liner);
1884 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001885 free(s);
1886}
1887
1888static void script_desc__add(struct script_desc *s)
1889{
1890 list_add_tail(&s->node, &script_descs);
1891}
1892
1893static struct script_desc *script_desc__find(const char *name)
1894{
1895 struct script_desc *s;
1896
1897 list_for_each_entry(s, &script_descs, node)
1898 if (strcasecmp(s->name, name) == 0)
1899 return s;
1900 return NULL;
1901}
1902
1903static struct script_desc *script_desc__findnew(const char *name)
1904{
1905 struct script_desc *s = script_desc__find(name);
1906
1907 if (s)
1908 return s;
1909
1910 s = script_desc__new(name);
1911 if (!s)
1912 goto out_delete_desc;
1913
1914 script_desc__add(s);
1915
1916 return s;
1917
1918out_delete_desc:
1919 script_desc__delete(s);
1920
1921 return NULL;
1922}
1923
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001924static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001925{
1926 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001927 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001928
1929 if (strlen(str) > suffix_len) {
1930 p = str + strlen(str) - suffix_len;
1931 if (!strncmp(p, suffix, suffix_len))
1932 return p;
1933 }
1934
1935 return NULL;
1936}
1937
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001938static int read_script_info(struct script_desc *desc, const char *filename)
1939{
1940 char line[BUFSIZ], *p;
1941 FILE *fp;
1942
1943 fp = fopen(filename, "r");
1944 if (!fp)
1945 return -1;
1946
1947 while (fgets(line, sizeof(line), fp)) {
1948 p = ltrim(line);
1949 if (strlen(p) == 0)
1950 continue;
1951 if (*p != '#')
1952 continue;
1953 p++;
1954 if (strlen(p) && *p == '!')
1955 continue;
1956
1957 p = ltrim(p);
1958 if (strlen(p) && p[strlen(p) - 1] == '\n')
1959 p[strlen(p) - 1] = '\0';
1960
1961 if (!strncmp(p, "description:", strlen("description:"))) {
1962 p += strlen("description:");
1963 desc->half_liner = strdup(ltrim(p));
1964 continue;
1965 }
1966
1967 if (!strncmp(p, "args:", strlen("args:"))) {
1968 p += strlen("args:");
1969 desc->args = strdup(ltrim(p));
1970 continue;
1971 }
1972 }
1973
1974 fclose(fp);
1975
1976 return 0;
1977}
1978
Robert Richter38efb532011-11-25 11:38:40 +01001979static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1980{
1981 char *script_root, *str;
1982
1983 script_root = strdup(script_dirent->d_name);
1984 if (!script_root)
1985 return NULL;
1986
1987 str = (char *)ends_with(script_root, suffix);
1988 if (!str) {
1989 free(script_root);
1990 return NULL;
1991 }
1992
1993 *str = '\0';
1994 return script_root;
1995}
1996
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001997static int list_available_scripts(const struct option *opt __maybe_unused,
1998 const char *s __maybe_unused,
1999 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002000{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002001 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002002 char scripts_path[MAXPATHLEN];
2003 DIR *scripts_dir, *lang_dir;
2004 char script_path[MAXPATHLEN];
2005 char lang_path[MAXPATHLEN];
2006 struct script_desc *desc;
2007 char first_half[BUFSIZ];
2008 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002009
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002010 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002011
2012 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002013 if (!scripts_dir) {
2014 fprintf(stdout,
2015 "open(%s) failed.\n"
2016 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2017 scripts_path);
2018 exit(-1);
2019 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002020
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002021 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002022 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002023 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002024 lang_dir = opendir(lang_path);
2025 if (!lang_dir)
2026 continue;
2027
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002028 for_each_script(lang_path, lang_dir, script_dirent) {
2029 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002030 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002031 desc = script_desc__findnew(script_root);
2032 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002033 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002034 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002035 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002036 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002037 }
2038 }
2039
2040 fprintf(stdout, "List of available trace scripts:\n");
2041 list_for_each_entry(desc, &script_descs, node) {
2042 sprintf(first_half, "%s %s", desc->name,
2043 desc->args ? desc->args : "");
2044 fprintf(stdout, " %-36s %s\n", first_half,
2045 desc->half_liner ? desc->half_liner : "");
2046 }
2047
2048 exit(0);
2049}
2050
Feng Tange5f37052012-09-07 16:42:26 +08002051/*
Feng Tang49e639e2012-10-30 11:56:03 +08002052 * Some scripts specify the required events in their "xxx-record" file,
2053 * this function will check if the events in perf.data match those
2054 * mentioned in the "xxx-record".
2055 *
2056 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2057 * which is covered well now. And new parsing code should be added to
2058 * cover the future complexing formats like event groups etc.
2059 */
2060static int check_ev_match(char *dir_name, char *scriptname,
2061 struct perf_session *session)
2062{
2063 char filename[MAXPATHLEN], evname[128];
2064 char line[BUFSIZ], *p;
2065 struct perf_evsel *pos;
2066 int match, len;
2067 FILE *fp;
2068
2069 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2070
2071 fp = fopen(filename, "r");
2072 if (!fp)
2073 return -1;
2074
2075 while (fgets(line, sizeof(line), fp)) {
2076 p = ltrim(line);
2077 if (*p == '#')
2078 continue;
2079
2080 while (strlen(p)) {
2081 p = strstr(p, "-e");
2082 if (!p)
2083 break;
2084
2085 p += 2;
2086 p = ltrim(p);
2087 len = strcspn(p, " \t");
2088 if (!len)
2089 break;
2090
2091 snprintf(evname, len + 1, "%s", p);
2092
2093 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002094 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002095 if (!strcmp(perf_evsel__name(pos), evname)) {
2096 match = 1;
2097 break;
2098 }
2099 }
2100
2101 if (!match) {
2102 fclose(fp);
2103 return -1;
2104 }
2105 }
2106 }
2107
2108 fclose(fp);
2109 return 0;
2110}
2111
2112/*
Feng Tange5f37052012-09-07 16:42:26 +08002113 * Return -1 if none is found, otherwise the actual scripts number.
2114 *
2115 * Currently the only user of this function is the script browser, which
2116 * will list all statically runnable scripts, select one, execute it and
2117 * show the output in a perf browser.
2118 */
2119int find_scripts(char **scripts_array, char **scripts_path_array)
2120{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002121 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002122 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002123 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002124 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002125 struct perf_data_file file = {
2126 .path = input_name,
2127 .mode = PERF_DATA_MODE_READ,
2128 };
Feng Tange5f37052012-09-07 16:42:26 +08002129 char *temp;
2130 int i = 0;
2131
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002132 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002133 if (!session)
2134 return -1;
2135
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002136 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002137
2138 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002139 if (!scripts_dir) {
2140 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002141 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002142 }
Feng Tange5f37052012-09-07 16:42:26 +08002143
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002144 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002145 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002146 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002147#ifdef NO_LIBPERL
2148 if (strstr(lang_path, "perl"))
2149 continue;
2150#endif
2151#ifdef NO_LIBPYTHON
2152 if (strstr(lang_path, "python"))
2153 continue;
2154#endif
2155
2156 lang_dir = opendir(lang_path);
2157 if (!lang_dir)
2158 continue;
2159
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002160 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002161 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002162 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002163 continue;
2164 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002165 script_dirent->d_name);
2166 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002167 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002168 (temp - script_dirent->d_name) + 1,
2169 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002170
2171 if (check_ev_match(lang_path,
2172 scripts_array[i], session))
2173 continue;
2174
Feng Tange5f37052012-09-07 16:42:26 +08002175 i++;
2176 }
Feng Tang49e639e2012-10-30 11:56:03 +08002177 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002178 }
2179
Feng Tang49e639e2012-10-30 11:56:03 +08002180 closedir(scripts_dir);
2181 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002182 return i;
2183}
2184
Tom Zanussi38752942009-12-15 02:53:39 -06002185static char *get_script_path(const char *script_root, const char *suffix)
2186{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002187 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002188 char scripts_path[MAXPATHLEN];
2189 char script_path[MAXPATHLEN];
2190 DIR *scripts_dir, *lang_dir;
2191 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002192 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002193
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002194 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002195
2196 scripts_dir = opendir(scripts_path);
2197 if (!scripts_dir)
2198 return NULL;
2199
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002200 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002201 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002202 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002203 lang_dir = opendir(lang_path);
2204 if (!lang_dir)
2205 continue;
2206
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002207 for_each_script(lang_path, lang_dir, script_dirent) {
2208 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002209 if (__script_root && !strcmp(script_root, __script_root)) {
2210 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002211 closedir(lang_dir);
2212 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002213 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002214 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002215 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002216 }
2217 free(__script_root);
2218 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002219 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002220 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002221 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002222
Robert Richter38efb532011-11-25 11:38:40 +01002223 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002224}
2225
Tom Zanussib5b87312010-11-10 08:16:51 -06002226static bool is_top_script(const char *script_path)
2227{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002228 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002229}
2230
2231static int has_required_arg(char *script_path)
2232{
2233 struct script_desc *desc;
2234 int n_args = 0;
2235 char *p;
2236
2237 desc = script_desc__new(NULL);
2238
2239 if (read_script_info(desc, script_path))
2240 goto out;
2241
2242 if (!desc->args)
2243 goto out;
2244
2245 for (p = desc->args; *p; p++)
2246 if (*p == '<')
2247 n_args++;
2248out:
2249 script_desc__delete(desc);
2250
2251 return n_args;
2252}
2253
David Ahernd54b1a92012-08-26 12:24:46 -06002254static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002255{
2256 char **__argv = malloc(sizeof(const char *) * argc);
2257
David Ahernd54b1a92012-08-26 12:24:46 -06002258 if (!__argv) {
2259 pr_err("malloc failed\n");
2260 return -1;
2261 }
2262
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002263 memcpy(__argv, argv, sizeof(const char *) * argc);
2264 argc = parse_options(argc, (const char **)__argv, record_options,
2265 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2266 free(__argv);
2267
David Ahernd54b1a92012-08-26 12:24:46 -06002268 system_wide = (argc == 0);
2269
2270 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002271}
2272
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002273static void script__setup_sample_type(struct perf_script *script)
2274{
2275 struct perf_session *session = script->session;
2276 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2277
2278 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2279 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2280 (sample_type & PERF_SAMPLE_STACK_USER))
2281 callchain_param.record_mode = CALLCHAIN_DWARF;
2282 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2283 callchain_param.record_mode = CALLCHAIN_LBR;
2284 else
2285 callchain_param.record_mode = CALLCHAIN_FP;
2286 }
2287}
2288
Jiri Olsae099eba2016-01-05 22:09:09 +01002289static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2290 union perf_event *event,
2291 struct perf_session *session)
2292{
2293 struct stat_round_event *round = &event->stat_round;
2294 struct perf_evsel *counter;
2295
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002296 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002297 perf_stat_process_counter(&stat_config, counter);
2298 process_stat(counter, round->time);
2299 }
2300
2301 process_stat_interval(round->time);
2302 return 0;
2303}
2304
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002305static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2306 union perf_event *event,
2307 struct perf_session *session __maybe_unused)
2308{
2309 perf_event__read_stat_config(&stat_config, &event->stat_config);
2310 return 0;
2311}
2312
Jiri Olsacfc88742016-01-05 22:09:06 +01002313static int set_maps(struct perf_script *script)
2314{
2315 struct perf_evlist *evlist = script->session->evlist;
2316
2317 if (!script->cpus || !script->threads)
2318 return 0;
2319
2320 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2321 return -EINVAL;
2322
2323 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2324
2325 if (perf_evlist__alloc_stats(evlist, true))
2326 return -ENOMEM;
2327
2328 script->allocated = true;
2329 return 0;
2330}
2331
2332static
2333int process_thread_map_event(struct perf_tool *tool,
2334 union perf_event *event,
2335 struct perf_session *session __maybe_unused)
2336{
2337 struct perf_script *script = container_of(tool, struct perf_script, tool);
2338
2339 if (script->threads) {
2340 pr_warning("Extra thread map event, ignoring.\n");
2341 return 0;
2342 }
2343
2344 script->threads = thread_map__new_event(&event->thread_map);
2345 if (!script->threads)
2346 return -ENOMEM;
2347
2348 return set_maps(script);
2349}
2350
2351static
2352int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2353 union perf_event *event,
2354 struct perf_session *session __maybe_unused)
2355{
2356 struct perf_script *script = container_of(tool, struct perf_script, tool);
2357
2358 if (script->cpus) {
2359 pr_warning("Extra cpu map event, ignoring.\n");
2360 return 0;
2361 }
2362
2363 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2364 if (!script->cpus)
2365 return -ENOMEM;
2366
2367 return set_maps(script);
2368}
2369
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002370int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002371{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002372 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002373 bool header = false;
2374 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002375 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002376 char *rec_script_path = NULL;
2377 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002378 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002379 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002380 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002381 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002382 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002383 struct perf_script script = {
2384 .tool = {
2385 .sample = process_sample_event,
2386 .mmap = perf_event__process_mmap,
2387 .mmap2 = perf_event__process_mmap2,
2388 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302389 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002390 .exit = perf_event__process_exit,
2391 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002392 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002393 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002394 .tracing_data = perf_event__process_tracing_data,
2395 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002396 .id_index = perf_event__process_id_index,
2397 .auxtrace_info = perf_event__process_auxtrace_info,
2398 .auxtrace = perf_event__process_auxtrace,
2399 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002400 .stat = perf_event__process_stat_event,
2401 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002402 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002403 .thread_map = process_thread_map_event,
2404 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002405 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002406 .ordering_requires_timestamps = true,
2407 },
2408 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002409 struct perf_data_file file = {
2410 .mode = PERF_DATA_MODE_READ,
2411 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002412 const struct option options[] = {
2413 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2414 "dump raw trace in ASCII"),
2415 OPT_INCR('v', "verbose", &verbose,
2416 "be more verbose (show symbol address, etc)"),
2417 OPT_BOOLEAN('L', "Latency", &latency_format,
2418 "show latency attributes (irqs/preemption disabled, etc)"),
2419 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2420 list_available_scripts),
2421 OPT_CALLBACK('s', "script", NULL, "name",
2422 "script file name (lang:script name, script name, or *)",
2423 parse_scriptname),
2424 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2425 "generate perf-script.xx script in specified language"),
2426 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2427 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2428 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002429 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2430 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002431 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2432 "file", "vmlinux pathname"),
2433 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2434 "file", "kallsyms pathname"),
2435 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2436 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002437 OPT_CALLBACK(0, "symfs", NULL, "directory",
2438 "Look for files with symbols relative to this directory",
2439 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002440 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002441 "comma separated output fields prepend with 'type:'. "
2442 "Valid types: hw,sw,trace,raw. "
2443 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Huntere2167082016-06-23 16:40:58 +03002444 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
Andi Kleen48d02a12017-02-23 15:46:34 -08002445 "bpf-output,callindent,insn,insnlen,brstackinsn",
2446 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002447 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2448 "system-wide collection from all CPUs"),
2449 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2450 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002451 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2452 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002453 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2454 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2455 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002456 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2457 "only consider symbols in these pids"),
2458 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2459 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002460 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2461 "Set the maximum stack depth when parsing the callchain, "
2462 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002463 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002464 OPT_BOOLEAN('I', "show-info", &show_full_info,
2465 "display extended information from perf.data file"),
2466 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2467 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002468 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2469 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002470 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2471 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002472 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2473 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302474 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2475 "Show namespace events (if recorded)"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002476 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002477 OPT_INTEGER(0, "max-blocks", &max_blocks,
2478 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002479 OPT_BOOLEAN(0, "ns", &nanosecs,
2480 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002481 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2482 "Instruction Tracing options",
2483 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002484 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2485 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002486 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2487 "Enable symbol demangling"),
2488 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2489 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002490 OPT_STRING(0, "time", &script.time_str, "str",
2491 "Time span of interest (start,stop)"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002492 OPT_END()
2493 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002494 const char * const script_subcommands[] = { "record", "report", NULL };
2495 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002496 "perf script [<options>]",
2497 "perf script [<options>] record <script> [<record-options>] <command>",
2498 "perf script [<options>] report <script> [script-args]",
2499 "perf script [<options>] <script> [<record-options>] <command>",
2500 "perf script [<options>] <top-script> [script-args]",
2501 NULL
2502 };
Tom Zanussi38752942009-12-15 02:53:39 -06002503
Tom Zanussib5b87312010-11-10 08:16:51 -06002504 setup_scripting();
2505
Yunlong Song40cae2b2015-03-18 21:35:54 +08002506 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002507 PARSE_OPT_STOP_AT_NON_OPTION);
2508
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002509 file.path = input_name;
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002510 file.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002511
Tom Zanussib5b87312010-11-10 08:16:51 -06002512 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2513 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2514 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002515 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002516 }
2517
Tom Zanussib5b87312010-11-10 08:16:51 -06002518 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2519 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2520 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002521 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002522 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002523 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002524 return -1;
2525 }
Tom Zanussi38752942009-12-15 02:53:39 -06002526 }
2527
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002528 if (itrace_synth_opts.callchain &&
2529 itrace_synth_opts.callchain_sz > scripting_max_stack)
2530 scripting_max_stack = itrace_synth_opts.callchain_sz;
2531
Ben Hutchings44e668c2010-10-10 16:10:03 +01002532 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002533 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002534
Tom Zanussib5b87312010-11-10 08:16:51 -06002535 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002536 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002537 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002538 pid_t pid;
2539
Tom Zanussib5b87312010-11-10 08:16:51 -06002540 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2541 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2542
2543 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002544 usage_with_options_msg(script_usage, options,
2545 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002546 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002547 }
2548
Tom Zanussib5b87312010-11-10 08:16:51 -06002549 if (is_top_script(argv[0])) {
2550 rep_args = argc - 1;
2551 } else {
2552 int rec_args;
2553
2554 rep_args = has_required_arg(rep_script_path);
2555 rec_args = (argc - 1) - rep_args;
2556 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002557 usage_with_options_msg(script_usage, options,
2558 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002559 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002560 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002561 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002562 }
2563
2564 if (pipe(live_pipe) < 0) {
2565 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002566 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002567 }
2568
2569 pid = fork();
2570 if (pid < 0) {
2571 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002572 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002573 }
2574
2575 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002576 j = 0;
2577
Tom Zanussia0cccc22010-04-01 23:59:25 -05002578 dup2(live_pipe[1], 1);
2579 close(live_pipe[0]);
2580
Robert Richter317df652011-11-25 15:05:25 +01002581 if (is_top_script(argv[0])) {
2582 system_wide = true;
2583 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002584 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2585 err = -1;
2586 goto out;
2587 }
Robert Richter317df652011-11-25 15:05:25 +01002588 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002589
2590 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002591 if (!__argv) {
2592 pr_err("malloc failed\n");
2593 err = -ENOMEM;
2594 goto out;
2595 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002596
Tom Zanussib5b87312010-11-10 08:16:51 -06002597 __argv[j++] = "/bin/sh";
2598 __argv[j++] = rec_script_path;
2599 if (system_wide)
2600 __argv[j++] = "-a";
2601 __argv[j++] = "-q";
2602 __argv[j++] = "-o";
2603 __argv[j++] = "-";
2604 for (i = rep_args + 1; i < argc; i++)
2605 __argv[j++] = argv[i];
2606 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002607
2608 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002609 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002610 exit(-1);
2611 }
2612
2613 dup2(live_pipe[0], 0);
2614 close(live_pipe[1]);
2615
Tom Zanussib5b87312010-11-10 08:16:51 -06002616 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002617 if (!__argv) {
2618 pr_err("malloc failed\n");
2619 err = -ENOMEM;
2620 goto out;
2621 }
2622
Tom Zanussib5b87312010-11-10 08:16:51 -06002623 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002624 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002625 __argv[j++] = rep_script_path;
2626 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002627 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002628 __argv[j++] = "-i";
2629 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002630 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002631
2632 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002633 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002634 exit(-1);
2635 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002636
Tom Zanussib5b87312010-11-10 08:16:51 -06002637 if (rec_script_path)
2638 script_path = rec_script_path;
2639 if (rep_script_path)
2640 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002641
Tom Zanussib5b87312010-11-10 08:16:51 -06002642 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002643 j = 0;
2644
Robert Richter317df652011-11-25 15:05:25 +01002645 if (!rec_script_path)
2646 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002647 else if (!system_wide) {
2648 if (have_cmd(argc - 1, &argv[1]) != 0) {
2649 err = -1;
2650 goto out;
2651 }
2652 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002653
2654 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002655 if (!__argv) {
2656 pr_err("malloc failed\n");
2657 err = -ENOMEM;
2658 goto out;
2659 }
2660
Tom Zanussib5b87312010-11-10 08:16:51 -06002661 __argv[j++] = "/bin/sh";
2662 __argv[j++] = script_path;
2663 if (system_wide)
2664 __argv[j++] = "-a";
2665 for (i = 2; i < argc; i++)
2666 __argv[j++] = argv[i];
2667 __argv[j++] = NULL;
2668
2669 execvp("/bin/sh", (char **)__argv);
2670 free(__argv);
2671 exit(-1);
2672 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002673
Tom Zanussicf4fee52010-03-03 01:04:33 -06002674 if (!script_name)
2675 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002676
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002677 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002678 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002679 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002680
Jiri Olsae90debd2013-12-09 11:02:50 +01002681 if (header || header_only) {
2682 perf_session__fprintf_info(session, stdout, show_full_info);
2683 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002684 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002685 }
2686
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002687 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002688 goto out_delete;
2689
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002690 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002691 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002692
Adrian Huntere2167082016-06-23 16:40:58 +03002693 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2694 itrace_synth_opts.thread_stack = true;
2695
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002696 session->itrace_synth_opts = &itrace_synth_opts;
2697
Anton Blanchard5d67be92011-07-04 21:57:50 +10002698 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002699 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2700 if (err < 0)
2701 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002702 }
2703
David Ahern1424dc92011-03-09 22:23:28 -07002704 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002705 symbol_conf.use_callchain = true;
2706 else
2707 symbol_conf.use_callchain = false;
2708
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002709 if (session->tevent.pevent &&
2710 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002711 machine__resolve_kernel_addr,
2712 &session->machines.host) < 0) {
2713 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2714 return -1;
2715 }
2716
Tom Zanussi956ffd02009-11-25 01:15:46 -06002717 if (generate_script_lang) {
2718 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002719 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002720
David Ahern2c9e45f72011-03-17 10:03:21 -06002721 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002722 fprintf(stderr,
2723 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002724 err = -EINVAL;
2725 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002726 }
2727
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002728 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002729 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002730 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002731 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002732 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002733 }
2734
2735 err = fstat(input, &perf_stat);
2736 if (err < 0) {
2737 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002738 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002739 }
2740
2741 if (!perf_stat.st_size) {
2742 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002743 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002744 }
2745
2746 scripting_ops = script_spec__lookup(generate_script_lang);
2747 if (!scripting_ops) {
2748 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002749 err = -ENOENT;
2750 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002751 }
2752
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002753 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002754 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002755 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002756 }
2757
2758 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002759 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002760 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002761 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002762 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002763 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002764 }
2765
David Ahern9cbdb702011-04-06 21:54:20 -06002766
2767 err = perf_session__check_output_opt(session);
2768 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002769 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002770
David Aherna91f4c42016-11-29 10:15:43 -07002771 /* needs to be parsed after looking up reference time */
2772 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
2773 pr_err("Invalid time string\n");
2774 return -EINVAL;
2775 }
2776
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002777 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002778
Adrian Hunterd445dd22014-08-15 22:08:37 +03002779 flush_scripting();
2780
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002781out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002782 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002783 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002784
2785 if (script_started)
2786 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002787out:
2788 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002789}