blob: c4e36b4743f4f267356349620d548e3c9a485da7 [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 Olsaf5fc1412013-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"
Adrian Huntere2167082016-06-23 16:40:58 +030024#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070025#include "util/time-utils.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100026#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030027#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030028#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030029#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010030#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010031#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080032#include "util/dump-insn.h"
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030033#include <inttypes.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020034
Tom Zanussi956ffd02009-11-25 01:15:46 -060035static char const *script_name;
36static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020037static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020038static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020039static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070040static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090041static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010042static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030043static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030044static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100045static const char *cpu_list;
46static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010047static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080048static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060049
Adrian Hunter44cbe722015-09-25 16:15:50 +030050unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030051
David Ahern745f43e2011-03-09 22:23:26 -070052enum perf_output_field {
53 PERF_OUTPUT_COMM = 1U << 0,
54 PERF_OUTPUT_TID = 1U << 1,
55 PERF_OUTPUT_PID = 1U << 2,
56 PERF_OUTPUT_TIME = 1U << 3,
57 PERF_OUTPUT_CPU = 1U << 4,
58 PERF_OUTPUT_EVNAME = 1U << 5,
59 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060060 PERF_OUTPUT_IP = 1U << 7,
61 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060062 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060063 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090064 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020065 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaa2014-08-25 16:45:42 +020066 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020067 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020068 PERF_OUTPUT_BRSTACK = 1U << 15,
69 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010070 PERF_OUTPUT_DATA_SRC = 1U << 17,
71 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000072 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030073 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030074 PERF_OUTPUT_INSN = 1U << 21,
75 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080076 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
David Ahern745f43e2011-03-09 22:23:26 -070077};
78
79struct output_option {
80 const char *str;
81 enum perf_output_field field;
82} all_output_options[] = {
83 {.str = "comm", .field = PERF_OUTPUT_COMM},
84 {.str = "tid", .field = PERF_OUTPUT_TID},
85 {.str = "pid", .field = PERF_OUTPUT_PID},
86 {.str = "time", .field = PERF_OUTPUT_TIME},
87 {.str = "cpu", .field = PERF_OUTPUT_CPU},
88 {.str = "event", .field = PERF_OUTPUT_EVNAME},
89 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060090 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070091 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060092 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060093 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090094 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020095 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaa2014-08-25 16:45:42 +020096 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +020097 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +020098 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
99 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100100 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
101 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000102 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300103 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300104 {.str = "insn", .field = PERF_OUTPUT_INSN},
105 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800106 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
David Ahern745f43e2011-03-09 22:23:26 -0700107};
108
109/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600110static struct {
111 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600112 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400113 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600114 u64 fields;
115 u64 invalid_fields;
116} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700117
David Ahern2c9e45f72011-03-17 10:03:21 -0600118 [PERF_TYPE_HARDWARE] = {
119 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700120
David Ahern2c9e45f72011-03-17 10:03:21 -0600121 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
122 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600123 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200124 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
125 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600126
Wang Nan30372f02016-02-24 11:20:45 +0000127 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600128 },
129
130 [PERF_TYPE_SOFTWARE] = {
131 .user_set = false,
132
133 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
134 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600135 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200136 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000137 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600138
139 .invalid_fields = PERF_OUTPUT_TRACE,
140 },
141
142 [PERF_TYPE_TRACEPOINT] = {
143 .user_set = false,
144
145 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
146 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000147 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600148 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700149
150 [PERF_TYPE_RAW] = {
151 .user_set = false,
152
153 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
154 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600155 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200156 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100157 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
158 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700159
Wang Nan30372f02016-02-24 11:20:45 +0000160 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700161 },
Wang Nan27cfef02015-12-08 02:25:43 +0000162
163 [PERF_TYPE_BREAKPOINT] = {
164 .user_set = false,
165
166 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
167 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
168 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
169 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
170 PERF_OUTPUT_PERIOD,
171
Wang Nan30372f02016-02-24 11:20:45 +0000172 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000173 },
David Ahern1424dc92011-03-09 22:23:28 -0700174};
David Ahern745f43e2011-03-09 22:23:26 -0700175
David Ahern2c9e45f72011-03-17 10:03:21 -0600176static bool output_set_by_user(void)
177{
178 int j;
179 for (j = 0; j < PERF_TYPE_MAX; ++j) {
180 if (output[j].user_set)
181 return true;
182 }
183 return false;
184}
David Ahern745f43e2011-03-09 22:23:26 -0700185
David Ahern9cbdb702011-04-06 21:54:20 -0600186static const char *output_field2str(enum perf_output_field field)
187{
188 int i, imax = ARRAY_SIZE(all_output_options);
189 const char *str = "";
190
191 for (i = 0; i < imax; ++i) {
192 if (all_output_options[i].field == field) {
193 str = all_output_options[i].str;
194 break;
195 }
196 }
197 return str;
198}
199
David Ahern2c9e45f72011-03-17 10:03:21 -0600200#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700201
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300202static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
203 u64 sample_type, const char *sample_msg,
204 enum perf_output_field field,
205 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700206{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300207 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600208 int type = attr->type;
209 const char *evname;
210
211 if (attr->sample_type & sample_type)
212 return 0;
213
214 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300215 if (allow_user_set)
216 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300217 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600218 pr_err("Samples for '%s' event do not have %s attribute set. "
219 "Cannot print '%s' field.\n",
220 evname, sample_msg, output_field2str(field));
221 return -1;
222 }
223
224 /* user did not ask for it explicitly so remove from the default list */
225 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300226 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600227 pr_debug("Samples for '%s' event do not have %s attribute set. "
228 "Skipping '%s' field.\n",
229 evname, sample_msg, output_field2str(field));
230
231 return 0;
232}
233
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300234static int perf_evsel__check_stype(struct perf_evsel *evsel,
235 u64 sample_type, const char *sample_msg,
236 enum perf_output_field field)
237{
238 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
239 false);
240}
241
David Ahern9cbdb702011-04-06 21:54:20 -0600242static int perf_evsel__check_attr(struct perf_evsel *evsel,
243 struct perf_session *session)
244{
245 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300246 bool allow_user_set;
247
Jiri Olsae099eba2016-01-05 22:09:09 +0100248 if (perf_header__has_feat(&session->header, HEADER_STAT))
249 return 0;
250
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300251 allow_user_set = perf_header__has_feat(&session->header,
252 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600253
David Ahern1424dc92011-03-09 22:23:28 -0700254 if (PRINT_FIELD(TRACE) &&
255 !perf_session__has_traces(session, "record -R"))
256 return -EINVAL;
257
David Ahern787bef12011-05-27 14:28:43 -0600258 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300259 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
260 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700261 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700262 }
David Ahern7cec0922011-05-30 13:08:23 -0600263
264 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300265 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
266 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600267 return -EINVAL;
268
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100269 if (PRINT_FIELD(DATA_SRC) &&
270 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
271 PERF_OUTPUT_DATA_SRC))
272 return -EINVAL;
273
274 if (PRINT_FIELD(WEIGHT) &&
275 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
276 PERF_OUTPUT_WEIGHT))
277 return -EINVAL;
278
David Ahern7cec0922011-05-30 13:08:23 -0600279 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
280 pr_err("Display of symbols requested but neither sample IP nor "
281 "sample address\nis selected. Hence, no addresses to convert "
282 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600283 return -EINVAL;
284 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900285 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
286 pr_err("Display of offsets requested but symbol is not"
287 "selected.\n");
288 return -EINVAL;
289 }
David Ahern7cec0922011-05-30 13:08:23 -0600290 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
291 pr_err("Display of DSO requested but neither sample IP nor "
292 "sample address\nis selected. Hence, no addresses to convert "
293 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600294 return -EINVAL;
295 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200296 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
297 pr_err("Display of source line number requested but sample IP is not\n"
298 "selected. Hence, no address to lookup the source line number.\n");
299 return -EINVAL;
300 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800301 if (PRINT_FIELD(BRSTACKINSN) &&
302 !(perf_evlist__combined_branch_type(session->evlist) &
303 PERF_SAMPLE_BRANCH_ANY)) {
304 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
305 "Hint: run 'perf record -b ...'\n");
306 return -EINVAL;
307 }
David Ahern1424dc92011-03-09 22:23:28 -0700308 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300309 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
310 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700311 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700312
313 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300314 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
315 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700316 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700317
318 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300319 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
320 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700321 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600322
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200323 if (PRINT_FIELD(PERIOD) &&
324 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
325 PERF_OUTPUT_PERIOD))
326 return -EINVAL;
327
Stephane Eranianfc36f942015-08-31 18:41:10 +0200328 if (PRINT_FIELD(IREGS) &&
329 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
330 PERF_OUTPUT_IREGS))
331 return -EINVAL;
332
David Ahern9cbdb702011-04-06 21:54:20 -0600333 return 0;
334}
335
Adrian Hunter7ea95722013-11-01 15:51:30 +0200336static void set_print_ip_opts(struct perf_event_attr *attr)
337{
338 unsigned int type = attr->type;
339
340 output[type].print_ip_opts = 0;
341 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300342 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200343
344 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300345 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200346
347 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300348 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200349
350 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300351 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200352
353 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300354 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200355}
356
David Ahern9cbdb702011-04-06 21:54:20 -0600357/*
358 * verify all user requested events exist and the samples
359 * have the expected data
360 */
361static int perf_session__check_output_opt(struct perf_session *session)
362{
He Kuang40f20e52016-05-16 04:51:19 +0000363 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600364 struct perf_evsel *evsel;
365
366 for (j = 0; j < PERF_TYPE_MAX; ++j) {
367 evsel = perf_session__find_first_evtype(session, j);
368
369 /*
370 * even if fields is set to 0 (ie., show nothing) event must
371 * exist if user explicitly includes it on the command line
372 */
373 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
374 pr_err("%s events do not exist. "
375 "Remove corresponding -f option to proceed.\n",
376 event_type(j));
377 return -1;
378 }
379
380 if (evsel && output[j].fields &&
381 perf_evsel__check_attr(evsel, session))
382 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400383
384 if (evsel == NULL)
385 continue;
386
Adrian Hunter7ea95722013-11-01 15:51:30 +0200387 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700388 }
389
Adrian Hunter98526ee2014-07-31 09:00:59 +0300390 if (!no_callchain) {
391 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000392 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300393
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300394 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000395 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300396 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
397 use_callchain = true;
398 break;
399 }
400 }
He Kuang71ac8992016-08-04 11:25:43 +0000401 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300402 symbol_conf.use_callchain = false;
403 }
404
David Ahern80b8b492013-11-19 21:07:37 -0700405 /*
406 * set default for tracepoints to print symbols only
407 * if callchains are present
408 */
409 if (symbol_conf.use_callchain &&
410 !output[PERF_TYPE_TRACEPOINT].user_set) {
411 struct perf_event_attr *attr;
412
413 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700414
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300415 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000416 if (evsel->attr.type != j)
417 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700418
He Kuang40f20e52016-05-16 04:51:19 +0000419 attr = &evsel->attr;
420
421 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
422 output[j].fields |= PERF_OUTPUT_IP;
423 output[j].fields |= PERF_OUTPUT_SYM;
424 output[j].fields |= PERF_OUTPUT_DSO;
425 set_print_ip_opts(attr);
426 goto out;
427 }
David Ahern80b8b492013-11-19 21:07:37 -0700428 }
429 }
430
431out:
David Ahern1424dc92011-03-09 22:23:28 -0700432 return 0;
433}
David Ahern745f43e2011-03-09 22:23:26 -0700434
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300435static void print_sample_iregs(struct perf_sample *sample,
Stephane Eranianfc36f942015-08-31 18:41:10 +0200436 struct perf_event_attr *attr)
437{
438 struct regs_dump *regs = &sample->intr_regs;
439 uint64_t mask = attr->sample_regs_intr;
440 unsigned i = 0, r;
441
442 if (!regs)
443 return;
444
445 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
446 u64 val = regs->regs[i++];
447 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
448 }
449}
450
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300451static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700452 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300453 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700454{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300455 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700456 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700457 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700458
David Ahern745f43e2011-03-09 22:23:26 -0700459 if (PRINT_FIELD(COMM)) {
460 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200461 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600462 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200463 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700464 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200465 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700466 }
David Ahernc70c94b2011-03-09 22:23:25 -0700467
David Ahern745f43e2011-03-09 22:23:26 -0700468 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
469 printf("%5d/%-5d ", sample->pid, sample->tid);
470 else if (PRINT_FIELD(PID))
471 printf("%5d ", sample->pid);
472 else if (PRINT_FIELD(TID))
473 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700474
David Ahern745f43e2011-03-09 22:23:26 -0700475 if (PRINT_FIELD(CPU)) {
476 if (latency_format)
477 printf("%3d ", sample->cpu);
478 else
479 printf("[%03d] ", sample->cpu);
480 }
David Ahernc70c94b2011-03-09 22:23:25 -0700481
David Ahern745f43e2011-03-09 22:23:26 -0700482 if (PRINT_FIELD(TIME)) {
483 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300484 secs = nsecs / NSEC_PER_SEC;
485 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900486
Adrian Hunter83e19862015-09-25 16:15:36 +0300487 if (nanosecs)
488 printf("%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900489 else {
490 char sample_time[32];
491 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
492 printf("%12s: ", sample_time);
493 }
David Ahern745f43e2011-03-09 22:23:26 -0700494 }
David Ahernc70c94b2011-03-09 22:23:25 -0700495}
496
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200497static inline char
498mispred_str(struct branch_entry *br)
499{
500 if (!(br->flags.mispred || br->flags.predicted))
501 return '-';
502
503 return br->flags.predicted ? 'P' : 'M';
504}
505
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300506static void print_sample_brstack(struct perf_sample *sample)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200507{
508 struct branch_stack *br = sample->branch_stack;
509 u64 i;
510
511 if (!(br && br->nr))
512 return;
513
514 for (i = 0; i < br->nr; i++) {
515 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
516 br->entries[i].from,
517 br->entries[i].to,
518 mispred_str( br->entries + i),
519 br->entries[i].flags.in_tx? 'X' : '-',
520 br->entries[i].flags.abort? 'A' : '-',
521 br->entries[i].flags.cycles);
522 }
523}
524
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300525static void print_sample_brstacksym(struct perf_sample *sample,
526 struct thread *thread)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200527{
528 struct branch_stack *br = sample->branch_stack;
529 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200530 u64 i, from, to;
531
532 if (!(br && br->nr))
533 return;
534
535 for (i = 0; i < br->nr; i++) {
536
537 memset(&alf, 0, sizeof(alf));
538 memset(&alt, 0, sizeof(alt));
539 from = br->entries[i].from;
540 to = br->entries[i].to;
541
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300542 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200543 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300544 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200545
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300546 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200547 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300548 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200549
550 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
551 putchar('/');
552 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
553 printf("/%c/%c/%c/%d ",
554 mispred_str( br->entries + i),
555 br->entries[i].flags.in_tx? 'X' : '-',
556 br->entries[i].flags.abort? 'A' : '-',
557 br->entries[i].flags.cycles);
558 }
559}
560
Andi Kleen48d02a12017-02-23 15:46:34 -0800561#define MAXBB 16384UL
562
563static int grab_bb(u8 *buffer, u64 start, u64 end,
564 struct machine *machine, struct thread *thread,
565 bool *is64bit, u8 *cpumode, bool last)
566{
567 long offset, len;
568 struct addr_location al;
569 bool kernel;
570
571 if (!start || !end)
572 return 0;
573
574 kernel = machine__kernel_ip(machine, start);
575 if (kernel)
576 *cpumode = PERF_RECORD_MISC_KERNEL;
577 else
578 *cpumode = PERF_RECORD_MISC_USER;
579
580 /*
581 * Block overlaps between kernel and user.
582 * This can happen due to ring filtering
583 * On Intel CPUs the entry into the kernel is filtered,
584 * but the exit is not. Let the caller patch it up.
585 */
586 if (kernel != machine__kernel_ip(machine, end)) {
587 printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
588 start, end);
589 return -ENXIO;
590 }
591
592 memset(&al, 0, sizeof(al));
593 if (end - start > MAXBB - MAXINSN) {
594 if (last)
595 printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
596 else
597 printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
598 return 0;
599 }
600
601 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
602 if (!al.map || !al.map->dso) {
603 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
604 return 0;
605 }
606 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
607 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
608 return 0;
609 }
610
611 /* Load maps to ensure dso->is_64_bit has been updated */
612 map__load(al.map);
613
614 offset = al.map->map_ip(al.map, start);
615 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
616 end - start + MAXINSN);
617
618 *is64bit = al.map->dso->is_64_bit;
619 if (len <= 0)
620 printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
621 start, end);
622 return len;
623}
624
625static void print_jump(uint64_t ip, struct branch_entry *en,
626 struct perf_insn *x, u8 *inbuf, int len,
627 int insn)
628{
629 printf("\t%016" PRIx64 "\t%-30s\t#%s%s%s%s",
630 ip,
631 dump_insn(x, ip, inbuf, len, NULL),
632 en->flags.predicted ? " PRED" : "",
633 en->flags.mispred ? " MISPRED" : "",
634 en->flags.in_tx ? " INTX" : "",
635 en->flags.abort ? " ABORT" : "");
636 if (en->flags.cycles) {
637 printf(" %d cycles", en->flags.cycles);
638 if (insn)
639 printf(" %.2f IPC", (float)insn / en->flags.cycles);
640 }
641 putchar('\n');
642}
643
644static void print_ip_sym(struct thread *thread, u8 cpumode, int cpu,
645 uint64_t addr, struct symbol **lastsym,
646 struct perf_event_attr *attr)
647{
648 struct addr_location al;
649 int off;
650
651 memset(&al, 0, sizeof(al));
652
653 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
654 if (!al.map)
655 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
656 addr, &al);
657 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
658 return;
659
660 al.cpu = cpu;
661 al.sym = NULL;
662 if (al.map)
663 al.sym = map__find_symbol(al.map, al.addr);
664
665 if (!al.sym)
666 return;
667
668 if (al.addr < al.sym->end)
669 off = al.addr - al.sym->start;
670 else
671 off = al.addr - al.map->start - al.sym->start;
672 printf("\t%s", al.sym->name);
673 if (off)
674 printf("%+d", off);
675 putchar(':');
676 if (PRINT_FIELD(SRCLINE))
677 map__fprintf_srcline(al.map, al.addr, "\t", stdout);
678 putchar('\n');
679 *lastsym = al.sym;
680}
681
682static void print_sample_brstackinsn(struct perf_sample *sample,
683 struct thread *thread,
684 struct perf_event_attr *attr,
685 struct machine *machine)
686{
687 struct branch_stack *br = sample->branch_stack;
688 u64 start, end;
689 int i, insn, len, nr, ilen;
690 struct perf_insn x;
691 u8 buffer[MAXBB];
692 unsigned off;
693 struct symbol *lastsym = NULL;
694
695 if (!(br && br->nr))
696 return;
697 nr = br->nr;
698 if (max_blocks && nr > max_blocks + 1)
699 nr = max_blocks + 1;
700
701 x.thread = thread;
702 x.cpu = sample->cpu;
703
704 putchar('\n');
705
706 /* Handle first from jump, of which we don't know the entry. */
707 len = grab_bb(buffer, br->entries[nr-1].from,
708 br->entries[nr-1].from,
709 machine, thread, &x.is64bit, &x.cpumode, false);
710 if (len > 0) {
711 print_ip_sym(thread, x.cpumode, x.cpu,
712 br->entries[nr - 1].from, &lastsym, attr);
713 print_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
714 &x, buffer, len, 0);
715 }
716
717 /* Print all blocks */
718 for (i = nr - 2; i >= 0; i--) {
719 if (br->entries[i].from || br->entries[i].to)
720 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
721 br->entries[i].from,
722 br->entries[i].to);
723 start = br->entries[i + 1].to;
724 end = br->entries[i].from;
725
726 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
727 /* Patch up missing kernel transfers due to ring filters */
728 if (len == -ENXIO && i > 0) {
729 end = br->entries[--i].from;
730 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
731 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
732 }
733 if (len <= 0)
734 continue;
735
736 insn = 0;
737 for (off = 0;; off += ilen) {
738 uint64_t ip = start + off;
739
740 print_ip_sym(thread, x.cpumode, x.cpu, ip, &lastsym, attr);
741 if (ip == end) {
742 print_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn);
743 break;
744 } else {
745 printf("\t%016" PRIx64 "\t%s\n", ip,
746 dump_insn(&x, ip, buffer + off, len - off, &ilen));
747 if (ilen == 0)
748 break;
749 insn++;
750 }
751 }
752 }
753
754 /*
755 * Hit the branch? In this case we are already done, and the target
756 * has not been executed yet.
757 */
758 if (br->entries[0].from == sample->ip)
759 return;
760 if (br->entries[0].flags.abort)
761 return;
762
763 /*
764 * Print final block upto sample
765 */
766 start = br->entries[0].to;
767 end = sample->ip;
768 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
769 print_ip_sym(thread, x.cpumode, x.cpu, start, &lastsym, attr);
770 if (len <= 0) {
771 /* Print at least last IP if basic block did not work */
772 len = grab_bb(buffer, sample->ip, sample->ip,
773 machine, thread, &x.is64bit, &x.cpumode, false);
774 if (len <= 0)
775 return;
776
777 printf("\t%016" PRIx64 "\t%s\n", sample->ip,
778 dump_insn(&x, sample->ip, buffer, len, NULL));
779 return;
780 }
781 for (off = 0; off <= end - start; off += ilen) {
782 printf("\t%016" PRIx64 "\t%s\n", start + off,
783 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
784 if (ilen == 0)
785 break;
786 }
787}
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200788
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300789static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600790 struct thread *thread,
791 struct perf_event_attr *attr)
792{
793 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600794
795 printf("%16" PRIx64, sample->addr);
796
797 if (!sample_addr_correlates_sym(attr))
798 return;
799
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300800 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600801
802 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900803 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900804 if (PRINT_FIELD(SYMOFFSET))
805 symbol__fprintf_symname_offs(al.sym, &al, stdout);
806 else
807 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600808 }
809
810 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900811 printf(" (");
812 map__fprintf_dsoname(al.map, stdout);
813 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600814 }
815}
816
Adrian Huntere2167082016-06-23 16:40:58 +0300817static void print_sample_callindent(struct perf_sample *sample,
818 struct perf_evsel *evsel,
819 struct thread *thread,
820 struct addr_location *al)
821{
822 struct perf_event_attr *attr = &evsel->attr;
823 size_t depth = thread_stack__depth(thread);
824 struct addr_location addr_al;
825 const char *name = NULL;
826 static int spacing;
827 int len = 0;
828 u64 ip = 0;
829
830 /*
831 * The 'return' has already been popped off the stack so the depth has
832 * to be adjusted to match the 'call'.
833 */
834 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
835 depth += 1;
836
837 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
838 if (sample_addr_correlates_sym(attr)) {
839 thread__resolve(thread, &addr_al, sample);
840 if (addr_al.sym)
841 name = addr_al.sym->name;
842 else
843 ip = sample->addr;
844 } else {
845 ip = sample->addr;
846 }
847 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
848 if (al->sym)
849 name = al->sym->name;
850 else
851 ip = sample->ip;
852 }
853
854 if (name)
855 len = printf("%*s%s", (int)depth * 4, "", name);
856 else if (ip)
857 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
858
859 if (len < 0)
860 return;
861
862 /*
863 * Try to keep the output length from changing frequently so that the
864 * output lines up more nicely.
865 */
866 if (len > spacing || (len && len < spacing - 52))
867 spacing = round_up(len + 4, 32);
868
869 if (len < spacing)
870 printf("%*s", spacing - len, "");
871}
872
Andi Kleen224e2c92016-10-07 16:42:27 +0300873static void print_insn(struct perf_sample *sample,
Andi Kleen48d02a12017-02-23 15:46:34 -0800874 struct perf_event_attr *attr,
875 struct thread *thread,
876 struct machine *machine)
Andi Kleen224e2c92016-10-07 16:42:27 +0300877{
878 if (PRINT_FIELD(INSNLEN))
879 printf(" ilen: %d", sample->insn_len);
880 if (PRINT_FIELD(INSN)) {
881 int i;
882
883 printf(" insn:");
884 for (i = 0; i < sample->insn_len; i++)
885 printf(" %02x", (unsigned char)sample->insn[i]);
886 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800887 if (PRINT_FIELD(BRSTACKINSN))
888 print_sample_brstackinsn(sample, thread, attr, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300889}
890
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300891static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +0900892 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200893 struct thread *thread,
Andi Kleen48d02a12017-02-23 15:46:34 -0800894 struct addr_location *al,
895 struct machine *machine)
Akihiro Nagai95582592012-01-30 13:43:09 +0900896{
897 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300898 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900899
Adrian Huntere2167082016-06-23 16:40:58 +0300900 if (PRINT_FIELD(CALLINDENT))
901 print_sample_callindent(sample, evsel, thread, al);
902
Akihiro Nagai95582592012-01-30 13:43:09 +0900903 /* print branch_from information */
904 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300905 unsigned int print_opts = output[attr->type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -0700906 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300907
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300908 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700909 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300910 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700911 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300912
913 if (cursor == NULL) {
914 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300915 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300916 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300917 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300918 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300919 } else
920 putchar('\n');
921
922 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +0900923 }
924
Akihiro Nagai95582592012-01-30 13:43:09 +0900925 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300926 if (PRINT_FIELD(ADDR) ||
927 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300928 !output[attr->type].user_set)) {
929 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300930 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300931 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900932
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300933 if (print_srcline_last)
934 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
935
Andi Kleen48d02a12017-02-23 15:46:34 -0800936 print_insn(sample, attr, thread, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300937
Akihiro Nagai95582592012-01-30 13:43:09 +0900938 printf("\n");
939}
940
Adrian Hunter055cd332016-06-23 16:40:56 +0300941static struct {
942 u32 flags;
943 const char *name;
944} sample_flags[] = {
945 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
946 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
947 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
948 {PERF_IP_FLAG_BRANCH, "jmp"},
949 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
950 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
951 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
952 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
953 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
954 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
955 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
956 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
957 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
958 {0, NULL}
959};
960
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300961static void print_sample_flags(u32 flags)
962{
963 const char *chars = PERF_IP_FLAG_CHARS;
964 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +0300965 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
966 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300967 char str[33];
968 int i, pos = 0;
969
Adrian Hunter055cd332016-06-23 16:40:56 +0300970 for (i = 0; sample_flags[i].name ; i++) {
971 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
972 name = sample_flags[i].name;
973 break;
974 }
975 }
976
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300977 for (i = 0; i < n; i++, flags >>= 1) {
978 if (flags & 1)
979 str[pos++] = chars[i];
980 }
981 for (; i < 32; i++, flags >>= 1) {
982 if (flags & 1)
983 str[pos++] = '?';
984 }
985 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +0300986
987 if (name)
988 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
989 else
990 printf(" %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300991}
992
Wang Nan30372f02016-02-24 11:20:45 +0000993struct printer_data {
994 int line_no;
995 bool hit_nul;
996 bool is_printable;
997};
998
999static void
1000print_sample_bpf_output_printer(enum binary_printer_ops op,
1001 unsigned int val,
1002 void *extra)
1003{
1004 unsigned char ch = (unsigned char)val;
1005 struct printer_data *printer_data = extra;
1006
1007 switch (op) {
1008 case BINARY_PRINT_DATA_BEGIN:
1009 printf("\n");
1010 break;
1011 case BINARY_PRINT_LINE_BEGIN:
1012 printf("%17s", !printer_data->line_no ? "BPF output:" :
1013 " ");
1014 break;
1015 case BINARY_PRINT_ADDR:
1016 printf(" %04x:", val);
1017 break;
1018 case BINARY_PRINT_NUM_DATA:
1019 printf(" %02x", val);
1020 break;
1021 case BINARY_PRINT_NUM_PAD:
1022 printf(" ");
1023 break;
1024 case BINARY_PRINT_SEP:
1025 printf(" ");
1026 break;
1027 case BINARY_PRINT_CHAR_DATA:
1028 if (printer_data->hit_nul && ch)
1029 printer_data->is_printable = false;
1030
1031 if (!isprint(ch)) {
1032 printf("%c", '.');
1033
1034 if (!printer_data->is_printable)
1035 break;
1036
1037 if (ch == '\0')
1038 printer_data->hit_nul = true;
1039 else
1040 printer_data->is_printable = false;
1041 } else {
1042 printf("%c", ch);
1043 }
1044 break;
1045 case BINARY_PRINT_CHAR_PAD:
1046 printf(" ");
1047 break;
1048 case BINARY_PRINT_LINE_END:
1049 printf("\n");
1050 printer_data->line_no++;
1051 break;
1052 case BINARY_PRINT_DATA_END:
1053 default:
1054 break;
1055 }
1056}
1057
1058static void print_sample_bpf_output(struct perf_sample *sample)
1059{
1060 unsigned int nr_bytes = sample->raw_size;
1061 struct printer_data printer_data = {0, false, true};
1062
1063 print_binary(sample->raw_data, nr_bytes, 8,
1064 print_sample_bpf_output_printer, &printer_data);
1065
1066 if (printer_data.is_printable && printer_data.hit_nul)
1067 printf("%17s \"%s\"\n", "BPF string:",
1068 (char *)(sample->raw_data));
1069}
1070
Jiri Olsa809e9422015-11-26 18:55:21 +01001071struct perf_script {
1072 struct perf_tool tool;
1073 struct perf_session *session;
1074 bool show_task_events;
1075 bool show_mmap_events;
1076 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301077 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001078 bool allocated;
1079 struct cpu_map *cpus;
1080 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001081 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001082 const char *time_str;
1083 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001084};
1085
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001086static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1087{
1088 struct perf_evsel *evsel;
1089 int max = 0;
1090
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001091 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001092 int len = strlen(perf_evsel__name(evsel));
1093
1094 max = MAX(len, max);
1095 }
1096
1097 return max;
1098}
1099
Jiri Olsac19ac912016-02-24 09:46:54 +01001100static size_t data_src__printf(u64 data_src)
1101{
1102 struct mem_info mi = { .data_src.val = data_src };
1103 char decode[100];
1104 char out[100];
1105 static int maxlen;
1106 int len;
1107
1108 perf_script__meminfo_scnprintf(decode, 100, &mi);
1109
1110 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1111 if (maxlen < len)
1112 maxlen = len;
1113
1114 return printf("%-*s", maxlen, out);
1115}
1116
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001117static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001118 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001119 struct addr_location *al,
1120 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001121{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001122 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001123 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -07001124
David Ahern2c9e45f72011-03-17 10:03:21 -06001125 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001126 return;
1127
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001128 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -07001129
Jiri Olsa535aeaa2014-08-25 16:45:42 +02001130 if (PRINT_FIELD(PERIOD))
1131 printf("%10" PRIu64 " ", sample->period);
1132
Namhyung Kime944d3d2013-11-18 14:34:52 +09001133 if (PRINT_FIELD(EVNAME)) {
1134 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001135
1136 if (!script->name_width)
1137 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1138
1139 printf("%*s: ", script->name_width,
1140 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001141 }
1142
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001143 if (print_flags)
1144 print_sample_flags(sample->flags);
1145
Akihiro Nagai95582592012-01-30 13:43:09 +09001146 if (is_bts_event(attr)) {
Andi Kleen48d02a12017-02-23 15:46:34 -08001147 print_sample_bts(sample, evsel, thread, al, machine);
Akihiro Nagai95582592012-01-30 13:43:09 +09001148 return;
1149 }
1150
David Ahern745f43e2011-03-09 22:23:26 -07001151 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001152 event_format__print(evsel->tp_format, sample->cpu,
1153 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -06001154 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001155 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -06001156
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001157 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +01001158 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001159
1160 if (PRINT_FIELD(WEIGHT))
1161 printf("%16" PRIu64, sample->weight);
1162
David Ahern787bef12011-05-27 14:28:43 -06001163 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001164 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001165
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001166 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001167 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001168 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001169 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001170
1171 putchar(cursor ? '\n' : ' ');
1172 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001173 }
1174
Stephane Eranianfc36f942015-08-31 18:41:10 +02001175 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001176 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001177
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001178 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001179 print_sample_brstack(sample);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001180 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001181 print_sample_brstacksym(sample, thread);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001182
Wang Nan30372f02016-02-24 11:20:45 +00001183 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1184 print_sample_bpf_output(sample);
Andi Kleen48d02a12017-02-23 15:46:34 -08001185 print_insn(sample, attr, thread, machine);
David Ahernc70c94b2011-03-09 22:23:25 -07001186 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001187}
1188
Tom Zanussi956ffd02009-11-25 01:15:46 -06001189static struct scripting_ops *scripting_ops;
1190
Jiri Olsa36e33c52016-01-06 11:49:56 +01001191static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1192{
1193 int nthreads = thread_map__nr(counter->threads);
1194 int ncpus = perf_evsel__nr_cpus(counter);
1195 int cpu, thread;
1196 static int header_printed;
1197
1198 if (counter->system_wide)
1199 nthreads = 1;
1200
1201 if (!header_printed) {
1202 printf("%3s %8s %15s %15s %15s %15s %s\n",
1203 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1204 header_printed = 1;
1205 }
1206
1207 for (thread = 0; thread < nthreads; thread++) {
1208 for (cpu = 0; cpu < ncpus; cpu++) {
1209 struct perf_counts_values *counts;
1210
1211 counts = perf_counts(counter->counts, cpu, thread);
1212
1213 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1214 counter->cpus->map[cpu],
1215 thread_map__pid(counter->threads, thread),
1216 counts->val,
1217 counts->ena,
1218 counts->run,
1219 tstamp,
1220 perf_evsel__name(counter));
1221 }
1222 }
1223}
1224
Jiri Olsae099eba2016-01-05 22:09:09 +01001225static void process_stat(struct perf_evsel *counter, u64 tstamp)
1226{
1227 if (scripting_ops && scripting_ops->process_stat)
1228 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001229 else
1230 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001231}
1232
1233static void process_stat_interval(u64 tstamp)
1234{
1235 if (scripting_ops && scripting_ops->process_stat_interval)
1236 scripting_ops->process_stat_interval(tstamp);
1237}
1238
Tom Zanussi956ffd02009-11-25 01:15:46 -06001239static void setup_scripting(void)
1240{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001241 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001242 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001243}
1244
Adrian Hunterd445dd22014-08-15 22:08:37 +03001245static int flush_scripting(void)
1246{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001247 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001248}
1249
Tom Zanussi956ffd02009-11-25 01:15:46 -06001250static int cleanup_scripting(void)
1251{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001252 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001253
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001254 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001255}
1256
Jiri Olsa809e9422015-11-26 18:55:21 +01001257static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001258 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001259 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001260 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001261 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001262{
Jiri Olsa809e9422015-11-26 18:55:21 +01001263 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001264 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001265
David Aherna91f4c42016-11-29 10:15:43 -07001266 if (perf_time__skip_sample(&scr->ptime, sample->time))
1267 return 0;
1268
David Ahern1424dc92011-03-09 22:23:28 -07001269 if (debug_mode) {
1270 if (sample->time < last_timestamp) {
1271 pr_err("Samples misordered, previous: %" PRIu64
1272 " this: %" PRIu64 "\n", last_timestamp,
1273 sample->time);
1274 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001275 }
David Ahern1424dc92011-03-09 22:23:28 -07001276 last_timestamp = sample->time;
1277 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001278 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001279
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001280 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001281 pr_err("problem processing %d event, skipping it.\n",
1282 event->header.type);
1283 return -1;
1284 }
1285
1286 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001287 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001288
Anton Blanchard5d67be92011-07-04 21:57:50 +10001289 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001290 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001291
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001292 if (scripting_ops)
1293 scripting_ops->process_event(event, sample, evsel, &al);
1294 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001295 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001296
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001297out_put:
1298 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001299 return 0;
1300}
1301
Adrian Hunter7ea95722013-11-01 15:51:30 +02001302static int process_attr(struct perf_tool *tool, union perf_event *event,
1303 struct perf_evlist **pevlist)
1304{
1305 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1306 struct perf_evlist *evlist;
1307 struct perf_evsel *evsel, *pos;
1308 int err;
1309
1310 err = perf_event__process_attr(tool, event, pevlist);
1311 if (err)
1312 return err;
1313
1314 evlist = *pevlist;
1315 evsel = perf_evlist__last(*pevlist);
1316
1317 if (evsel->attr.type >= PERF_TYPE_MAX)
1318 return 0;
1319
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001320 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001321 if (pos->attr.type == evsel->attr.type && pos != evsel)
1322 return 0;
1323 }
1324
1325 set_print_ip_opts(&evsel->attr);
1326
Jiri Olsad2b5a312015-10-16 12:41:25 +02001327 if (evsel->attr.sample_type)
1328 err = perf_evsel__check_attr(evsel, scr->session);
1329
1330 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001331}
1332
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001333static int process_comm_event(struct perf_tool *tool,
1334 union perf_event *event,
1335 struct perf_sample *sample,
1336 struct machine *machine)
1337{
1338 struct thread *thread;
1339 struct perf_script *script = container_of(tool, struct perf_script, tool);
1340 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001341 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001342 int ret = -1;
1343
1344 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1345 if (thread == NULL) {
1346 pr_debug("problem processing COMM event, skipping it.\n");
1347 return -1;
1348 }
1349
1350 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1351 goto out;
1352
1353 if (!evsel->attr.sample_id_all) {
1354 sample->cpu = 0;
1355 sample->time = 0;
1356 sample->tid = event->comm.tid;
1357 sample->pid = event->comm.pid;
1358 }
1359 print_sample_start(sample, thread, evsel);
1360 perf_event__fprintf(event, stdout);
1361 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001362out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001363 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001364 return ret;
1365}
1366
Hari Bathini96a44bb2017-03-08 02:12:06 +05301367static int process_namespaces_event(struct perf_tool *tool,
1368 union perf_event *event,
1369 struct perf_sample *sample,
1370 struct machine *machine)
1371{
1372 struct thread *thread;
1373 struct perf_script *script = container_of(tool, struct perf_script, tool);
1374 struct perf_session *session = script->session;
1375 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1376 int ret = -1;
1377
1378 thread = machine__findnew_thread(machine, event->namespaces.pid,
1379 event->namespaces.tid);
1380 if (thread == NULL) {
1381 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1382 return -1;
1383 }
1384
1385 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1386 goto out;
1387
1388 if (!evsel->attr.sample_id_all) {
1389 sample->cpu = 0;
1390 sample->time = 0;
1391 sample->tid = event->namespaces.tid;
1392 sample->pid = event->namespaces.pid;
1393 }
1394 print_sample_start(sample, thread, evsel);
1395 perf_event__fprintf(event, stdout);
1396 ret = 0;
1397out:
1398 thread__put(thread);
1399 return ret;
1400}
1401
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001402static int process_fork_event(struct perf_tool *tool,
1403 union perf_event *event,
1404 struct perf_sample *sample,
1405 struct machine *machine)
1406{
1407 struct thread *thread;
1408 struct perf_script *script = container_of(tool, struct perf_script, tool);
1409 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001410 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001411
1412 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1413 return -1;
1414
1415 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1416 if (thread == NULL) {
1417 pr_debug("problem processing FORK event, skipping it.\n");
1418 return -1;
1419 }
1420
1421 if (!evsel->attr.sample_id_all) {
1422 sample->cpu = 0;
1423 sample->time = event->fork.time;
1424 sample->tid = event->fork.tid;
1425 sample->pid = event->fork.pid;
1426 }
1427 print_sample_start(sample, thread, evsel);
1428 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001429 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001430
1431 return 0;
1432}
1433static int process_exit_event(struct perf_tool *tool,
1434 union perf_event *event,
1435 struct perf_sample *sample,
1436 struct machine *machine)
1437{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001438 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001439 struct thread *thread;
1440 struct perf_script *script = container_of(tool, struct perf_script, tool);
1441 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001442 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001443
1444 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1445 if (thread == NULL) {
1446 pr_debug("problem processing EXIT event, skipping it.\n");
1447 return -1;
1448 }
1449
1450 if (!evsel->attr.sample_id_all) {
1451 sample->cpu = 0;
1452 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001453 sample->tid = event->fork.tid;
1454 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001455 }
1456 print_sample_start(sample, thread, evsel);
1457 perf_event__fprintf(event, stdout);
1458
1459 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001460 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001461
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001462 thread__put(thread);
1463 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001464}
1465
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001466static int process_mmap_event(struct perf_tool *tool,
1467 union perf_event *event,
1468 struct perf_sample *sample,
1469 struct machine *machine)
1470{
1471 struct thread *thread;
1472 struct perf_script *script = container_of(tool, struct perf_script, tool);
1473 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001474 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001475
1476 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1477 return -1;
1478
1479 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1480 if (thread == NULL) {
1481 pr_debug("problem processing MMAP event, skipping it.\n");
1482 return -1;
1483 }
1484
1485 if (!evsel->attr.sample_id_all) {
1486 sample->cpu = 0;
1487 sample->time = 0;
1488 sample->tid = event->mmap.tid;
1489 sample->pid = event->mmap.pid;
1490 }
1491 print_sample_start(sample, thread, evsel);
1492 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001493 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001494 return 0;
1495}
1496
1497static int process_mmap2_event(struct perf_tool *tool,
1498 union perf_event *event,
1499 struct perf_sample *sample,
1500 struct machine *machine)
1501{
1502 struct thread *thread;
1503 struct perf_script *script = container_of(tool, struct perf_script, tool);
1504 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001505 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001506
1507 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1508 return -1;
1509
1510 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1511 if (thread == NULL) {
1512 pr_debug("problem processing MMAP2 event, skipping it.\n");
1513 return -1;
1514 }
1515
1516 if (!evsel->attr.sample_id_all) {
1517 sample->cpu = 0;
1518 sample->time = 0;
1519 sample->tid = event->mmap2.tid;
1520 sample->pid = event->mmap2.pid;
1521 }
1522 print_sample_start(sample, thread, evsel);
1523 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001524 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001525 return 0;
1526}
1527
Adrian Hunter7c148982015-07-21 12:44:06 +03001528static int process_switch_event(struct perf_tool *tool,
1529 union perf_event *event,
1530 struct perf_sample *sample,
1531 struct machine *machine)
1532{
1533 struct thread *thread;
1534 struct perf_script *script = container_of(tool, struct perf_script, tool);
1535 struct perf_session *session = script->session;
1536 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1537
1538 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1539 return -1;
1540
1541 thread = machine__findnew_thread(machine, sample->pid,
1542 sample->tid);
1543 if (thread == NULL) {
1544 pr_debug("problem processing SWITCH event, skipping it.\n");
1545 return -1;
1546 }
1547
1548 print_sample_start(sample, thread, evsel);
1549 perf_event__fprintf(event, stdout);
1550 thread__put(thread);
1551 return 0;
1552}
1553
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001554static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001555{
1556 session_done = 1;
1557}
1558
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001559static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001560{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001561 int ret;
1562
Tom Zanussic239da32010-04-01 23:59:18 -05001563 signal(SIGINT, sig_handler);
1564
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001565 /* override event processing functions */
1566 if (script->show_task_events) {
1567 script->tool.comm = process_comm_event;
1568 script->tool.fork = process_fork_event;
1569 script->tool.exit = process_exit_event;
1570 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001571 if (script->show_mmap_events) {
1572 script->tool.mmap = process_mmap_event;
1573 script->tool.mmap2 = process_mmap2_event;
1574 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001575 if (script->show_switch_events)
1576 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301577 if (script->show_namespace_events)
1578 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001579
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001580 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001581
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001582 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001583 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001584
1585 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001586}
1587
Tom Zanussi956ffd02009-11-25 01:15:46 -06001588struct script_spec {
1589 struct list_head node;
1590 struct scripting_ops *ops;
1591 char spec[0];
1592};
1593
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001594static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001595
1596static struct script_spec *script_spec__new(const char *spec,
1597 struct scripting_ops *ops)
1598{
1599 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1600
1601 if (s != NULL) {
1602 strcpy(s->spec, spec);
1603 s->ops = ops;
1604 }
1605
1606 return s;
1607}
1608
Tom Zanussi956ffd02009-11-25 01:15:46 -06001609static void script_spec__add(struct script_spec *s)
1610{
1611 list_add_tail(&s->node, &script_specs);
1612}
1613
1614static struct script_spec *script_spec__find(const char *spec)
1615{
1616 struct script_spec *s;
1617
1618 list_for_each_entry(s, &script_specs, node)
1619 if (strcasecmp(s->spec, spec) == 0)
1620 return s;
1621 return NULL;
1622}
1623
Tom Zanussi956ffd02009-11-25 01:15:46 -06001624int script_spec_register(const char *spec, struct scripting_ops *ops)
1625{
1626 struct script_spec *s;
1627
1628 s = script_spec__find(spec);
1629 if (s)
1630 return -1;
1631
Taeung Song8560bae2016-02-26 00:13:10 +09001632 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001633 if (!s)
1634 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001635 else
1636 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001637
1638 return 0;
1639}
1640
1641static struct scripting_ops *script_spec__lookup(const char *spec)
1642{
1643 struct script_spec *s = script_spec__find(spec);
1644 if (!s)
1645 return NULL;
1646
1647 return s->ops;
1648}
1649
1650static void list_available_languages(void)
1651{
1652 struct script_spec *s;
1653
1654 fprintf(stderr, "\n");
1655 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001656 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001657
1658 list_for_each_entry(s, &script_specs, node)
1659 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1660
1661 fprintf(stderr, "\n");
1662}
1663
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001664static int parse_scriptname(const struct option *opt __maybe_unused,
1665 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001666{
1667 char spec[PATH_MAX];
1668 const char *script, *ext;
1669 int len;
1670
Tom Zanussif526d682010-01-27 02:27:52 -06001671 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001672 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001673 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001674 }
1675
1676 script = strchr(str, ':');
1677 if (script) {
1678 len = script - str;
1679 if (len >= PATH_MAX) {
1680 fprintf(stderr, "invalid language specifier");
1681 return -1;
1682 }
1683 strncpy(spec, str, len);
1684 spec[len] = '\0';
1685 scripting_ops = script_spec__lookup(spec);
1686 if (!scripting_ops) {
1687 fprintf(stderr, "invalid language specifier");
1688 return -1;
1689 }
1690 script++;
1691 } else {
1692 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001693 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001694 if (!ext) {
1695 fprintf(stderr, "invalid script extension");
1696 return -1;
1697 }
1698 scripting_ops = script_spec__lookup(++ext);
1699 if (!scripting_ops) {
1700 fprintf(stderr, "invalid script extension");
1701 return -1;
1702 }
1703 }
1704
1705 script_name = strdup(script);
1706
1707 return 0;
1708}
1709
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001710static int parse_output_fields(const struct option *opt __maybe_unused,
1711 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001712{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001713 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001714 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001715 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001716 int rc = 0;
1717 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001718 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001719
1720 if (!str)
1721 return -ENOMEM;
1722
David Ahern2c9e45f72011-03-17 10:03:21 -06001723 /* first word can state for which event type the user is specifying
1724 * the fields. If no type exists, the specified fields apply to all
1725 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001726 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001727 tok = strchr(str, ':');
1728 if (tok) {
1729 *tok = '\0';
1730 tok++;
1731 if (!strcmp(str, "hw"))
1732 type = PERF_TYPE_HARDWARE;
1733 else if (!strcmp(str, "sw"))
1734 type = PERF_TYPE_SOFTWARE;
1735 else if (!strcmp(str, "trace"))
1736 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001737 else if (!strcmp(str, "raw"))
1738 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001739 else if (!strcmp(str, "break"))
1740 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001741 else {
1742 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001743 rc = -EINVAL;
1744 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001745 }
1746
1747 if (output[type].user_set)
1748 pr_warning("Overriding previous field request for %s events.\n",
1749 event_type(type));
1750
1751 output[type].fields = 0;
1752 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001753 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001754
1755 } else {
1756 tok = str;
1757 if (strlen(str) == 0) {
1758 fprintf(stderr,
1759 "Cannot set fields to 'none' for all event types.\n");
1760 rc = -EINVAL;
1761 goto out;
1762 }
1763
1764 if (output_set_by_user())
1765 pr_warning("Overriding previous field request for all events.\n");
1766
1767 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1768 output[j].fields = 0;
1769 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001770 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001771 }
David Ahern1424dc92011-03-09 22:23:28 -07001772 }
1773
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001774 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
David Ahern745f43e2011-03-09 22:23:26 -07001775 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001776 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001777 break;
David Ahern745f43e2011-03-09 22:23:26 -07001778 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001779 if (i == imax && strcmp(tok, "flags") == 0) {
1780 print_flags = true;
1781 continue;
1782 }
David Ahern745f43e2011-03-09 22:23:26 -07001783 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001784 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001785 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001786 goto out;
1787 }
1788
1789 if (type == -1) {
1790 /* add user option to all events types for
1791 * which it is valid
1792 */
1793 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1794 if (output[j].invalid_fields & all_output_options[i].field) {
1795 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1796 all_output_options[i].str, event_type(j));
1797 } else
1798 output[j].fields |= all_output_options[i].field;
1799 }
1800 } else {
1801 if (output[type].invalid_fields & all_output_options[i].field) {
1802 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1803 all_output_options[i].str, event_type(type));
1804
1805 rc = -EINVAL;
1806 goto out;
1807 }
1808 output[type].fields |= all_output_options[i].field;
1809 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001810 }
1811
1812 if (type >= 0) {
1813 if (output[type].fields == 0) {
1814 pr_debug("No fields requested for %s type. "
1815 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001816 }
David Ahern1424dc92011-03-09 22:23:28 -07001817 }
David Ahern745f43e2011-03-09 22:23:26 -07001818
David Ahern2c9e45f72011-03-17 10:03:21 -06001819out:
David Ahern745f43e2011-03-09 22:23:26 -07001820 free(str);
1821 return rc;
1822}
1823
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001824/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1825static int is_directory(const char *base_path, const struct dirent *dent)
1826{
1827 char path[PATH_MAX];
1828 struct stat st;
1829
1830 sprintf(path, "%s/%s", base_path, dent->d_name);
1831 if (stat(path, &st))
1832 return 0;
1833
1834 return S_ISDIR(st.st_mode);
1835}
1836
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001837#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1838 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1839 if ((lang_dirent->d_type == DT_DIR || \
1840 (lang_dirent->d_type == DT_UNKNOWN && \
1841 is_directory(scripts_path, lang_dirent))) && \
1842 (strcmp(lang_dirent->d_name, ".")) && \
1843 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001844
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001845#define for_each_script(lang_path, lang_dir, script_dirent) \
1846 while ((script_dirent = readdir(lang_dir)) != NULL) \
1847 if (script_dirent->d_type != DT_DIR && \
1848 (script_dirent->d_type != DT_UNKNOWN || \
1849 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001850
1851
1852#define RECORD_SUFFIX "-record"
1853#define REPORT_SUFFIX "-report"
1854
1855struct script_desc {
1856 struct list_head node;
1857 char *name;
1858 char *half_liner;
1859 char *args;
1860};
1861
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001862static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001863
1864static struct script_desc *script_desc__new(const char *name)
1865{
1866 struct script_desc *s = zalloc(sizeof(*s));
1867
Tom Zanussib5b87312010-11-10 08:16:51 -06001868 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001869 s->name = strdup(name);
1870
1871 return s;
1872}
1873
1874static void script_desc__delete(struct script_desc *s)
1875{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001876 zfree(&s->name);
1877 zfree(&s->half_liner);
1878 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001879 free(s);
1880}
1881
1882static void script_desc__add(struct script_desc *s)
1883{
1884 list_add_tail(&s->node, &script_descs);
1885}
1886
1887static struct script_desc *script_desc__find(const char *name)
1888{
1889 struct script_desc *s;
1890
1891 list_for_each_entry(s, &script_descs, node)
1892 if (strcasecmp(s->name, name) == 0)
1893 return s;
1894 return NULL;
1895}
1896
1897static struct script_desc *script_desc__findnew(const char *name)
1898{
1899 struct script_desc *s = script_desc__find(name);
1900
1901 if (s)
1902 return s;
1903
1904 s = script_desc__new(name);
1905 if (!s)
1906 goto out_delete_desc;
1907
1908 script_desc__add(s);
1909
1910 return s;
1911
1912out_delete_desc:
1913 script_desc__delete(s);
1914
1915 return NULL;
1916}
1917
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001918static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001919{
1920 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001921 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001922
1923 if (strlen(str) > suffix_len) {
1924 p = str + strlen(str) - suffix_len;
1925 if (!strncmp(p, suffix, suffix_len))
1926 return p;
1927 }
1928
1929 return NULL;
1930}
1931
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001932static int read_script_info(struct script_desc *desc, const char *filename)
1933{
1934 char line[BUFSIZ], *p;
1935 FILE *fp;
1936
1937 fp = fopen(filename, "r");
1938 if (!fp)
1939 return -1;
1940
1941 while (fgets(line, sizeof(line), fp)) {
1942 p = ltrim(line);
1943 if (strlen(p) == 0)
1944 continue;
1945 if (*p != '#')
1946 continue;
1947 p++;
1948 if (strlen(p) && *p == '!')
1949 continue;
1950
1951 p = ltrim(p);
1952 if (strlen(p) && p[strlen(p) - 1] == '\n')
1953 p[strlen(p) - 1] = '\0';
1954
1955 if (!strncmp(p, "description:", strlen("description:"))) {
1956 p += strlen("description:");
1957 desc->half_liner = strdup(ltrim(p));
1958 continue;
1959 }
1960
1961 if (!strncmp(p, "args:", strlen("args:"))) {
1962 p += strlen("args:");
1963 desc->args = strdup(ltrim(p));
1964 continue;
1965 }
1966 }
1967
1968 fclose(fp);
1969
1970 return 0;
1971}
1972
Robert Richter38efb532011-11-25 11:38:40 +01001973static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1974{
1975 char *script_root, *str;
1976
1977 script_root = strdup(script_dirent->d_name);
1978 if (!script_root)
1979 return NULL;
1980
1981 str = (char *)ends_with(script_root, suffix);
1982 if (!str) {
1983 free(script_root);
1984 return NULL;
1985 }
1986
1987 *str = '\0';
1988 return script_root;
1989}
1990
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001991static int list_available_scripts(const struct option *opt __maybe_unused,
1992 const char *s __maybe_unused,
1993 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001994{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001995 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001996 char scripts_path[MAXPATHLEN];
1997 DIR *scripts_dir, *lang_dir;
1998 char script_path[MAXPATHLEN];
1999 char lang_path[MAXPATHLEN];
2000 struct script_desc *desc;
2001 char first_half[BUFSIZ];
2002 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002003
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002004 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002005
2006 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002007 if (!scripts_dir) {
2008 fprintf(stdout,
2009 "open(%s) failed.\n"
2010 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2011 scripts_path);
2012 exit(-1);
2013 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002014
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002015 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002016 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002017 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002018 lang_dir = opendir(lang_path);
2019 if (!lang_dir)
2020 continue;
2021
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002022 for_each_script(lang_path, lang_dir, script_dirent) {
2023 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002024 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002025 desc = script_desc__findnew(script_root);
2026 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002027 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002028 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002029 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002030 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002031 }
2032 }
2033
2034 fprintf(stdout, "List of available trace scripts:\n");
2035 list_for_each_entry(desc, &script_descs, node) {
2036 sprintf(first_half, "%s %s", desc->name,
2037 desc->args ? desc->args : "");
2038 fprintf(stdout, " %-36s %s\n", first_half,
2039 desc->half_liner ? desc->half_liner : "");
2040 }
2041
2042 exit(0);
2043}
2044
Feng Tange5f37052012-09-07 16:42:26 +08002045/*
Feng Tang49e639e2012-10-30 11:56:03 +08002046 * Some scripts specify the required events in their "xxx-record" file,
2047 * this function will check if the events in perf.data match those
2048 * mentioned in the "xxx-record".
2049 *
2050 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2051 * which is covered well now. And new parsing code should be added to
2052 * cover the future complexing formats like event groups etc.
2053 */
2054static int check_ev_match(char *dir_name, char *scriptname,
2055 struct perf_session *session)
2056{
2057 char filename[MAXPATHLEN], evname[128];
2058 char line[BUFSIZ], *p;
2059 struct perf_evsel *pos;
2060 int match, len;
2061 FILE *fp;
2062
2063 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2064
2065 fp = fopen(filename, "r");
2066 if (!fp)
2067 return -1;
2068
2069 while (fgets(line, sizeof(line), fp)) {
2070 p = ltrim(line);
2071 if (*p == '#')
2072 continue;
2073
2074 while (strlen(p)) {
2075 p = strstr(p, "-e");
2076 if (!p)
2077 break;
2078
2079 p += 2;
2080 p = ltrim(p);
2081 len = strcspn(p, " \t");
2082 if (!len)
2083 break;
2084
2085 snprintf(evname, len + 1, "%s", p);
2086
2087 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002088 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002089 if (!strcmp(perf_evsel__name(pos), evname)) {
2090 match = 1;
2091 break;
2092 }
2093 }
2094
2095 if (!match) {
2096 fclose(fp);
2097 return -1;
2098 }
2099 }
2100 }
2101
2102 fclose(fp);
2103 return 0;
2104}
2105
2106/*
Feng Tange5f37052012-09-07 16:42:26 +08002107 * Return -1 if none is found, otherwise the actual scripts number.
2108 *
2109 * Currently the only user of this function is the script browser, which
2110 * will list all statically runnable scripts, select one, execute it and
2111 * show the output in a perf browser.
2112 */
2113int find_scripts(char **scripts_array, char **scripts_path_array)
2114{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002115 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002116 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002117 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002118 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002119 struct perf_data_file file = {
2120 .path = input_name,
2121 .mode = PERF_DATA_MODE_READ,
2122 };
Feng Tange5f37052012-09-07 16:42:26 +08002123 char *temp;
2124 int i = 0;
2125
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002126 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002127 if (!session)
2128 return -1;
2129
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002130 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002131
2132 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002133 if (!scripts_dir) {
2134 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002135 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002136 }
Feng Tange5f37052012-09-07 16:42:26 +08002137
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002138 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002139 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002140 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002141#ifdef NO_LIBPERL
2142 if (strstr(lang_path, "perl"))
2143 continue;
2144#endif
2145#ifdef NO_LIBPYTHON
2146 if (strstr(lang_path, "python"))
2147 continue;
2148#endif
2149
2150 lang_dir = opendir(lang_path);
2151 if (!lang_dir)
2152 continue;
2153
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002154 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002155 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002156 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002157 continue;
2158 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002159 script_dirent->d_name);
2160 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002161 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002162 (temp - script_dirent->d_name) + 1,
2163 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002164
2165 if (check_ev_match(lang_path,
2166 scripts_array[i], session))
2167 continue;
2168
Feng Tange5f37052012-09-07 16:42:26 +08002169 i++;
2170 }
Feng Tang49e639e2012-10-30 11:56:03 +08002171 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002172 }
2173
Feng Tang49e639e2012-10-30 11:56:03 +08002174 closedir(scripts_dir);
2175 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002176 return i;
2177}
2178
Tom Zanussi38752942009-12-15 02:53:39 -06002179static char *get_script_path(const char *script_root, const char *suffix)
2180{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002181 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002182 char scripts_path[MAXPATHLEN];
2183 char script_path[MAXPATHLEN];
2184 DIR *scripts_dir, *lang_dir;
2185 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002186 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002187
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002188 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002189
2190 scripts_dir = opendir(scripts_path);
2191 if (!scripts_dir)
2192 return NULL;
2193
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002194 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002195 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002196 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002197 lang_dir = opendir(lang_path);
2198 if (!lang_dir)
2199 continue;
2200
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002201 for_each_script(lang_path, lang_dir, script_dirent) {
2202 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002203 if (__script_root && !strcmp(script_root, __script_root)) {
2204 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002205 closedir(lang_dir);
2206 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002207 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002208 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002209 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002210 }
2211 free(__script_root);
2212 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002213 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002214 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002215 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002216
Robert Richter38efb532011-11-25 11:38:40 +01002217 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002218}
2219
Tom Zanussib5b87312010-11-10 08:16:51 -06002220static bool is_top_script(const char *script_path)
2221{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002222 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002223}
2224
2225static int has_required_arg(char *script_path)
2226{
2227 struct script_desc *desc;
2228 int n_args = 0;
2229 char *p;
2230
2231 desc = script_desc__new(NULL);
2232
2233 if (read_script_info(desc, script_path))
2234 goto out;
2235
2236 if (!desc->args)
2237 goto out;
2238
2239 for (p = desc->args; *p; p++)
2240 if (*p == '<')
2241 n_args++;
2242out:
2243 script_desc__delete(desc);
2244
2245 return n_args;
2246}
2247
David Ahernd54b1a92012-08-26 12:24:46 -06002248static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002249{
2250 char **__argv = malloc(sizeof(const char *) * argc);
2251
David Ahernd54b1a92012-08-26 12:24:46 -06002252 if (!__argv) {
2253 pr_err("malloc failed\n");
2254 return -1;
2255 }
2256
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002257 memcpy(__argv, argv, sizeof(const char *) * argc);
2258 argc = parse_options(argc, (const char **)__argv, record_options,
2259 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2260 free(__argv);
2261
David Ahernd54b1a92012-08-26 12:24:46 -06002262 system_wide = (argc == 0);
2263
2264 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002265}
2266
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002267static void script__setup_sample_type(struct perf_script *script)
2268{
2269 struct perf_session *session = script->session;
2270 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2271
2272 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2273 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2274 (sample_type & PERF_SAMPLE_STACK_USER))
2275 callchain_param.record_mode = CALLCHAIN_DWARF;
2276 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2277 callchain_param.record_mode = CALLCHAIN_LBR;
2278 else
2279 callchain_param.record_mode = CALLCHAIN_FP;
2280 }
2281}
2282
Jiri Olsae099eba2016-01-05 22:09:09 +01002283static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2284 union perf_event *event,
2285 struct perf_session *session)
2286{
2287 struct stat_round_event *round = &event->stat_round;
2288 struct perf_evsel *counter;
2289
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002290 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002291 perf_stat_process_counter(&stat_config, counter);
2292 process_stat(counter, round->time);
2293 }
2294
2295 process_stat_interval(round->time);
2296 return 0;
2297}
2298
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002299static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2300 union perf_event *event,
2301 struct perf_session *session __maybe_unused)
2302{
2303 perf_event__read_stat_config(&stat_config, &event->stat_config);
2304 return 0;
2305}
2306
Jiri Olsacfc88742016-01-05 22:09:06 +01002307static int set_maps(struct perf_script *script)
2308{
2309 struct perf_evlist *evlist = script->session->evlist;
2310
2311 if (!script->cpus || !script->threads)
2312 return 0;
2313
2314 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2315 return -EINVAL;
2316
2317 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2318
2319 if (perf_evlist__alloc_stats(evlist, true))
2320 return -ENOMEM;
2321
2322 script->allocated = true;
2323 return 0;
2324}
2325
2326static
2327int process_thread_map_event(struct perf_tool *tool,
2328 union perf_event *event,
2329 struct perf_session *session __maybe_unused)
2330{
2331 struct perf_script *script = container_of(tool, struct perf_script, tool);
2332
2333 if (script->threads) {
2334 pr_warning("Extra thread map event, ignoring.\n");
2335 return 0;
2336 }
2337
2338 script->threads = thread_map__new_event(&event->thread_map);
2339 if (!script->threads)
2340 return -ENOMEM;
2341
2342 return set_maps(script);
2343}
2344
2345static
2346int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2347 union perf_event *event,
2348 struct perf_session *session __maybe_unused)
2349{
2350 struct perf_script *script = container_of(tool, struct perf_script, tool);
2351
2352 if (script->cpus) {
2353 pr_warning("Extra cpu map event, ignoring.\n");
2354 return 0;
2355 }
2356
2357 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2358 if (!script->cpus)
2359 return -ENOMEM;
2360
2361 return set_maps(script);
2362}
2363
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002364int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002365{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002366 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002367 bool header = false;
2368 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002369 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002370 char *rec_script_path = NULL;
2371 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002372 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002373 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002374 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002375 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002376 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002377 struct perf_script script = {
2378 .tool = {
2379 .sample = process_sample_event,
2380 .mmap = perf_event__process_mmap,
2381 .mmap2 = perf_event__process_mmap2,
2382 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302383 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002384 .exit = perf_event__process_exit,
2385 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002386 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002387 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002388 .tracing_data = perf_event__process_tracing_data,
2389 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002390 .id_index = perf_event__process_id_index,
2391 .auxtrace_info = perf_event__process_auxtrace_info,
2392 .auxtrace = perf_event__process_auxtrace,
2393 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002394 .stat = perf_event__process_stat_event,
2395 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002396 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002397 .thread_map = process_thread_map_event,
2398 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002399 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002400 .ordering_requires_timestamps = true,
2401 },
2402 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002403 struct perf_data_file file = {
2404 .mode = PERF_DATA_MODE_READ,
2405 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002406 const struct option options[] = {
2407 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2408 "dump raw trace in ASCII"),
2409 OPT_INCR('v', "verbose", &verbose,
2410 "be more verbose (show symbol address, etc)"),
2411 OPT_BOOLEAN('L', "Latency", &latency_format,
2412 "show latency attributes (irqs/preemption disabled, etc)"),
2413 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2414 list_available_scripts),
2415 OPT_CALLBACK('s', "script", NULL, "name",
2416 "script file name (lang:script name, script name, or *)",
2417 parse_scriptname),
2418 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2419 "generate perf-script.xx script in specified language"),
2420 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2421 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2422 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002423 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2424 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002425 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2426 "file", "vmlinux pathname"),
2427 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2428 "file", "kallsyms pathname"),
2429 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2430 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002431 OPT_CALLBACK(0, "symfs", NULL, "directory",
2432 "Look for files with symbols relative to this directory",
2433 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002434 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002435 "comma separated output fields prepend with 'type:'. "
2436 "Valid types: hw,sw,trace,raw. "
2437 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Huntere2167082016-06-23 16:40:58 +03002438 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
Andi Kleen48d02a12017-02-23 15:46:34 -08002439 "bpf-output,callindent,insn,insnlen,brstackinsn",
2440 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002441 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2442 "system-wide collection from all CPUs"),
2443 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2444 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002445 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2446 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002447 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2448 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2449 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002450 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2451 "only consider symbols in these pids"),
2452 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2453 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002454 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2455 "Set the maximum stack depth when parsing the callchain, "
2456 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002457 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002458 OPT_BOOLEAN('I', "show-info", &show_full_info,
2459 "display extended information from perf.data file"),
2460 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2461 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002462 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2463 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002464 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2465 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002466 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2467 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302468 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2469 "Show namespace events (if recorded)"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002470 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002471 OPT_INTEGER(0, "max-blocks", &max_blocks,
2472 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002473 OPT_BOOLEAN(0, "ns", &nanosecs,
2474 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002475 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2476 "Instruction Tracing options",
2477 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002478 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2479 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002480 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2481 "Enable symbol demangling"),
2482 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2483 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002484 OPT_STRING(0, "time", &script.time_str, "str",
2485 "Time span of interest (start,stop)"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002486 OPT_END()
2487 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002488 const char * const script_subcommands[] = { "record", "report", NULL };
2489 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002490 "perf script [<options>]",
2491 "perf script [<options>] record <script> [<record-options>] <command>",
2492 "perf script [<options>] report <script> [script-args]",
2493 "perf script [<options>] <script> [<record-options>] <command>",
2494 "perf script [<options>] <top-script> [script-args]",
2495 NULL
2496 };
Tom Zanussi38752942009-12-15 02:53:39 -06002497
Tom Zanussib5b87312010-11-10 08:16:51 -06002498 setup_scripting();
2499
Yunlong Song40cae2b2015-03-18 21:35:54 +08002500 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002501 PARSE_OPT_STOP_AT_NON_OPTION);
2502
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002503 file.path = input_name;
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002504 file.force = symbol_conf.force;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002505
Tom Zanussib5b87312010-11-10 08:16:51 -06002506 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2507 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2508 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002509 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002510 }
2511
Tom Zanussib5b87312010-11-10 08:16:51 -06002512 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2513 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2514 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002515 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002516 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002517 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002518 return -1;
2519 }
Tom Zanussi38752942009-12-15 02:53:39 -06002520 }
2521
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002522 if (itrace_synth_opts.callchain &&
2523 itrace_synth_opts.callchain_sz > scripting_max_stack)
2524 scripting_max_stack = itrace_synth_opts.callchain_sz;
2525
Ben Hutchings44e668c2010-10-10 16:10:03 +01002526 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002527 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002528
Tom Zanussib5b87312010-11-10 08:16:51 -06002529 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002530 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002531 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002532 pid_t pid;
2533
Tom Zanussib5b87312010-11-10 08:16:51 -06002534 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2535 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2536
2537 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002538 usage_with_options_msg(script_usage, options,
2539 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002540 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002541 }
2542
Tom Zanussib5b87312010-11-10 08:16:51 -06002543 if (is_top_script(argv[0])) {
2544 rep_args = argc - 1;
2545 } else {
2546 int rec_args;
2547
2548 rep_args = has_required_arg(rep_script_path);
2549 rec_args = (argc - 1) - rep_args;
2550 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002551 usage_with_options_msg(script_usage, options,
2552 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002553 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002554 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002555 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002556 }
2557
2558 if (pipe(live_pipe) < 0) {
2559 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002560 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002561 }
2562
2563 pid = fork();
2564 if (pid < 0) {
2565 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002566 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002567 }
2568
2569 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002570 j = 0;
2571
Tom Zanussia0cccc22010-04-01 23:59:25 -05002572 dup2(live_pipe[1], 1);
2573 close(live_pipe[0]);
2574
Robert Richter317df652011-11-25 15:05:25 +01002575 if (is_top_script(argv[0])) {
2576 system_wide = true;
2577 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002578 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2579 err = -1;
2580 goto out;
2581 }
Robert Richter317df652011-11-25 15:05:25 +01002582 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002583
2584 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002585 if (!__argv) {
2586 pr_err("malloc failed\n");
2587 err = -ENOMEM;
2588 goto out;
2589 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002590
Tom Zanussib5b87312010-11-10 08:16:51 -06002591 __argv[j++] = "/bin/sh";
2592 __argv[j++] = rec_script_path;
2593 if (system_wide)
2594 __argv[j++] = "-a";
2595 __argv[j++] = "-q";
2596 __argv[j++] = "-o";
2597 __argv[j++] = "-";
2598 for (i = rep_args + 1; i < argc; i++)
2599 __argv[j++] = argv[i];
2600 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002601
2602 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002603 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002604 exit(-1);
2605 }
2606
2607 dup2(live_pipe[0], 0);
2608 close(live_pipe[1]);
2609
Tom Zanussib5b87312010-11-10 08:16:51 -06002610 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002611 if (!__argv) {
2612 pr_err("malloc failed\n");
2613 err = -ENOMEM;
2614 goto out;
2615 }
2616
Tom Zanussib5b87312010-11-10 08:16:51 -06002617 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002618 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002619 __argv[j++] = rep_script_path;
2620 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002621 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002622 __argv[j++] = "-i";
2623 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002624 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002625
2626 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002627 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002628 exit(-1);
2629 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002630
Tom Zanussib5b87312010-11-10 08:16:51 -06002631 if (rec_script_path)
2632 script_path = rec_script_path;
2633 if (rep_script_path)
2634 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002635
Tom Zanussib5b87312010-11-10 08:16:51 -06002636 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002637 j = 0;
2638
Robert Richter317df652011-11-25 15:05:25 +01002639 if (!rec_script_path)
2640 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002641 else if (!system_wide) {
2642 if (have_cmd(argc - 1, &argv[1]) != 0) {
2643 err = -1;
2644 goto out;
2645 }
2646 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002647
2648 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002649 if (!__argv) {
2650 pr_err("malloc failed\n");
2651 err = -ENOMEM;
2652 goto out;
2653 }
2654
Tom Zanussib5b87312010-11-10 08:16:51 -06002655 __argv[j++] = "/bin/sh";
2656 __argv[j++] = script_path;
2657 if (system_wide)
2658 __argv[j++] = "-a";
2659 for (i = 2; i < argc; i++)
2660 __argv[j++] = argv[i];
2661 __argv[j++] = NULL;
2662
2663 execvp("/bin/sh", (char **)__argv);
2664 free(__argv);
2665 exit(-1);
2666 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002667
Tom Zanussicf4fee52010-03-03 01:04:33 -06002668 if (!script_name)
2669 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002670
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002671 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002672 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002673 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002674
Jiri Olsae90debd2013-12-09 11:02:50 +01002675 if (header || header_only) {
2676 perf_session__fprintf_info(session, stdout, show_full_info);
2677 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002678 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002679 }
2680
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002681 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002682 goto out_delete;
2683
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002684 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002685 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002686
Adrian Huntere2167082016-06-23 16:40:58 +03002687 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2688 itrace_synth_opts.thread_stack = true;
2689
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002690 session->itrace_synth_opts = &itrace_synth_opts;
2691
Anton Blanchard5d67be92011-07-04 21:57:50 +10002692 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002693 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2694 if (err < 0)
2695 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002696 }
2697
David Ahern1424dc92011-03-09 22:23:28 -07002698 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002699 symbol_conf.use_callchain = true;
2700 else
2701 symbol_conf.use_callchain = false;
2702
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002703 if (session->tevent.pevent &&
2704 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002705 machine__resolve_kernel_addr,
2706 &session->machines.host) < 0) {
2707 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2708 return -1;
2709 }
2710
Tom Zanussi956ffd02009-11-25 01:15:46 -06002711 if (generate_script_lang) {
2712 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002713 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002714
David Ahern2c9e45f72011-03-17 10:03:21 -06002715 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002716 fprintf(stderr,
2717 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002718 err = -EINVAL;
2719 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002720 }
2721
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002722 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002723 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002724 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002725 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002726 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002727 }
2728
2729 err = fstat(input, &perf_stat);
2730 if (err < 0) {
2731 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002732 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002733 }
2734
2735 if (!perf_stat.st_size) {
2736 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002737 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002738 }
2739
2740 scripting_ops = script_spec__lookup(generate_script_lang);
2741 if (!scripting_ops) {
2742 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002743 err = -ENOENT;
2744 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002745 }
2746
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002747 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002748 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002749 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002750 }
2751
2752 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002753 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002754 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002755 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002756 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002757 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002758 }
2759
David Ahern9cbdb702011-04-06 21:54:20 -06002760
2761 err = perf_session__check_output_opt(session);
2762 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002763 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002764
David Aherna91f4c42016-11-29 10:15:43 -07002765 /* needs to be parsed after looking up reference time */
2766 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
2767 pr_err("Invalid time string\n");
2768 return -EINVAL;
2769 }
2770
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002771 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002772
Adrian Hunterd445dd22014-08-15 22:08:37 +03002773 flush_scripting();
2774
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002775out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002776 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002777 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002778
2779 if (script_started)
2780 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002781out:
2782 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002783}