blob: 853651a0f7202f4a6505e1312da3a53d078bdac8 [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#include "builtin.h"
2
Andrea Gelminib7eead82010-08-05 15:51:38 +02003#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02004#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02005#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06006#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02007#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06008#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +02009#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020010#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020011#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020012#include "util/symbol.h"
13#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010014#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020015#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070016#include "util/evlist.h"
17#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080018#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030020#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010021#include "util/cpumap.h"
22#include "util/thread_map.h"
23#include "util/stat.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030024#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030025#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070026#include "util/time-utils.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030027#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100028#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030029#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030030#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030031#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010032#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010033#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080034#include "util/dump-insn.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030035#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030036#include <inttypes.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020037
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030038#include "sane_ctype.h"
39
Tom Zanussi956ffd02009-11-25 01:15:46 -060040static char const *script_name;
41static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020042static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020043static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020044static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070045static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090046static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010047static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030048static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030049static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100050static const char *cpu_list;
51static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010052static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080053static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060054
Adrian Hunter44cbe722015-09-25 16:15:50 +030055unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030056
David Ahern745f43e2011-03-09 22:23:26 -070057enum perf_output_field {
58 PERF_OUTPUT_COMM = 1U << 0,
59 PERF_OUTPUT_TID = 1U << 1,
60 PERF_OUTPUT_PID = 1U << 2,
61 PERF_OUTPUT_TIME = 1U << 3,
62 PERF_OUTPUT_CPU = 1U << 4,
63 PERF_OUTPUT_EVNAME = 1U << 5,
64 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060065 PERF_OUTPUT_IP = 1U << 7,
66 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060067 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060068 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090069 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020070 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020071 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020072 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020073 PERF_OUTPUT_BRSTACK = 1U << 15,
74 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010075 PERF_OUTPUT_DATA_SRC = 1U << 17,
76 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000077 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030078 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030079 PERF_OUTPUT_INSN = 1U << 21,
80 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080081 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
David Ahern745f43e2011-03-09 22:23:26 -070082};
83
84struct output_option {
85 const char *str;
86 enum perf_output_field field;
87} all_output_options[] = {
88 {.str = "comm", .field = PERF_OUTPUT_COMM},
89 {.str = "tid", .field = PERF_OUTPUT_TID},
90 {.str = "pid", .field = PERF_OUTPUT_PID},
91 {.str = "time", .field = PERF_OUTPUT_TIME},
92 {.str = "cpu", .field = PERF_OUTPUT_CPU},
93 {.str = "event", .field = PERF_OUTPUT_EVNAME},
94 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060095 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070096 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060097 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060098 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090099 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200100 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200101 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200102 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200103 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
104 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100105 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
106 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000107 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300108 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300109 {.str = "insn", .field = PERF_OUTPUT_INSN},
110 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800111 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
David Ahern745f43e2011-03-09 22:23:26 -0700112};
113
114/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600115static struct {
116 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600117 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400118 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600119 u64 fields;
120 u64 invalid_fields;
121} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700122
David Ahern2c9e45f72011-03-17 10:03:21 -0600123 [PERF_TYPE_HARDWARE] = {
124 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700125
David Ahern2c9e45f72011-03-17 10:03:21 -0600126 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
127 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600128 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200129 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
130 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600131
Wang Nan30372f02016-02-24 11:20:45 +0000132 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600133 },
134
135 [PERF_TYPE_SOFTWARE] = {
136 .user_set = false,
137
138 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
139 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600140 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200141 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000142 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600143
144 .invalid_fields = PERF_OUTPUT_TRACE,
145 },
146
147 [PERF_TYPE_TRACEPOINT] = {
148 .user_set = false,
149
150 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
151 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000152 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600153 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700154
155 [PERF_TYPE_RAW] = {
156 .user_set = false,
157
158 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
159 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600160 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200161 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100162 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
163 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700164
Wang Nan30372f02016-02-24 11:20:45 +0000165 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700166 },
Wang Nan27cfef02015-12-08 02:25:43 +0000167
168 [PERF_TYPE_BREAKPOINT] = {
169 .user_set = false,
170
171 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
172 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
173 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
174 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
175 PERF_OUTPUT_PERIOD,
176
Wang Nan30372f02016-02-24 11:20:45 +0000177 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000178 },
David Ahern1424dc92011-03-09 22:23:28 -0700179};
David Ahern745f43e2011-03-09 22:23:26 -0700180
David Ahern2c9e45f72011-03-17 10:03:21 -0600181static bool output_set_by_user(void)
182{
183 int j;
184 for (j = 0; j < PERF_TYPE_MAX; ++j) {
185 if (output[j].user_set)
186 return true;
187 }
188 return false;
189}
David Ahern745f43e2011-03-09 22:23:26 -0700190
David Ahern9cbdb702011-04-06 21:54:20 -0600191static const char *output_field2str(enum perf_output_field field)
192{
193 int i, imax = ARRAY_SIZE(all_output_options);
194 const char *str = "";
195
196 for (i = 0; i < imax; ++i) {
197 if (all_output_options[i].field == field) {
198 str = all_output_options[i].str;
199 break;
200 }
201 }
202 return str;
203}
204
David Ahern2c9e45f72011-03-17 10:03:21 -0600205#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700206
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300207static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
208 u64 sample_type, const char *sample_msg,
209 enum perf_output_field field,
210 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700211{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300212 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600213 int type = attr->type;
214 const char *evname;
215
216 if (attr->sample_type & sample_type)
217 return 0;
218
219 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300220 if (allow_user_set)
221 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300222 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600223 pr_err("Samples for '%s' event do not have %s attribute set. "
224 "Cannot print '%s' field.\n",
225 evname, sample_msg, output_field2str(field));
226 return -1;
227 }
228
229 /* user did not ask for it explicitly so remove from the default list */
230 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300231 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600232 pr_debug("Samples for '%s' event do not have %s attribute set. "
233 "Skipping '%s' field.\n",
234 evname, sample_msg, output_field2str(field));
235
236 return 0;
237}
238
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300239static int perf_evsel__check_stype(struct perf_evsel *evsel,
240 u64 sample_type, const char *sample_msg,
241 enum perf_output_field field)
242{
243 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
244 false);
245}
246
David Ahern9cbdb702011-04-06 21:54:20 -0600247static int perf_evsel__check_attr(struct perf_evsel *evsel,
248 struct perf_session *session)
249{
250 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300251 bool allow_user_set;
252
Jiri Olsae099eba2016-01-05 22:09:09 +0100253 if (perf_header__has_feat(&session->header, HEADER_STAT))
254 return 0;
255
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300256 allow_user_set = perf_header__has_feat(&session->header,
257 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600258
David Ahern1424dc92011-03-09 22:23:28 -0700259 if (PRINT_FIELD(TRACE) &&
260 !perf_session__has_traces(session, "record -R"))
261 return -EINVAL;
262
David Ahern787bef12011-05-27 14:28:43 -0600263 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300264 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
265 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700266 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700267 }
David Ahern7cec0922011-05-30 13:08:23 -0600268
269 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300270 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
271 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600272 return -EINVAL;
273
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100274 if (PRINT_FIELD(DATA_SRC) &&
275 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
276 PERF_OUTPUT_DATA_SRC))
277 return -EINVAL;
278
279 if (PRINT_FIELD(WEIGHT) &&
280 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
281 PERF_OUTPUT_WEIGHT))
282 return -EINVAL;
283
David Ahern7cec0922011-05-30 13:08:23 -0600284 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
285 pr_err("Display of symbols requested but neither sample IP nor "
286 "sample address\nis selected. Hence, no addresses to convert "
287 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600288 return -EINVAL;
289 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900290 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
291 pr_err("Display of offsets requested but symbol is not"
292 "selected.\n");
293 return -EINVAL;
294 }
David Ahern7cec0922011-05-30 13:08:23 -0600295 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
296 pr_err("Display of DSO requested but neither sample IP nor "
297 "sample address\nis selected. Hence, no addresses to convert "
298 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600299 return -EINVAL;
300 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200301 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
302 pr_err("Display of source line number requested but sample IP is not\n"
303 "selected. Hence, no address to lookup the source line number.\n");
304 return -EINVAL;
305 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800306 if (PRINT_FIELD(BRSTACKINSN) &&
307 !(perf_evlist__combined_branch_type(session->evlist) &
308 PERF_SAMPLE_BRANCH_ANY)) {
309 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
310 "Hint: run 'perf record -b ...'\n");
311 return -EINVAL;
312 }
David Ahern1424dc92011-03-09 22:23:28 -0700313 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300314 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
315 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700316 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700317
318 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300319 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
320 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700321 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700322
323 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300324 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
325 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700326 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600327
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200328 if (PRINT_FIELD(PERIOD) &&
329 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
330 PERF_OUTPUT_PERIOD))
331 return -EINVAL;
332
Stephane Eranianfc36f942015-08-31 18:41:10 +0200333 if (PRINT_FIELD(IREGS) &&
334 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
335 PERF_OUTPUT_IREGS))
336 return -EINVAL;
337
David Ahern9cbdb702011-04-06 21:54:20 -0600338 return 0;
339}
340
Adrian Hunter7ea95722013-11-01 15:51:30 +0200341static void set_print_ip_opts(struct perf_event_attr *attr)
342{
343 unsigned int type = attr->type;
344
345 output[type].print_ip_opts = 0;
346 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300347 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200348
349 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300350 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200351
352 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300353 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200354
355 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300356 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200357
358 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300359 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200360}
361
David Ahern9cbdb702011-04-06 21:54:20 -0600362/*
363 * verify all user requested events exist and the samples
364 * have the expected data
365 */
366static int perf_session__check_output_opt(struct perf_session *session)
367{
He Kuang40f20e52016-05-16 04:51:19 +0000368 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600369 struct perf_evsel *evsel;
370
371 for (j = 0; j < PERF_TYPE_MAX; ++j) {
372 evsel = perf_session__find_first_evtype(session, j);
373
374 /*
375 * even if fields is set to 0 (ie., show nothing) event must
376 * exist if user explicitly includes it on the command line
377 */
378 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
379 pr_err("%s events do not exist. "
380 "Remove corresponding -f option to proceed.\n",
381 event_type(j));
382 return -1;
383 }
384
385 if (evsel && output[j].fields &&
386 perf_evsel__check_attr(evsel, session))
387 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400388
389 if (evsel == NULL)
390 continue;
391
Adrian Hunter7ea95722013-11-01 15:51:30 +0200392 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700393 }
394
Adrian Hunter98526ee2014-07-31 09:00:59 +0300395 if (!no_callchain) {
396 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000397 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300398
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300399 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000400 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300401 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
402 use_callchain = true;
403 break;
404 }
405 }
He Kuang71ac8992016-08-04 11:25:43 +0000406 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300407 symbol_conf.use_callchain = false;
408 }
409
David Ahern80b8b492013-11-19 21:07:37 -0700410 /*
411 * set default for tracepoints to print symbols only
412 * if callchains are present
413 */
414 if (symbol_conf.use_callchain &&
415 !output[PERF_TYPE_TRACEPOINT].user_set) {
416 struct perf_event_attr *attr;
417
418 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700419
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300420 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000421 if (evsel->attr.type != j)
422 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700423
He Kuang40f20e52016-05-16 04:51:19 +0000424 attr = &evsel->attr;
425
426 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
427 output[j].fields |= PERF_OUTPUT_IP;
428 output[j].fields |= PERF_OUTPUT_SYM;
429 output[j].fields |= PERF_OUTPUT_DSO;
430 set_print_ip_opts(attr);
431 goto out;
432 }
David Ahern80b8b492013-11-19 21:07:37 -0700433 }
434 }
435
436out:
David Ahern1424dc92011-03-09 22:23:28 -0700437 return 0;
438}
David Ahern745f43e2011-03-09 22:23:26 -0700439
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300440static void print_sample_iregs(struct perf_sample *sample,
Stephane Eranianfc36f942015-08-31 18:41:10 +0200441 struct perf_event_attr *attr)
442{
443 struct regs_dump *regs = &sample->intr_regs;
444 uint64_t mask = attr->sample_regs_intr;
445 unsigned i = 0, r;
446
447 if (!regs)
448 return;
449
450 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
451 u64 val = regs->regs[i++];
452 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
453 }
454}
455
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300456static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700457 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300458 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700459{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300460 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700461 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700462 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700463
David Ahern745f43e2011-03-09 22:23:26 -0700464 if (PRINT_FIELD(COMM)) {
465 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200466 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600467 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200468 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700469 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200470 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700471 }
David Ahernc70c94b2011-03-09 22:23:25 -0700472
David Ahern745f43e2011-03-09 22:23:26 -0700473 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
474 printf("%5d/%-5d ", sample->pid, sample->tid);
475 else if (PRINT_FIELD(PID))
476 printf("%5d ", sample->pid);
477 else if (PRINT_FIELD(TID))
478 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700479
David Ahern745f43e2011-03-09 22:23:26 -0700480 if (PRINT_FIELD(CPU)) {
481 if (latency_format)
482 printf("%3d ", sample->cpu);
483 else
484 printf("[%03d] ", sample->cpu);
485 }
David Ahernc70c94b2011-03-09 22:23:25 -0700486
David Ahern745f43e2011-03-09 22:23:26 -0700487 if (PRINT_FIELD(TIME)) {
488 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300489 secs = nsecs / NSEC_PER_SEC;
490 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900491
Adrian Hunter83e19862015-09-25 16:15:36 +0300492 if (nanosecs)
493 printf("%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900494 else {
495 char sample_time[32];
496 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
497 printf("%12s: ", sample_time);
498 }
David Ahern745f43e2011-03-09 22:23:26 -0700499 }
David Ahernc70c94b2011-03-09 22:23:25 -0700500}
501
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200502static inline char
503mispred_str(struct branch_entry *br)
504{
505 if (!(br->flags.mispred || br->flags.predicted))
506 return '-';
507
508 return br->flags.predicted ? 'P' : 'M';
509}
510
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300511static void print_sample_brstack(struct perf_sample *sample)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200512{
513 struct branch_stack *br = sample->branch_stack;
514 u64 i;
515
516 if (!(br && br->nr))
517 return;
518
519 for (i = 0; i < br->nr; i++) {
520 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
521 br->entries[i].from,
522 br->entries[i].to,
523 mispred_str( br->entries + i),
524 br->entries[i].flags.in_tx? 'X' : '-',
525 br->entries[i].flags.abort? 'A' : '-',
526 br->entries[i].flags.cycles);
527 }
528}
529
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300530static void print_sample_brstacksym(struct perf_sample *sample,
531 struct thread *thread)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200532{
533 struct branch_stack *br = sample->branch_stack;
534 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200535 u64 i, from, to;
536
537 if (!(br && br->nr))
538 return;
539
540 for (i = 0; i < br->nr; i++) {
541
542 memset(&alf, 0, sizeof(alf));
543 memset(&alt, 0, sizeof(alt));
544 from = br->entries[i].from;
545 to = br->entries[i].to;
546
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300547 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200548 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300549 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200550
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300551 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200552 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300553 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200554
555 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
556 putchar('/');
557 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
558 printf("/%c/%c/%c/%d ",
559 mispred_str( br->entries + i),
560 br->entries[i].flags.in_tx? 'X' : '-',
561 br->entries[i].flags.abort? 'A' : '-',
562 br->entries[i].flags.cycles);
563 }
564}
565
Andi Kleen48d02a12017-02-23 15:46:34 -0800566#define MAXBB 16384UL
567
568static int grab_bb(u8 *buffer, u64 start, u64 end,
569 struct machine *machine, struct thread *thread,
570 bool *is64bit, u8 *cpumode, bool last)
571{
572 long offset, len;
573 struct addr_location al;
574 bool kernel;
575
576 if (!start || !end)
577 return 0;
578
579 kernel = machine__kernel_ip(machine, start);
580 if (kernel)
581 *cpumode = PERF_RECORD_MISC_KERNEL;
582 else
583 *cpumode = PERF_RECORD_MISC_USER;
584
585 /*
586 * Block overlaps between kernel and user.
587 * This can happen due to ring filtering
588 * On Intel CPUs the entry into the kernel is filtered,
589 * but the exit is not. Let the caller patch it up.
590 */
591 if (kernel != machine__kernel_ip(machine, end)) {
592 printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
593 start, end);
594 return -ENXIO;
595 }
596
597 memset(&al, 0, sizeof(al));
598 if (end - start > MAXBB - MAXINSN) {
599 if (last)
600 printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
601 else
602 printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
603 return 0;
604 }
605
606 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
607 if (!al.map || !al.map->dso) {
608 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
609 return 0;
610 }
611 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
612 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
613 return 0;
614 }
615
616 /* Load maps to ensure dso->is_64_bit has been updated */
617 map__load(al.map);
618
619 offset = al.map->map_ip(al.map, start);
620 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
621 end - start + MAXINSN);
622
623 *is64bit = al.map->dso->is_64_bit;
624 if (len <= 0)
625 printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
626 start, end);
627 return len;
628}
629
630static void print_jump(uint64_t ip, struct branch_entry *en,
631 struct perf_insn *x, u8 *inbuf, int len,
632 int insn)
633{
634 printf("\t%016" PRIx64 "\t%-30s\t#%s%s%s%s",
635 ip,
636 dump_insn(x, ip, inbuf, len, NULL),
637 en->flags.predicted ? " PRED" : "",
638 en->flags.mispred ? " MISPRED" : "",
639 en->flags.in_tx ? " INTX" : "",
640 en->flags.abort ? " ABORT" : "");
641 if (en->flags.cycles) {
642 printf(" %d cycles", en->flags.cycles);
643 if (insn)
644 printf(" %.2f IPC", (float)insn / en->flags.cycles);
645 }
646 putchar('\n');
647}
648
649static void print_ip_sym(struct thread *thread, u8 cpumode, int cpu,
650 uint64_t addr, struct symbol **lastsym,
651 struct perf_event_attr *attr)
652{
653 struct addr_location al;
654 int off;
655
656 memset(&al, 0, sizeof(al));
657
658 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
659 if (!al.map)
660 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
661 addr, &al);
662 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
663 return;
664
665 al.cpu = cpu;
666 al.sym = NULL;
667 if (al.map)
668 al.sym = map__find_symbol(al.map, al.addr);
669
670 if (!al.sym)
671 return;
672
673 if (al.addr < al.sym->end)
674 off = al.addr - al.sym->start;
675 else
676 off = al.addr - al.map->start - al.sym->start;
677 printf("\t%s", al.sym->name);
678 if (off)
679 printf("%+d", off);
680 putchar(':');
681 if (PRINT_FIELD(SRCLINE))
682 map__fprintf_srcline(al.map, al.addr, "\t", stdout);
683 putchar('\n');
684 *lastsym = al.sym;
685}
686
687static void print_sample_brstackinsn(struct perf_sample *sample,
688 struct thread *thread,
689 struct perf_event_attr *attr,
690 struct machine *machine)
691{
692 struct branch_stack *br = sample->branch_stack;
693 u64 start, end;
694 int i, insn, len, nr, ilen;
695 struct perf_insn x;
696 u8 buffer[MAXBB];
697 unsigned off;
698 struct symbol *lastsym = NULL;
699
700 if (!(br && br->nr))
701 return;
702 nr = br->nr;
703 if (max_blocks && nr > max_blocks + 1)
704 nr = max_blocks + 1;
705
706 x.thread = thread;
707 x.cpu = sample->cpu;
708
709 putchar('\n');
710
711 /* Handle first from jump, of which we don't know the entry. */
712 len = grab_bb(buffer, br->entries[nr-1].from,
713 br->entries[nr-1].from,
714 machine, thread, &x.is64bit, &x.cpumode, false);
715 if (len > 0) {
716 print_ip_sym(thread, x.cpumode, x.cpu,
717 br->entries[nr - 1].from, &lastsym, attr);
718 print_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
719 &x, buffer, len, 0);
720 }
721
722 /* Print all blocks */
723 for (i = nr - 2; i >= 0; i--) {
724 if (br->entries[i].from || br->entries[i].to)
725 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
726 br->entries[i].from,
727 br->entries[i].to);
728 start = br->entries[i + 1].to;
729 end = br->entries[i].from;
730
731 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
732 /* Patch up missing kernel transfers due to ring filters */
733 if (len == -ENXIO && i > 0) {
734 end = br->entries[--i].from;
735 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
736 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
737 }
738 if (len <= 0)
739 continue;
740
741 insn = 0;
742 for (off = 0;; off += ilen) {
743 uint64_t ip = start + off;
744
745 print_ip_sym(thread, x.cpumode, x.cpu, ip, &lastsym, attr);
746 if (ip == end) {
747 print_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn);
748 break;
749 } else {
750 printf("\t%016" PRIx64 "\t%s\n", ip,
751 dump_insn(&x, ip, buffer + off, len - off, &ilen));
752 if (ilen == 0)
753 break;
754 insn++;
755 }
756 }
757 }
758
759 /*
760 * Hit the branch? In this case we are already done, and the target
761 * has not been executed yet.
762 */
763 if (br->entries[0].from == sample->ip)
764 return;
765 if (br->entries[0].flags.abort)
766 return;
767
768 /*
769 * Print final block upto sample
770 */
771 start = br->entries[0].to;
772 end = sample->ip;
773 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
774 print_ip_sym(thread, x.cpumode, x.cpu, start, &lastsym, attr);
775 if (len <= 0) {
776 /* Print at least last IP if basic block did not work */
777 len = grab_bb(buffer, sample->ip, sample->ip,
778 machine, thread, &x.is64bit, &x.cpumode, false);
779 if (len <= 0)
780 return;
781
782 printf("\t%016" PRIx64 "\t%s\n", sample->ip,
783 dump_insn(&x, sample->ip, buffer, len, NULL));
784 return;
785 }
786 for (off = 0; off <= end - start; off += ilen) {
787 printf("\t%016" PRIx64 "\t%s\n", start + off,
788 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
789 if (ilen == 0)
790 break;
791 }
792}
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200793
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300794static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600795 struct thread *thread,
796 struct perf_event_attr *attr)
797{
798 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600799
800 printf("%16" PRIx64, sample->addr);
801
802 if (!sample_addr_correlates_sym(attr))
803 return;
804
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300805 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600806
807 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900808 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900809 if (PRINT_FIELD(SYMOFFSET))
810 symbol__fprintf_symname_offs(al.sym, &al, stdout);
811 else
812 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600813 }
814
815 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900816 printf(" (");
817 map__fprintf_dsoname(al.map, stdout);
818 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600819 }
820}
821
Adrian Huntere2167082016-06-23 16:40:58 +0300822static void print_sample_callindent(struct perf_sample *sample,
823 struct perf_evsel *evsel,
824 struct thread *thread,
825 struct addr_location *al)
826{
827 struct perf_event_attr *attr = &evsel->attr;
828 size_t depth = thread_stack__depth(thread);
829 struct addr_location addr_al;
830 const char *name = NULL;
831 static int spacing;
832 int len = 0;
833 u64 ip = 0;
834
835 /*
836 * The 'return' has already been popped off the stack so the depth has
837 * to be adjusted to match the 'call'.
838 */
839 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
840 depth += 1;
841
842 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
843 if (sample_addr_correlates_sym(attr)) {
844 thread__resolve(thread, &addr_al, sample);
845 if (addr_al.sym)
846 name = addr_al.sym->name;
847 else
848 ip = sample->addr;
849 } else {
850 ip = sample->addr;
851 }
852 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
853 if (al->sym)
854 name = al->sym->name;
855 else
856 ip = sample->ip;
857 }
858
859 if (name)
860 len = printf("%*s%s", (int)depth * 4, "", name);
861 else if (ip)
862 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
863
864 if (len < 0)
865 return;
866
867 /*
868 * Try to keep the output length from changing frequently so that the
869 * output lines up more nicely.
870 */
871 if (len > spacing || (len && len < spacing - 52))
872 spacing = round_up(len + 4, 32);
873
874 if (len < spacing)
875 printf("%*s", spacing - len, "");
876}
877
Andi Kleen224e2c92016-10-07 16:42:27 +0300878static void print_insn(struct perf_sample *sample,
Andi Kleen48d02a12017-02-23 15:46:34 -0800879 struct perf_event_attr *attr,
880 struct thread *thread,
881 struct machine *machine)
Andi Kleen224e2c92016-10-07 16:42:27 +0300882{
883 if (PRINT_FIELD(INSNLEN))
884 printf(" ilen: %d", sample->insn_len);
885 if (PRINT_FIELD(INSN)) {
886 int i;
887
888 printf(" insn:");
889 for (i = 0; i < sample->insn_len; i++)
890 printf(" %02x", (unsigned char)sample->insn[i]);
891 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800892 if (PRINT_FIELD(BRSTACKINSN))
893 print_sample_brstackinsn(sample, thread, attr, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300894}
895
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300896static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +0900897 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200898 struct thread *thread,
Andi Kleen48d02a12017-02-23 15:46:34 -0800899 struct addr_location *al,
900 struct machine *machine)
Akihiro Nagai95582592012-01-30 13:43:09 +0900901{
902 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300903 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900904
Adrian Huntere2167082016-06-23 16:40:58 +0300905 if (PRINT_FIELD(CALLINDENT))
906 print_sample_callindent(sample, evsel, thread, al);
907
Akihiro Nagai95582592012-01-30 13:43:09 +0900908 /* print branch_from information */
909 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300910 unsigned int print_opts = output[attr->type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -0700911 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300912
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300913 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700914 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300915 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700916 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300917
918 if (cursor == NULL) {
919 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300920 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300921 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300922 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300923 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300924 } else
925 putchar('\n');
926
927 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +0900928 }
929
Akihiro Nagai95582592012-01-30 13:43:09 +0900930 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300931 if (PRINT_FIELD(ADDR) ||
932 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300933 !output[attr->type].user_set)) {
934 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300935 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300936 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900937
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300938 if (print_srcline_last)
939 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
940
Andi Kleen48d02a12017-02-23 15:46:34 -0800941 print_insn(sample, attr, thread, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300942
Akihiro Nagai95582592012-01-30 13:43:09 +0900943 printf("\n");
944}
945
Adrian Hunter055cd332016-06-23 16:40:56 +0300946static struct {
947 u32 flags;
948 const char *name;
949} sample_flags[] = {
950 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
951 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
952 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
953 {PERF_IP_FLAG_BRANCH, "jmp"},
954 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
955 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
956 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
957 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
958 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
959 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
960 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
961 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
962 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
963 {0, NULL}
964};
965
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300966static void print_sample_flags(u32 flags)
967{
968 const char *chars = PERF_IP_FLAG_CHARS;
969 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +0300970 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
971 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300972 char str[33];
973 int i, pos = 0;
974
Adrian Hunter055cd332016-06-23 16:40:56 +0300975 for (i = 0; sample_flags[i].name ; i++) {
976 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
977 name = sample_flags[i].name;
978 break;
979 }
980 }
981
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300982 for (i = 0; i < n; i++, flags >>= 1) {
983 if (flags & 1)
984 str[pos++] = chars[i];
985 }
986 for (; i < 32; i++, flags >>= 1) {
987 if (flags & 1)
988 str[pos++] = '?';
989 }
990 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +0300991
992 if (name)
993 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
994 else
995 printf(" %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300996}
997
Wang Nan30372f02016-02-24 11:20:45 +0000998struct printer_data {
999 int line_no;
1000 bool hit_nul;
1001 bool is_printable;
1002};
1003
1004static void
1005print_sample_bpf_output_printer(enum binary_printer_ops op,
1006 unsigned int val,
1007 void *extra)
1008{
1009 unsigned char ch = (unsigned char)val;
1010 struct printer_data *printer_data = extra;
1011
1012 switch (op) {
1013 case BINARY_PRINT_DATA_BEGIN:
1014 printf("\n");
1015 break;
1016 case BINARY_PRINT_LINE_BEGIN:
1017 printf("%17s", !printer_data->line_no ? "BPF output:" :
1018 " ");
1019 break;
1020 case BINARY_PRINT_ADDR:
1021 printf(" %04x:", val);
1022 break;
1023 case BINARY_PRINT_NUM_DATA:
1024 printf(" %02x", val);
1025 break;
1026 case BINARY_PRINT_NUM_PAD:
1027 printf(" ");
1028 break;
1029 case BINARY_PRINT_SEP:
1030 printf(" ");
1031 break;
1032 case BINARY_PRINT_CHAR_DATA:
1033 if (printer_data->hit_nul && ch)
1034 printer_data->is_printable = false;
1035
1036 if (!isprint(ch)) {
1037 printf("%c", '.');
1038
1039 if (!printer_data->is_printable)
1040 break;
1041
1042 if (ch == '\0')
1043 printer_data->hit_nul = true;
1044 else
1045 printer_data->is_printable = false;
1046 } else {
1047 printf("%c", ch);
1048 }
1049 break;
1050 case BINARY_PRINT_CHAR_PAD:
1051 printf(" ");
1052 break;
1053 case BINARY_PRINT_LINE_END:
1054 printf("\n");
1055 printer_data->line_no++;
1056 break;
1057 case BINARY_PRINT_DATA_END:
1058 default:
1059 break;
1060 }
1061}
1062
1063static void print_sample_bpf_output(struct perf_sample *sample)
1064{
1065 unsigned int nr_bytes = sample->raw_size;
1066 struct printer_data printer_data = {0, false, true};
1067
1068 print_binary(sample->raw_data, nr_bytes, 8,
1069 print_sample_bpf_output_printer, &printer_data);
1070
1071 if (printer_data.is_printable && printer_data.hit_nul)
1072 printf("%17s \"%s\"\n", "BPF string:",
1073 (char *)(sample->raw_data));
1074}
1075
Jiri Olsa809e9422015-11-26 18:55:21 +01001076struct perf_script {
1077 struct perf_tool tool;
1078 struct perf_session *session;
1079 bool show_task_events;
1080 bool show_mmap_events;
1081 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301082 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001083 bool allocated;
1084 struct cpu_map *cpus;
1085 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001086 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001087 const char *time_str;
1088 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001089};
1090
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001091static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1092{
1093 struct perf_evsel *evsel;
1094 int max = 0;
1095
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001096 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001097 int len = strlen(perf_evsel__name(evsel));
1098
1099 max = MAX(len, max);
1100 }
1101
1102 return max;
1103}
1104
Jiri Olsac19ac912016-02-24 09:46:54 +01001105static size_t data_src__printf(u64 data_src)
1106{
1107 struct mem_info mi = { .data_src.val = data_src };
1108 char decode[100];
1109 char out[100];
1110 static int maxlen;
1111 int len;
1112
1113 perf_script__meminfo_scnprintf(decode, 100, &mi);
1114
1115 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1116 if (maxlen < len)
1117 maxlen = len;
1118
1119 return printf("%-*s", maxlen, out);
1120}
1121
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001122static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001123 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001124 struct addr_location *al,
1125 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001126{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001127 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001128 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -07001129
David Ahern2c9e45f72011-03-17 10:03:21 -06001130 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001131 return;
1132
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001133 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -07001134
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001135 if (PRINT_FIELD(PERIOD))
1136 printf("%10" PRIu64 " ", sample->period);
1137
Namhyung Kime944d3d2013-11-18 14:34:52 +09001138 if (PRINT_FIELD(EVNAME)) {
1139 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001140
1141 if (!script->name_width)
1142 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1143
1144 printf("%*s: ", script->name_width,
1145 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001146 }
1147
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001148 if (print_flags)
1149 print_sample_flags(sample->flags);
1150
Akihiro Nagai95582592012-01-30 13:43:09 +09001151 if (is_bts_event(attr)) {
Andi Kleen48d02a12017-02-23 15:46:34 -08001152 print_sample_bts(sample, evsel, thread, al, machine);
Akihiro Nagai95582592012-01-30 13:43:09 +09001153 return;
1154 }
1155
David Ahern745f43e2011-03-09 22:23:26 -07001156 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001157 event_format__print(evsel->tp_format, sample->cpu,
1158 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -06001159 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001160 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -06001161
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001162 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +01001163 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001164
1165 if (PRINT_FIELD(WEIGHT))
1166 printf("%16" PRIu64, sample->weight);
1167
David Ahern787bef12011-05-27 14:28:43 -06001168 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001169 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001170
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001171 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001172 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001173 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001174 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001175
1176 putchar(cursor ? '\n' : ' ');
1177 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001178 }
1179
Stephane Eranianfc36f942015-08-31 18:41:10 +02001180 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001181 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001182
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001183 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001184 print_sample_brstack(sample);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001185 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001186 print_sample_brstacksym(sample, thread);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001187
Wang Nan30372f02016-02-24 11:20:45 +00001188 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1189 print_sample_bpf_output(sample);
Andi Kleen48d02a12017-02-23 15:46:34 -08001190 print_insn(sample, attr, thread, machine);
David Ahernc70c94b2011-03-09 22:23:25 -07001191 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001192}
1193
Tom Zanussi956ffd02009-11-25 01:15:46 -06001194static struct scripting_ops *scripting_ops;
1195
Jiri Olsa36e33c52016-01-06 11:49:56 +01001196static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1197{
1198 int nthreads = thread_map__nr(counter->threads);
1199 int ncpus = perf_evsel__nr_cpus(counter);
1200 int cpu, thread;
1201 static int header_printed;
1202
1203 if (counter->system_wide)
1204 nthreads = 1;
1205
1206 if (!header_printed) {
1207 printf("%3s %8s %15s %15s %15s %15s %s\n",
1208 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1209 header_printed = 1;
1210 }
1211
1212 for (thread = 0; thread < nthreads; thread++) {
1213 for (cpu = 0; cpu < ncpus; cpu++) {
1214 struct perf_counts_values *counts;
1215
1216 counts = perf_counts(counter->counts, cpu, thread);
1217
1218 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1219 counter->cpus->map[cpu],
1220 thread_map__pid(counter->threads, thread),
1221 counts->val,
1222 counts->ena,
1223 counts->run,
1224 tstamp,
1225 perf_evsel__name(counter));
1226 }
1227 }
1228}
1229
Jiri Olsae099eba2016-01-05 22:09:09 +01001230static void process_stat(struct perf_evsel *counter, u64 tstamp)
1231{
1232 if (scripting_ops && scripting_ops->process_stat)
1233 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001234 else
1235 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001236}
1237
1238static void process_stat_interval(u64 tstamp)
1239{
1240 if (scripting_ops && scripting_ops->process_stat_interval)
1241 scripting_ops->process_stat_interval(tstamp);
1242}
1243
Tom Zanussi956ffd02009-11-25 01:15:46 -06001244static void setup_scripting(void)
1245{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001246 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001247 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001248}
1249
Adrian Hunterd445dd22014-08-15 22:08:37 +03001250static int flush_scripting(void)
1251{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001252 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001253}
1254
Tom Zanussi956ffd02009-11-25 01:15:46 -06001255static int cleanup_scripting(void)
1256{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001257 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001258
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001259 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001260}
1261
Jiri Olsa809e9422015-11-26 18:55:21 +01001262static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001263 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001264 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001265 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001266 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001267{
Jiri Olsa809e9422015-11-26 18:55:21 +01001268 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001269 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001270
David Aherna91f4c42016-11-29 10:15:43 -07001271 if (perf_time__skip_sample(&scr->ptime, sample->time))
1272 return 0;
1273
David Ahern1424dc92011-03-09 22:23:28 -07001274 if (debug_mode) {
1275 if (sample->time < last_timestamp) {
1276 pr_err("Samples misordered, previous: %" PRIu64
1277 " this: %" PRIu64 "\n", last_timestamp,
1278 sample->time);
1279 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001280 }
David Ahern1424dc92011-03-09 22:23:28 -07001281 last_timestamp = sample->time;
1282 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001283 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001284
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001285 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001286 pr_err("problem processing %d event, skipping it.\n",
1287 event->header.type);
1288 return -1;
1289 }
1290
1291 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001292 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001293
Anton Blanchard5d67be92011-07-04 21:57:50 +10001294 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001295 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001296
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001297 if (scripting_ops)
1298 scripting_ops->process_event(event, sample, evsel, &al);
1299 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001300 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001301
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001302out_put:
1303 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001304 return 0;
1305}
1306
Adrian Hunter7ea95722013-11-01 15:51:30 +02001307static int process_attr(struct perf_tool *tool, union perf_event *event,
1308 struct perf_evlist **pevlist)
1309{
1310 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1311 struct perf_evlist *evlist;
1312 struct perf_evsel *evsel, *pos;
1313 int err;
1314
1315 err = perf_event__process_attr(tool, event, pevlist);
1316 if (err)
1317 return err;
1318
1319 evlist = *pevlist;
1320 evsel = perf_evlist__last(*pevlist);
1321
1322 if (evsel->attr.type >= PERF_TYPE_MAX)
1323 return 0;
1324
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001325 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001326 if (pos->attr.type == evsel->attr.type && pos != evsel)
1327 return 0;
1328 }
1329
1330 set_print_ip_opts(&evsel->attr);
1331
Jiri Olsad2b5a312015-10-16 12:41:25 +02001332 if (evsel->attr.sample_type)
1333 err = perf_evsel__check_attr(evsel, scr->session);
1334
1335 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001336}
1337
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001338static int process_comm_event(struct perf_tool *tool,
1339 union perf_event *event,
1340 struct perf_sample *sample,
1341 struct machine *machine)
1342{
1343 struct thread *thread;
1344 struct perf_script *script = container_of(tool, struct perf_script, tool);
1345 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001346 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001347 int ret = -1;
1348
1349 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1350 if (thread == NULL) {
1351 pr_debug("problem processing COMM event, skipping it.\n");
1352 return -1;
1353 }
1354
1355 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1356 goto out;
1357
1358 if (!evsel->attr.sample_id_all) {
1359 sample->cpu = 0;
1360 sample->time = 0;
1361 sample->tid = event->comm.tid;
1362 sample->pid = event->comm.pid;
1363 }
1364 print_sample_start(sample, thread, evsel);
1365 perf_event__fprintf(event, stdout);
1366 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001367out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001368 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001369 return ret;
1370}
1371
Hari Bathini96a44bb2017-03-08 02:12:06 +05301372static int process_namespaces_event(struct perf_tool *tool,
1373 union perf_event *event,
1374 struct perf_sample *sample,
1375 struct machine *machine)
1376{
1377 struct thread *thread;
1378 struct perf_script *script = container_of(tool, struct perf_script, tool);
1379 struct perf_session *session = script->session;
1380 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1381 int ret = -1;
1382
1383 thread = machine__findnew_thread(machine, event->namespaces.pid,
1384 event->namespaces.tid);
1385 if (thread == NULL) {
1386 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1387 return -1;
1388 }
1389
1390 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1391 goto out;
1392
1393 if (!evsel->attr.sample_id_all) {
1394 sample->cpu = 0;
1395 sample->time = 0;
1396 sample->tid = event->namespaces.tid;
1397 sample->pid = event->namespaces.pid;
1398 }
1399 print_sample_start(sample, thread, evsel);
1400 perf_event__fprintf(event, stdout);
1401 ret = 0;
1402out:
1403 thread__put(thread);
1404 return ret;
1405}
1406
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001407static int process_fork_event(struct perf_tool *tool,
1408 union perf_event *event,
1409 struct perf_sample *sample,
1410 struct machine *machine)
1411{
1412 struct thread *thread;
1413 struct perf_script *script = container_of(tool, struct perf_script, tool);
1414 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001415 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001416
1417 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1418 return -1;
1419
1420 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1421 if (thread == NULL) {
1422 pr_debug("problem processing FORK event, skipping it.\n");
1423 return -1;
1424 }
1425
1426 if (!evsel->attr.sample_id_all) {
1427 sample->cpu = 0;
1428 sample->time = event->fork.time;
1429 sample->tid = event->fork.tid;
1430 sample->pid = event->fork.pid;
1431 }
1432 print_sample_start(sample, thread, evsel);
1433 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001434 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001435
1436 return 0;
1437}
1438static int process_exit_event(struct perf_tool *tool,
1439 union perf_event *event,
1440 struct perf_sample *sample,
1441 struct machine *machine)
1442{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001443 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001444 struct thread *thread;
1445 struct perf_script *script = container_of(tool, struct perf_script, tool);
1446 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001447 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001448
1449 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1450 if (thread == NULL) {
1451 pr_debug("problem processing EXIT event, skipping it.\n");
1452 return -1;
1453 }
1454
1455 if (!evsel->attr.sample_id_all) {
1456 sample->cpu = 0;
1457 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001458 sample->tid = event->fork.tid;
1459 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001460 }
1461 print_sample_start(sample, thread, evsel);
1462 perf_event__fprintf(event, stdout);
1463
1464 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001465 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001466
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001467 thread__put(thread);
1468 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001469}
1470
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001471static int process_mmap_event(struct perf_tool *tool,
1472 union perf_event *event,
1473 struct perf_sample *sample,
1474 struct machine *machine)
1475{
1476 struct thread *thread;
1477 struct perf_script *script = container_of(tool, struct perf_script, tool);
1478 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001479 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001480
1481 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1482 return -1;
1483
1484 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1485 if (thread == NULL) {
1486 pr_debug("problem processing MMAP event, skipping it.\n");
1487 return -1;
1488 }
1489
1490 if (!evsel->attr.sample_id_all) {
1491 sample->cpu = 0;
1492 sample->time = 0;
1493 sample->tid = event->mmap.tid;
1494 sample->pid = event->mmap.pid;
1495 }
1496 print_sample_start(sample, thread, evsel);
1497 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001498 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001499 return 0;
1500}
1501
1502static int process_mmap2_event(struct perf_tool *tool,
1503 union perf_event *event,
1504 struct perf_sample *sample,
1505 struct machine *machine)
1506{
1507 struct thread *thread;
1508 struct perf_script *script = container_of(tool, struct perf_script, tool);
1509 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001510 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001511
1512 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1513 return -1;
1514
1515 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1516 if (thread == NULL) {
1517 pr_debug("problem processing MMAP2 event, skipping it.\n");
1518 return -1;
1519 }
1520
1521 if (!evsel->attr.sample_id_all) {
1522 sample->cpu = 0;
1523 sample->time = 0;
1524 sample->tid = event->mmap2.tid;
1525 sample->pid = event->mmap2.pid;
1526 }
1527 print_sample_start(sample, thread, evsel);
1528 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001529 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001530 return 0;
1531}
1532
Adrian Hunter7c148982015-07-21 12:44:06 +03001533static int process_switch_event(struct perf_tool *tool,
1534 union perf_event *event,
1535 struct perf_sample *sample,
1536 struct machine *machine)
1537{
1538 struct thread *thread;
1539 struct perf_script *script = container_of(tool, struct perf_script, tool);
1540 struct perf_session *session = script->session;
1541 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1542
1543 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1544 return -1;
1545
1546 thread = machine__findnew_thread(machine, sample->pid,
1547 sample->tid);
1548 if (thread == NULL) {
1549 pr_debug("problem processing SWITCH event, skipping it.\n");
1550 return -1;
1551 }
1552
1553 print_sample_start(sample, thread, evsel);
1554 perf_event__fprintf(event, stdout);
1555 thread__put(thread);
1556 return 0;
1557}
1558
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001559static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001560{
1561 session_done = 1;
1562}
1563
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001564static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001565{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001566 int ret;
1567
Tom Zanussic239da32010-04-01 23:59:18 -05001568 signal(SIGINT, sig_handler);
1569
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001570 /* override event processing functions */
1571 if (script->show_task_events) {
1572 script->tool.comm = process_comm_event;
1573 script->tool.fork = process_fork_event;
1574 script->tool.exit = process_exit_event;
1575 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001576 if (script->show_mmap_events) {
1577 script->tool.mmap = process_mmap_event;
1578 script->tool.mmap2 = process_mmap2_event;
1579 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001580 if (script->show_switch_events)
1581 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301582 if (script->show_namespace_events)
1583 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001584
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001585 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001586
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001587 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001588 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001589
1590 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001591}
1592
Tom Zanussi956ffd02009-11-25 01:15:46 -06001593struct script_spec {
1594 struct list_head node;
1595 struct scripting_ops *ops;
1596 char spec[0];
1597};
1598
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001599static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001600
1601static struct script_spec *script_spec__new(const char *spec,
1602 struct scripting_ops *ops)
1603{
1604 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1605
1606 if (s != NULL) {
1607 strcpy(s->spec, spec);
1608 s->ops = ops;
1609 }
1610
1611 return s;
1612}
1613
Tom Zanussi956ffd02009-11-25 01:15:46 -06001614static void script_spec__add(struct script_spec *s)
1615{
1616 list_add_tail(&s->node, &script_specs);
1617}
1618
1619static struct script_spec *script_spec__find(const char *spec)
1620{
1621 struct script_spec *s;
1622
1623 list_for_each_entry(s, &script_specs, node)
1624 if (strcasecmp(s->spec, spec) == 0)
1625 return s;
1626 return NULL;
1627}
1628
Tom Zanussi956ffd02009-11-25 01:15:46 -06001629int script_spec_register(const char *spec, struct scripting_ops *ops)
1630{
1631 struct script_spec *s;
1632
1633 s = script_spec__find(spec);
1634 if (s)
1635 return -1;
1636
Taeung Song8560bae2016-02-26 00:13:10 +09001637 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001638 if (!s)
1639 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001640 else
1641 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001642
1643 return 0;
1644}
1645
1646static struct scripting_ops *script_spec__lookup(const char *spec)
1647{
1648 struct script_spec *s = script_spec__find(spec);
1649 if (!s)
1650 return NULL;
1651
1652 return s->ops;
1653}
1654
1655static void list_available_languages(void)
1656{
1657 struct script_spec *s;
1658
1659 fprintf(stderr, "\n");
1660 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001661 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001662
1663 list_for_each_entry(s, &script_specs, node)
1664 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1665
1666 fprintf(stderr, "\n");
1667}
1668
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001669static int parse_scriptname(const struct option *opt __maybe_unused,
1670 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001671{
1672 char spec[PATH_MAX];
1673 const char *script, *ext;
1674 int len;
1675
Tom Zanussif526d682010-01-27 02:27:52 -06001676 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001677 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001678 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001679 }
1680
1681 script = strchr(str, ':');
1682 if (script) {
1683 len = script - str;
1684 if (len >= PATH_MAX) {
1685 fprintf(stderr, "invalid language specifier");
1686 return -1;
1687 }
1688 strncpy(spec, str, len);
1689 spec[len] = '\0';
1690 scripting_ops = script_spec__lookup(spec);
1691 if (!scripting_ops) {
1692 fprintf(stderr, "invalid language specifier");
1693 return -1;
1694 }
1695 script++;
1696 } else {
1697 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001698 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001699 if (!ext) {
1700 fprintf(stderr, "invalid script extension");
1701 return -1;
1702 }
1703 scripting_ops = script_spec__lookup(++ext);
1704 if (!scripting_ops) {
1705 fprintf(stderr, "invalid script extension");
1706 return -1;
1707 }
1708 }
1709
1710 script_name = strdup(script);
1711
1712 return 0;
1713}
1714
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001715static int parse_output_fields(const struct option *opt __maybe_unused,
1716 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001717{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001718 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001719 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001720 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001721 int rc = 0;
1722 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001723 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001724
1725 if (!str)
1726 return -ENOMEM;
1727
David Ahern2c9e45f72011-03-17 10:03:21 -06001728 /* first word can state for which event type the user is specifying
1729 * the fields. If no type exists, the specified fields apply to all
1730 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001731 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001732 tok = strchr(str, ':');
1733 if (tok) {
1734 *tok = '\0';
1735 tok++;
1736 if (!strcmp(str, "hw"))
1737 type = PERF_TYPE_HARDWARE;
1738 else if (!strcmp(str, "sw"))
1739 type = PERF_TYPE_SOFTWARE;
1740 else if (!strcmp(str, "trace"))
1741 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001742 else if (!strcmp(str, "raw"))
1743 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001744 else if (!strcmp(str, "break"))
1745 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001746 else {
1747 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001748 rc = -EINVAL;
1749 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001750 }
1751
1752 if (output[type].user_set)
1753 pr_warning("Overriding previous field request for %s events.\n",
1754 event_type(type));
1755
1756 output[type].fields = 0;
1757 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001758 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001759
1760 } else {
1761 tok = str;
1762 if (strlen(str) == 0) {
1763 fprintf(stderr,
1764 "Cannot set fields to 'none' for all event types.\n");
1765 rc = -EINVAL;
1766 goto out;
1767 }
1768
1769 if (output_set_by_user())
1770 pr_warning("Overriding previous field request for all events.\n");
1771
1772 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1773 output[j].fields = 0;
1774 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001775 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001776 }
David Ahern1424dc92011-03-09 22:23:28 -07001777 }
1778
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001779 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
David Ahern745f43e2011-03-09 22:23:26 -07001780 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001781 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001782 break;
David Ahern745f43e2011-03-09 22:23:26 -07001783 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001784 if (i == imax && strcmp(tok, "flags") == 0) {
1785 print_flags = true;
1786 continue;
1787 }
David Ahern745f43e2011-03-09 22:23:26 -07001788 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001789 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001790 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001791 goto out;
1792 }
1793
1794 if (type == -1) {
1795 /* add user option to all events types for
1796 * which it is valid
1797 */
1798 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1799 if (output[j].invalid_fields & all_output_options[i].field) {
1800 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1801 all_output_options[i].str, event_type(j));
1802 } else
1803 output[j].fields |= all_output_options[i].field;
1804 }
1805 } else {
1806 if (output[type].invalid_fields & all_output_options[i].field) {
1807 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1808 all_output_options[i].str, event_type(type));
1809
1810 rc = -EINVAL;
1811 goto out;
1812 }
1813 output[type].fields |= all_output_options[i].field;
1814 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001815 }
1816
1817 if (type >= 0) {
1818 if (output[type].fields == 0) {
1819 pr_debug("No fields requested for %s type. "
1820 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001821 }
David Ahern1424dc92011-03-09 22:23:28 -07001822 }
David Ahern745f43e2011-03-09 22:23:26 -07001823
David Ahern2c9e45f72011-03-17 10:03:21 -06001824out:
David Ahern745f43e2011-03-09 22:23:26 -07001825 free(str);
1826 return rc;
1827}
1828
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001829/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1830static int is_directory(const char *base_path, const struct dirent *dent)
1831{
1832 char path[PATH_MAX];
1833 struct stat st;
1834
1835 sprintf(path, "%s/%s", base_path, dent->d_name);
1836 if (stat(path, &st))
1837 return 0;
1838
1839 return S_ISDIR(st.st_mode);
1840}
1841
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001842#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1843 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1844 if ((lang_dirent->d_type == DT_DIR || \
1845 (lang_dirent->d_type == DT_UNKNOWN && \
1846 is_directory(scripts_path, lang_dirent))) && \
1847 (strcmp(lang_dirent->d_name, ".")) && \
1848 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001849
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001850#define for_each_script(lang_path, lang_dir, script_dirent) \
1851 while ((script_dirent = readdir(lang_dir)) != NULL) \
1852 if (script_dirent->d_type != DT_DIR && \
1853 (script_dirent->d_type != DT_UNKNOWN || \
1854 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001855
1856
1857#define RECORD_SUFFIX "-record"
1858#define REPORT_SUFFIX "-report"
1859
1860struct script_desc {
1861 struct list_head node;
1862 char *name;
1863 char *half_liner;
1864 char *args;
1865};
1866
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001867static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001868
1869static struct script_desc *script_desc__new(const char *name)
1870{
1871 struct script_desc *s = zalloc(sizeof(*s));
1872
Tom Zanussib5b87312010-11-10 08:16:51 -06001873 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001874 s->name = strdup(name);
1875
1876 return s;
1877}
1878
1879static void script_desc__delete(struct script_desc *s)
1880{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001881 zfree(&s->name);
1882 zfree(&s->half_liner);
1883 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001884 free(s);
1885}
1886
1887static void script_desc__add(struct script_desc *s)
1888{
1889 list_add_tail(&s->node, &script_descs);
1890}
1891
1892static struct script_desc *script_desc__find(const char *name)
1893{
1894 struct script_desc *s;
1895
1896 list_for_each_entry(s, &script_descs, node)
1897 if (strcasecmp(s->name, name) == 0)
1898 return s;
1899 return NULL;
1900}
1901
1902static struct script_desc *script_desc__findnew(const char *name)
1903{
1904 struct script_desc *s = script_desc__find(name);
1905
1906 if (s)
1907 return s;
1908
1909 s = script_desc__new(name);
1910 if (!s)
1911 goto out_delete_desc;
1912
1913 script_desc__add(s);
1914
1915 return s;
1916
1917out_delete_desc:
1918 script_desc__delete(s);
1919
1920 return NULL;
1921}
1922
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001923static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001924{
1925 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001926 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001927
1928 if (strlen(str) > suffix_len) {
1929 p = str + strlen(str) - suffix_len;
1930 if (!strncmp(p, suffix, suffix_len))
1931 return p;
1932 }
1933
1934 return NULL;
1935}
1936
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001937static int read_script_info(struct script_desc *desc, const char *filename)
1938{
1939 char line[BUFSIZ], *p;
1940 FILE *fp;
1941
1942 fp = fopen(filename, "r");
1943 if (!fp)
1944 return -1;
1945
1946 while (fgets(line, sizeof(line), fp)) {
1947 p = ltrim(line);
1948 if (strlen(p) == 0)
1949 continue;
1950 if (*p != '#')
1951 continue;
1952 p++;
1953 if (strlen(p) && *p == '!')
1954 continue;
1955
1956 p = ltrim(p);
1957 if (strlen(p) && p[strlen(p) - 1] == '\n')
1958 p[strlen(p) - 1] = '\0';
1959
1960 if (!strncmp(p, "description:", strlen("description:"))) {
1961 p += strlen("description:");
1962 desc->half_liner = strdup(ltrim(p));
1963 continue;
1964 }
1965
1966 if (!strncmp(p, "args:", strlen("args:"))) {
1967 p += strlen("args:");
1968 desc->args = strdup(ltrim(p));
1969 continue;
1970 }
1971 }
1972
1973 fclose(fp);
1974
1975 return 0;
1976}
1977
Robert Richter38efb532011-11-25 11:38:40 +01001978static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1979{
1980 char *script_root, *str;
1981
1982 script_root = strdup(script_dirent->d_name);
1983 if (!script_root)
1984 return NULL;
1985
1986 str = (char *)ends_with(script_root, suffix);
1987 if (!str) {
1988 free(script_root);
1989 return NULL;
1990 }
1991
1992 *str = '\0';
1993 return script_root;
1994}
1995
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001996static int list_available_scripts(const struct option *opt __maybe_unused,
1997 const char *s __maybe_unused,
1998 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001999{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002000 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002001 char scripts_path[MAXPATHLEN];
2002 DIR *scripts_dir, *lang_dir;
2003 char script_path[MAXPATHLEN];
2004 char lang_path[MAXPATHLEN];
2005 struct script_desc *desc;
2006 char first_half[BUFSIZ];
2007 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002008
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002009 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002010
2011 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002012 if (!scripts_dir) {
2013 fprintf(stdout,
2014 "open(%s) failed.\n"
2015 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2016 scripts_path);
2017 exit(-1);
2018 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002019
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002020 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002021 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002022 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002023 lang_dir = opendir(lang_path);
2024 if (!lang_dir)
2025 continue;
2026
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002027 for_each_script(lang_path, lang_dir, script_dirent) {
2028 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002029 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002030 desc = script_desc__findnew(script_root);
2031 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002032 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002033 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002034 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002035 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002036 }
2037 }
2038
2039 fprintf(stdout, "List of available trace scripts:\n");
2040 list_for_each_entry(desc, &script_descs, node) {
2041 sprintf(first_half, "%s %s", desc->name,
2042 desc->args ? desc->args : "");
2043 fprintf(stdout, " %-36s %s\n", first_half,
2044 desc->half_liner ? desc->half_liner : "");
2045 }
2046
2047 exit(0);
2048}
2049
Feng Tange5f37052012-09-07 16:42:26 +08002050/*
Feng Tang49e639e2012-10-30 11:56:03 +08002051 * Some scripts specify the required events in their "xxx-record" file,
2052 * this function will check if the events in perf.data match those
2053 * mentioned in the "xxx-record".
2054 *
2055 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2056 * which is covered well now. And new parsing code should be added to
2057 * cover the future complexing formats like event groups etc.
2058 */
2059static int check_ev_match(char *dir_name, char *scriptname,
2060 struct perf_session *session)
2061{
2062 char filename[MAXPATHLEN], evname[128];
2063 char line[BUFSIZ], *p;
2064 struct perf_evsel *pos;
2065 int match, len;
2066 FILE *fp;
2067
2068 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2069
2070 fp = fopen(filename, "r");
2071 if (!fp)
2072 return -1;
2073
2074 while (fgets(line, sizeof(line), fp)) {
2075 p = ltrim(line);
2076 if (*p == '#')
2077 continue;
2078
2079 while (strlen(p)) {
2080 p = strstr(p, "-e");
2081 if (!p)
2082 break;
2083
2084 p += 2;
2085 p = ltrim(p);
2086 len = strcspn(p, " \t");
2087 if (!len)
2088 break;
2089
2090 snprintf(evname, len + 1, "%s", p);
2091
2092 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002093 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002094 if (!strcmp(perf_evsel__name(pos), evname)) {
2095 match = 1;
2096 break;
2097 }
2098 }
2099
2100 if (!match) {
2101 fclose(fp);
2102 return -1;
2103 }
2104 }
2105 }
2106
2107 fclose(fp);
2108 return 0;
2109}
2110
2111/*
Feng Tange5f37052012-09-07 16:42:26 +08002112 * Return -1 if none is found, otherwise the actual scripts number.
2113 *
2114 * Currently the only user of this function is the script browser, which
2115 * will list all statically runnable scripts, select one, execute it and
2116 * show the output in a perf browser.
2117 */
2118int find_scripts(char **scripts_array, char **scripts_path_array)
2119{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002120 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002121 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002122 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002123 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002124 struct perf_data_file file = {
2125 .path = input_name,
2126 .mode = PERF_DATA_MODE_READ,
2127 };
Feng Tange5f37052012-09-07 16:42:26 +08002128 char *temp;
2129 int i = 0;
2130
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002131 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002132 if (!session)
2133 return -1;
2134
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002135 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002136
2137 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002138 if (!scripts_dir) {
2139 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002140 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002141 }
Feng Tange5f37052012-09-07 16:42:26 +08002142
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002143 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002144 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002145 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002146#ifdef NO_LIBPERL
2147 if (strstr(lang_path, "perl"))
2148 continue;
2149#endif
2150#ifdef NO_LIBPYTHON
2151 if (strstr(lang_path, "python"))
2152 continue;
2153#endif
2154
2155 lang_dir = opendir(lang_path);
2156 if (!lang_dir)
2157 continue;
2158
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002159 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002160 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002161 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002162 continue;
2163 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002164 script_dirent->d_name);
2165 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002166 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002167 (temp - script_dirent->d_name) + 1,
2168 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002169
2170 if (check_ev_match(lang_path,
2171 scripts_array[i], session))
2172 continue;
2173
Feng Tange5f37052012-09-07 16:42:26 +08002174 i++;
2175 }
Feng Tang49e639e2012-10-30 11:56:03 +08002176 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002177 }
2178
Feng Tang49e639e2012-10-30 11:56:03 +08002179 closedir(scripts_dir);
2180 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002181 return i;
2182}
2183
Tom Zanussi38752942009-12-15 02:53:39 -06002184static char *get_script_path(const char *script_root, const char *suffix)
2185{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002186 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002187 char scripts_path[MAXPATHLEN];
2188 char script_path[MAXPATHLEN];
2189 DIR *scripts_dir, *lang_dir;
2190 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002191 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002192
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002193 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002194
2195 scripts_dir = opendir(scripts_path);
2196 if (!scripts_dir)
2197 return NULL;
2198
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002199 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002200 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002201 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002202 lang_dir = opendir(lang_path);
2203 if (!lang_dir)
2204 continue;
2205
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002206 for_each_script(lang_path, lang_dir, script_dirent) {
2207 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002208 if (__script_root && !strcmp(script_root, __script_root)) {
2209 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002210 closedir(lang_dir);
2211 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002212 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002213 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002214 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002215 }
2216 free(__script_root);
2217 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002218 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002219 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002220 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002221
Robert Richter38efb532011-11-25 11:38:40 +01002222 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002223}
2224
Tom Zanussib5b87312010-11-10 08:16:51 -06002225static bool is_top_script(const char *script_path)
2226{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002227 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002228}
2229
2230static int has_required_arg(char *script_path)
2231{
2232 struct script_desc *desc;
2233 int n_args = 0;
2234 char *p;
2235
2236 desc = script_desc__new(NULL);
2237
2238 if (read_script_info(desc, script_path))
2239 goto out;
2240
2241 if (!desc->args)
2242 goto out;
2243
2244 for (p = desc->args; *p; p++)
2245 if (*p == '<')
2246 n_args++;
2247out:
2248 script_desc__delete(desc);
2249
2250 return n_args;
2251}
2252
David Ahernd54b1a92012-08-26 12:24:46 -06002253static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002254{
2255 char **__argv = malloc(sizeof(const char *) * argc);
2256
David Ahernd54b1a92012-08-26 12:24:46 -06002257 if (!__argv) {
2258 pr_err("malloc failed\n");
2259 return -1;
2260 }
2261
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002262 memcpy(__argv, argv, sizeof(const char *) * argc);
2263 argc = parse_options(argc, (const char **)__argv, record_options,
2264 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2265 free(__argv);
2266
David Ahernd54b1a92012-08-26 12:24:46 -06002267 system_wide = (argc == 0);
2268
2269 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002270}
2271
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002272static void script__setup_sample_type(struct perf_script *script)
2273{
2274 struct perf_session *session = script->session;
2275 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2276
2277 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2278 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2279 (sample_type & PERF_SAMPLE_STACK_USER))
2280 callchain_param.record_mode = CALLCHAIN_DWARF;
2281 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2282 callchain_param.record_mode = CALLCHAIN_LBR;
2283 else
2284 callchain_param.record_mode = CALLCHAIN_FP;
2285 }
2286}
2287
Jiri Olsae099eba2016-01-05 22:09:09 +01002288static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2289 union perf_event *event,
2290 struct perf_session *session)
2291{
2292 struct stat_round_event *round = &event->stat_round;
2293 struct perf_evsel *counter;
2294
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002295 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002296 perf_stat_process_counter(&stat_config, counter);
2297 process_stat(counter, round->time);
2298 }
2299
2300 process_stat_interval(round->time);
2301 return 0;
2302}
2303
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002304static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2305 union perf_event *event,
2306 struct perf_session *session __maybe_unused)
2307{
2308 perf_event__read_stat_config(&stat_config, &event->stat_config);
2309 return 0;
2310}
2311
Jiri Olsacfc88742016-01-05 22:09:06 +01002312static int set_maps(struct perf_script *script)
2313{
2314 struct perf_evlist *evlist = script->session->evlist;
2315
2316 if (!script->cpus || !script->threads)
2317 return 0;
2318
2319 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2320 return -EINVAL;
2321
2322 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2323
2324 if (perf_evlist__alloc_stats(evlist, true))
2325 return -ENOMEM;
2326
2327 script->allocated = true;
2328 return 0;
2329}
2330
2331static
2332int process_thread_map_event(struct perf_tool *tool,
2333 union perf_event *event,
2334 struct perf_session *session __maybe_unused)
2335{
2336 struct perf_script *script = container_of(tool, struct perf_script, tool);
2337
2338 if (script->threads) {
2339 pr_warning("Extra thread map event, ignoring.\n");
2340 return 0;
2341 }
2342
2343 script->threads = thread_map__new_event(&event->thread_map);
2344 if (!script->threads)
2345 return -ENOMEM;
2346
2347 return set_maps(script);
2348}
2349
2350static
2351int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2352 union perf_event *event,
2353 struct perf_session *session __maybe_unused)
2354{
2355 struct perf_script *script = container_of(tool, struct perf_script, tool);
2356
2357 if (script->cpus) {
2358 pr_warning("Extra cpu map event, ignoring.\n");
2359 return 0;
2360 }
2361
2362 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2363 if (!script->cpus)
2364 return -ENOMEM;
2365
2366 return set_maps(script);
2367}
2368
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002369int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002370{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002371 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002372 bool header = false;
2373 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002374 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002375 char *rec_script_path = NULL;
2376 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002377 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002378 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002379 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002380 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002381 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002382 struct perf_script script = {
2383 .tool = {
2384 .sample = process_sample_event,
2385 .mmap = perf_event__process_mmap,
2386 .mmap2 = perf_event__process_mmap2,
2387 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302388 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002389 .exit = perf_event__process_exit,
2390 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002391 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002392 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002393 .tracing_data = perf_event__process_tracing_data,
2394 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002395 .id_index = perf_event__process_id_index,
2396 .auxtrace_info = perf_event__process_auxtrace_info,
2397 .auxtrace = perf_event__process_auxtrace,
2398 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002399 .stat = perf_event__process_stat_event,
2400 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002401 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002402 .thread_map = process_thread_map_event,
2403 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002404 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002405 .ordering_requires_timestamps = true,
2406 },
2407 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002408 struct perf_data_file file = {
2409 .mode = PERF_DATA_MODE_READ,
2410 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002411 const struct option options[] = {
2412 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2413 "dump raw trace in ASCII"),
2414 OPT_INCR('v', "verbose", &verbose,
2415 "be more verbose (show symbol address, etc)"),
2416 OPT_BOOLEAN('L', "Latency", &latency_format,
2417 "show latency attributes (irqs/preemption disabled, etc)"),
2418 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2419 list_available_scripts),
2420 OPT_CALLBACK('s', "script", NULL, "name",
2421 "script file name (lang:script name, script name, or *)",
2422 parse_scriptname),
2423 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2424 "generate perf-script.xx script in specified language"),
2425 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2426 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2427 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002428 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2429 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002430 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2431 "file", "vmlinux pathname"),
2432 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2433 "file", "kallsyms pathname"),
2434 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2435 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002436 OPT_CALLBACK(0, "symfs", NULL, "directory",
2437 "Look for files with symbols relative to this directory",
2438 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002439 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002440 "comma separated output fields prepend with 'type:'. "
2441 "Valid types: hw,sw,trace,raw. "
2442 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Huntere2167082016-06-23 16:40:58 +03002443 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
Andi Kleen48d02a12017-02-23 15:46:34 -08002444 "bpf-output,callindent,insn,insnlen,brstackinsn",
2445 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002446 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2447 "system-wide collection from all CPUs"),
2448 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2449 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002450 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2451 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002452 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2453 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2454 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002455 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2456 "only consider symbols in these pids"),
2457 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2458 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002459 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2460 "Set the maximum stack depth when parsing the callchain, "
2461 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002462 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002463 OPT_BOOLEAN('I', "show-info", &show_full_info,
2464 "display extended information from perf.data file"),
2465 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2466 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002467 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2468 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002469 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2470 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002471 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2472 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302473 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2474 "Show namespace events (if recorded)"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002475 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002476 OPT_INTEGER(0, "max-blocks", &max_blocks,
2477 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002478 OPT_BOOLEAN(0, "ns", &nanosecs,
2479 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002480 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2481 "Instruction Tracing options",
2482 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002483 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2484 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002485 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2486 "Enable symbol demangling"),
2487 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2488 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002489 OPT_STRING(0, "time", &script.time_str, "str",
2490 "Time span of interest (start,stop)"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002491 OPT_END()
2492 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002493 const char * const script_subcommands[] = { "record", "report", NULL };
2494 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002495 "perf script [<options>]",
2496 "perf script [<options>] record <script> [<record-options>] <command>",
2497 "perf script [<options>] report <script> [script-args]",
2498 "perf script [<options>] <script> [<record-options>] <command>",
2499 "perf script [<options>] <top-script> [script-args]",
2500 NULL
2501 };
Tom Zanussi38752942009-12-15 02:53:39 -06002502
Tom Zanussib5b87312010-11-10 08:16:51 -06002503 setup_scripting();
2504
Yunlong Song40cae2b2015-03-18 21:35:54 +08002505 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002506 PARSE_OPT_STOP_AT_NON_OPTION);
2507
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002508 file.path = input_name;
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002509 file.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002510
Tom Zanussib5b87312010-11-10 08:16:51 -06002511 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2512 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2513 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002514 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002515 }
2516
Tom Zanussib5b87312010-11-10 08:16:51 -06002517 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2518 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2519 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002520 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002521 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002522 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002523 return -1;
2524 }
Tom Zanussi38752942009-12-15 02:53:39 -06002525 }
2526
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002527 if (itrace_synth_opts.callchain &&
2528 itrace_synth_opts.callchain_sz > scripting_max_stack)
2529 scripting_max_stack = itrace_synth_opts.callchain_sz;
2530
Ben Hutchings44e668c2010-10-10 16:10:03 +01002531 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002532 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002533
Tom Zanussib5b87312010-11-10 08:16:51 -06002534 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002535 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002536 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002537 pid_t pid;
2538
Tom Zanussib5b87312010-11-10 08:16:51 -06002539 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2540 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2541
2542 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002543 usage_with_options_msg(script_usage, options,
2544 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002545 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002546 }
2547
Tom Zanussib5b87312010-11-10 08:16:51 -06002548 if (is_top_script(argv[0])) {
2549 rep_args = argc - 1;
2550 } else {
2551 int rec_args;
2552
2553 rep_args = has_required_arg(rep_script_path);
2554 rec_args = (argc - 1) - rep_args;
2555 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002556 usage_with_options_msg(script_usage, options,
2557 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002558 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002559 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002560 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002561 }
2562
2563 if (pipe(live_pipe) < 0) {
2564 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002565 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002566 }
2567
2568 pid = fork();
2569 if (pid < 0) {
2570 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002571 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002572 }
2573
2574 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002575 j = 0;
2576
Tom Zanussia0cccc22010-04-01 23:59:25 -05002577 dup2(live_pipe[1], 1);
2578 close(live_pipe[0]);
2579
Robert Richter317df652011-11-25 15:05:25 +01002580 if (is_top_script(argv[0])) {
2581 system_wide = true;
2582 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002583 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2584 err = -1;
2585 goto out;
2586 }
Robert Richter317df652011-11-25 15:05:25 +01002587 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002588
2589 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002590 if (!__argv) {
2591 pr_err("malloc failed\n");
2592 err = -ENOMEM;
2593 goto out;
2594 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002595
Tom Zanussib5b87312010-11-10 08:16:51 -06002596 __argv[j++] = "/bin/sh";
2597 __argv[j++] = rec_script_path;
2598 if (system_wide)
2599 __argv[j++] = "-a";
2600 __argv[j++] = "-q";
2601 __argv[j++] = "-o";
2602 __argv[j++] = "-";
2603 for (i = rep_args + 1; i < argc; i++)
2604 __argv[j++] = argv[i];
2605 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002606
2607 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002608 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002609 exit(-1);
2610 }
2611
2612 dup2(live_pipe[0], 0);
2613 close(live_pipe[1]);
2614
Tom Zanussib5b87312010-11-10 08:16:51 -06002615 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002616 if (!__argv) {
2617 pr_err("malloc failed\n");
2618 err = -ENOMEM;
2619 goto out;
2620 }
2621
Tom Zanussib5b87312010-11-10 08:16:51 -06002622 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002623 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002624 __argv[j++] = rep_script_path;
2625 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002626 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002627 __argv[j++] = "-i";
2628 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002629 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002630
2631 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002632 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002633 exit(-1);
2634 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002635
Tom Zanussib5b87312010-11-10 08:16:51 -06002636 if (rec_script_path)
2637 script_path = rec_script_path;
2638 if (rep_script_path)
2639 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002640
Tom Zanussib5b87312010-11-10 08:16:51 -06002641 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002642 j = 0;
2643
Robert Richter317df652011-11-25 15:05:25 +01002644 if (!rec_script_path)
2645 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002646 else if (!system_wide) {
2647 if (have_cmd(argc - 1, &argv[1]) != 0) {
2648 err = -1;
2649 goto out;
2650 }
2651 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002652
2653 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002654 if (!__argv) {
2655 pr_err("malloc failed\n");
2656 err = -ENOMEM;
2657 goto out;
2658 }
2659
Tom Zanussib5b87312010-11-10 08:16:51 -06002660 __argv[j++] = "/bin/sh";
2661 __argv[j++] = script_path;
2662 if (system_wide)
2663 __argv[j++] = "-a";
2664 for (i = 2; i < argc; i++)
2665 __argv[j++] = argv[i];
2666 __argv[j++] = NULL;
2667
2668 execvp("/bin/sh", (char **)__argv);
2669 free(__argv);
2670 exit(-1);
2671 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002672
Tom Zanussicf4fee52010-03-03 01:04:33 -06002673 if (!script_name)
2674 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002675
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002676 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002677 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002678 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002679
Jiri Olsae90debd2013-12-09 11:02:50 +01002680 if (header || header_only) {
2681 perf_session__fprintf_info(session, stdout, show_full_info);
2682 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002683 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002684 }
2685
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002686 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002687 goto out_delete;
2688
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002689 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002690 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002691
Adrian Huntere2167082016-06-23 16:40:58 +03002692 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2693 itrace_synth_opts.thread_stack = true;
2694
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002695 session->itrace_synth_opts = &itrace_synth_opts;
2696
Anton Blanchard5d67be92011-07-04 21:57:50 +10002697 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002698 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2699 if (err < 0)
2700 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002701 }
2702
David Ahern1424dc92011-03-09 22:23:28 -07002703 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002704 symbol_conf.use_callchain = true;
2705 else
2706 symbol_conf.use_callchain = false;
2707
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002708 if (session->tevent.pevent &&
2709 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002710 machine__resolve_kernel_addr,
2711 &session->machines.host) < 0) {
2712 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2713 return -1;
2714 }
2715
Tom Zanussi956ffd02009-11-25 01:15:46 -06002716 if (generate_script_lang) {
2717 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002718 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002719
David Ahern2c9e45f72011-03-17 10:03:21 -06002720 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002721 fprintf(stderr,
2722 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002723 err = -EINVAL;
2724 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002725 }
2726
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002727 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002728 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002729 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002730 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002731 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002732 }
2733
2734 err = fstat(input, &perf_stat);
2735 if (err < 0) {
2736 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002737 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002738 }
2739
2740 if (!perf_stat.st_size) {
2741 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002742 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002743 }
2744
2745 scripting_ops = script_spec__lookup(generate_script_lang);
2746 if (!scripting_ops) {
2747 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002748 err = -ENOENT;
2749 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002750 }
2751
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002752 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002753 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002754 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002755 }
2756
2757 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002758 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002759 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002760 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002761 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002762 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002763 }
2764
David Ahern9cbdb702011-04-06 21:54:20 -06002765
2766 err = perf_session__check_output_opt(session);
2767 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002768 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002769
David Aherna91f4c42016-11-29 10:15:43 -07002770 /* needs to be parsed after looking up reference time */
2771 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
2772 pr_err("Invalid time string\n");
2773 return -EINVAL;
2774 }
2775
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002776 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002777
Adrian Hunterd445dd22014-08-15 22:08:37 +03002778 flush_scripting();
2779
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002780out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002781 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002782 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002783
2784 if (script_started)
2785 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002786out:
2787 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002788}