blob: cd2f38bf757397d3a3f6e1770ba1ed9c992ef7b8 [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"
6#include "util/exec_cmd.h"
7#include "util/header.h"
8#include "util/parse-options.h"
9#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020010#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020011#include "util/symbol.h"
12#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010013#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020014#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070015#include "util/evlist.h"
16#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080017#include "util/sort.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020018#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030019#include "util/auxtrace.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100020#include <linux/bitmap.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020021
Tom Zanussi956ffd02009-11-25 01:15:46 -060022static char const *script_name;
23static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020024static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020025static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020026static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070027static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090028static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010029static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030030static bool print_flags;
Anton Blanchard5d67be92011-07-04 21:57:50 +100031static const char *cpu_list;
32static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Tom Zanussi956ffd02009-11-25 01:15:46 -060033
David Ahern745f43e2011-03-09 22:23:26 -070034enum perf_output_field {
35 PERF_OUTPUT_COMM = 1U << 0,
36 PERF_OUTPUT_TID = 1U << 1,
37 PERF_OUTPUT_PID = 1U << 2,
38 PERF_OUTPUT_TIME = 1U << 3,
39 PERF_OUTPUT_CPU = 1U << 4,
40 PERF_OUTPUT_EVNAME = 1U << 5,
41 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060042 PERF_OUTPUT_IP = 1U << 7,
43 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060044 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060045 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090046 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020047 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaa2014-08-25 16:45:42 +020048 PERF_OUTPUT_PERIOD = 1U << 13,
David Ahern745f43e2011-03-09 22:23:26 -070049};
50
51struct output_option {
52 const char *str;
53 enum perf_output_field field;
54} all_output_options[] = {
55 {.str = "comm", .field = PERF_OUTPUT_COMM},
56 {.str = "tid", .field = PERF_OUTPUT_TID},
57 {.str = "pid", .field = PERF_OUTPUT_PID},
58 {.str = "time", .field = PERF_OUTPUT_TIME},
59 {.str = "cpu", .field = PERF_OUTPUT_CPU},
60 {.str = "event", .field = PERF_OUTPUT_EVNAME},
61 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060062 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070063 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060064 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060065 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090066 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020067 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaa2014-08-25 16:45:42 +020068 {.str = "period", .field = PERF_OUTPUT_PERIOD},
David Ahern745f43e2011-03-09 22:23:26 -070069};
70
71/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -060072static struct {
73 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060074 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -040075 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -060076 u64 fields;
77 u64 invalid_fields;
78} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -070079
David Ahern2c9e45f72011-03-17 10:03:21 -060080 [PERF_TYPE_HARDWARE] = {
81 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -070082
David Ahern2c9e45f72011-03-17 10:03:21 -060083 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
84 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060085 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +020086 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
87 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -060088
89 .invalid_fields = PERF_OUTPUT_TRACE,
90 },
91
92 [PERF_TYPE_SOFTWARE] = {
93 .user_set = false,
94
95 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
96 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060097 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +020098 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
99 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600100
101 .invalid_fields = PERF_OUTPUT_TRACE,
102 },
103
104 [PERF_TYPE_TRACEPOINT] = {
105 .user_set = false,
106
107 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
108 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
109 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
110 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700111
112 [PERF_TYPE_RAW] = {
113 .user_set = false,
114
115 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
116 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600117 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200118 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
119 PERF_OUTPUT_PERIOD,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700120
121 .invalid_fields = PERF_OUTPUT_TRACE,
122 },
David Ahern1424dc92011-03-09 22:23:28 -0700123};
David Ahern745f43e2011-03-09 22:23:26 -0700124
David Ahern2c9e45f72011-03-17 10:03:21 -0600125static bool output_set_by_user(void)
126{
127 int j;
128 for (j = 0; j < PERF_TYPE_MAX; ++j) {
129 if (output[j].user_set)
130 return true;
131 }
132 return false;
133}
David Ahern745f43e2011-03-09 22:23:26 -0700134
David Ahern9cbdb702011-04-06 21:54:20 -0600135static const char *output_field2str(enum perf_output_field field)
136{
137 int i, imax = ARRAY_SIZE(all_output_options);
138 const char *str = "";
139
140 for (i = 0; i < imax; ++i) {
141 if (all_output_options[i].field == field) {
142 str = all_output_options[i].str;
143 break;
144 }
145 }
146 return str;
147}
148
David Ahern2c9e45f72011-03-17 10:03:21 -0600149#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700150
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300151static int perf_evsel__check_stype(struct perf_evsel *evsel,
152 u64 sample_type, const char *sample_msg,
153 enum perf_output_field field)
David Ahern1424dc92011-03-09 22:23:28 -0700154{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300155 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600156 int type = attr->type;
157 const char *evname;
158
159 if (attr->sample_type & sample_type)
160 return 0;
161
162 if (output[type].user_set) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300163 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600164 pr_err("Samples for '%s' event do not have %s attribute set. "
165 "Cannot print '%s' field.\n",
166 evname, sample_msg, output_field2str(field));
167 return -1;
168 }
169
170 /* user did not ask for it explicitly so remove from the default list */
171 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300172 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600173 pr_debug("Samples for '%s' event do not have %s attribute set. "
174 "Skipping '%s' field.\n",
175 evname, sample_msg, output_field2str(field));
176
177 return 0;
178}
179
180static int perf_evsel__check_attr(struct perf_evsel *evsel,
181 struct perf_session *session)
182{
183 struct perf_event_attr *attr = &evsel->attr;
184
David Ahern1424dc92011-03-09 22:23:28 -0700185 if (PRINT_FIELD(TRACE) &&
186 !perf_session__has_traces(session, "record -R"))
187 return -EINVAL;
188
David Ahern787bef12011-05-27 14:28:43 -0600189 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300190 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
191 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700192 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700193 }
David Ahern7cec0922011-05-30 13:08:23 -0600194
195 if (PRINT_FIELD(ADDR) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300196 perf_evsel__check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
197 PERF_OUTPUT_ADDR))
David Ahern7cec0922011-05-30 13:08:23 -0600198 return -EINVAL;
199
200 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
201 pr_err("Display of symbols requested but neither sample IP nor "
202 "sample address\nis selected. Hence, no addresses to convert "
203 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600204 return -EINVAL;
205 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900206 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
207 pr_err("Display of offsets requested but symbol is not"
208 "selected.\n");
209 return -EINVAL;
210 }
David Ahern7cec0922011-05-30 13:08:23 -0600211 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
212 pr_err("Display of DSO requested but neither sample IP nor "
213 "sample address\nis selected. Hence, no addresses to convert "
214 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600215 return -EINVAL;
216 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200217 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
218 pr_err("Display of source line number requested but sample IP is not\n"
219 "selected. Hence, no address to lookup the source line number.\n");
220 return -EINVAL;
221 }
David Ahern1424dc92011-03-09 22:23:28 -0700222
223 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300224 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
225 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700226 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700227
228 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300229 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
230 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700231 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700232
233 if (PRINT_FIELD(CPU) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300234 perf_evsel__check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
235 PERF_OUTPUT_CPU))
David Ahern1424dc92011-03-09 22:23:28 -0700236 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600237
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200238 if (PRINT_FIELD(PERIOD) &&
239 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
240 PERF_OUTPUT_PERIOD))
241 return -EINVAL;
242
David Ahern9cbdb702011-04-06 21:54:20 -0600243 return 0;
244}
245
Adrian Hunter7ea95722013-11-01 15:51:30 +0200246static void set_print_ip_opts(struct perf_event_attr *attr)
247{
248 unsigned int type = attr->type;
249
250 output[type].print_ip_opts = 0;
251 if (PRINT_FIELD(IP))
252 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
253
254 if (PRINT_FIELD(SYM))
255 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
256
257 if (PRINT_FIELD(DSO))
258 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
259
260 if (PRINT_FIELD(SYMOFFSET))
261 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200262
263 if (PRINT_FIELD(SRCLINE))
264 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200265}
266
David Ahern9cbdb702011-04-06 21:54:20 -0600267/*
268 * verify all user requested events exist and the samples
269 * have the expected data
270 */
271static int perf_session__check_output_opt(struct perf_session *session)
272{
273 int j;
274 struct perf_evsel *evsel;
275
276 for (j = 0; j < PERF_TYPE_MAX; ++j) {
277 evsel = perf_session__find_first_evtype(session, j);
278
279 /*
280 * even if fields is set to 0 (ie., show nothing) event must
281 * exist if user explicitly includes it on the command line
282 */
283 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
284 pr_err("%s events do not exist. "
285 "Remove corresponding -f option to proceed.\n",
286 event_type(j));
287 return -1;
288 }
289
290 if (evsel && output[j].fields &&
291 perf_evsel__check_attr(evsel, session))
292 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400293
294 if (evsel == NULL)
295 continue;
296
Adrian Hunter7ea95722013-11-01 15:51:30 +0200297 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700298 }
299
Adrian Hunter98526ee2014-07-31 09:00:59 +0300300 if (!no_callchain) {
301 bool use_callchain = false;
302
303 evlist__for_each(session->evlist, evsel) {
304 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
305 use_callchain = true;
306 break;
307 }
308 }
309 if (!use_callchain)
310 symbol_conf.use_callchain = false;
311 }
312
David Ahern80b8b492013-11-19 21:07:37 -0700313 /*
314 * set default for tracepoints to print symbols only
315 * if callchains are present
316 */
317 if (symbol_conf.use_callchain &&
318 !output[PERF_TYPE_TRACEPOINT].user_set) {
319 struct perf_event_attr *attr;
320
321 j = PERF_TYPE_TRACEPOINT;
322 evsel = perf_session__find_first_evtype(session, j);
323 if (evsel == NULL)
324 goto out;
325
326 attr = &evsel->attr;
327
328 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
329 output[j].fields |= PERF_OUTPUT_IP;
330 output[j].fields |= PERF_OUTPUT_SYM;
331 output[j].fields |= PERF_OUTPUT_DSO;
332 set_print_ip_opts(attr);
333 }
334 }
335
336out:
David Ahern1424dc92011-03-09 22:23:28 -0700337 return 0;
338}
David Ahern745f43e2011-03-09 22:23:26 -0700339
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300340static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700341 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300342 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700343{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300344 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700345 unsigned long secs;
346 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700347 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700348
David Ahern745f43e2011-03-09 22:23:26 -0700349 if (PRINT_FIELD(COMM)) {
350 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200351 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600352 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200353 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700354 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200355 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700356 }
David Ahernc70c94b2011-03-09 22:23:25 -0700357
David Ahern745f43e2011-03-09 22:23:26 -0700358 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
359 printf("%5d/%-5d ", sample->pid, sample->tid);
360 else if (PRINT_FIELD(PID))
361 printf("%5d ", sample->pid);
362 else if (PRINT_FIELD(TID))
363 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700364
David Ahern745f43e2011-03-09 22:23:26 -0700365 if (PRINT_FIELD(CPU)) {
366 if (latency_format)
367 printf("%3d ", sample->cpu);
368 else
369 printf("[%03d] ", sample->cpu);
370 }
David Ahernc70c94b2011-03-09 22:23:25 -0700371
David Ahern745f43e2011-03-09 22:23:26 -0700372 if (PRINT_FIELD(TIME)) {
373 nsecs = sample->time;
374 secs = nsecs / NSECS_PER_SEC;
375 nsecs -= secs * NSECS_PER_SEC;
376 usecs = nsecs / NSECS_PER_USEC;
377 printf("%5lu.%06lu: ", secs, usecs);
378 }
David Ahernc70c94b2011-03-09 22:23:25 -0700379}
380
David Ahern7cec0922011-05-30 13:08:23 -0600381static void print_sample_addr(union perf_event *event,
382 struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600383 struct thread *thread,
384 struct perf_event_attr *attr)
385{
386 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600387
388 printf("%16" PRIx64, sample->addr);
389
390 if (!sample_addr_correlates_sym(attr))
391 return;
392
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300393 perf_event__preprocess_sample_addr(event, sample, thread, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600394
395 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900396 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900397 if (PRINT_FIELD(SYMOFFSET))
398 symbol__fprintf_symname_offs(al.sym, &al, stdout);
399 else
400 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600401 }
402
403 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900404 printf(" (");
405 map__fprintf_dsoname(al.map, stdout);
406 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600407 }
408}
409
Akihiro Nagai95582592012-01-30 13:43:09 +0900410static void print_sample_bts(union perf_event *event,
411 struct perf_sample *sample,
412 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200413 struct thread *thread,
414 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900415{
416 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300417 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900418
419 /* print branch_from information */
420 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300421 unsigned int print_opts = output[attr->type].print_ip_opts;
422
423 if (symbol_conf.use_callchain && sample->callchain) {
Akihiro Nagai95582592012-01-30 13:43:09 +0900424 printf("\n");
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300425 } else {
426 printf(" ");
427 if (print_opts & PRINT_IP_OPT_SRCLINE) {
428 print_srcline_last = true;
429 print_opts &= ~PRINT_IP_OPT_SRCLINE;
430 }
431 }
432 perf_evsel__print_ip(evsel, sample, al, print_opts,
David Ahern307cbb92013-08-07 22:50:53 -0400433 PERF_MAX_STACK_DEPTH);
Akihiro Nagai95582592012-01-30 13:43:09 +0900434 }
435
Akihiro Nagai95582592012-01-30 13:43:09 +0900436 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300437 if (PRINT_FIELD(ADDR) ||
438 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300439 !output[attr->type].user_set)) {
440 printf(" => ");
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300441 print_sample_addr(event, sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300442 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900443
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300444 if (print_srcline_last)
445 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
446
Akihiro Nagai95582592012-01-30 13:43:09 +0900447 printf("\n");
448}
449
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300450static void print_sample_flags(u32 flags)
451{
452 const char *chars = PERF_IP_FLAG_CHARS;
453 const int n = strlen(PERF_IP_FLAG_CHARS);
454 char str[33];
455 int i, pos = 0;
456
457 for (i = 0; i < n; i++, flags >>= 1) {
458 if (flags & 1)
459 str[pos++] = chars[i];
460 }
461 for (; i < 32; i++, flags >>= 1) {
462 if (flags & 1)
463 str[pos++] = '?';
464 }
465 str[pos] = 0;
466 printf(" %-4s ", str);
467}
468
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300469static void process_event(union perf_event *event, struct perf_sample *sample,
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300470 struct perf_evsel *evsel, struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700471{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300472 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300473 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700474
David Ahern2c9e45f72011-03-17 10:03:21 -0600475 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700476 return;
477
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300478 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700479
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200480 if (PRINT_FIELD(PERIOD))
481 printf("%10" PRIu64 " ", sample->period);
482
Namhyung Kime944d3d2013-11-18 14:34:52 +0900483 if (PRINT_FIELD(EVNAME)) {
484 const char *evname = perf_evsel__name(evsel);
485 printf("%s: ", evname ? evname : "[unknown]");
486 }
487
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300488 if (print_flags)
489 print_sample_flags(sample->flags);
490
Akihiro Nagai95582592012-01-30 13:43:09 +0900491 if (is_bts_event(attr)) {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300492 print_sample_bts(event, sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900493 return;
494 }
495
David Ahern745f43e2011-03-09 22:23:26 -0700496 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300497 event_format__print(evsel->tp_format, sample->cpu,
498 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600499 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300500 print_sample_addr(event, sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600501
David Ahern787bef12011-05-27 14:28:43 -0600502 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700503 if (!symbol_conf.use_callchain)
504 printf(" ");
505 else
506 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400507
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300508 perf_evsel__print_ip(evsel, sample, al,
David Ahern307cbb92013-08-07 22:50:53 -0400509 output[attr->type].print_ip_opts,
510 PERF_MAX_STACK_DEPTH);
David Ahernc0230b22011-03-09 22:23:27 -0700511 }
512
David Ahernc70c94b2011-03-09 22:23:25 -0700513 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700514}
515
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300516static int default_start_script(const char *script __maybe_unused,
517 int argc __maybe_unused,
518 const char **argv __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600519{
520 return 0;
521}
522
Adrian Hunterd445dd22014-08-15 22:08:37 +0300523static int default_flush_script(void)
524{
525 return 0;
526}
527
Tom Zanussi956ffd02009-11-25 01:15:46 -0600528static int default_stop_script(void)
529{
530 return 0;
531}
532
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300533static int default_generate_script(struct pevent *pevent __maybe_unused,
534 const char *outfile __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600535{
536 return 0;
537}
538
539static struct scripting_ops default_scripting_ops = {
540 .start_script = default_start_script,
Adrian Hunterd445dd22014-08-15 22:08:37 +0300541 .flush_script = default_flush_script,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600542 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700543 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600544 .generate_script = default_generate_script,
545};
546
547static struct scripting_ops *scripting_ops;
548
549static void setup_scripting(void)
550{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600551 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600552 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600553
Tom Zanussi956ffd02009-11-25 01:15:46 -0600554 scripting_ops = &default_scripting_ops;
555}
556
Adrian Hunterd445dd22014-08-15 22:08:37 +0300557static int flush_scripting(void)
558{
559 return scripting_ops->flush_script();
560}
561
Tom Zanussi956ffd02009-11-25 01:15:46 -0600562static int cleanup_scripting(void)
563{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100564 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500565
Tom Zanussi956ffd02009-11-25 01:15:46 -0600566 return scripting_ops->stop_script();
567}
568
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300569static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200570 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200571 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300572 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200573 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200574{
David Aherne7984b72011-11-21 10:02:52 -0700575 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200576
David Ahern1424dc92011-03-09 22:23:28 -0700577 if (debug_mode) {
578 if (sample->time < last_timestamp) {
579 pr_err("Samples misordered, previous: %" PRIu64
580 " this: %" PRIu64 "\n", last_timestamp,
581 sample->time);
582 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200583 }
David Ahern1424dc92011-03-09 22:23:28 -0700584 last_timestamp = sample->time;
585 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200586 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000587
Adrian Huntere44baa32013-08-08 14:32:25 +0300588 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700589 pr_err("problem processing %d event, skipping it.\n",
590 event->header.type);
591 return -1;
592 }
593
594 if (al.filtered)
595 return 0;
596
Anton Blanchard5d67be92011-07-04 21:57:50 +1000597 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
598 return 0;
599
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300600 scripting_ops->process_event(event, sample, evsel, &al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200601
602 return 0;
603}
604
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300605struct perf_script {
606 struct perf_tool tool;
607 struct perf_session *session;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900608 bool show_task_events;
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900609 bool show_mmap_events;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200610};
611
Adrian Hunter7ea95722013-11-01 15:51:30 +0200612static int process_attr(struct perf_tool *tool, union perf_event *event,
613 struct perf_evlist **pevlist)
614{
615 struct perf_script *scr = container_of(tool, struct perf_script, tool);
616 struct perf_evlist *evlist;
617 struct perf_evsel *evsel, *pos;
618 int err;
619
620 err = perf_event__process_attr(tool, event, pevlist);
621 if (err)
622 return err;
623
624 evlist = *pevlist;
625 evsel = perf_evlist__last(*pevlist);
626
627 if (evsel->attr.type >= PERF_TYPE_MAX)
628 return 0;
629
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300630 evlist__for_each(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +0200631 if (pos->attr.type == evsel->attr.type && pos != evsel)
632 return 0;
633 }
634
635 set_print_ip_opts(&evsel->attr);
636
637 return perf_evsel__check_attr(evsel, scr->session);
638}
639
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900640static int process_comm_event(struct perf_tool *tool,
641 union perf_event *event,
642 struct perf_sample *sample,
643 struct machine *machine)
644{
645 struct thread *thread;
646 struct perf_script *script = container_of(tool, struct perf_script, tool);
647 struct perf_session *session = script->session;
648 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
649 int ret = -1;
650
651 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
652 if (thread == NULL) {
653 pr_debug("problem processing COMM event, skipping it.\n");
654 return -1;
655 }
656
657 if (perf_event__process_comm(tool, event, sample, machine) < 0)
658 goto out;
659
660 if (!evsel->attr.sample_id_all) {
661 sample->cpu = 0;
662 sample->time = 0;
663 sample->tid = event->comm.tid;
664 sample->pid = event->comm.pid;
665 }
666 print_sample_start(sample, thread, evsel);
667 perf_event__fprintf(event, stdout);
668 ret = 0;
669
670out:
671 return ret;
672}
673
674static int process_fork_event(struct perf_tool *tool,
675 union perf_event *event,
676 struct perf_sample *sample,
677 struct machine *machine)
678{
679 struct thread *thread;
680 struct perf_script *script = container_of(tool, struct perf_script, tool);
681 struct perf_session *session = script->session;
682 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
683
684 if (perf_event__process_fork(tool, event, sample, machine) < 0)
685 return -1;
686
687 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
688 if (thread == NULL) {
689 pr_debug("problem processing FORK event, skipping it.\n");
690 return -1;
691 }
692
693 if (!evsel->attr.sample_id_all) {
694 sample->cpu = 0;
695 sample->time = event->fork.time;
696 sample->tid = event->fork.tid;
697 sample->pid = event->fork.pid;
698 }
699 print_sample_start(sample, thread, evsel);
700 perf_event__fprintf(event, stdout);
701
702 return 0;
703}
704static int process_exit_event(struct perf_tool *tool,
705 union perf_event *event,
706 struct perf_sample *sample,
707 struct machine *machine)
708{
709 struct thread *thread;
710 struct perf_script *script = container_of(tool, struct perf_script, tool);
711 struct perf_session *session = script->session;
712 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
713
714 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
715 if (thread == NULL) {
716 pr_debug("problem processing EXIT event, skipping it.\n");
717 return -1;
718 }
719
720 if (!evsel->attr.sample_id_all) {
721 sample->cpu = 0;
722 sample->time = 0;
723 sample->tid = event->comm.tid;
724 sample->pid = event->comm.pid;
725 }
726 print_sample_start(sample, thread, evsel);
727 perf_event__fprintf(event, stdout);
728
729 if (perf_event__process_exit(tool, event, sample, machine) < 0)
730 return -1;
731
732 return 0;
733}
734
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900735static int process_mmap_event(struct perf_tool *tool,
736 union perf_event *event,
737 struct perf_sample *sample,
738 struct machine *machine)
739{
740 struct thread *thread;
741 struct perf_script *script = container_of(tool, struct perf_script, tool);
742 struct perf_session *session = script->session;
743 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
744
745 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
746 return -1;
747
748 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
749 if (thread == NULL) {
750 pr_debug("problem processing MMAP event, skipping it.\n");
751 return -1;
752 }
753
754 if (!evsel->attr.sample_id_all) {
755 sample->cpu = 0;
756 sample->time = 0;
757 sample->tid = event->mmap.tid;
758 sample->pid = event->mmap.pid;
759 }
760 print_sample_start(sample, thread, evsel);
761 perf_event__fprintf(event, stdout);
762
763 return 0;
764}
765
766static int process_mmap2_event(struct perf_tool *tool,
767 union perf_event *event,
768 struct perf_sample *sample,
769 struct machine *machine)
770{
771 struct thread *thread;
772 struct perf_script *script = container_of(tool, struct perf_script, tool);
773 struct perf_session *session = script->session;
774 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
775
776 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
777 return -1;
778
779 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
780 if (thread == NULL) {
781 pr_debug("problem processing MMAP2 event, skipping it.\n");
782 return -1;
783 }
784
785 if (!evsel->attr.sample_id_all) {
786 sample->cpu = 0;
787 sample->time = 0;
788 sample->tid = event->mmap2.tid;
789 sample->pid = event->mmap2.pid;
790 }
791 print_sample_start(sample, thread, evsel);
792 perf_event__fprintf(event, stdout);
793
794 return 0;
795}
796
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300797static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500798{
799 session_done = 1;
800}
801
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300802static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200803{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200804 int ret;
805
Tom Zanussic239da32010-04-01 23:59:18 -0500806 signal(SIGINT, sig_handler);
807
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900808 /* override event processing functions */
809 if (script->show_task_events) {
810 script->tool.comm = process_comm_event;
811 script->tool.fork = process_fork_event;
812 script->tool.exit = process_exit_event;
813 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900814 if (script->show_mmap_events) {
815 script->tool.mmap = process_mmap_event;
816 script->tool.mmap2 = process_mmap2_event;
817 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900818
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300819 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200820
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200821 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200822 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200823
824 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200825}
826
Tom Zanussi956ffd02009-11-25 01:15:46 -0600827struct script_spec {
828 struct list_head node;
829 struct scripting_ops *ops;
830 char spec[0];
831};
832
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200833static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600834
835static struct script_spec *script_spec__new(const char *spec,
836 struct scripting_ops *ops)
837{
838 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
839
840 if (s != NULL) {
841 strcpy(s->spec, spec);
842 s->ops = ops;
843 }
844
845 return s;
846}
847
Tom Zanussi956ffd02009-11-25 01:15:46 -0600848static void script_spec__add(struct script_spec *s)
849{
850 list_add_tail(&s->node, &script_specs);
851}
852
853static struct script_spec *script_spec__find(const char *spec)
854{
855 struct script_spec *s;
856
857 list_for_each_entry(s, &script_specs, node)
858 if (strcasecmp(s->spec, spec) == 0)
859 return s;
860 return NULL;
861}
862
863static struct script_spec *script_spec__findnew(const char *spec,
864 struct scripting_ops *ops)
865{
866 struct script_spec *s = script_spec__find(spec);
867
868 if (s)
869 return s;
870
871 s = script_spec__new(spec, ops);
872 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +0900873 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600874
875 script_spec__add(s);
876
877 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600878}
879
880int script_spec_register(const char *spec, struct scripting_ops *ops)
881{
882 struct script_spec *s;
883
884 s = script_spec__find(spec);
885 if (s)
886 return -1;
887
888 s = script_spec__findnew(spec, ops);
889 if (!s)
890 return -1;
891
892 return 0;
893}
894
895static struct scripting_ops *script_spec__lookup(const char *spec)
896{
897 struct script_spec *s = script_spec__find(spec);
898 if (!s)
899 return NULL;
900
901 return s->ops;
902}
903
904static void list_available_languages(void)
905{
906 struct script_spec *s;
907
908 fprintf(stderr, "\n");
909 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100910 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600911
912 list_for_each_entry(s, &script_specs, node)
913 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
914
915 fprintf(stderr, "\n");
916}
917
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300918static int parse_scriptname(const struct option *opt __maybe_unused,
919 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600920{
921 char spec[PATH_MAX];
922 const char *script, *ext;
923 int len;
924
Tom Zanussif526d682010-01-27 02:27:52 -0600925 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600926 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600927 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600928 }
929
930 script = strchr(str, ':');
931 if (script) {
932 len = script - str;
933 if (len >= PATH_MAX) {
934 fprintf(stderr, "invalid language specifier");
935 return -1;
936 }
937 strncpy(spec, str, len);
938 spec[len] = '\0';
939 scripting_ops = script_spec__lookup(spec);
940 if (!scripting_ops) {
941 fprintf(stderr, "invalid language specifier");
942 return -1;
943 }
944 script++;
945 } else {
946 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100947 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600948 if (!ext) {
949 fprintf(stderr, "invalid script extension");
950 return -1;
951 }
952 scripting_ops = script_spec__lookup(++ext);
953 if (!scripting_ops) {
954 fprintf(stderr, "invalid script extension");
955 return -1;
956 }
957 }
958
959 script_name = strdup(script);
960
961 return 0;
962}
963
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300964static int parse_output_fields(const struct option *opt __maybe_unused,
965 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -0700966{
967 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -0500968 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -0600969 int j;
David Ahern745f43e2011-03-09 22:23:26 -0700970 int rc = 0;
971 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -0700972 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -0700973
974 if (!str)
975 return -ENOMEM;
976
David Ahern2c9e45f72011-03-17 10:03:21 -0600977 /* first word can state for which event type the user is specifying
978 * the fields. If no type exists, the specified fields apply to all
979 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -0700980 */
David Ahern2c9e45f72011-03-17 10:03:21 -0600981 tok = strchr(str, ':');
982 if (tok) {
983 *tok = '\0';
984 tok++;
985 if (!strcmp(str, "hw"))
986 type = PERF_TYPE_HARDWARE;
987 else if (!strcmp(str, "sw"))
988 type = PERF_TYPE_SOFTWARE;
989 else if (!strcmp(str, "trace"))
990 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -0700991 else if (!strcmp(str, "raw"))
992 type = PERF_TYPE_RAW;
David Ahern2c9e45f72011-03-17 10:03:21 -0600993 else {
994 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +0100995 rc = -EINVAL;
996 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -0600997 }
998
999 if (output[type].user_set)
1000 pr_warning("Overriding previous field request for %s events.\n",
1001 event_type(type));
1002
1003 output[type].fields = 0;
1004 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001005 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001006
1007 } else {
1008 tok = str;
1009 if (strlen(str) == 0) {
1010 fprintf(stderr,
1011 "Cannot set fields to 'none' for all event types.\n");
1012 rc = -EINVAL;
1013 goto out;
1014 }
1015
1016 if (output_set_by_user())
1017 pr_warning("Overriding previous field request for all events.\n");
1018
1019 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1020 output[j].fields = 0;
1021 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001022 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001023 }
David Ahern1424dc92011-03-09 22:23:28 -07001024 }
1025
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001026 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001027 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001028 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001029 break;
David Ahern745f43e2011-03-09 22:23:26 -07001030 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001031 if (i == imax && strcmp(tok, "flags") == 0) {
1032 print_flags = true;
1033 continue;
1034 }
David Ahern745f43e2011-03-09 22:23:26 -07001035 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001036 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001037 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001038 goto out;
1039 }
1040
1041 if (type == -1) {
1042 /* add user option to all events types for
1043 * which it is valid
1044 */
1045 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1046 if (output[j].invalid_fields & all_output_options[i].field) {
1047 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1048 all_output_options[i].str, event_type(j));
1049 } else
1050 output[j].fields |= all_output_options[i].field;
1051 }
1052 } else {
1053 if (output[type].invalid_fields & all_output_options[i].field) {
1054 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1055 all_output_options[i].str, event_type(type));
1056
1057 rc = -EINVAL;
1058 goto out;
1059 }
1060 output[type].fields |= all_output_options[i].field;
1061 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001062 }
1063
1064 if (type >= 0) {
1065 if (output[type].fields == 0) {
1066 pr_debug("No fields requested for %s type. "
1067 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001068 }
David Ahern1424dc92011-03-09 22:23:28 -07001069 }
David Ahern745f43e2011-03-09 22:23:26 -07001070
David Ahern2c9e45f72011-03-17 10:03:21 -06001071out:
David Ahern745f43e2011-03-09 22:23:26 -07001072 free(str);
1073 return rc;
1074}
1075
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001076/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1077static int is_directory(const char *base_path, const struct dirent *dent)
1078{
1079 char path[PATH_MAX];
1080 struct stat st;
1081
1082 sprintf(path, "%s/%s", base_path, dent->d_name);
1083 if (stat(path, &st))
1084 return 0;
1085
1086 return S_ISDIR(st.st_mode);
1087}
1088
1089#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001090 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1091 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001092 if ((lang_dirent.d_type == DT_DIR || \
1093 (lang_dirent.d_type == DT_UNKNOWN && \
1094 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001095 (strcmp(lang_dirent.d_name, ".")) && \
1096 (strcmp(lang_dirent.d_name, "..")))
1097
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001098#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001099 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1100 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001101 if (script_dirent.d_type != DT_DIR && \
1102 (script_dirent.d_type != DT_UNKNOWN || \
1103 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001104
1105
1106#define RECORD_SUFFIX "-record"
1107#define REPORT_SUFFIX "-report"
1108
1109struct script_desc {
1110 struct list_head node;
1111 char *name;
1112 char *half_liner;
1113 char *args;
1114};
1115
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001116static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001117
1118static struct script_desc *script_desc__new(const char *name)
1119{
1120 struct script_desc *s = zalloc(sizeof(*s));
1121
Tom Zanussib5b87312010-11-10 08:16:51 -06001122 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001123 s->name = strdup(name);
1124
1125 return s;
1126}
1127
1128static void script_desc__delete(struct script_desc *s)
1129{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001130 zfree(&s->name);
1131 zfree(&s->half_liner);
1132 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001133 free(s);
1134}
1135
1136static void script_desc__add(struct script_desc *s)
1137{
1138 list_add_tail(&s->node, &script_descs);
1139}
1140
1141static struct script_desc *script_desc__find(const char *name)
1142{
1143 struct script_desc *s;
1144
1145 list_for_each_entry(s, &script_descs, node)
1146 if (strcasecmp(s->name, name) == 0)
1147 return s;
1148 return NULL;
1149}
1150
1151static struct script_desc *script_desc__findnew(const char *name)
1152{
1153 struct script_desc *s = script_desc__find(name);
1154
1155 if (s)
1156 return s;
1157
1158 s = script_desc__new(name);
1159 if (!s)
1160 goto out_delete_desc;
1161
1162 script_desc__add(s);
1163
1164 return s;
1165
1166out_delete_desc:
1167 script_desc__delete(s);
1168
1169 return NULL;
1170}
1171
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001172static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001173{
1174 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001175 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001176
1177 if (strlen(str) > suffix_len) {
1178 p = str + strlen(str) - suffix_len;
1179 if (!strncmp(p, suffix, suffix_len))
1180 return p;
1181 }
1182
1183 return NULL;
1184}
1185
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001186static int read_script_info(struct script_desc *desc, const char *filename)
1187{
1188 char line[BUFSIZ], *p;
1189 FILE *fp;
1190
1191 fp = fopen(filename, "r");
1192 if (!fp)
1193 return -1;
1194
1195 while (fgets(line, sizeof(line), fp)) {
1196 p = ltrim(line);
1197 if (strlen(p) == 0)
1198 continue;
1199 if (*p != '#')
1200 continue;
1201 p++;
1202 if (strlen(p) && *p == '!')
1203 continue;
1204
1205 p = ltrim(p);
1206 if (strlen(p) && p[strlen(p) - 1] == '\n')
1207 p[strlen(p) - 1] = '\0';
1208
1209 if (!strncmp(p, "description:", strlen("description:"))) {
1210 p += strlen("description:");
1211 desc->half_liner = strdup(ltrim(p));
1212 continue;
1213 }
1214
1215 if (!strncmp(p, "args:", strlen("args:"))) {
1216 p += strlen("args:");
1217 desc->args = strdup(ltrim(p));
1218 continue;
1219 }
1220 }
1221
1222 fclose(fp);
1223
1224 return 0;
1225}
1226
Robert Richter38efb532011-11-25 11:38:40 +01001227static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1228{
1229 char *script_root, *str;
1230
1231 script_root = strdup(script_dirent->d_name);
1232 if (!script_root)
1233 return NULL;
1234
1235 str = (char *)ends_with(script_root, suffix);
1236 if (!str) {
1237 free(script_root);
1238 return NULL;
1239 }
1240
1241 *str = '\0';
1242 return script_root;
1243}
1244
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001245static int list_available_scripts(const struct option *opt __maybe_unused,
1246 const char *s __maybe_unused,
1247 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001248{
1249 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1250 char scripts_path[MAXPATHLEN];
1251 DIR *scripts_dir, *lang_dir;
1252 char script_path[MAXPATHLEN];
1253 char lang_path[MAXPATHLEN];
1254 struct script_desc *desc;
1255 char first_half[BUFSIZ];
1256 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001257
1258 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1259
1260 scripts_dir = opendir(scripts_path);
1261 if (!scripts_dir)
1262 return -1;
1263
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001264 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001265 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1266 lang_dirent.d_name);
1267 lang_dir = opendir(lang_path);
1268 if (!lang_dir)
1269 continue;
1270
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001271 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001272 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1273 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001274 desc = script_desc__findnew(script_root);
1275 snprintf(script_path, MAXPATHLEN, "%s/%s",
1276 lang_path, script_dirent.d_name);
1277 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001278 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001279 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001280 }
1281 }
1282
1283 fprintf(stdout, "List of available trace scripts:\n");
1284 list_for_each_entry(desc, &script_descs, node) {
1285 sprintf(first_half, "%s %s", desc->name,
1286 desc->args ? desc->args : "");
1287 fprintf(stdout, " %-36s %s\n", first_half,
1288 desc->half_liner ? desc->half_liner : "");
1289 }
1290
1291 exit(0);
1292}
1293
Feng Tange5f37052012-09-07 16:42:26 +08001294/*
Feng Tang49e639e2012-10-30 11:56:03 +08001295 * Some scripts specify the required events in their "xxx-record" file,
1296 * this function will check if the events in perf.data match those
1297 * mentioned in the "xxx-record".
1298 *
1299 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1300 * which is covered well now. And new parsing code should be added to
1301 * cover the future complexing formats like event groups etc.
1302 */
1303static int check_ev_match(char *dir_name, char *scriptname,
1304 struct perf_session *session)
1305{
1306 char filename[MAXPATHLEN], evname[128];
1307 char line[BUFSIZ], *p;
1308 struct perf_evsel *pos;
1309 int match, len;
1310 FILE *fp;
1311
1312 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1313
1314 fp = fopen(filename, "r");
1315 if (!fp)
1316 return -1;
1317
1318 while (fgets(line, sizeof(line), fp)) {
1319 p = ltrim(line);
1320 if (*p == '#')
1321 continue;
1322
1323 while (strlen(p)) {
1324 p = strstr(p, "-e");
1325 if (!p)
1326 break;
1327
1328 p += 2;
1329 p = ltrim(p);
1330 len = strcspn(p, " \t");
1331 if (!len)
1332 break;
1333
1334 snprintf(evname, len + 1, "%s", p);
1335
1336 match = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001337 evlist__for_each(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001338 if (!strcmp(perf_evsel__name(pos), evname)) {
1339 match = 1;
1340 break;
1341 }
1342 }
1343
1344 if (!match) {
1345 fclose(fp);
1346 return -1;
1347 }
1348 }
1349 }
1350
1351 fclose(fp);
1352 return 0;
1353}
1354
1355/*
Feng Tange5f37052012-09-07 16:42:26 +08001356 * Return -1 if none is found, otherwise the actual scripts number.
1357 *
1358 * Currently the only user of this function is the script browser, which
1359 * will list all statically runnable scripts, select one, execute it and
1360 * show the output in a perf browser.
1361 */
1362int find_scripts(char **scripts_array, char **scripts_path_array)
1363{
1364 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001365 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001366 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001367 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001368 struct perf_data_file file = {
1369 .path = input_name,
1370 .mode = PERF_DATA_MODE_READ,
1371 };
Feng Tange5f37052012-09-07 16:42:26 +08001372 char *temp;
1373 int i = 0;
1374
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001375 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001376 if (!session)
1377 return -1;
1378
Feng Tange5f37052012-09-07 16:42:26 +08001379 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1380
1381 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001382 if (!scripts_dir) {
1383 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001384 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001385 }
Feng Tange5f37052012-09-07 16:42:26 +08001386
1387 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1388 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1389 lang_dirent.d_name);
1390#ifdef NO_LIBPERL
1391 if (strstr(lang_path, "perl"))
1392 continue;
1393#endif
1394#ifdef NO_LIBPYTHON
1395 if (strstr(lang_path, "python"))
1396 continue;
1397#endif
1398
1399 lang_dir = opendir(lang_path);
1400 if (!lang_dir)
1401 continue;
1402
1403 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1404 /* Skip those real time scripts: xxxtop.p[yl] */
1405 if (strstr(script_dirent.d_name, "top."))
1406 continue;
1407 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1408 script_dirent.d_name);
1409 temp = strchr(script_dirent.d_name, '.');
1410 snprintf(scripts_array[i],
1411 (temp - script_dirent.d_name) + 1,
1412 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001413
1414 if (check_ev_match(lang_path,
1415 scripts_array[i], session))
1416 continue;
1417
Feng Tange5f37052012-09-07 16:42:26 +08001418 i++;
1419 }
Feng Tang49e639e2012-10-30 11:56:03 +08001420 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001421 }
1422
Feng Tang49e639e2012-10-30 11:56:03 +08001423 closedir(scripts_dir);
1424 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001425 return i;
1426}
1427
Tom Zanussi38752942009-12-15 02:53:39 -06001428static char *get_script_path(const char *script_root, const char *suffix)
1429{
1430 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1431 char scripts_path[MAXPATHLEN];
1432 char script_path[MAXPATHLEN];
1433 DIR *scripts_dir, *lang_dir;
1434 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001435 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001436
1437 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1438
1439 scripts_dir = opendir(scripts_path);
1440 if (!scripts_dir)
1441 return NULL;
1442
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001443 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001444 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1445 lang_dirent.d_name);
1446 lang_dir = opendir(lang_path);
1447 if (!lang_dir)
1448 continue;
1449
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001450 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001451 __script_root = get_script_root(&script_dirent, suffix);
1452 if (__script_root && !strcmp(script_root, __script_root)) {
1453 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001454 closedir(lang_dir);
1455 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001456 snprintf(script_path, MAXPATHLEN, "%s/%s",
1457 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001458 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001459 }
1460 free(__script_root);
1461 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001462 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001463 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001464 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001465
Robert Richter38efb532011-11-25 11:38:40 +01001466 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001467}
1468
Tom Zanussib5b87312010-11-10 08:16:51 -06001469static bool is_top_script(const char *script_path)
1470{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001471 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001472}
1473
1474static int has_required_arg(char *script_path)
1475{
1476 struct script_desc *desc;
1477 int n_args = 0;
1478 char *p;
1479
1480 desc = script_desc__new(NULL);
1481
1482 if (read_script_info(desc, script_path))
1483 goto out;
1484
1485 if (!desc->args)
1486 goto out;
1487
1488 for (p = desc->args; *p; p++)
1489 if (*p == '<')
1490 n_args++;
1491out:
1492 script_desc__delete(desc);
1493
1494 return n_args;
1495}
1496
David Ahernd54b1a92012-08-26 12:24:46 -06001497static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001498{
1499 char **__argv = malloc(sizeof(const char *) * argc);
1500
David Ahernd54b1a92012-08-26 12:24:46 -06001501 if (!__argv) {
1502 pr_err("malloc failed\n");
1503 return -1;
1504 }
1505
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001506 memcpy(__argv, argv, sizeof(const char *) * argc);
1507 argc = parse_options(argc, (const char **)__argv, record_options,
1508 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1509 free(__argv);
1510
David Ahernd54b1a92012-08-26 12:24:46 -06001511 system_wide = (argc == 0);
1512
1513 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001514}
1515
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001516int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001517{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001518 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01001519 bool header = false;
1520 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001521 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001522 char *rec_script_path = NULL;
1523 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001524 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001525 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06001526 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001527 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001528 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001529 struct perf_script script = {
1530 .tool = {
1531 .sample = process_sample_event,
1532 .mmap = perf_event__process_mmap,
1533 .mmap2 = perf_event__process_mmap2,
1534 .comm = perf_event__process_comm,
1535 .exit = perf_event__process_exit,
1536 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001537 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001538 .tracing_data = perf_event__process_tracing_data,
1539 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001540 .id_index = perf_event__process_id_index,
1541 .auxtrace_info = perf_event__process_auxtrace_info,
1542 .auxtrace = perf_event__process_auxtrace,
1543 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001544 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001545 .ordering_requires_timestamps = true,
1546 },
1547 };
Yunlong Song06af0f22015-04-02 21:47:16 +08001548 struct perf_data_file file = {
1549 .mode = PERF_DATA_MODE_READ,
1550 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001551 const struct option options[] = {
1552 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1553 "dump raw trace in ASCII"),
1554 OPT_INCR('v', "verbose", &verbose,
1555 "be more verbose (show symbol address, etc)"),
1556 OPT_BOOLEAN('L', "Latency", &latency_format,
1557 "show latency attributes (irqs/preemption disabled, etc)"),
1558 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1559 list_available_scripts),
1560 OPT_CALLBACK('s', "script", NULL, "name",
1561 "script file name (lang:script name, script name, or *)",
1562 parse_scriptname),
1563 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1564 "generate perf-script.xx script in specified language"),
1565 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1566 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1567 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01001568 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1569 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001570 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1571 "file", "vmlinux pathname"),
1572 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1573 "file", "kallsyms pathname"),
1574 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1575 "When printing symbols do not display call chain"),
1576 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1577 "Look for files with symbols relative to this directory"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001578 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001579 "comma separated output fields prepend with 'type:'. "
1580 "Valid types: hw,sw,trace,raw. "
1581 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001582 "addr,symoff,period,flags", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001583 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1584 "system-wide collection from all CPUs"),
1585 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1586 "only consider these symbols"),
1587 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1588 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1589 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06001590 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1591 "only consider symbols in these pids"),
1592 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1593 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001594 OPT_BOOLEAN('I', "show-info", &show_full_info,
1595 "display extended information from perf.data file"),
1596 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1597 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001598 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1599 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001600 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1601 "Show the mmap events"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001602 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001603 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1604 "Instruction Tracing options",
1605 itrace_parse_synth_opts),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001606 OPT_END()
1607 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08001608 const char * const script_subcommands[] = { "record", "report", NULL };
1609 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001610 "perf script [<options>]",
1611 "perf script [<options>] record <script> [<record-options>] <command>",
1612 "perf script [<options>] report <script> [script-args]",
1613 "perf script [<options>] <script> [<record-options>] <command>",
1614 "perf script [<options>] <top-script> [script-args]",
1615 NULL
1616 };
Tom Zanussi38752942009-12-15 02:53:39 -06001617
Tom Zanussib5b87312010-11-10 08:16:51 -06001618 setup_scripting();
1619
Yunlong Song40cae2b2015-03-18 21:35:54 +08001620 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001621 PARSE_OPT_STOP_AT_NON_OPTION);
1622
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001623 file.path = input_name;
1624
Tom Zanussib5b87312010-11-10 08:16:51 -06001625 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1626 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1627 if (!rec_script_path)
1628 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001629 }
1630
Tom Zanussib5b87312010-11-10 08:16:51 -06001631 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1632 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1633 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001634 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001635 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001636 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001637 return -1;
1638 }
Tom Zanussi38752942009-12-15 02:53:39 -06001639 }
1640
Ben Hutchings44e668c2010-10-10 16:10:03 +01001641 /* make sure PERF_EXEC_PATH is set for scripts */
1642 perf_set_argv_exec_path(perf_exec_path());
1643
Tom Zanussib5b87312010-11-10 08:16:51 -06001644 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001645 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001646 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001647 pid_t pid;
1648
Tom Zanussib5b87312010-11-10 08:16:51 -06001649 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1650 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1651
1652 if (!rec_script_path && !rep_script_path) {
1653 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001654 " script -l for available scripts.\n", argv[0]);
1655 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001656 }
1657
Tom Zanussib5b87312010-11-10 08:16:51 -06001658 if (is_top_script(argv[0])) {
1659 rep_args = argc - 1;
1660 } else {
1661 int rec_args;
1662
1663 rep_args = has_required_arg(rep_script_path);
1664 rec_args = (argc - 1) - rep_args;
1665 if (rec_args < 0) {
1666 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001667 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001668 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001669 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -06001670 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001671 }
1672
1673 if (pipe(live_pipe) < 0) {
1674 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001675 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001676 }
1677
1678 pid = fork();
1679 if (pid < 0) {
1680 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001681 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001682 }
1683
1684 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001685 j = 0;
1686
Tom Zanussia0cccc22010-04-01 23:59:25 -05001687 dup2(live_pipe[1], 1);
1688 close(live_pipe[0]);
1689
Robert Richter317df652011-11-25 15:05:25 +01001690 if (is_top_script(argv[0])) {
1691 system_wide = true;
1692 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001693 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1694 err = -1;
1695 goto out;
1696 }
Robert Richter317df652011-11-25 15:05:25 +01001697 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001698
1699 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001700 if (!__argv) {
1701 pr_err("malloc failed\n");
1702 err = -ENOMEM;
1703 goto out;
1704 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001705
Tom Zanussib5b87312010-11-10 08:16:51 -06001706 __argv[j++] = "/bin/sh";
1707 __argv[j++] = rec_script_path;
1708 if (system_wide)
1709 __argv[j++] = "-a";
1710 __argv[j++] = "-q";
1711 __argv[j++] = "-o";
1712 __argv[j++] = "-";
1713 for (i = rep_args + 1; i < argc; i++)
1714 __argv[j++] = argv[i];
1715 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001716
1717 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001718 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001719 exit(-1);
1720 }
1721
1722 dup2(live_pipe[0], 0);
1723 close(live_pipe[1]);
1724
Tom Zanussib5b87312010-11-10 08:16:51 -06001725 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001726 if (!__argv) {
1727 pr_err("malloc failed\n");
1728 err = -ENOMEM;
1729 goto out;
1730 }
1731
Tom Zanussib5b87312010-11-10 08:16:51 -06001732 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001733 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001734 __argv[j++] = rep_script_path;
1735 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001736 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001737 __argv[j++] = "-i";
1738 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001739 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001740
1741 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001742 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001743 exit(-1);
1744 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001745
Tom Zanussib5b87312010-11-10 08:16:51 -06001746 if (rec_script_path)
1747 script_path = rec_script_path;
1748 if (rep_script_path)
1749 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001750
Tom Zanussib5b87312010-11-10 08:16:51 -06001751 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001752 j = 0;
1753
Robert Richter317df652011-11-25 15:05:25 +01001754 if (!rec_script_path)
1755 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06001756 else if (!system_wide) {
1757 if (have_cmd(argc - 1, &argv[1]) != 0) {
1758 err = -1;
1759 goto out;
1760 }
1761 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001762
1763 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001764 if (!__argv) {
1765 pr_err("malloc failed\n");
1766 err = -ENOMEM;
1767 goto out;
1768 }
1769
Tom Zanussib5b87312010-11-10 08:16:51 -06001770 __argv[j++] = "/bin/sh";
1771 __argv[j++] = script_path;
1772 if (system_wide)
1773 __argv[j++] = "-a";
1774 for (i = 2; i < argc; i++)
1775 __argv[j++] = argv[i];
1776 __argv[j++] = NULL;
1777
1778 execvp("/bin/sh", (char **)__argv);
1779 free(__argv);
1780 exit(-1);
1781 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001782
Tom Zanussicf4fee52010-03-03 01:04:33 -06001783 if (!script_name)
1784 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001785
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001786 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001787 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001788 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001789
Jiri Olsae90debd2013-12-09 11:02:50 +01001790 if (header || header_only) {
1791 perf_session__fprintf_info(session, stdout, show_full_info);
1792 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001793 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01001794 }
1795
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001796 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09001797 goto out_delete;
1798
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001799 script.session = session;
1800
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001801 session->itrace_synth_opts = &itrace_synth_opts;
1802
Anton Blanchard5d67be92011-07-04 21:57:50 +10001803 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001804 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
1805 if (err < 0)
1806 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001807 }
1808
David Ahern1424dc92011-03-09 22:23:28 -07001809 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001810 symbol_conf.use_callchain = true;
1811 else
1812 symbol_conf.use_callchain = false;
1813
Tom Zanussi956ffd02009-11-25 01:15:46 -06001814 if (generate_script_lang) {
1815 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001816 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001817
David Ahern2c9e45f72011-03-17 10:03:21 -06001818 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001819 fprintf(stderr,
1820 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001821 err = -EINVAL;
1822 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07001823 }
1824
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001825 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06001826 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001827 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001828 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001829 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001830 }
1831
1832 err = fstat(input, &perf_stat);
1833 if (err < 0) {
1834 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001835 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001836 }
1837
1838 if (!perf_stat.st_size) {
1839 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001840 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001841 }
1842
1843 scripting_ops = script_spec__lookup(generate_script_lang);
1844 if (!scripting_ops) {
1845 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001846 err = -ENOENT;
1847 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001848 }
1849
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01001850 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001851 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001852 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001853 }
1854
1855 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001856 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001857 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001858 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001859 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001860 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001861 }
1862
David Ahern9cbdb702011-04-06 21:54:20 -06001863
1864 err = perf_session__check_output_opt(session);
1865 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001866 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06001867
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001868 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001869
Adrian Hunterd445dd22014-08-15 22:08:37 +03001870 flush_scripting();
1871
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001872out_delete:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001873 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001874
1875 if (script_started)
1876 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001877out:
1878 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001879}