blob: 5afd9a62360a2c984266e2aa5d0dc972c0c7e0db [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#include "builtin.h"
2
Andrea Gelminib7eead82010-08-05 15:51:38 +02003#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02004#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02005#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06006#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02007#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06008#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +02009#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020010#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020011#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020012#include "util/symbol.h"
13#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010014#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020015#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070016#include "util/evlist.h"
17#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080018#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030020#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010021#include "util/cpumap.h"
22#include "util/thread_map.h"
23#include "util/stat.h"
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
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030035#include "sane_ctype.h"
36
Tom Zanussi956ffd02009-11-25 01:15:46 -060037static char const *script_name;
38static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020039static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020040static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020041static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070042static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090043static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010044static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030045static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030046static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100047static const char *cpu_list;
48static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010049static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080050static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060051
Adrian Hunter44cbe722015-09-25 16:15:50 +030052unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030053
David Ahern745f43e2011-03-09 22:23:26 -070054enum perf_output_field {
55 PERF_OUTPUT_COMM = 1U << 0,
56 PERF_OUTPUT_TID = 1U << 1,
57 PERF_OUTPUT_PID = 1U << 2,
58 PERF_OUTPUT_TIME = 1U << 3,
59 PERF_OUTPUT_CPU = 1U << 4,
60 PERF_OUTPUT_EVNAME = 1U << 5,
61 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060062 PERF_OUTPUT_IP = 1U << 7,
63 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060064 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060065 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090066 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020067 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020068 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020069 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020070 PERF_OUTPUT_BRSTACK = 1U << 15,
71 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010072 PERF_OUTPUT_DATA_SRC = 1U << 17,
73 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000074 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030075 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030076 PERF_OUTPUT_INSN = 1U << 21,
77 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080078 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
David Ahern745f43e2011-03-09 22:23:26 -070079};
80
81struct output_option {
82 const char *str;
83 enum perf_output_field field;
84} all_output_options[] = {
85 {.str = "comm", .field = PERF_OUTPUT_COMM},
86 {.str = "tid", .field = PERF_OUTPUT_TID},
87 {.str = "pid", .field = PERF_OUTPUT_PID},
88 {.str = "time", .field = PERF_OUTPUT_TIME},
89 {.str = "cpu", .field = PERF_OUTPUT_CPU},
90 {.str = "event", .field = PERF_OUTPUT_EVNAME},
91 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060092 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070093 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060094 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060095 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090096 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020097 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +020098 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +020099 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200100 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
101 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100102 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
103 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000104 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300105 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300106 {.str = "insn", .field = PERF_OUTPUT_INSN},
107 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800108 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
David Ahern745f43e2011-03-09 22:23:26 -0700109};
110
111/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600112static struct {
113 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600114 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400115 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600116 u64 fields;
117 u64 invalid_fields;
118} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700119
David Ahern2c9e45f72011-03-17 10:03:21 -0600120 [PERF_TYPE_HARDWARE] = {
121 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700122
David Ahern2c9e45f72011-03-17 10:03:21 -0600123 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
124 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600125 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200126 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
127 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600128
Wang Nan30372f02016-02-24 11:20:45 +0000129 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600130 },
131
132 [PERF_TYPE_SOFTWARE] = {
133 .user_set = false,
134
135 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
136 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600137 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200138 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000139 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600140
141 .invalid_fields = PERF_OUTPUT_TRACE,
142 },
143
144 [PERF_TYPE_TRACEPOINT] = {
145 .user_set = false,
146
147 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
148 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000149 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600150 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700151
152 [PERF_TYPE_RAW] = {
153 .user_set = false,
154
155 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
156 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600157 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200158 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100159 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
160 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700161
Wang Nan30372f02016-02-24 11:20:45 +0000162 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700163 },
Wang Nan27cfef02015-12-08 02:25:43 +0000164
165 [PERF_TYPE_BREAKPOINT] = {
166 .user_set = false,
167
168 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
169 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
170 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
171 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
172 PERF_OUTPUT_PERIOD,
173
Wang Nan30372f02016-02-24 11:20:45 +0000174 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000175 },
David Ahern1424dc92011-03-09 22:23:28 -0700176};
David Ahern745f43e2011-03-09 22:23:26 -0700177
David Ahern2c9e45f72011-03-17 10:03:21 -0600178static bool output_set_by_user(void)
179{
180 int j;
181 for (j = 0; j < PERF_TYPE_MAX; ++j) {
182 if (output[j].user_set)
183 return true;
184 }
185 return false;
186}
David Ahern745f43e2011-03-09 22:23:26 -0700187
David Ahern9cbdb702011-04-06 21:54:20 -0600188static const char *output_field2str(enum perf_output_field field)
189{
190 int i, imax = ARRAY_SIZE(all_output_options);
191 const char *str = "";
192
193 for (i = 0; i < imax; ++i) {
194 if (all_output_options[i].field == field) {
195 str = all_output_options[i].str;
196 break;
197 }
198 }
199 return str;
200}
201
David Ahern2c9e45f72011-03-17 10:03:21 -0600202#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700203
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300204static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
205 u64 sample_type, const char *sample_msg,
206 enum perf_output_field field,
207 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700208{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300209 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600210 int type = attr->type;
211 const char *evname;
212
213 if (attr->sample_type & sample_type)
214 return 0;
215
216 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300217 if (allow_user_set)
218 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300219 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600220 pr_err("Samples for '%s' event do not have %s attribute set. "
221 "Cannot print '%s' field.\n",
222 evname, sample_msg, output_field2str(field));
223 return -1;
224 }
225
226 /* user did not ask for it explicitly so remove from the default list */
227 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300228 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600229 pr_debug("Samples for '%s' event do not have %s attribute set. "
230 "Skipping '%s' field.\n",
231 evname, sample_msg, output_field2str(field));
232
233 return 0;
234}
235
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300236static int perf_evsel__check_stype(struct perf_evsel *evsel,
237 u64 sample_type, const char *sample_msg,
238 enum perf_output_field field)
239{
240 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
241 false);
242}
243
David Ahern9cbdb702011-04-06 21:54:20 -0600244static int perf_evsel__check_attr(struct perf_evsel *evsel,
245 struct perf_session *session)
246{
247 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300248 bool allow_user_set;
249
Jiri Olsae099eba2016-01-05 22:09:09 +0100250 if (perf_header__has_feat(&session->header, HEADER_STAT))
251 return 0;
252
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300253 allow_user_set = perf_header__has_feat(&session->header,
254 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600255
David Ahern1424dc92011-03-09 22:23:28 -0700256 if (PRINT_FIELD(TRACE) &&
257 !perf_session__has_traces(session, "record -R"))
258 return -EINVAL;
259
David Ahern787bef12011-05-27 14:28:43 -0600260 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300261 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
262 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700263 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700264 }
David Ahern7cec0922011-05-30 13:08:23 -0600265
266 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300267 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
268 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600269 return -EINVAL;
270
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100271 if (PRINT_FIELD(DATA_SRC) &&
272 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
273 PERF_OUTPUT_DATA_SRC))
274 return -EINVAL;
275
276 if (PRINT_FIELD(WEIGHT) &&
277 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
278 PERF_OUTPUT_WEIGHT))
279 return -EINVAL;
280
David Ahern7cec0922011-05-30 13:08:23 -0600281 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
282 pr_err("Display of symbols requested but neither sample IP nor "
283 "sample address\nis selected. Hence, no addresses to convert "
284 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600285 return -EINVAL;
286 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900287 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
288 pr_err("Display of offsets requested but symbol is not"
289 "selected.\n");
290 return -EINVAL;
291 }
David Ahern7cec0922011-05-30 13:08:23 -0600292 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
293 pr_err("Display of DSO requested but neither sample IP nor "
294 "sample address\nis selected. Hence, no addresses to convert "
295 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600296 return -EINVAL;
297 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200298 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
299 pr_err("Display of source line number requested but sample IP is not\n"
300 "selected. Hence, no address to lookup the source line number.\n");
301 return -EINVAL;
302 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800303 if (PRINT_FIELD(BRSTACKINSN) &&
304 !(perf_evlist__combined_branch_type(session->evlist) &
305 PERF_SAMPLE_BRANCH_ANY)) {
306 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
307 "Hint: run 'perf record -b ...'\n");
308 return -EINVAL;
309 }
David Ahern1424dc92011-03-09 22:23:28 -0700310 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300311 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
312 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700313 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700314
315 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300316 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
317 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700318 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700319
320 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300321 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
322 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700323 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600324
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200325 if (PRINT_FIELD(PERIOD) &&
326 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
327 PERF_OUTPUT_PERIOD))
328 return -EINVAL;
329
Stephane Eranianfc36f942015-08-31 18:41:10 +0200330 if (PRINT_FIELD(IREGS) &&
331 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
332 PERF_OUTPUT_IREGS))
333 return -EINVAL;
334
David Ahern9cbdb702011-04-06 21:54:20 -0600335 return 0;
336}
337
Adrian Hunter7ea95722013-11-01 15:51:30 +0200338static void set_print_ip_opts(struct perf_event_attr *attr)
339{
340 unsigned int type = attr->type;
341
342 output[type].print_ip_opts = 0;
343 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300344 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200345
346 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300347 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200348
349 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300350 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200351
352 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300353 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200354
355 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300356 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200357}
358
David Ahern9cbdb702011-04-06 21:54:20 -0600359/*
360 * verify all user requested events exist and the samples
361 * have the expected data
362 */
363static int perf_session__check_output_opt(struct perf_session *session)
364{
He Kuang40f20e52016-05-16 04:51:19 +0000365 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600366 struct perf_evsel *evsel;
367
368 for (j = 0; j < PERF_TYPE_MAX; ++j) {
369 evsel = perf_session__find_first_evtype(session, j);
370
371 /*
372 * even if fields is set to 0 (ie., show nothing) event must
373 * exist if user explicitly includes it on the command line
374 */
375 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
376 pr_err("%s events do not exist. "
377 "Remove corresponding -f option to proceed.\n",
378 event_type(j));
379 return -1;
380 }
381
382 if (evsel && output[j].fields &&
383 perf_evsel__check_attr(evsel, session))
384 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400385
386 if (evsel == NULL)
387 continue;
388
Adrian Hunter7ea95722013-11-01 15:51:30 +0200389 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700390 }
391
Adrian Hunter98526ee2014-07-31 09:00:59 +0300392 if (!no_callchain) {
393 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000394 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300395
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300396 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000397 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300398 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
399 use_callchain = true;
400 break;
401 }
402 }
He Kuang71ac8992016-08-04 11:25:43 +0000403 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300404 symbol_conf.use_callchain = false;
405 }
406
David Ahern80b8b492013-11-19 21:07:37 -0700407 /*
408 * set default for tracepoints to print symbols only
409 * if callchains are present
410 */
411 if (symbol_conf.use_callchain &&
412 !output[PERF_TYPE_TRACEPOINT].user_set) {
413 struct perf_event_attr *attr;
414
415 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700416
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300417 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000418 if (evsel->attr.type != j)
419 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700420
He Kuang40f20e52016-05-16 04:51:19 +0000421 attr = &evsel->attr;
422
423 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
424 output[j].fields |= PERF_OUTPUT_IP;
425 output[j].fields |= PERF_OUTPUT_SYM;
426 output[j].fields |= PERF_OUTPUT_DSO;
427 set_print_ip_opts(attr);
428 goto out;
429 }
David Ahern80b8b492013-11-19 21:07:37 -0700430 }
431 }
432
433out:
David Ahern1424dc92011-03-09 22:23:28 -0700434 return 0;
435}
David Ahern745f43e2011-03-09 22:23:26 -0700436
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300437static void print_sample_iregs(struct perf_sample *sample,
Stephane Eranianfc36f942015-08-31 18:41:10 +0200438 struct perf_event_attr *attr)
439{
440 struct regs_dump *regs = &sample->intr_regs;
441 uint64_t mask = attr->sample_regs_intr;
442 unsigned i = 0, r;
443
444 if (!regs)
445 return;
446
447 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
448 u64 val = regs->regs[i++];
449 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
450 }
451}
452
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300453static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700454 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300455 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700456{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300457 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700458 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700459 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700460
David Ahern745f43e2011-03-09 22:23:26 -0700461 if (PRINT_FIELD(COMM)) {
462 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200463 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600464 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200465 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700466 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200467 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700468 }
David Ahernc70c94b2011-03-09 22:23:25 -0700469
David Ahern745f43e2011-03-09 22:23:26 -0700470 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
471 printf("%5d/%-5d ", sample->pid, sample->tid);
472 else if (PRINT_FIELD(PID))
473 printf("%5d ", sample->pid);
474 else if (PRINT_FIELD(TID))
475 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700476
David Ahern745f43e2011-03-09 22:23:26 -0700477 if (PRINT_FIELD(CPU)) {
478 if (latency_format)
479 printf("%3d ", sample->cpu);
480 else
481 printf("[%03d] ", sample->cpu);
482 }
David Ahernc70c94b2011-03-09 22:23:25 -0700483
David Ahern745f43e2011-03-09 22:23:26 -0700484 if (PRINT_FIELD(TIME)) {
485 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300486 secs = nsecs / NSEC_PER_SEC;
487 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900488
Adrian Hunter83e19862015-09-25 16:15:36 +0300489 if (nanosecs)
490 printf("%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900491 else {
492 char sample_time[32];
493 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
494 printf("%12s: ", sample_time);
495 }
David Ahern745f43e2011-03-09 22:23:26 -0700496 }
David Ahernc70c94b2011-03-09 22:23:25 -0700497}
498
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200499static inline char
500mispred_str(struct branch_entry *br)
501{
502 if (!(br->flags.mispred || br->flags.predicted))
503 return '-';
504
505 return br->flags.predicted ? 'P' : 'M';
506}
507
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300508static void print_sample_brstack(struct perf_sample *sample)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200509{
510 struct branch_stack *br = sample->branch_stack;
511 u64 i;
512
513 if (!(br && br->nr))
514 return;
515
516 for (i = 0; i < br->nr; i++) {
517 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
518 br->entries[i].from,
519 br->entries[i].to,
520 mispred_str( br->entries + i),
521 br->entries[i].flags.in_tx? 'X' : '-',
522 br->entries[i].flags.abort? 'A' : '-',
523 br->entries[i].flags.cycles);
524 }
525}
526
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300527static void print_sample_brstacksym(struct perf_sample *sample,
528 struct thread *thread)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200529{
530 struct branch_stack *br = sample->branch_stack;
531 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200532 u64 i, from, to;
533
534 if (!(br && br->nr))
535 return;
536
537 for (i = 0; i < br->nr; i++) {
538
539 memset(&alf, 0, sizeof(alf));
540 memset(&alt, 0, sizeof(alt));
541 from = br->entries[i].from;
542 to = br->entries[i].to;
543
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300544 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200545 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300546 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200547
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300548 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200549 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300550 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200551
552 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
553 putchar('/');
554 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
555 printf("/%c/%c/%c/%d ",
556 mispred_str( br->entries + i),
557 br->entries[i].flags.in_tx? 'X' : '-',
558 br->entries[i].flags.abort? 'A' : '-',
559 br->entries[i].flags.cycles);
560 }
561}
562
Andi Kleen48d02a12017-02-23 15:46:34 -0800563#define MAXBB 16384UL
564
565static int grab_bb(u8 *buffer, u64 start, u64 end,
566 struct machine *machine, struct thread *thread,
567 bool *is64bit, u8 *cpumode, bool last)
568{
569 long offset, len;
570 struct addr_location al;
571 bool kernel;
572
573 if (!start || !end)
574 return 0;
575
576 kernel = machine__kernel_ip(machine, start);
577 if (kernel)
578 *cpumode = PERF_RECORD_MISC_KERNEL;
579 else
580 *cpumode = PERF_RECORD_MISC_USER;
581
582 /*
583 * Block overlaps between kernel and user.
584 * This can happen due to ring filtering
585 * On Intel CPUs the entry into the kernel is filtered,
586 * but the exit is not. Let the caller patch it up.
587 */
588 if (kernel != machine__kernel_ip(machine, end)) {
589 printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
590 start, end);
591 return -ENXIO;
592 }
593
594 memset(&al, 0, sizeof(al));
595 if (end - start > MAXBB - MAXINSN) {
596 if (last)
597 printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
598 else
599 printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
600 return 0;
601 }
602
603 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
604 if (!al.map || !al.map->dso) {
605 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
606 return 0;
607 }
608 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
609 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
610 return 0;
611 }
612
613 /* Load maps to ensure dso->is_64_bit has been updated */
614 map__load(al.map);
615
616 offset = al.map->map_ip(al.map, start);
617 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
618 end - start + MAXINSN);
619
620 *is64bit = al.map->dso->is_64_bit;
621 if (len <= 0)
622 printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
623 start, end);
624 return len;
625}
626
627static void print_jump(uint64_t ip, struct branch_entry *en,
628 struct perf_insn *x, u8 *inbuf, int len,
629 int insn)
630{
631 printf("\t%016" PRIx64 "\t%-30s\t#%s%s%s%s",
632 ip,
633 dump_insn(x, ip, inbuf, len, NULL),
634 en->flags.predicted ? " PRED" : "",
635 en->flags.mispred ? " MISPRED" : "",
636 en->flags.in_tx ? " INTX" : "",
637 en->flags.abort ? " ABORT" : "");
638 if (en->flags.cycles) {
639 printf(" %d cycles", en->flags.cycles);
640 if (insn)
641 printf(" %.2f IPC", (float)insn / en->flags.cycles);
642 }
643 putchar('\n');
644}
645
646static void print_ip_sym(struct thread *thread, u8 cpumode, int cpu,
647 uint64_t addr, struct symbol **lastsym,
648 struct perf_event_attr *attr)
649{
650 struct addr_location al;
651 int off;
652
653 memset(&al, 0, sizeof(al));
654
655 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
656 if (!al.map)
657 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
658 addr, &al);
659 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
660 return;
661
662 al.cpu = cpu;
663 al.sym = NULL;
664 if (al.map)
665 al.sym = map__find_symbol(al.map, al.addr);
666
667 if (!al.sym)
668 return;
669
670 if (al.addr < al.sym->end)
671 off = al.addr - al.sym->start;
672 else
673 off = al.addr - al.map->start - al.sym->start;
674 printf("\t%s", al.sym->name);
675 if (off)
676 printf("%+d", off);
677 putchar(':');
678 if (PRINT_FIELD(SRCLINE))
679 map__fprintf_srcline(al.map, al.addr, "\t", stdout);
680 putchar('\n');
681 *lastsym = al.sym;
682}
683
684static void print_sample_brstackinsn(struct perf_sample *sample,
685 struct thread *thread,
686 struct perf_event_attr *attr,
687 struct machine *machine)
688{
689 struct branch_stack *br = sample->branch_stack;
690 u64 start, end;
691 int i, insn, len, nr, ilen;
692 struct perf_insn x;
693 u8 buffer[MAXBB];
694 unsigned off;
695 struct symbol *lastsym = NULL;
696
697 if (!(br && br->nr))
698 return;
699 nr = br->nr;
700 if (max_blocks && nr > max_blocks + 1)
701 nr = max_blocks + 1;
702
703 x.thread = thread;
704 x.cpu = sample->cpu;
705
706 putchar('\n');
707
708 /* Handle first from jump, of which we don't know the entry. */
709 len = grab_bb(buffer, br->entries[nr-1].from,
710 br->entries[nr-1].from,
711 machine, thread, &x.is64bit, &x.cpumode, false);
712 if (len > 0) {
713 print_ip_sym(thread, x.cpumode, x.cpu,
714 br->entries[nr - 1].from, &lastsym, attr);
715 print_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
716 &x, buffer, len, 0);
717 }
718
719 /* Print all blocks */
720 for (i = nr - 2; i >= 0; i--) {
721 if (br->entries[i].from || br->entries[i].to)
722 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
723 br->entries[i].from,
724 br->entries[i].to);
725 start = br->entries[i + 1].to;
726 end = br->entries[i].from;
727
728 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
729 /* Patch up missing kernel transfers due to ring filters */
730 if (len == -ENXIO && i > 0) {
731 end = br->entries[--i].from;
732 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
733 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
734 }
735 if (len <= 0)
736 continue;
737
738 insn = 0;
739 for (off = 0;; off += ilen) {
740 uint64_t ip = start + off;
741
742 print_ip_sym(thread, x.cpumode, x.cpu, ip, &lastsym, attr);
743 if (ip == end) {
744 print_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn);
745 break;
746 } else {
747 printf("\t%016" PRIx64 "\t%s\n", ip,
748 dump_insn(&x, ip, buffer + off, len - off, &ilen));
749 if (ilen == 0)
750 break;
751 insn++;
752 }
753 }
754 }
755
756 /*
757 * Hit the branch? In this case we are already done, and the target
758 * has not been executed yet.
759 */
760 if (br->entries[0].from == sample->ip)
761 return;
762 if (br->entries[0].flags.abort)
763 return;
764
765 /*
766 * Print final block upto sample
767 */
768 start = br->entries[0].to;
769 end = sample->ip;
770 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
771 print_ip_sym(thread, x.cpumode, x.cpu, start, &lastsym, attr);
772 if (len <= 0) {
773 /* Print at least last IP if basic block did not work */
774 len = grab_bb(buffer, sample->ip, sample->ip,
775 machine, thread, &x.is64bit, &x.cpumode, false);
776 if (len <= 0)
777 return;
778
779 printf("\t%016" PRIx64 "\t%s\n", sample->ip,
780 dump_insn(&x, sample->ip, buffer, len, NULL));
781 return;
782 }
783 for (off = 0; off <= end - start; off += ilen) {
784 printf("\t%016" PRIx64 "\t%s\n", start + off,
785 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
786 if (ilen == 0)
787 break;
788 }
789}
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200790
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300791static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600792 struct thread *thread,
793 struct perf_event_attr *attr)
794{
795 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600796
797 printf("%16" PRIx64, sample->addr);
798
799 if (!sample_addr_correlates_sym(attr))
800 return;
801
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300802 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600803
804 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900805 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900806 if (PRINT_FIELD(SYMOFFSET))
807 symbol__fprintf_symname_offs(al.sym, &al, stdout);
808 else
809 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600810 }
811
812 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900813 printf(" (");
814 map__fprintf_dsoname(al.map, stdout);
815 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600816 }
817}
818
Adrian Huntere2167082016-06-23 16:40:58 +0300819static void print_sample_callindent(struct perf_sample *sample,
820 struct perf_evsel *evsel,
821 struct thread *thread,
822 struct addr_location *al)
823{
824 struct perf_event_attr *attr = &evsel->attr;
825 size_t depth = thread_stack__depth(thread);
826 struct addr_location addr_al;
827 const char *name = NULL;
828 static int spacing;
829 int len = 0;
830 u64 ip = 0;
831
832 /*
833 * The 'return' has already been popped off the stack so the depth has
834 * to be adjusted to match the 'call'.
835 */
836 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
837 depth += 1;
838
839 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
840 if (sample_addr_correlates_sym(attr)) {
841 thread__resolve(thread, &addr_al, sample);
842 if (addr_al.sym)
843 name = addr_al.sym->name;
844 else
845 ip = sample->addr;
846 } else {
847 ip = sample->addr;
848 }
849 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
850 if (al->sym)
851 name = al->sym->name;
852 else
853 ip = sample->ip;
854 }
855
856 if (name)
857 len = printf("%*s%s", (int)depth * 4, "", name);
858 else if (ip)
859 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
860
861 if (len < 0)
862 return;
863
864 /*
865 * Try to keep the output length from changing frequently so that the
866 * output lines up more nicely.
867 */
868 if (len > spacing || (len && len < spacing - 52))
869 spacing = round_up(len + 4, 32);
870
871 if (len < spacing)
872 printf("%*s", spacing - len, "");
873}
874
Andi Kleen224e2c92016-10-07 16:42:27 +0300875static void print_insn(struct perf_sample *sample,
Andi Kleen48d02a12017-02-23 15:46:34 -0800876 struct perf_event_attr *attr,
877 struct thread *thread,
878 struct machine *machine)
Andi Kleen224e2c92016-10-07 16:42:27 +0300879{
880 if (PRINT_FIELD(INSNLEN))
881 printf(" ilen: %d", sample->insn_len);
882 if (PRINT_FIELD(INSN)) {
883 int i;
884
885 printf(" insn:");
886 for (i = 0; i < sample->insn_len; i++)
887 printf(" %02x", (unsigned char)sample->insn[i]);
888 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800889 if (PRINT_FIELD(BRSTACKINSN))
890 print_sample_brstackinsn(sample, thread, attr, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300891}
892
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300893static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +0900894 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200895 struct thread *thread,
Andi Kleen48d02a12017-02-23 15:46:34 -0800896 struct addr_location *al,
897 struct machine *machine)
Akihiro Nagai95582592012-01-30 13:43:09 +0900898{
899 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300900 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900901
Adrian Huntere2167082016-06-23 16:40:58 +0300902 if (PRINT_FIELD(CALLINDENT))
903 print_sample_callindent(sample, evsel, thread, al);
904
Akihiro Nagai95582592012-01-30 13:43:09 +0900905 /* print branch_from information */
906 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300907 unsigned int print_opts = output[attr->type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -0700908 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300909
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300910 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700911 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300912 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700913 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300914
915 if (cursor == NULL) {
916 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300917 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300918 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300919 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300920 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300921 } else
922 putchar('\n');
923
924 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +0900925 }
926
Akihiro Nagai95582592012-01-30 13:43:09 +0900927 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300928 if (PRINT_FIELD(ADDR) ||
929 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300930 !output[attr->type].user_set)) {
931 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300932 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300933 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900934
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300935 if (print_srcline_last)
936 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
937
Andi Kleen48d02a12017-02-23 15:46:34 -0800938 print_insn(sample, attr, thread, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300939
Akihiro Nagai95582592012-01-30 13:43:09 +0900940 printf("\n");
941}
942
Adrian Hunter055cd332016-06-23 16:40:56 +0300943static struct {
944 u32 flags;
945 const char *name;
946} sample_flags[] = {
947 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
948 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
949 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
950 {PERF_IP_FLAG_BRANCH, "jmp"},
951 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
952 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
953 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
954 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
955 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
956 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
957 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
958 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
959 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
960 {0, NULL}
961};
962
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300963static void print_sample_flags(u32 flags)
964{
965 const char *chars = PERF_IP_FLAG_CHARS;
966 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +0300967 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
968 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300969 char str[33];
970 int i, pos = 0;
971
Adrian Hunter055cd332016-06-23 16:40:56 +0300972 for (i = 0; sample_flags[i].name ; i++) {
973 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
974 name = sample_flags[i].name;
975 break;
976 }
977 }
978
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300979 for (i = 0; i < n; i++, flags >>= 1) {
980 if (flags & 1)
981 str[pos++] = chars[i];
982 }
983 for (; i < 32; i++, flags >>= 1) {
984 if (flags & 1)
985 str[pos++] = '?';
986 }
987 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +0300988
989 if (name)
990 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
991 else
992 printf(" %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300993}
994
Wang Nan30372f02016-02-24 11:20:45 +0000995struct printer_data {
996 int line_no;
997 bool hit_nul;
998 bool is_printable;
999};
1000
1001static void
1002print_sample_bpf_output_printer(enum binary_printer_ops op,
1003 unsigned int val,
1004 void *extra)
1005{
1006 unsigned char ch = (unsigned char)val;
1007 struct printer_data *printer_data = extra;
1008
1009 switch (op) {
1010 case BINARY_PRINT_DATA_BEGIN:
1011 printf("\n");
1012 break;
1013 case BINARY_PRINT_LINE_BEGIN:
1014 printf("%17s", !printer_data->line_no ? "BPF output:" :
1015 " ");
1016 break;
1017 case BINARY_PRINT_ADDR:
1018 printf(" %04x:", val);
1019 break;
1020 case BINARY_PRINT_NUM_DATA:
1021 printf(" %02x", val);
1022 break;
1023 case BINARY_PRINT_NUM_PAD:
1024 printf(" ");
1025 break;
1026 case BINARY_PRINT_SEP:
1027 printf(" ");
1028 break;
1029 case BINARY_PRINT_CHAR_DATA:
1030 if (printer_data->hit_nul && ch)
1031 printer_data->is_printable = false;
1032
1033 if (!isprint(ch)) {
1034 printf("%c", '.');
1035
1036 if (!printer_data->is_printable)
1037 break;
1038
1039 if (ch == '\0')
1040 printer_data->hit_nul = true;
1041 else
1042 printer_data->is_printable = false;
1043 } else {
1044 printf("%c", ch);
1045 }
1046 break;
1047 case BINARY_PRINT_CHAR_PAD:
1048 printf(" ");
1049 break;
1050 case BINARY_PRINT_LINE_END:
1051 printf("\n");
1052 printer_data->line_no++;
1053 break;
1054 case BINARY_PRINT_DATA_END:
1055 default:
1056 break;
1057 }
1058}
1059
1060static void print_sample_bpf_output(struct perf_sample *sample)
1061{
1062 unsigned int nr_bytes = sample->raw_size;
1063 struct printer_data printer_data = {0, false, true};
1064
1065 print_binary(sample->raw_data, nr_bytes, 8,
1066 print_sample_bpf_output_printer, &printer_data);
1067
1068 if (printer_data.is_printable && printer_data.hit_nul)
1069 printf("%17s \"%s\"\n", "BPF string:",
1070 (char *)(sample->raw_data));
1071}
1072
Jiri Olsa809e9422015-11-26 18:55:21 +01001073struct perf_script {
1074 struct perf_tool tool;
1075 struct perf_session *session;
1076 bool show_task_events;
1077 bool show_mmap_events;
1078 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301079 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001080 bool allocated;
1081 struct cpu_map *cpus;
1082 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001083 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001084 const char *time_str;
1085 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001086};
1087
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001088static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1089{
1090 struct perf_evsel *evsel;
1091 int max = 0;
1092
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001093 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001094 int len = strlen(perf_evsel__name(evsel));
1095
1096 max = MAX(len, max);
1097 }
1098
1099 return max;
1100}
1101
Jiri Olsac19ac912016-02-24 09:46:54 +01001102static size_t data_src__printf(u64 data_src)
1103{
1104 struct mem_info mi = { .data_src.val = data_src };
1105 char decode[100];
1106 char out[100];
1107 static int maxlen;
1108 int len;
1109
1110 perf_script__meminfo_scnprintf(decode, 100, &mi);
1111
1112 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1113 if (maxlen < len)
1114 maxlen = len;
1115
1116 return printf("%-*s", maxlen, out);
1117}
1118
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001119static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001120 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001121 struct addr_location *al,
1122 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001123{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001124 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001125 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -07001126
David Ahern2c9e45f72011-03-17 10:03:21 -06001127 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001128 return;
1129
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001130 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -07001131
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001132 if (PRINT_FIELD(PERIOD))
1133 printf("%10" PRIu64 " ", sample->period);
1134
Namhyung Kime944d3d2013-11-18 14:34:52 +09001135 if (PRINT_FIELD(EVNAME)) {
1136 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001137
1138 if (!script->name_width)
1139 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1140
1141 printf("%*s: ", script->name_width,
1142 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001143 }
1144
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001145 if (print_flags)
1146 print_sample_flags(sample->flags);
1147
Akihiro Nagai95582592012-01-30 13:43:09 +09001148 if (is_bts_event(attr)) {
Andi Kleen48d02a12017-02-23 15:46:34 -08001149 print_sample_bts(sample, evsel, thread, al, machine);
Akihiro Nagai95582592012-01-30 13:43:09 +09001150 return;
1151 }
1152
David Ahern745f43e2011-03-09 22:23:26 -07001153 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001154 event_format__print(evsel->tp_format, sample->cpu,
1155 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -06001156 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001157 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -06001158
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001159 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +01001160 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001161
1162 if (PRINT_FIELD(WEIGHT))
1163 printf("%16" PRIu64, sample->weight);
1164
David Ahern787bef12011-05-27 14:28:43 -06001165 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001166 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001167
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001168 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001169 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001170 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001171 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001172
1173 putchar(cursor ? '\n' : ' ');
1174 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001175 }
1176
Stephane Eranianfc36f942015-08-31 18:41:10 +02001177 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001178 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001179
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001180 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001181 print_sample_brstack(sample);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001182 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001183 print_sample_brstacksym(sample, thread);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001184
Wang Nan30372f02016-02-24 11:20:45 +00001185 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1186 print_sample_bpf_output(sample);
Andi Kleen48d02a12017-02-23 15:46:34 -08001187 print_insn(sample, attr, thread, machine);
David Ahernc70c94b2011-03-09 22:23:25 -07001188 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001189}
1190
Tom Zanussi956ffd02009-11-25 01:15:46 -06001191static struct scripting_ops *scripting_ops;
1192
Jiri Olsa36e33c52016-01-06 11:49:56 +01001193static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1194{
1195 int nthreads = thread_map__nr(counter->threads);
1196 int ncpus = perf_evsel__nr_cpus(counter);
1197 int cpu, thread;
1198 static int header_printed;
1199
1200 if (counter->system_wide)
1201 nthreads = 1;
1202
1203 if (!header_printed) {
1204 printf("%3s %8s %15s %15s %15s %15s %s\n",
1205 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1206 header_printed = 1;
1207 }
1208
1209 for (thread = 0; thread < nthreads; thread++) {
1210 for (cpu = 0; cpu < ncpus; cpu++) {
1211 struct perf_counts_values *counts;
1212
1213 counts = perf_counts(counter->counts, cpu, thread);
1214
1215 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1216 counter->cpus->map[cpu],
1217 thread_map__pid(counter->threads, thread),
1218 counts->val,
1219 counts->ena,
1220 counts->run,
1221 tstamp,
1222 perf_evsel__name(counter));
1223 }
1224 }
1225}
1226
Jiri Olsae099eba2016-01-05 22:09:09 +01001227static void process_stat(struct perf_evsel *counter, u64 tstamp)
1228{
1229 if (scripting_ops && scripting_ops->process_stat)
1230 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001231 else
1232 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001233}
1234
1235static void process_stat_interval(u64 tstamp)
1236{
1237 if (scripting_ops && scripting_ops->process_stat_interval)
1238 scripting_ops->process_stat_interval(tstamp);
1239}
1240
Tom Zanussi956ffd02009-11-25 01:15:46 -06001241static void setup_scripting(void)
1242{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001243 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001244 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001245}
1246
Adrian Hunterd445dd22014-08-15 22:08:37 +03001247static int flush_scripting(void)
1248{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001249 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001250}
1251
Tom Zanussi956ffd02009-11-25 01:15:46 -06001252static int cleanup_scripting(void)
1253{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001254 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001255
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001256 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001257}
1258
Jiri Olsa809e9422015-11-26 18:55:21 +01001259static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001260 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001261 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001262 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001263 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001264{
Jiri Olsa809e9422015-11-26 18:55:21 +01001265 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001266 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001267
David Aherna91f4c42016-11-29 10:15:43 -07001268 if (perf_time__skip_sample(&scr->ptime, sample->time))
1269 return 0;
1270
David Ahern1424dc92011-03-09 22:23:28 -07001271 if (debug_mode) {
1272 if (sample->time < last_timestamp) {
1273 pr_err("Samples misordered, previous: %" PRIu64
1274 " this: %" PRIu64 "\n", last_timestamp,
1275 sample->time);
1276 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001277 }
David Ahern1424dc92011-03-09 22:23:28 -07001278 last_timestamp = sample->time;
1279 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001280 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001281
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001282 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001283 pr_err("problem processing %d event, skipping it.\n",
1284 event->header.type);
1285 return -1;
1286 }
1287
1288 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001289 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001290
Anton Blanchard5d67be92011-07-04 21:57:50 +10001291 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001292 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001293
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001294 if (scripting_ops)
1295 scripting_ops->process_event(event, sample, evsel, &al);
1296 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001297 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001298
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001299out_put:
1300 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001301 return 0;
1302}
1303
Adrian Hunter7ea95722013-11-01 15:51:30 +02001304static int process_attr(struct perf_tool *tool, union perf_event *event,
1305 struct perf_evlist **pevlist)
1306{
1307 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1308 struct perf_evlist *evlist;
1309 struct perf_evsel *evsel, *pos;
1310 int err;
1311
1312 err = perf_event__process_attr(tool, event, pevlist);
1313 if (err)
1314 return err;
1315
1316 evlist = *pevlist;
1317 evsel = perf_evlist__last(*pevlist);
1318
1319 if (evsel->attr.type >= PERF_TYPE_MAX)
1320 return 0;
1321
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001322 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001323 if (pos->attr.type == evsel->attr.type && pos != evsel)
1324 return 0;
1325 }
1326
1327 set_print_ip_opts(&evsel->attr);
1328
Jiri Olsad2b5a312015-10-16 12:41:25 +02001329 if (evsel->attr.sample_type)
1330 err = perf_evsel__check_attr(evsel, scr->session);
1331
1332 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001333}
1334
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001335static int process_comm_event(struct perf_tool *tool,
1336 union perf_event *event,
1337 struct perf_sample *sample,
1338 struct machine *machine)
1339{
1340 struct thread *thread;
1341 struct perf_script *script = container_of(tool, struct perf_script, tool);
1342 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001343 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001344 int ret = -1;
1345
1346 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1347 if (thread == NULL) {
1348 pr_debug("problem processing COMM event, skipping it.\n");
1349 return -1;
1350 }
1351
1352 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1353 goto out;
1354
1355 if (!evsel->attr.sample_id_all) {
1356 sample->cpu = 0;
1357 sample->time = 0;
1358 sample->tid = event->comm.tid;
1359 sample->pid = event->comm.pid;
1360 }
1361 print_sample_start(sample, thread, evsel);
1362 perf_event__fprintf(event, stdout);
1363 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001364out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001365 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001366 return ret;
1367}
1368
Hari Bathini96a44bb2017-03-08 02:12:06 +05301369static int process_namespaces_event(struct perf_tool *tool,
1370 union perf_event *event,
1371 struct perf_sample *sample,
1372 struct machine *machine)
1373{
1374 struct thread *thread;
1375 struct perf_script *script = container_of(tool, struct perf_script, tool);
1376 struct perf_session *session = script->session;
1377 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1378 int ret = -1;
1379
1380 thread = machine__findnew_thread(machine, event->namespaces.pid,
1381 event->namespaces.tid);
1382 if (thread == NULL) {
1383 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1384 return -1;
1385 }
1386
1387 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1388 goto out;
1389
1390 if (!evsel->attr.sample_id_all) {
1391 sample->cpu = 0;
1392 sample->time = 0;
1393 sample->tid = event->namespaces.tid;
1394 sample->pid = event->namespaces.pid;
1395 }
1396 print_sample_start(sample, thread, evsel);
1397 perf_event__fprintf(event, stdout);
1398 ret = 0;
1399out:
1400 thread__put(thread);
1401 return ret;
1402}
1403
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001404static int process_fork_event(struct perf_tool *tool,
1405 union perf_event *event,
1406 struct perf_sample *sample,
1407 struct machine *machine)
1408{
1409 struct thread *thread;
1410 struct perf_script *script = container_of(tool, struct perf_script, tool);
1411 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001412 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001413
1414 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1415 return -1;
1416
1417 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1418 if (thread == NULL) {
1419 pr_debug("problem processing FORK event, skipping it.\n");
1420 return -1;
1421 }
1422
1423 if (!evsel->attr.sample_id_all) {
1424 sample->cpu = 0;
1425 sample->time = event->fork.time;
1426 sample->tid = event->fork.tid;
1427 sample->pid = event->fork.pid;
1428 }
1429 print_sample_start(sample, thread, evsel);
1430 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001431 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001432
1433 return 0;
1434}
1435static int process_exit_event(struct perf_tool *tool,
1436 union perf_event *event,
1437 struct perf_sample *sample,
1438 struct machine *machine)
1439{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001440 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001441 struct thread *thread;
1442 struct perf_script *script = container_of(tool, struct perf_script, tool);
1443 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001444 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001445
1446 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1447 if (thread == NULL) {
1448 pr_debug("problem processing EXIT event, skipping it.\n");
1449 return -1;
1450 }
1451
1452 if (!evsel->attr.sample_id_all) {
1453 sample->cpu = 0;
1454 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001455 sample->tid = event->fork.tid;
1456 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001457 }
1458 print_sample_start(sample, thread, evsel);
1459 perf_event__fprintf(event, stdout);
1460
1461 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001462 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001463
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001464 thread__put(thread);
1465 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001466}
1467
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001468static int process_mmap_event(struct perf_tool *tool,
1469 union perf_event *event,
1470 struct perf_sample *sample,
1471 struct machine *machine)
1472{
1473 struct thread *thread;
1474 struct perf_script *script = container_of(tool, struct perf_script, tool);
1475 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001476 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001477
1478 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1479 return -1;
1480
1481 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1482 if (thread == NULL) {
1483 pr_debug("problem processing MMAP event, skipping it.\n");
1484 return -1;
1485 }
1486
1487 if (!evsel->attr.sample_id_all) {
1488 sample->cpu = 0;
1489 sample->time = 0;
1490 sample->tid = event->mmap.tid;
1491 sample->pid = event->mmap.pid;
1492 }
1493 print_sample_start(sample, thread, evsel);
1494 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001495 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001496 return 0;
1497}
1498
1499static int process_mmap2_event(struct perf_tool *tool,
1500 union perf_event *event,
1501 struct perf_sample *sample,
1502 struct machine *machine)
1503{
1504 struct thread *thread;
1505 struct perf_script *script = container_of(tool, struct perf_script, tool);
1506 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001507 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001508
1509 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1510 return -1;
1511
1512 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1513 if (thread == NULL) {
1514 pr_debug("problem processing MMAP2 event, skipping it.\n");
1515 return -1;
1516 }
1517
1518 if (!evsel->attr.sample_id_all) {
1519 sample->cpu = 0;
1520 sample->time = 0;
1521 sample->tid = event->mmap2.tid;
1522 sample->pid = event->mmap2.pid;
1523 }
1524 print_sample_start(sample, thread, evsel);
1525 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001526 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001527 return 0;
1528}
1529
Adrian Hunter7c148982015-07-21 12:44:06 +03001530static int process_switch_event(struct perf_tool *tool,
1531 union perf_event *event,
1532 struct perf_sample *sample,
1533 struct machine *machine)
1534{
1535 struct thread *thread;
1536 struct perf_script *script = container_of(tool, struct perf_script, tool);
1537 struct perf_session *session = script->session;
1538 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1539
1540 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1541 return -1;
1542
1543 thread = machine__findnew_thread(machine, sample->pid,
1544 sample->tid);
1545 if (thread == NULL) {
1546 pr_debug("problem processing SWITCH event, skipping it.\n");
1547 return -1;
1548 }
1549
1550 print_sample_start(sample, thread, evsel);
1551 perf_event__fprintf(event, stdout);
1552 thread__put(thread);
1553 return 0;
1554}
1555
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001556static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001557{
1558 session_done = 1;
1559}
1560
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001561static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001562{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001563 int ret;
1564
Tom Zanussic239da32010-04-01 23:59:18 -05001565 signal(SIGINT, sig_handler);
1566
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001567 /* override event processing functions */
1568 if (script->show_task_events) {
1569 script->tool.comm = process_comm_event;
1570 script->tool.fork = process_fork_event;
1571 script->tool.exit = process_exit_event;
1572 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001573 if (script->show_mmap_events) {
1574 script->tool.mmap = process_mmap_event;
1575 script->tool.mmap2 = process_mmap2_event;
1576 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001577 if (script->show_switch_events)
1578 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301579 if (script->show_namespace_events)
1580 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001581
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001582 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001583
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001584 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001585 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001586
1587 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001588}
1589
Tom Zanussi956ffd02009-11-25 01:15:46 -06001590struct script_spec {
1591 struct list_head node;
1592 struct scripting_ops *ops;
1593 char spec[0];
1594};
1595
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001596static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001597
1598static struct script_spec *script_spec__new(const char *spec,
1599 struct scripting_ops *ops)
1600{
1601 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1602
1603 if (s != NULL) {
1604 strcpy(s->spec, spec);
1605 s->ops = ops;
1606 }
1607
1608 return s;
1609}
1610
Tom Zanussi956ffd02009-11-25 01:15:46 -06001611static void script_spec__add(struct script_spec *s)
1612{
1613 list_add_tail(&s->node, &script_specs);
1614}
1615
1616static struct script_spec *script_spec__find(const char *spec)
1617{
1618 struct script_spec *s;
1619
1620 list_for_each_entry(s, &script_specs, node)
1621 if (strcasecmp(s->spec, spec) == 0)
1622 return s;
1623 return NULL;
1624}
1625
Tom Zanussi956ffd02009-11-25 01:15:46 -06001626int script_spec_register(const char *spec, struct scripting_ops *ops)
1627{
1628 struct script_spec *s;
1629
1630 s = script_spec__find(spec);
1631 if (s)
1632 return -1;
1633
Taeung Song8560bae2016-02-26 00:13:10 +09001634 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001635 if (!s)
1636 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001637 else
1638 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001639
1640 return 0;
1641}
1642
1643static struct scripting_ops *script_spec__lookup(const char *spec)
1644{
1645 struct script_spec *s = script_spec__find(spec);
1646 if (!s)
1647 return NULL;
1648
1649 return s->ops;
1650}
1651
1652static void list_available_languages(void)
1653{
1654 struct script_spec *s;
1655
1656 fprintf(stderr, "\n");
1657 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001658 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001659
1660 list_for_each_entry(s, &script_specs, node)
1661 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1662
1663 fprintf(stderr, "\n");
1664}
1665
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001666static int parse_scriptname(const struct option *opt __maybe_unused,
1667 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001668{
1669 char spec[PATH_MAX];
1670 const char *script, *ext;
1671 int len;
1672
Tom Zanussif526d682010-01-27 02:27:52 -06001673 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001674 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001675 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001676 }
1677
1678 script = strchr(str, ':');
1679 if (script) {
1680 len = script - str;
1681 if (len >= PATH_MAX) {
1682 fprintf(stderr, "invalid language specifier");
1683 return -1;
1684 }
1685 strncpy(spec, str, len);
1686 spec[len] = '\0';
1687 scripting_ops = script_spec__lookup(spec);
1688 if (!scripting_ops) {
1689 fprintf(stderr, "invalid language specifier");
1690 return -1;
1691 }
1692 script++;
1693 } else {
1694 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001695 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001696 if (!ext) {
1697 fprintf(stderr, "invalid script extension");
1698 return -1;
1699 }
1700 scripting_ops = script_spec__lookup(++ext);
1701 if (!scripting_ops) {
1702 fprintf(stderr, "invalid script extension");
1703 return -1;
1704 }
1705 }
1706
1707 script_name = strdup(script);
1708
1709 return 0;
1710}
1711
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001712static int parse_output_fields(const struct option *opt __maybe_unused,
1713 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001714{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001715 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001716 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001717 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001718 int rc = 0;
1719 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001720 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001721
1722 if (!str)
1723 return -ENOMEM;
1724
David Ahern2c9e45f72011-03-17 10:03:21 -06001725 /* first word can state for which event type the user is specifying
1726 * the fields. If no type exists, the specified fields apply to all
1727 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001728 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001729 tok = strchr(str, ':');
1730 if (tok) {
1731 *tok = '\0';
1732 tok++;
1733 if (!strcmp(str, "hw"))
1734 type = PERF_TYPE_HARDWARE;
1735 else if (!strcmp(str, "sw"))
1736 type = PERF_TYPE_SOFTWARE;
1737 else if (!strcmp(str, "trace"))
1738 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001739 else if (!strcmp(str, "raw"))
1740 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001741 else if (!strcmp(str, "break"))
1742 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001743 else {
1744 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001745 rc = -EINVAL;
1746 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001747 }
1748
1749 if (output[type].user_set)
1750 pr_warning("Overriding previous field request for %s events.\n",
1751 event_type(type));
1752
1753 output[type].fields = 0;
1754 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001755 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001756
1757 } else {
1758 tok = str;
1759 if (strlen(str) == 0) {
1760 fprintf(stderr,
1761 "Cannot set fields to 'none' for all event types.\n");
1762 rc = -EINVAL;
1763 goto out;
1764 }
1765
1766 if (output_set_by_user())
1767 pr_warning("Overriding previous field request for all events.\n");
1768
1769 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1770 output[j].fields = 0;
1771 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001772 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001773 }
David Ahern1424dc92011-03-09 22:23:28 -07001774 }
1775
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001776 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
David Ahern745f43e2011-03-09 22:23:26 -07001777 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001778 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001779 break;
David Ahern745f43e2011-03-09 22:23:26 -07001780 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001781 if (i == imax && strcmp(tok, "flags") == 0) {
1782 print_flags = true;
1783 continue;
1784 }
David Ahern745f43e2011-03-09 22:23:26 -07001785 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001786 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001787 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001788 goto out;
1789 }
1790
1791 if (type == -1) {
1792 /* add user option to all events types for
1793 * which it is valid
1794 */
1795 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1796 if (output[j].invalid_fields & all_output_options[i].field) {
1797 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1798 all_output_options[i].str, event_type(j));
1799 } else
1800 output[j].fields |= all_output_options[i].field;
1801 }
1802 } else {
1803 if (output[type].invalid_fields & all_output_options[i].field) {
1804 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1805 all_output_options[i].str, event_type(type));
1806
1807 rc = -EINVAL;
1808 goto out;
1809 }
1810 output[type].fields |= all_output_options[i].field;
1811 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001812 }
1813
1814 if (type >= 0) {
1815 if (output[type].fields == 0) {
1816 pr_debug("No fields requested for %s type. "
1817 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001818 }
David Ahern1424dc92011-03-09 22:23:28 -07001819 }
David Ahern745f43e2011-03-09 22:23:26 -07001820
David Ahern2c9e45f72011-03-17 10:03:21 -06001821out:
David Ahern745f43e2011-03-09 22:23:26 -07001822 free(str);
1823 return rc;
1824}
1825
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001826/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1827static int is_directory(const char *base_path, const struct dirent *dent)
1828{
1829 char path[PATH_MAX];
1830 struct stat st;
1831
1832 sprintf(path, "%s/%s", base_path, dent->d_name);
1833 if (stat(path, &st))
1834 return 0;
1835
1836 return S_ISDIR(st.st_mode);
1837}
1838
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001839#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1840 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1841 if ((lang_dirent->d_type == DT_DIR || \
1842 (lang_dirent->d_type == DT_UNKNOWN && \
1843 is_directory(scripts_path, lang_dirent))) && \
1844 (strcmp(lang_dirent->d_name, ".")) && \
1845 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001846
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001847#define for_each_script(lang_path, lang_dir, script_dirent) \
1848 while ((script_dirent = readdir(lang_dir)) != NULL) \
1849 if (script_dirent->d_type != DT_DIR && \
1850 (script_dirent->d_type != DT_UNKNOWN || \
1851 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001852
1853
1854#define RECORD_SUFFIX "-record"
1855#define REPORT_SUFFIX "-report"
1856
1857struct script_desc {
1858 struct list_head node;
1859 char *name;
1860 char *half_liner;
1861 char *args;
1862};
1863
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001864static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001865
1866static struct script_desc *script_desc__new(const char *name)
1867{
1868 struct script_desc *s = zalloc(sizeof(*s));
1869
Tom Zanussib5b87312010-11-10 08:16:51 -06001870 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001871 s->name = strdup(name);
1872
1873 return s;
1874}
1875
1876static void script_desc__delete(struct script_desc *s)
1877{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001878 zfree(&s->name);
1879 zfree(&s->half_liner);
1880 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001881 free(s);
1882}
1883
1884static void script_desc__add(struct script_desc *s)
1885{
1886 list_add_tail(&s->node, &script_descs);
1887}
1888
1889static struct script_desc *script_desc__find(const char *name)
1890{
1891 struct script_desc *s;
1892
1893 list_for_each_entry(s, &script_descs, node)
1894 if (strcasecmp(s->name, name) == 0)
1895 return s;
1896 return NULL;
1897}
1898
1899static struct script_desc *script_desc__findnew(const char *name)
1900{
1901 struct script_desc *s = script_desc__find(name);
1902
1903 if (s)
1904 return s;
1905
1906 s = script_desc__new(name);
1907 if (!s)
1908 goto out_delete_desc;
1909
1910 script_desc__add(s);
1911
1912 return s;
1913
1914out_delete_desc:
1915 script_desc__delete(s);
1916
1917 return NULL;
1918}
1919
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001920static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001921{
1922 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001923 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001924
1925 if (strlen(str) > suffix_len) {
1926 p = str + strlen(str) - suffix_len;
1927 if (!strncmp(p, suffix, suffix_len))
1928 return p;
1929 }
1930
1931 return NULL;
1932}
1933
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001934static int read_script_info(struct script_desc *desc, const char *filename)
1935{
1936 char line[BUFSIZ], *p;
1937 FILE *fp;
1938
1939 fp = fopen(filename, "r");
1940 if (!fp)
1941 return -1;
1942
1943 while (fgets(line, sizeof(line), fp)) {
1944 p = ltrim(line);
1945 if (strlen(p) == 0)
1946 continue;
1947 if (*p != '#')
1948 continue;
1949 p++;
1950 if (strlen(p) && *p == '!')
1951 continue;
1952
1953 p = ltrim(p);
1954 if (strlen(p) && p[strlen(p) - 1] == '\n')
1955 p[strlen(p) - 1] = '\0';
1956
1957 if (!strncmp(p, "description:", strlen("description:"))) {
1958 p += strlen("description:");
1959 desc->half_liner = strdup(ltrim(p));
1960 continue;
1961 }
1962
1963 if (!strncmp(p, "args:", strlen("args:"))) {
1964 p += strlen("args:");
1965 desc->args = strdup(ltrim(p));
1966 continue;
1967 }
1968 }
1969
1970 fclose(fp);
1971
1972 return 0;
1973}
1974
Robert Richter38efb532011-11-25 11:38:40 +01001975static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1976{
1977 char *script_root, *str;
1978
1979 script_root = strdup(script_dirent->d_name);
1980 if (!script_root)
1981 return NULL;
1982
1983 str = (char *)ends_with(script_root, suffix);
1984 if (!str) {
1985 free(script_root);
1986 return NULL;
1987 }
1988
1989 *str = '\0';
1990 return script_root;
1991}
1992
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001993static int list_available_scripts(const struct option *opt __maybe_unused,
1994 const char *s __maybe_unused,
1995 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001996{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001997 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001998 char scripts_path[MAXPATHLEN];
1999 DIR *scripts_dir, *lang_dir;
2000 char script_path[MAXPATHLEN];
2001 char lang_path[MAXPATHLEN];
2002 struct script_desc *desc;
2003 char first_half[BUFSIZ];
2004 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002005
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002006 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002007
2008 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002009 if (!scripts_dir) {
2010 fprintf(stdout,
2011 "open(%s) failed.\n"
2012 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2013 scripts_path);
2014 exit(-1);
2015 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002016
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002017 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002018 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002019 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002020 lang_dir = opendir(lang_path);
2021 if (!lang_dir)
2022 continue;
2023
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002024 for_each_script(lang_path, lang_dir, script_dirent) {
2025 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002026 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002027 desc = script_desc__findnew(script_root);
2028 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002029 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002030 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002031 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002032 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002033 }
2034 }
2035
2036 fprintf(stdout, "List of available trace scripts:\n");
2037 list_for_each_entry(desc, &script_descs, node) {
2038 sprintf(first_half, "%s %s", desc->name,
2039 desc->args ? desc->args : "");
2040 fprintf(stdout, " %-36s %s\n", first_half,
2041 desc->half_liner ? desc->half_liner : "");
2042 }
2043
2044 exit(0);
2045}
2046
Feng Tange5f37052012-09-07 16:42:26 +08002047/*
Feng Tang49e639e2012-10-30 11:56:03 +08002048 * Some scripts specify the required events in their "xxx-record" file,
2049 * this function will check if the events in perf.data match those
2050 * mentioned in the "xxx-record".
2051 *
2052 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2053 * which is covered well now. And new parsing code should be added to
2054 * cover the future complexing formats like event groups etc.
2055 */
2056static int check_ev_match(char *dir_name, char *scriptname,
2057 struct perf_session *session)
2058{
2059 char filename[MAXPATHLEN], evname[128];
2060 char line[BUFSIZ], *p;
2061 struct perf_evsel *pos;
2062 int match, len;
2063 FILE *fp;
2064
2065 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2066
2067 fp = fopen(filename, "r");
2068 if (!fp)
2069 return -1;
2070
2071 while (fgets(line, sizeof(line), fp)) {
2072 p = ltrim(line);
2073 if (*p == '#')
2074 continue;
2075
2076 while (strlen(p)) {
2077 p = strstr(p, "-e");
2078 if (!p)
2079 break;
2080
2081 p += 2;
2082 p = ltrim(p);
2083 len = strcspn(p, " \t");
2084 if (!len)
2085 break;
2086
2087 snprintf(evname, len + 1, "%s", p);
2088
2089 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002090 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002091 if (!strcmp(perf_evsel__name(pos), evname)) {
2092 match = 1;
2093 break;
2094 }
2095 }
2096
2097 if (!match) {
2098 fclose(fp);
2099 return -1;
2100 }
2101 }
2102 }
2103
2104 fclose(fp);
2105 return 0;
2106}
2107
2108/*
Feng Tange5f37052012-09-07 16:42:26 +08002109 * Return -1 if none is found, otherwise the actual scripts number.
2110 *
2111 * Currently the only user of this function is the script browser, which
2112 * will list all statically runnable scripts, select one, execute it and
2113 * show the output in a perf browser.
2114 */
2115int find_scripts(char **scripts_array, char **scripts_path_array)
2116{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002117 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002118 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002119 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002120 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002121 struct perf_data_file file = {
2122 .path = input_name,
2123 .mode = PERF_DATA_MODE_READ,
2124 };
Feng Tange5f37052012-09-07 16:42:26 +08002125 char *temp;
2126 int i = 0;
2127
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002128 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002129 if (!session)
2130 return -1;
2131
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002132 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002133
2134 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002135 if (!scripts_dir) {
2136 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002137 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002138 }
Feng Tange5f37052012-09-07 16:42:26 +08002139
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002140 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002141 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002142 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002143#ifdef NO_LIBPERL
2144 if (strstr(lang_path, "perl"))
2145 continue;
2146#endif
2147#ifdef NO_LIBPYTHON
2148 if (strstr(lang_path, "python"))
2149 continue;
2150#endif
2151
2152 lang_dir = opendir(lang_path);
2153 if (!lang_dir)
2154 continue;
2155
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002156 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002157 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002158 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002159 continue;
2160 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002161 script_dirent->d_name);
2162 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002163 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002164 (temp - script_dirent->d_name) + 1,
2165 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002166
2167 if (check_ev_match(lang_path,
2168 scripts_array[i], session))
2169 continue;
2170
Feng Tange5f37052012-09-07 16:42:26 +08002171 i++;
2172 }
Feng Tang49e639e2012-10-30 11:56:03 +08002173 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002174 }
2175
Feng Tang49e639e2012-10-30 11:56:03 +08002176 closedir(scripts_dir);
2177 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002178 return i;
2179}
2180
Tom Zanussi38752942009-12-15 02:53:39 -06002181static char *get_script_path(const char *script_root, const char *suffix)
2182{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002183 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002184 char scripts_path[MAXPATHLEN];
2185 char script_path[MAXPATHLEN];
2186 DIR *scripts_dir, *lang_dir;
2187 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002188 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002189
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002190 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002191
2192 scripts_dir = opendir(scripts_path);
2193 if (!scripts_dir)
2194 return NULL;
2195
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002196 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002197 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002198 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002199 lang_dir = opendir(lang_path);
2200 if (!lang_dir)
2201 continue;
2202
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002203 for_each_script(lang_path, lang_dir, script_dirent) {
2204 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002205 if (__script_root && !strcmp(script_root, __script_root)) {
2206 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002207 closedir(lang_dir);
2208 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002209 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002210 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002211 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002212 }
2213 free(__script_root);
2214 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002215 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002216 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002217 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002218
Robert Richter38efb532011-11-25 11:38:40 +01002219 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002220}
2221
Tom Zanussib5b87312010-11-10 08:16:51 -06002222static bool is_top_script(const char *script_path)
2223{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002224 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002225}
2226
2227static int has_required_arg(char *script_path)
2228{
2229 struct script_desc *desc;
2230 int n_args = 0;
2231 char *p;
2232
2233 desc = script_desc__new(NULL);
2234
2235 if (read_script_info(desc, script_path))
2236 goto out;
2237
2238 if (!desc->args)
2239 goto out;
2240
2241 for (p = desc->args; *p; p++)
2242 if (*p == '<')
2243 n_args++;
2244out:
2245 script_desc__delete(desc);
2246
2247 return n_args;
2248}
2249
David Ahernd54b1a92012-08-26 12:24:46 -06002250static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002251{
2252 char **__argv = malloc(sizeof(const char *) * argc);
2253
David Ahernd54b1a92012-08-26 12:24:46 -06002254 if (!__argv) {
2255 pr_err("malloc failed\n");
2256 return -1;
2257 }
2258
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002259 memcpy(__argv, argv, sizeof(const char *) * argc);
2260 argc = parse_options(argc, (const char **)__argv, record_options,
2261 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2262 free(__argv);
2263
David Ahernd54b1a92012-08-26 12:24:46 -06002264 system_wide = (argc == 0);
2265
2266 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002267}
2268
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002269static void script__setup_sample_type(struct perf_script *script)
2270{
2271 struct perf_session *session = script->session;
2272 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2273
2274 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2275 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2276 (sample_type & PERF_SAMPLE_STACK_USER))
2277 callchain_param.record_mode = CALLCHAIN_DWARF;
2278 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2279 callchain_param.record_mode = CALLCHAIN_LBR;
2280 else
2281 callchain_param.record_mode = CALLCHAIN_FP;
2282 }
2283}
2284
Jiri Olsae099eba2016-01-05 22:09:09 +01002285static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2286 union perf_event *event,
2287 struct perf_session *session)
2288{
2289 struct stat_round_event *round = &event->stat_round;
2290 struct perf_evsel *counter;
2291
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002292 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002293 perf_stat_process_counter(&stat_config, counter);
2294 process_stat(counter, round->time);
2295 }
2296
2297 process_stat_interval(round->time);
2298 return 0;
2299}
2300
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002301static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2302 union perf_event *event,
2303 struct perf_session *session __maybe_unused)
2304{
2305 perf_event__read_stat_config(&stat_config, &event->stat_config);
2306 return 0;
2307}
2308
Jiri Olsacfc88742016-01-05 22:09:06 +01002309static int set_maps(struct perf_script *script)
2310{
2311 struct perf_evlist *evlist = script->session->evlist;
2312
2313 if (!script->cpus || !script->threads)
2314 return 0;
2315
2316 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2317 return -EINVAL;
2318
2319 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2320
2321 if (perf_evlist__alloc_stats(evlist, true))
2322 return -ENOMEM;
2323
2324 script->allocated = true;
2325 return 0;
2326}
2327
2328static
2329int process_thread_map_event(struct perf_tool *tool,
2330 union perf_event *event,
2331 struct perf_session *session __maybe_unused)
2332{
2333 struct perf_script *script = container_of(tool, struct perf_script, tool);
2334
2335 if (script->threads) {
2336 pr_warning("Extra thread map event, ignoring.\n");
2337 return 0;
2338 }
2339
2340 script->threads = thread_map__new_event(&event->thread_map);
2341 if (!script->threads)
2342 return -ENOMEM;
2343
2344 return set_maps(script);
2345}
2346
2347static
2348int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2349 union perf_event *event,
2350 struct perf_session *session __maybe_unused)
2351{
2352 struct perf_script *script = container_of(tool, struct perf_script, tool);
2353
2354 if (script->cpus) {
2355 pr_warning("Extra cpu map event, ignoring.\n");
2356 return 0;
2357 }
2358
2359 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2360 if (!script->cpus)
2361 return -ENOMEM;
2362
2363 return set_maps(script);
2364}
2365
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002366int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002367{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002368 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002369 bool header = false;
2370 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002371 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002372 char *rec_script_path = NULL;
2373 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002374 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002375 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002376 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002377 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002378 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002379 struct perf_script script = {
2380 .tool = {
2381 .sample = process_sample_event,
2382 .mmap = perf_event__process_mmap,
2383 .mmap2 = perf_event__process_mmap2,
2384 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302385 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002386 .exit = perf_event__process_exit,
2387 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002388 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002389 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002390 .tracing_data = perf_event__process_tracing_data,
2391 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002392 .id_index = perf_event__process_id_index,
2393 .auxtrace_info = perf_event__process_auxtrace_info,
2394 .auxtrace = perf_event__process_auxtrace,
2395 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002396 .stat = perf_event__process_stat_event,
2397 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002398 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002399 .thread_map = process_thread_map_event,
2400 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002401 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002402 .ordering_requires_timestamps = true,
2403 },
2404 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002405 struct perf_data_file file = {
2406 .mode = PERF_DATA_MODE_READ,
2407 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002408 const struct option options[] = {
2409 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2410 "dump raw trace in ASCII"),
2411 OPT_INCR('v', "verbose", &verbose,
2412 "be more verbose (show symbol address, etc)"),
2413 OPT_BOOLEAN('L', "Latency", &latency_format,
2414 "show latency attributes (irqs/preemption disabled, etc)"),
2415 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2416 list_available_scripts),
2417 OPT_CALLBACK('s', "script", NULL, "name",
2418 "script file name (lang:script name, script name, or *)",
2419 parse_scriptname),
2420 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2421 "generate perf-script.xx script in specified language"),
2422 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2423 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2424 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002425 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2426 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002427 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2428 "file", "vmlinux pathname"),
2429 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2430 "file", "kallsyms pathname"),
2431 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2432 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002433 OPT_CALLBACK(0, "symfs", NULL, "directory",
2434 "Look for files with symbols relative to this directory",
2435 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002436 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002437 "comma separated output fields prepend with 'type:'. "
2438 "Valid types: hw,sw,trace,raw. "
2439 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Huntere2167082016-06-23 16:40:58 +03002440 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
Andi Kleen48d02a12017-02-23 15:46:34 -08002441 "bpf-output,callindent,insn,insnlen,brstackinsn",
2442 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002443 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2444 "system-wide collection from all CPUs"),
2445 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2446 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002447 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2448 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002449 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2450 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2451 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002452 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2453 "only consider symbols in these pids"),
2454 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2455 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002456 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2457 "Set the maximum stack depth when parsing the callchain, "
2458 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002459 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002460 OPT_BOOLEAN('I', "show-info", &show_full_info,
2461 "display extended information from perf.data file"),
2462 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2463 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002464 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2465 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002466 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2467 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002468 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2469 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302470 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2471 "Show namespace events (if recorded)"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002472 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002473 OPT_INTEGER(0, "max-blocks", &max_blocks,
2474 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002475 OPT_BOOLEAN(0, "ns", &nanosecs,
2476 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002477 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2478 "Instruction Tracing options",
2479 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002480 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2481 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002482 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2483 "Enable symbol demangling"),
2484 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2485 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002486 OPT_STRING(0, "time", &script.time_str, "str",
2487 "Time span of interest (start,stop)"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002488 OPT_END()
2489 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002490 const char * const script_subcommands[] = { "record", "report", NULL };
2491 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002492 "perf script [<options>]",
2493 "perf script [<options>] record <script> [<record-options>] <command>",
2494 "perf script [<options>] report <script> [script-args]",
2495 "perf script [<options>] <script> [<record-options>] <command>",
2496 "perf script [<options>] <top-script> [script-args]",
2497 NULL
2498 };
Tom Zanussi38752942009-12-15 02:53:39 -06002499
Tom Zanussib5b87312010-11-10 08:16:51 -06002500 setup_scripting();
2501
Yunlong Song40cae2b2015-03-18 21:35:54 +08002502 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002503 PARSE_OPT_STOP_AT_NON_OPTION);
2504
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002505 file.path = input_name;
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002506 file.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002507
Tom Zanussib5b87312010-11-10 08:16:51 -06002508 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2509 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2510 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002511 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002512 }
2513
Tom Zanussib5b87312010-11-10 08:16:51 -06002514 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2515 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2516 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002517 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002518 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002519 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002520 return -1;
2521 }
Tom Zanussi38752942009-12-15 02:53:39 -06002522 }
2523
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002524 if (itrace_synth_opts.callchain &&
2525 itrace_synth_opts.callchain_sz > scripting_max_stack)
2526 scripting_max_stack = itrace_synth_opts.callchain_sz;
2527
Ben Hutchings44e668c2010-10-10 16:10:03 +01002528 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002529 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002530
Tom Zanussib5b87312010-11-10 08:16:51 -06002531 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002532 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002533 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002534 pid_t pid;
2535
Tom Zanussib5b87312010-11-10 08:16:51 -06002536 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2537 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2538
2539 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002540 usage_with_options_msg(script_usage, options,
2541 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002542 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002543 }
2544
Tom Zanussib5b87312010-11-10 08:16:51 -06002545 if (is_top_script(argv[0])) {
2546 rep_args = argc - 1;
2547 } else {
2548 int rec_args;
2549
2550 rep_args = has_required_arg(rep_script_path);
2551 rec_args = (argc - 1) - rep_args;
2552 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002553 usage_with_options_msg(script_usage, options,
2554 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002555 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002556 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002557 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002558 }
2559
2560 if (pipe(live_pipe) < 0) {
2561 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002562 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002563 }
2564
2565 pid = fork();
2566 if (pid < 0) {
2567 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002568 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002569 }
2570
2571 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002572 j = 0;
2573
Tom Zanussia0cccc22010-04-01 23:59:25 -05002574 dup2(live_pipe[1], 1);
2575 close(live_pipe[0]);
2576
Robert Richter317df652011-11-25 15:05:25 +01002577 if (is_top_script(argv[0])) {
2578 system_wide = true;
2579 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002580 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2581 err = -1;
2582 goto out;
2583 }
Robert Richter317df652011-11-25 15:05:25 +01002584 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002585
2586 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002587 if (!__argv) {
2588 pr_err("malloc failed\n");
2589 err = -ENOMEM;
2590 goto out;
2591 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002592
Tom Zanussib5b87312010-11-10 08:16:51 -06002593 __argv[j++] = "/bin/sh";
2594 __argv[j++] = rec_script_path;
2595 if (system_wide)
2596 __argv[j++] = "-a";
2597 __argv[j++] = "-q";
2598 __argv[j++] = "-o";
2599 __argv[j++] = "-";
2600 for (i = rep_args + 1; i < argc; i++)
2601 __argv[j++] = argv[i];
2602 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002603
2604 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002605 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002606 exit(-1);
2607 }
2608
2609 dup2(live_pipe[0], 0);
2610 close(live_pipe[1]);
2611
Tom Zanussib5b87312010-11-10 08:16:51 -06002612 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002613 if (!__argv) {
2614 pr_err("malloc failed\n");
2615 err = -ENOMEM;
2616 goto out;
2617 }
2618
Tom Zanussib5b87312010-11-10 08:16:51 -06002619 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002620 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002621 __argv[j++] = rep_script_path;
2622 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002623 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002624 __argv[j++] = "-i";
2625 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002626 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002627
2628 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002629 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002630 exit(-1);
2631 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002632
Tom Zanussib5b87312010-11-10 08:16:51 -06002633 if (rec_script_path)
2634 script_path = rec_script_path;
2635 if (rep_script_path)
2636 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002637
Tom Zanussib5b87312010-11-10 08:16:51 -06002638 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002639 j = 0;
2640
Robert Richter317df652011-11-25 15:05:25 +01002641 if (!rec_script_path)
2642 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002643 else if (!system_wide) {
2644 if (have_cmd(argc - 1, &argv[1]) != 0) {
2645 err = -1;
2646 goto out;
2647 }
2648 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002649
2650 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002651 if (!__argv) {
2652 pr_err("malloc failed\n");
2653 err = -ENOMEM;
2654 goto out;
2655 }
2656
Tom Zanussib5b87312010-11-10 08:16:51 -06002657 __argv[j++] = "/bin/sh";
2658 __argv[j++] = script_path;
2659 if (system_wide)
2660 __argv[j++] = "-a";
2661 for (i = 2; i < argc; i++)
2662 __argv[j++] = argv[i];
2663 __argv[j++] = NULL;
2664
2665 execvp("/bin/sh", (char **)__argv);
2666 free(__argv);
2667 exit(-1);
2668 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002669
Tom Zanussicf4fee52010-03-03 01:04:33 -06002670 if (!script_name)
2671 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002672
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002673 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002674 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002675 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002676
Jiri Olsae90debd2013-12-09 11:02:50 +01002677 if (header || header_only) {
2678 perf_session__fprintf_info(session, stdout, show_full_info);
2679 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002680 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002681 }
2682
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002683 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002684 goto out_delete;
2685
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002686 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002687 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002688
Adrian Huntere2167082016-06-23 16:40:58 +03002689 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2690 itrace_synth_opts.thread_stack = true;
2691
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002692 session->itrace_synth_opts = &itrace_synth_opts;
2693
Anton Blanchard5d67be92011-07-04 21:57:50 +10002694 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002695 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2696 if (err < 0)
2697 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002698 }
2699
David Ahern1424dc92011-03-09 22:23:28 -07002700 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002701 symbol_conf.use_callchain = true;
2702 else
2703 symbol_conf.use_callchain = false;
2704
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002705 if (session->tevent.pevent &&
2706 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002707 machine__resolve_kernel_addr,
2708 &session->machines.host) < 0) {
2709 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2710 return -1;
2711 }
2712
Tom Zanussi956ffd02009-11-25 01:15:46 -06002713 if (generate_script_lang) {
2714 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002715 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002716
David Ahern2c9e45f72011-03-17 10:03:21 -06002717 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002718 fprintf(stderr,
2719 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002720 err = -EINVAL;
2721 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002722 }
2723
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002724 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002725 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002726 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002727 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002728 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002729 }
2730
2731 err = fstat(input, &perf_stat);
2732 if (err < 0) {
2733 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002734 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002735 }
2736
2737 if (!perf_stat.st_size) {
2738 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002739 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002740 }
2741
2742 scripting_ops = script_spec__lookup(generate_script_lang);
2743 if (!scripting_ops) {
2744 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002745 err = -ENOENT;
2746 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002747 }
2748
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002749 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002750 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002751 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002752 }
2753
2754 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002755 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002756 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002757 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002758 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002759 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002760 }
2761
David Ahern9cbdb702011-04-06 21:54:20 -06002762
2763 err = perf_session__check_output_opt(session);
2764 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002765 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002766
David Aherna91f4c42016-11-29 10:15:43 -07002767 /* needs to be parsed after looking up reference time */
2768 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
2769 pr_err("Invalid time string\n");
2770 return -EINVAL;
2771 }
2772
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002773 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002774
Adrian Hunterd445dd22014-08-15 22:08:37 +03002775 flush_scripting();
2776
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002777out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002778 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002779 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002780
2781 if (script_started)
2782 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002783out:
2784 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002785}