blob: 0659dff4a21d58bc196bf0b5ef81d8f35df71bfa [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 Olsaf5fc14122013-10-15 16:27:32 +020018#include "util/data.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100019#include <linux/bitmap.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020020
Tom Zanussi956ffd02009-11-25 01:15:46 -060021static char const *script_name;
22static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020023static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020024static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020025static u64 nr_unordered;
Tom Zanussi34c86ea2010-11-10 08:15:43 -060026extern const struct option record_options[];
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;
Anton Blanchard5d67be92011-07-04 21:57:50 +100030static const char *cpu_list;
31static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Tom Zanussi956ffd02009-11-25 01:15:46 -060032
David Ahern745f43e2011-03-09 22:23:26 -070033enum perf_output_field {
34 PERF_OUTPUT_COMM = 1U << 0,
35 PERF_OUTPUT_TID = 1U << 1,
36 PERF_OUTPUT_PID = 1U << 2,
37 PERF_OUTPUT_TIME = 1U << 3,
38 PERF_OUTPUT_CPU = 1U << 4,
39 PERF_OUTPUT_EVNAME = 1U << 5,
40 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060041 PERF_OUTPUT_IP = 1U << 7,
42 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060043 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060044 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090045 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020046 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020047 PERF_OUTPUT_PERIOD = 1U << 13,
David Ahern745f43e2011-03-09 22:23:26 -070048};
49
50struct output_option {
51 const char *str;
52 enum perf_output_field field;
53} all_output_options[] = {
54 {.str = "comm", .field = PERF_OUTPUT_COMM},
55 {.str = "tid", .field = PERF_OUTPUT_TID},
56 {.str = "pid", .field = PERF_OUTPUT_PID},
57 {.str = "time", .field = PERF_OUTPUT_TIME},
58 {.str = "cpu", .field = PERF_OUTPUT_CPU},
59 {.str = "event", .field = PERF_OUTPUT_EVNAME},
60 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060061 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070062 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060063 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060064 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090065 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020066 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +020067 {.str = "period", .field = PERF_OUTPUT_PERIOD},
David Ahern745f43e2011-03-09 22:23:26 -070068};
69
70/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -060071static struct {
72 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060073 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -040074 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -060075 u64 fields;
76 u64 invalid_fields;
77} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -070078
David Ahern2c9e45f72011-03-17 10:03:21 -060079 [PERF_TYPE_HARDWARE] = {
80 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -070081
David Ahern2c9e45f72011-03-17 10:03:21 -060082 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
83 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060084 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
David Ahern610723f2011-05-27 14:28:44 -060085 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
David Ahern2c9e45f72011-03-17 10:03:21 -060086
87 .invalid_fields = PERF_OUTPUT_TRACE,
88 },
89
90 [PERF_TYPE_SOFTWARE] = {
91 .user_set = false,
92
93 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
94 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060095 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
David Ahern610723f2011-05-27 14:28:44 -060096 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
David Ahern2c9e45f72011-03-17 10:03:21 -060097
98 .invalid_fields = PERF_OUTPUT_TRACE,
99 },
100
101 [PERF_TYPE_TRACEPOINT] = {
102 .user_set = false,
103
104 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
105 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
106 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
107 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700108
109 [PERF_TYPE_RAW] = {
110 .user_set = false,
111
112 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
113 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600114 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
David Ahern610723f2011-05-27 14:28:44 -0600115 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700116
117 .invalid_fields = PERF_OUTPUT_TRACE,
118 },
David Ahern1424dc92011-03-09 22:23:28 -0700119};
David Ahern745f43e2011-03-09 22:23:26 -0700120
David Ahern2c9e45f72011-03-17 10:03:21 -0600121static bool output_set_by_user(void)
122{
123 int j;
124 for (j = 0; j < PERF_TYPE_MAX; ++j) {
125 if (output[j].user_set)
126 return true;
127 }
128 return false;
129}
David Ahern745f43e2011-03-09 22:23:26 -0700130
David Ahern9cbdb702011-04-06 21:54:20 -0600131static const char *output_field2str(enum perf_output_field field)
132{
133 int i, imax = ARRAY_SIZE(all_output_options);
134 const char *str = "";
135
136 for (i = 0; i < imax; ++i) {
137 if (all_output_options[i].field == field) {
138 str = all_output_options[i].str;
139 break;
140 }
141 }
142 return str;
143}
144
David Ahern2c9e45f72011-03-17 10:03:21 -0600145#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700146
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300147static int perf_evsel__check_stype(struct perf_evsel *evsel,
148 u64 sample_type, const char *sample_msg,
149 enum perf_output_field field)
David Ahern1424dc92011-03-09 22:23:28 -0700150{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300151 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600152 int type = attr->type;
153 const char *evname;
154
155 if (attr->sample_type & sample_type)
156 return 0;
157
158 if (output[type].user_set) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300159 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600160 pr_err("Samples for '%s' event do not have %s attribute set. "
161 "Cannot print '%s' field.\n",
162 evname, sample_msg, output_field2str(field));
163 return -1;
164 }
165
166 /* user did not ask for it explicitly so remove from the default list */
167 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300168 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600169 pr_debug("Samples for '%s' event do not have %s attribute set. "
170 "Skipping '%s' field.\n",
171 evname, sample_msg, output_field2str(field));
172
173 return 0;
174}
175
176static int perf_evsel__check_attr(struct perf_evsel *evsel,
177 struct perf_session *session)
178{
179 struct perf_event_attr *attr = &evsel->attr;
180
David Ahern1424dc92011-03-09 22:23:28 -0700181 if (PRINT_FIELD(TRACE) &&
182 !perf_session__has_traces(session, "record -R"))
183 return -EINVAL;
184
David Ahern787bef12011-05-27 14:28:43 -0600185 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300186 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
187 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700188 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700189 }
David Ahern7cec0922011-05-30 13:08:23 -0600190
191 if (PRINT_FIELD(ADDR) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300192 perf_evsel__check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
193 PERF_OUTPUT_ADDR))
David Ahern7cec0922011-05-30 13:08:23 -0600194 return -EINVAL;
195
196 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
197 pr_err("Display of symbols requested but neither sample IP nor "
198 "sample address\nis selected. Hence, no addresses to convert "
199 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600200 return -EINVAL;
201 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900202 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
203 pr_err("Display of offsets requested but symbol is not"
204 "selected.\n");
205 return -EINVAL;
206 }
David Ahern7cec0922011-05-30 13:08:23 -0600207 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
208 pr_err("Display of DSO requested but neither sample IP nor "
209 "sample address\nis selected. Hence, no addresses to convert "
210 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600211 return -EINVAL;
212 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200213 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
214 pr_err("Display of source line number requested but sample IP is not\n"
215 "selected. Hence, no address to lookup the source line number.\n");
216 return -EINVAL;
217 }
David Ahern1424dc92011-03-09 22:23:28 -0700218
219 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300220 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
221 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700222 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700223
224 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300225 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
226 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700227 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700228
229 if (PRINT_FIELD(CPU) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300230 perf_evsel__check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
231 PERF_OUTPUT_CPU))
David Ahern1424dc92011-03-09 22:23:28 -0700232 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600233
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200234 if (PRINT_FIELD(PERIOD) &&
235 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
236 PERF_OUTPUT_PERIOD))
237 return -EINVAL;
238
David Ahern9cbdb702011-04-06 21:54:20 -0600239 return 0;
240}
241
Adrian Hunter7ea95722013-11-01 15:51:30 +0200242static void set_print_ip_opts(struct perf_event_attr *attr)
243{
244 unsigned int type = attr->type;
245
246 output[type].print_ip_opts = 0;
247 if (PRINT_FIELD(IP))
248 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
249
250 if (PRINT_FIELD(SYM))
251 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
252
253 if (PRINT_FIELD(DSO))
254 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
255
256 if (PRINT_FIELD(SYMOFFSET))
257 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200258
259 if (PRINT_FIELD(SRCLINE))
260 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200261}
262
David Ahern9cbdb702011-04-06 21:54:20 -0600263/*
264 * verify all user requested events exist and the samples
265 * have the expected data
266 */
267static int perf_session__check_output_opt(struct perf_session *session)
268{
269 int j;
270 struct perf_evsel *evsel;
271
272 for (j = 0; j < PERF_TYPE_MAX; ++j) {
273 evsel = perf_session__find_first_evtype(session, j);
274
275 /*
276 * even if fields is set to 0 (ie., show nothing) event must
277 * exist if user explicitly includes it on the command line
278 */
279 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
280 pr_err("%s events do not exist. "
281 "Remove corresponding -f option to proceed.\n",
282 event_type(j));
283 return -1;
284 }
285
286 if (evsel && output[j].fields &&
287 perf_evsel__check_attr(evsel, session))
288 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400289
290 if (evsel == NULL)
291 continue;
292
Adrian Hunter7ea95722013-11-01 15:51:30 +0200293 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700294 }
295
Adrian Hunter98526ee2014-07-31 09:00:59 +0300296 if (!no_callchain) {
297 bool use_callchain = false;
298
299 evlist__for_each(session->evlist, evsel) {
300 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
301 use_callchain = true;
302 break;
303 }
304 }
305 if (!use_callchain)
306 symbol_conf.use_callchain = false;
307 }
308
David Ahern80b8b492013-11-19 21:07:37 -0700309 /*
310 * set default for tracepoints to print symbols only
311 * if callchains are present
312 */
313 if (symbol_conf.use_callchain &&
314 !output[PERF_TYPE_TRACEPOINT].user_set) {
315 struct perf_event_attr *attr;
316
317 j = PERF_TYPE_TRACEPOINT;
318 evsel = perf_session__find_first_evtype(session, j);
319 if (evsel == NULL)
320 goto out;
321
322 attr = &evsel->attr;
323
324 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
325 output[j].fields |= PERF_OUTPUT_IP;
326 output[j].fields |= PERF_OUTPUT_SYM;
327 output[j].fields |= PERF_OUTPUT_DSO;
328 set_print_ip_opts(attr);
329 }
330 }
331
332out:
David Ahern1424dc92011-03-09 22:23:28 -0700333 return 0;
334}
David Ahern745f43e2011-03-09 22:23:26 -0700335
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300336static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700337 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300338 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700339{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300340 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700341 unsigned long secs;
342 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700343 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700344
David Ahern745f43e2011-03-09 22:23:26 -0700345 if (PRINT_FIELD(COMM)) {
346 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200347 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600348 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200349 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700350 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200351 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700352 }
David Ahernc70c94b2011-03-09 22:23:25 -0700353
David Ahern745f43e2011-03-09 22:23:26 -0700354 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
355 printf("%5d/%-5d ", sample->pid, sample->tid);
356 else if (PRINT_FIELD(PID))
357 printf("%5d ", sample->pid);
358 else if (PRINT_FIELD(TID))
359 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700360
David Ahern745f43e2011-03-09 22:23:26 -0700361 if (PRINT_FIELD(CPU)) {
362 if (latency_format)
363 printf("%3d ", sample->cpu);
364 else
365 printf("[%03d] ", sample->cpu);
366 }
David Ahernc70c94b2011-03-09 22:23:25 -0700367
David Ahern745f43e2011-03-09 22:23:26 -0700368 if (PRINT_FIELD(TIME)) {
369 nsecs = sample->time;
370 secs = nsecs / NSECS_PER_SEC;
371 nsecs -= secs * NSECS_PER_SEC;
372 usecs = nsecs / NSECS_PER_USEC;
373 printf("%5lu.%06lu: ", secs, usecs);
374 }
David Ahernc70c94b2011-03-09 22:23:25 -0700375}
376
David Ahern7cec0922011-05-30 13:08:23 -0600377static void print_sample_addr(union perf_event *event,
378 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200379 struct machine *machine,
David Ahern7cec0922011-05-30 13:08:23 -0600380 struct thread *thread,
381 struct perf_event_attr *attr)
382{
383 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600384
385 printf("%16" PRIx64, sample->addr);
386
387 if (!sample_addr_correlates_sym(attr))
388 return;
389
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300390 perf_event__preprocess_sample_addr(event, sample, machine, thread, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600391
392 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900393 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900394 if (PRINT_FIELD(SYMOFFSET))
395 symbol__fprintf_symname_offs(al.sym, &al, stdout);
396 else
397 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600398 }
399
400 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900401 printf(" (");
402 map__fprintf_dsoname(al.map, stdout);
403 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600404 }
405}
406
Akihiro Nagai95582592012-01-30 13:43:09 +0900407static void print_sample_bts(union perf_event *event,
408 struct perf_sample *sample,
409 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200410 struct thread *thread,
411 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900412{
413 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300414 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900415
416 /* print branch_from information */
417 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300418 unsigned int print_opts = output[attr->type].print_ip_opts;
419
420 if (symbol_conf.use_callchain && sample->callchain) {
Akihiro Nagai95582592012-01-30 13:43:09 +0900421 printf("\n");
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300422 } else {
423 printf(" ");
424 if (print_opts & PRINT_IP_OPT_SRCLINE) {
425 print_srcline_last = true;
426 print_opts &= ~PRINT_IP_OPT_SRCLINE;
427 }
428 }
429 perf_evsel__print_ip(evsel, sample, al, print_opts,
David Ahern307cbb92013-08-07 22:50:53 -0400430 PERF_MAX_STACK_DEPTH);
Akihiro Nagai95582592012-01-30 13:43:09 +0900431 }
432
Akihiro Nagai95582592012-01-30 13:43:09 +0900433 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300434 if (PRINT_FIELD(ADDR) ||
435 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300436 !output[attr->type].user_set)) {
437 printf(" => ");
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300438 print_sample_addr(event, sample, al->machine, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300439 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900440
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300441 if (print_srcline_last)
442 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
443
Akihiro Nagai95582592012-01-30 13:43:09 +0900444 printf("\n");
445}
446
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300447static void process_event(union perf_event *event, struct perf_sample *sample,
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300448 struct perf_evsel *evsel, struct thread *thread,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200449 struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700450{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300451 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700452
David Ahern2c9e45f72011-03-17 10:03:21 -0600453 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700454 return;
455
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300456 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700457
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200458 if (PRINT_FIELD(PERIOD))
459 printf("%10" PRIu64 " ", sample->period);
460
Namhyung Kime944d3d2013-11-18 14:34:52 +0900461 if (PRINT_FIELD(EVNAME)) {
462 const char *evname = perf_evsel__name(evsel);
463 printf("%s: ", evname ? evname : "[unknown]");
464 }
465
Akihiro Nagai95582592012-01-30 13:43:09 +0900466 if (is_bts_event(attr)) {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300467 print_sample_bts(event, sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900468 return;
469 }
470
David Ahern745f43e2011-03-09 22:23:26 -0700471 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300472 event_format__print(evsel->tp_format, sample->cpu,
473 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600474 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300475 print_sample_addr(event, sample, al->machine, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600476
David Ahern787bef12011-05-27 14:28:43 -0600477 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700478 if (!symbol_conf.use_callchain)
479 printf(" ");
480 else
481 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400482
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300483 perf_evsel__print_ip(evsel, sample, al,
David Ahern307cbb92013-08-07 22:50:53 -0400484 output[attr->type].print_ip_opts,
485 PERF_MAX_STACK_DEPTH);
David Ahernc0230b22011-03-09 22:23:27 -0700486 }
487
David Ahernc70c94b2011-03-09 22:23:25 -0700488 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700489}
490
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300491static int default_start_script(const char *script __maybe_unused,
492 int argc __maybe_unused,
493 const char **argv __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600494{
495 return 0;
496}
497
Adrian Hunterd445dd22014-08-15 22:08:37 +0300498static int default_flush_script(void)
499{
500 return 0;
501}
502
Tom Zanussi956ffd02009-11-25 01:15:46 -0600503static int default_stop_script(void)
504{
505 return 0;
506}
507
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300508static int default_generate_script(struct pevent *pevent __maybe_unused,
509 const char *outfile __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600510{
511 return 0;
512}
513
514static struct scripting_ops default_scripting_ops = {
515 .start_script = default_start_script,
Adrian Hunterd445dd22014-08-15 22:08:37 +0300516 .flush_script = default_flush_script,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600517 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700518 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600519 .generate_script = default_generate_script,
520};
521
522static struct scripting_ops *scripting_ops;
523
524static void setup_scripting(void)
525{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600526 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600527 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600528
Tom Zanussi956ffd02009-11-25 01:15:46 -0600529 scripting_ops = &default_scripting_ops;
530}
531
Adrian Hunterd445dd22014-08-15 22:08:37 +0300532static int flush_scripting(void)
533{
534 return scripting_ops->flush_script();
535}
536
Tom Zanussi956ffd02009-11-25 01:15:46 -0600537static int cleanup_scripting(void)
538{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100539 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500540
Tom Zanussi956ffd02009-11-25 01:15:46 -0600541 return scripting_ops->stop_script();
542}
543
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300544static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200545 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200546 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300547 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200548 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200549{
David Aherne7984b72011-11-21 10:02:52 -0700550 struct addr_location al;
Adrian Hunteref893252013-08-27 11:23:06 +0300551 struct thread *thread = machine__findnew_thread(machine, sample->pid,
552 sample->tid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200553
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200554 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200555 pr_debug("problem processing %d event, skipping it.\n",
556 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200557 return -1;
558 }
559
David Ahern1424dc92011-03-09 22:23:28 -0700560 if (debug_mode) {
561 if (sample->time < last_timestamp) {
562 pr_err("Samples misordered, previous: %" PRIu64
563 " this: %" PRIu64 "\n", last_timestamp,
564 sample->time);
565 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200566 }
David Ahern1424dc92011-03-09 22:23:28 -0700567 last_timestamp = sample->time;
568 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200569 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000570
Adrian Huntere44baa32013-08-08 14:32:25 +0300571 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700572 pr_err("problem processing %d event, skipping it.\n",
573 event->header.type);
574 return -1;
575 }
576
577 if (al.filtered)
578 return 0;
579
Anton Blanchard5d67be92011-07-04 21:57:50 +1000580 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
581 return 0;
582
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300583 scripting_ops->process_event(event, sample, evsel, thread, &al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200584
585 return 0;
586}
587
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300588struct perf_script {
589 struct perf_tool tool;
590 struct perf_session *session;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900591 bool show_task_events;
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900592 bool show_mmap_events;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200593};
594
Adrian Hunter7ea95722013-11-01 15:51:30 +0200595static int process_attr(struct perf_tool *tool, union perf_event *event,
596 struct perf_evlist **pevlist)
597{
598 struct perf_script *scr = container_of(tool, struct perf_script, tool);
599 struct perf_evlist *evlist;
600 struct perf_evsel *evsel, *pos;
601 int err;
602
603 err = perf_event__process_attr(tool, event, pevlist);
604 if (err)
605 return err;
606
607 evlist = *pevlist;
608 evsel = perf_evlist__last(*pevlist);
609
610 if (evsel->attr.type >= PERF_TYPE_MAX)
611 return 0;
612
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300613 evlist__for_each(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +0200614 if (pos->attr.type == evsel->attr.type && pos != evsel)
615 return 0;
616 }
617
618 set_print_ip_opts(&evsel->attr);
619
620 return perf_evsel__check_attr(evsel, scr->session);
621}
622
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900623static int process_comm_event(struct perf_tool *tool,
624 union perf_event *event,
625 struct perf_sample *sample,
626 struct machine *machine)
627{
628 struct thread *thread;
629 struct perf_script *script = container_of(tool, struct perf_script, tool);
630 struct perf_session *session = script->session;
631 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
632 int ret = -1;
633
634 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
635 if (thread == NULL) {
636 pr_debug("problem processing COMM event, skipping it.\n");
637 return -1;
638 }
639
640 if (perf_event__process_comm(tool, event, sample, machine) < 0)
641 goto out;
642
643 if (!evsel->attr.sample_id_all) {
644 sample->cpu = 0;
645 sample->time = 0;
646 sample->tid = event->comm.tid;
647 sample->pid = event->comm.pid;
648 }
649 print_sample_start(sample, thread, evsel);
650 perf_event__fprintf(event, stdout);
651 ret = 0;
652
653out:
654 return ret;
655}
656
657static int process_fork_event(struct perf_tool *tool,
658 union perf_event *event,
659 struct perf_sample *sample,
660 struct machine *machine)
661{
662 struct thread *thread;
663 struct perf_script *script = container_of(tool, struct perf_script, tool);
664 struct perf_session *session = script->session;
665 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
666
667 if (perf_event__process_fork(tool, event, sample, machine) < 0)
668 return -1;
669
670 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
671 if (thread == NULL) {
672 pr_debug("problem processing FORK event, skipping it.\n");
673 return -1;
674 }
675
676 if (!evsel->attr.sample_id_all) {
677 sample->cpu = 0;
678 sample->time = event->fork.time;
679 sample->tid = event->fork.tid;
680 sample->pid = event->fork.pid;
681 }
682 print_sample_start(sample, thread, evsel);
683 perf_event__fprintf(event, stdout);
684
685 return 0;
686}
687static int process_exit_event(struct perf_tool *tool,
688 union perf_event *event,
689 struct perf_sample *sample,
690 struct machine *machine)
691{
692 struct thread *thread;
693 struct perf_script *script = container_of(tool, struct perf_script, tool);
694 struct perf_session *session = script->session;
695 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
696
697 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
698 if (thread == NULL) {
699 pr_debug("problem processing EXIT event, skipping it.\n");
700 return -1;
701 }
702
703 if (!evsel->attr.sample_id_all) {
704 sample->cpu = 0;
705 sample->time = 0;
706 sample->tid = event->comm.tid;
707 sample->pid = event->comm.pid;
708 }
709 print_sample_start(sample, thread, evsel);
710 perf_event__fprintf(event, stdout);
711
712 if (perf_event__process_exit(tool, event, sample, machine) < 0)
713 return -1;
714
715 return 0;
716}
717
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900718static int process_mmap_event(struct perf_tool *tool,
719 union perf_event *event,
720 struct perf_sample *sample,
721 struct machine *machine)
722{
723 struct thread *thread;
724 struct perf_script *script = container_of(tool, struct perf_script, tool);
725 struct perf_session *session = script->session;
726 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
727
728 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
729 return -1;
730
731 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
732 if (thread == NULL) {
733 pr_debug("problem processing MMAP event, skipping it.\n");
734 return -1;
735 }
736
737 if (!evsel->attr.sample_id_all) {
738 sample->cpu = 0;
739 sample->time = 0;
740 sample->tid = event->mmap.tid;
741 sample->pid = event->mmap.pid;
742 }
743 print_sample_start(sample, thread, evsel);
744 perf_event__fprintf(event, stdout);
745
746 return 0;
747}
748
749static int process_mmap2_event(struct perf_tool *tool,
750 union perf_event *event,
751 struct perf_sample *sample,
752 struct machine *machine)
753{
754 struct thread *thread;
755 struct perf_script *script = container_of(tool, struct perf_script, tool);
756 struct perf_session *session = script->session;
757 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
758
759 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
760 return -1;
761
762 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
763 if (thread == NULL) {
764 pr_debug("problem processing MMAP2 event, skipping it.\n");
765 return -1;
766 }
767
768 if (!evsel->attr.sample_id_all) {
769 sample->cpu = 0;
770 sample->time = 0;
771 sample->tid = event->mmap2.tid;
772 sample->pid = event->mmap2.pid;
773 }
774 print_sample_start(sample, thread, evsel);
775 perf_event__fprintf(event, stdout);
776
777 return 0;
778}
779
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300780static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500781{
782 session_done = 1;
783}
784
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300785static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200786{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200787 int ret;
788
Tom Zanussic239da32010-04-01 23:59:18 -0500789 signal(SIGINT, sig_handler);
790
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900791 /* override event processing functions */
792 if (script->show_task_events) {
793 script->tool.comm = process_comm_event;
794 script->tool.fork = process_fork_event;
795 script->tool.exit = process_exit_event;
796 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900797 if (script->show_mmap_events) {
798 script->tool.mmap = process_mmap_event;
799 script->tool.mmap2 = process_mmap2_event;
800 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900801
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300802 ret = perf_session__process_events(script->session, &script->tool);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200803
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200804 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200805 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200806
807 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200808}
809
Tom Zanussi956ffd02009-11-25 01:15:46 -0600810struct script_spec {
811 struct list_head node;
812 struct scripting_ops *ops;
813 char spec[0];
814};
815
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200816static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600817
818static struct script_spec *script_spec__new(const char *spec,
819 struct scripting_ops *ops)
820{
821 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
822
823 if (s != NULL) {
824 strcpy(s->spec, spec);
825 s->ops = ops;
826 }
827
828 return s;
829}
830
Tom Zanussi956ffd02009-11-25 01:15:46 -0600831static void script_spec__add(struct script_spec *s)
832{
833 list_add_tail(&s->node, &script_specs);
834}
835
836static struct script_spec *script_spec__find(const char *spec)
837{
838 struct script_spec *s;
839
840 list_for_each_entry(s, &script_specs, node)
841 if (strcasecmp(s->spec, spec) == 0)
842 return s;
843 return NULL;
844}
845
846static struct script_spec *script_spec__findnew(const char *spec,
847 struct scripting_ops *ops)
848{
849 struct script_spec *s = script_spec__find(spec);
850
851 if (s)
852 return s;
853
854 s = script_spec__new(spec, ops);
855 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +0900856 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600857
858 script_spec__add(s);
859
860 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600861}
862
863int script_spec_register(const char *spec, struct scripting_ops *ops)
864{
865 struct script_spec *s;
866
867 s = script_spec__find(spec);
868 if (s)
869 return -1;
870
871 s = script_spec__findnew(spec, ops);
872 if (!s)
873 return -1;
874
875 return 0;
876}
877
878static struct scripting_ops *script_spec__lookup(const char *spec)
879{
880 struct script_spec *s = script_spec__find(spec);
881 if (!s)
882 return NULL;
883
884 return s->ops;
885}
886
887static void list_available_languages(void)
888{
889 struct script_spec *s;
890
891 fprintf(stderr, "\n");
892 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100893 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600894
895 list_for_each_entry(s, &script_specs, node)
896 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
897
898 fprintf(stderr, "\n");
899}
900
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300901static int parse_scriptname(const struct option *opt __maybe_unused,
902 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600903{
904 char spec[PATH_MAX];
905 const char *script, *ext;
906 int len;
907
Tom Zanussif526d682010-01-27 02:27:52 -0600908 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600909 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600910 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600911 }
912
913 script = strchr(str, ':');
914 if (script) {
915 len = script - str;
916 if (len >= PATH_MAX) {
917 fprintf(stderr, "invalid language specifier");
918 return -1;
919 }
920 strncpy(spec, str, len);
921 spec[len] = '\0';
922 scripting_ops = script_spec__lookup(spec);
923 if (!scripting_ops) {
924 fprintf(stderr, "invalid language specifier");
925 return -1;
926 }
927 script++;
928 } else {
929 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100930 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600931 if (!ext) {
932 fprintf(stderr, "invalid script extension");
933 return -1;
934 }
935 scripting_ops = script_spec__lookup(++ext);
936 if (!scripting_ops) {
937 fprintf(stderr, "invalid script extension");
938 return -1;
939 }
940 }
941
942 script_name = strdup(script);
943
944 return 0;
945}
946
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300947static int parse_output_fields(const struct option *opt __maybe_unused,
948 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -0700949{
950 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -0500951 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -0600952 int j;
David Ahern745f43e2011-03-09 22:23:26 -0700953 int rc = 0;
954 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -0700955 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -0700956
957 if (!str)
958 return -ENOMEM;
959
David Ahern2c9e45f72011-03-17 10:03:21 -0600960 /* first word can state for which event type the user is specifying
961 * the fields. If no type exists, the specified fields apply to all
962 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -0700963 */
David Ahern2c9e45f72011-03-17 10:03:21 -0600964 tok = strchr(str, ':');
965 if (tok) {
966 *tok = '\0';
967 tok++;
968 if (!strcmp(str, "hw"))
969 type = PERF_TYPE_HARDWARE;
970 else if (!strcmp(str, "sw"))
971 type = PERF_TYPE_SOFTWARE;
972 else if (!strcmp(str, "trace"))
973 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -0700974 else if (!strcmp(str, "raw"))
975 type = PERF_TYPE_RAW;
David Ahern2c9e45f72011-03-17 10:03:21 -0600976 else {
977 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +0100978 rc = -EINVAL;
979 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -0600980 }
981
982 if (output[type].user_set)
983 pr_warning("Overriding previous field request for %s events.\n",
984 event_type(type));
985
986 output[type].fields = 0;
987 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600988 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -0600989
990 } else {
991 tok = str;
992 if (strlen(str) == 0) {
993 fprintf(stderr,
994 "Cannot set fields to 'none' for all event types.\n");
995 rc = -EINVAL;
996 goto out;
997 }
998
999 if (output_set_by_user())
1000 pr_warning("Overriding previous field request for all events.\n");
1001
1002 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1003 output[j].fields = 0;
1004 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001005 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001006 }
David Ahern1424dc92011-03-09 22:23:28 -07001007 }
1008
David Ahern2c9e45f72011-03-17 10:03:21 -06001009 tok = strtok(tok, ",");
1010 while (tok) {
David Ahern745f43e2011-03-09 22:23:26 -07001011 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001012 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001013 break;
David Ahern745f43e2011-03-09 22:23:26 -07001014 }
1015 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001016 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001017 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001018 goto out;
1019 }
1020
1021 if (type == -1) {
1022 /* add user option to all events types for
1023 * which it is valid
1024 */
1025 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1026 if (output[j].invalid_fields & all_output_options[i].field) {
1027 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1028 all_output_options[i].str, event_type(j));
1029 } else
1030 output[j].fields |= all_output_options[i].field;
1031 }
1032 } else {
1033 if (output[type].invalid_fields & all_output_options[i].field) {
1034 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1035 all_output_options[i].str, event_type(type));
1036
1037 rc = -EINVAL;
1038 goto out;
1039 }
1040 output[type].fields |= all_output_options[i].field;
1041 }
1042
1043 tok = strtok(NULL, ",");
1044 }
1045
1046 if (type >= 0) {
1047 if (output[type].fields == 0) {
1048 pr_debug("No fields requested for %s type. "
1049 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001050 }
David Ahern1424dc92011-03-09 22:23:28 -07001051 }
David Ahern745f43e2011-03-09 22:23:26 -07001052
David Ahern2c9e45f72011-03-17 10:03:21 -06001053out:
David Ahern745f43e2011-03-09 22:23:26 -07001054 free(str);
1055 return rc;
1056}
1057
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001058/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1059static int is_directory(const char *base_path, const struct dirent *dent)
1060{
1061 char path[PATH_MAX];
1062 struct stat st;
1063
1064 sprintf(path, "%s/%s", base_path, dent->d_name);
1065 if (stat(path, &st))
1066 return 0;
1067
1068 return S_ISDIR(st.st_mode);
1069}
1070
1071#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001072 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1073 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001074 if ((lang_dirent.d_type == DT_DIR || \
1075 (lang_dirent.d_type == DT_UNKNOWN && \
1076 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001077 (strcmp(lang_dirent.d_name, ".")) && \
1078 (strcmp(lang_dirent.d_name, "..")))
1079
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001080#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001081 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1082 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001083 if (script_dirent.d_type != DT_DIR && \
1084 (script_dirent.d_type != DT_UNKNOWN || \
1085 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001086
1087
1088#define RECORD_SUFFIX "-record"
1089#define REPORT_SUFFIX "-report"
1090
1091struct script_desc {
1092 struct list_head node;
1093 char *name;
1094 char *half_liner;
1095 char *args;
1096};
1097
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001098static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001099
1100static struct script_desc *script_desc__new(const char *name)
1101{
1102 struct script_desc *s = zalloc(sizeof(*s));
1103
Tom Zanussib5b87312010-11-10 08:16:51 -06001104 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001105 s->name = strdup(name);
1106
1107 return s;
1108}
1109
1110static void script_desc__delete(struct script_desc *s)
1111{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001112 zfree(&s->name);
1113 zfree(&s->half_liner);
1114 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001115 free(s);
1116}
1117
1118static void script_desc__add(struct script_desc *s)
1119{
1120 list_add_tail(&s->node, &script_descs);
1121}
1122
1123static struct script_desc *script_desc__find(const char *name)
1124{
1125 struct script_desc *s;
1126
1127 list_for_each_entry(s, &script_descs, node)
1128 if (strcasecmp(s->name, name) == 0)
1129 return s;
1130 return NULL;
1131}
1132
1133static struct script_desc *script_desc__findnew(const char *name)
1134{
1135 struct script_desc *s = script_desc__find(name);
1136
1137 if (s)
1138 return s;
1139
1140 s = script_desc__new(name);
1141 if (!s)
1142 goto out_delete_desc;
1143
1144 script_desc__add(s);
1145
1146 return s;
1147
1148out_delete_desc:
1149 script_desc__delete(s);
1150
1151 return NULL;
1152}
1153
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001154static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001155{
1156 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001157 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001158
1159 if (strlen(str) > suffix_len) {
1160 p = str + strlen(str) - suffix_len;
1161 if (!strncmp(p, suffix, suffix_len))
1162 return p;
1163 }
1164
1165 return NULL;
1166}
1167
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001168static int read_script_info(struct script_desc *desc, const char *filename)
1169{
1170 char line[BUFSIZ], *p;
1171 FILE *fp;
1172
1173 fp = fopen(filename, "r");
1174 if (!fp)
1175 return -1;
1176
1177 while (fgets(line, sizeof(line), fp)) {
1178 p = ltrim(line);
1179 if (strlen(p) == 0)
1180 continue;
1181 if (*p != '#')
1182 continue;
1183 p++;
1184 if (strlen(p) && *p == '!')
1185 continue;
1186
1187 p = ltrim(p);
1188 if (strlen(p) && p[strlen(p) - 1] == '\n')
1189 p[strlen(p) - 1] = '\0';
1190
1191 if (!strncmp(p, "description:", strlen("description:"))) {
1192 p += strlen("description:");
1193 desc->half_liner = strdup(ltrim(p));
1194 continue;
1195 }
1196
1197 if (!strncmp(p, "args:", strlen("args:"))) {
1198 p += strlen("args:");
1199 desc->args = strdup(ltrim(p));
1200 continue;
1201 }
1202 }
1203
1204 fclose(fp);
1205
1206 return 0;
1207}
1208
Robert Richter38efb532011-11-25 11:38:40 +01001209static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1210{
1211 char *script_root, *str;
1212
1213 script_root = strdup(script_dirent->d_name);
1214 if (!script_root)
1215 return NULL;
1216
1217 str = (char *)ends_with(script_root, suffix);
1218 if (!str) {
1219 free(script_root);
1220 return NULL;
1221 }
1222
1223 *str = '\0';
1224 return script_root;
1225}
1226
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001227static int list_available_scripts(const struct option *opt __maybe_unused,
1228 const char *s __maybe_unused,
1229 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001230{
1231 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1232 char scripts_path[MAXPATHLEN];
1233 DIR *scripts_dir, *lang_dir;
1234 char script_path[MAXPATHLEN];
1235 char lang_path[MAXPATHLEN];
1236 struct script_desc *desc;
1237 char first_half[BUFSIZ];
1238 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001239
1240 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1241
1242 scripts_dir = opendir(scripts_path);
1243 if (!scripts_dir)
1244 return -1;
1245
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001246 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001247 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1248 lang_dirent.d_name);
1249 lang_dir = opendir(lang_path);
1250 if (!lang_dir)
1251 continue;
1252
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001253 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001254 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1255 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001256 desc = script_desc__findnew(script_root);
1257 snprintf(script_path, MAXPATHLEN, "%s/%s",
1258 lang_path, script_dirent.d_name);
1259 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001260 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001261 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001262 }
1263 }
1264
1265 fprintf(stdout, "List of available trace scripts:\n");
1266 list_for_each_entry(desc, &script_descs, node) {
1267 sprintf(first_half, "%s %s", desc->name,
1268 desc->args ? desc->args : "");
1269 fprintf(stdout, " %-36s %s\n", first_half,
1270 desc->half_liner ? desc->half_liner : "");
1271 }
1272
1273 exit(0);
1274}
1275
Feng Tange5f37052012-09-07 16:42:26 +08001276/*
Feng Tang49e639e2012-10-30 11:56:03 +08001277 * Some scripts specify the required events in their "xxx-record" file,
1278 * this function will check if the events in perf.data match those
1279 * mentioned in the "xxx-record".
1280 *
1281 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1282 * which is covered well now. And new parsing code should be added to
1283 * cover the future complexing formats like event groups etc.
1284 */
1285static int check_ev_match(char *dir_name, char *scriptname,
1286 struct perf_session *session)
1287{
1288 char filename[MAXPATHLEN], evname[128];
1289 char line[BUFSIZ], *p;
1290 struct perf_evsel *pos;
1291 int match, len;
1292 FILE *fp;
1293
1294 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1295
1296 fp = fopen(filename, "r");
1297 if (!fp)
1298 return -1;
1299
1300 while (fgets(line, sizeof(line), fp)) {
1301 p = ltrim(line);
1302 if (*p == '#')
1303 continue;
1304
1305 while (strlen(p)) {
1306 p = strstr(p, "-e");
1307 if (!p)
1308 break;
1309
1310 p += 2;
1311 p = ltrim(p);
1312 len = strcspn(p, " \t");
1313 if (!len)
1314 break;
1315
1316 snprintf(evname, len + 1, "%s", p);
1317
1318 match = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001319 evlist__for_each(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001320 if (!strcmp(perf_evsel__name(pos), evname)) {
1321 match = 1;
1322 break;
1323 }
1324 }
1325
1326 if (!match) {
1327 fclose(fp);
1328 return -1;
1329 }
1330 }
1331 }
1332
1333 fclose(fp);
1334 return 0;
1335}
1336
1337/*
Feng Tange5f37052012-09-07 16:42:26 +08001338 * Return -1 if none is found, otherwise the actual scripts number.
1339 *
1340 * Currently the only user of this function is the script browser, which
1341 * will list all statically runnable scripts, select one, execute it and
1342 * show the output in a perf browser.
1343 */
1344int find_scripts(char **scripts_array, char **scripts_path_array)
1345{
1346 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001347 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001348 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001349 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001350 struct perf_data_file file = {
1351 .path = input_name,
1352 .mode = PERF_DATA_MODE_READ,
1353 };
Feng Tange5f37052012-09-07 16:42:26 +08001354 char *temp;
1355 int i = 0;
1356
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001357 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001358 if (!session)
1359 return -1;
1360
Feng Tange5f37052012-09-07 16:42:26 +08001361 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1362
1363 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001364 if (!scripts_dir) {
1365 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001366 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001367 }
Feng Tange5f37052012-09-07 16:42:26 +08001368
1369 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1370 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1371 lang_dirent.d_name);
1372#ifdef NO_LIBPERL
1373 if (strstr(lang_path, "perl"))
1374 continue;
1375#endif
1376#ifdef NO_LIBPYTHON
1377 if (strstr(lang_path, "python"))
1378 continue;
1379#endif
1380
1381 lang_dir = opendir(lang_path);
1382 if (!lang_dir)
1383 continue;
1384
1385 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1386 /* Skip those real time scripts: xxxtop.p[yl] */
1387 if (strstr(script_dirent.d_name, "top."))
1388 continue;
1389 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1390 script_dirent.d_name);
1391 temp = strchr(script_dirent.d_name, '.');
1392 snprintf(scripts_array[i],
1393 (temp - script_dirent.d_name) + 1,
1394 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001395
1396 if (check_ev_match(lang_path,
1397 scripts_array[i], session))
1398 continue;
1399
Feng Tange5f37052012-09-07 16:42:26 +08001400 i++;
1401 }
Feng Tang49e639e2012-10-30 11:56:03 +08001402 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001403 }
1404
Feng Tang49e639e2012-10-30 11:56:03 +08001405 closedir(scripts_dir);
1406 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001407 return i;
1408}
1409
Tom Zanussi38752942009-12-15 02:53:39 -06001410static char *get_script_path(const char *script_root, const char *suffix)
1411{
1412 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1413 char scripts_path[MAXPATHLEN];
1414 char script_path[MAXPATHLEN];
1415 DIR *scripts_dir, *lang_dir;
1416 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001417 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001418
1419 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1420
1421 scripts_dir = opendir(scripts_path);
1422 if (!scripts_dir)
1423 return NULL;
1424
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001425 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001426 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1427 lang_dirent.d_name);
1428 lang_dir = opendir(lang_path);
1429 if (!lang_dir)
1430 continue;
1431
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001432 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001433 __script_root = get_script_root(&script_dirent, suffix);
1434 if (__script_root && !strcmp(script_root, __script_root)) {
1435 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001436 closedir(lang_dir);
1437 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001438 snprintf(script_path, MAXPATHLEN, "%s/%s",
1439 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001440 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001441 }
1442 free(__script_root);
1443 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001444 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001445 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001446 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001447
Robert Richter38efb532011-11-25 11:38:40 +01001448 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001449}
1450
Tom Zanussib5b87312010-11-10 08:16:51 -06001451static bool is_top_script(const char *script_path)
1452{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001453 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001454}
1455
1456static int has_required_arg(char *script_path)
1457{
1458 struct script_desc *desc;
1459 int n_args = 0;
1460 char *p;
1461
1462 desc = script_desc__new(NULL);
1463
1464 if (read_script_info(desc, script_path))
1465 goto out;
1466
1467 if (!desc->args)
1468 goto out;
1469
1470 for (p = desc->args; *p; p++)
1471 if (*p == '<')
1472 n_args++;
1473out:
1474 script_desc__delete(desc);
1475
1476 return n_args;
1477}
1478
David Ahernd54b1a92012-08-26 12:24:46 -06001479static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001480{
1481 char **__argv = malloc(sizeof(const char *) * argc);
1482
David Ahernd54b1a92012-08-26 12:24:46 -06001483 if (!__argv) {
1484 pr_err("malloc failed\n");
1485 return -1;
1486 }
1487
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001488 memcpy(__argv, argv, sizeof(const char *) * argc);
1489 argc = parse_options(argc, (const char **)__argv, record_options,
1490 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1491 free(__argv);
1492
David Ahernd54b1a92012-08-26 12:24:46 -06001493 system_wide = (argc == 0);
1494
1495 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001496}
1497
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001498int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001499{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001500 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01001501 bool header = false;
1502 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001503 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001504 char *rec_script_path = NULL;
1505 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001506 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -06001507 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001508 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001509 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001510 struct perf_script script = {
1511 .tool = {
1512 .sample = process_sample_event,
1513 .mmap = perf_event__process_mmap,
1514 .mmap2 = perf_event__process_mmap2,
1515 .comm = perf_event__process_comm,
1516 .exit = perf_event__process_exit,
1517 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001518 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001519 .tracing_data = perf_event__process_tracing_data,
1520 .build_id = perf_event__process_build_id,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001521 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001522 .ordering_requires_timestamps = true,
1523 },
1524 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001525 const struct option options[] = {
1526 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1527 "dump raw trace in ASCII"),
1528 OPT_INCR('v', "verbose", &verbose,
1529 "be more verbose (show symbol address, etc)"),
1530 OPT_BOOLEAN('L', "Latency", &latency_format,
1531 "show latency attributes (irqs/preemption disabled, etc)"),
1532 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1533 list_available_scripts),
1534 OPT_CALLBACK('s', "script", NULL, "name",
1535 "script file name (lang:script name, script name, or *)",
1536 parse_scriptname),
1537 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1538 "generate perf-script.xx script in specified language"),
1539 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1540 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1541 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01001542 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1543 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001544 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1545 "file", "vmlinux pathname"),
1546 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1547 "file", "kallsyms pathname"),
1548 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1549 "When printing symbols do not display call chain"),
1550 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1551 "Look for files with symbols relative to this directory"),
1552 OPT_CALLBACK('f', "fields", NULL, "str",
1553 "comma separated output fields prepend with 'type:'. "
1554 "Valid types: hw,sw,trace,raw. "
1555 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001556 "addr,symoff,period", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001557 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1558 "system-wide collection from all CPUs"),
1559 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1560 "only consider these symbols"),
1561 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1562 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1563 "only display events for these comms"),
1564 OPT_BOOLEAN('I', "show-info", &show_full_info,
1565 "display extended information from perf.data file"),
1566 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1567 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001568 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1569 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001570 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1571 "Show the mmap events"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001572 OPT_END()
1573 };
1574 const char * const script_usage[] = {
1575 "perf script [<options>]",
1576 "perf script [<options>] record <script> [<record-options>] <command>",
1577 "perf script [<options>] report <script> [script-args]",
1578 "perf script [<options>] <script> [<record-options>] <command>",
1579 "perf script [<options>] <top-script> [script-args]",
1580 NULL
1581 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001582 struct perf_data_file file = {
1583 .mode = PERF_DATA_MODE_READ,
1584 };
Tom Zanussi38752942009-12-15 02:53:39 -06001585
Tom Zanussib5b87312010-11-10 08:16:51 -06001586 setup_scripting();
1587
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001588 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001589 PARSE_OPT_STOP_AT_NON_OPTION);
1590
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001591 file.path = input_name;
1592
Tom Zanussib5b87312010-11-10 08:16:51 -06001593 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1594 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1595 if (!rec_script_path)
1596 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001597 }
1598
Tom Zanussib5b87312010-11-10 08:16:51 -06001599 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1600 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1601 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001602 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001603 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001604 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001605 return -1;
1606 }
Tom Zanussi38752942009-12-15 02:53:39 -06001607 }
1608
Ben Hutchings44e668c2010-10-10 16:10:03 +01001609 /* make sure PERF_EXEC_PATH is set for scripts */
1610 perf_set_argv_exec_path(perf_exec_path());
1611
Tom Zanussib5b87312010-11-10 08:16:51 -06001612 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001613 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001614 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001615 pid_t pid;
1616
Tom Zanussib5b87312010-11-10 08:16:51 -06001617 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1618 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1619
1620 if (!rec_script_path && !rep_script_path) {
1621 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001622 " script -l for available scripts.\n", argv[0]);
1623 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001624 }
1625
Tom Zanussib5b87312010-11-10 08:16:51 -06001626 if (is_top_script(argv[0])) {
1627 rep_args = argc - 1;
1628 } else {
1629 int rec_args;
1630
1631 rep_args = has_required_arg(rep_script_path);
1632 rec_args = (argc - 1) - rep_args;
1633 if (rec_args < 0) {
1634 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001635 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001636 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001637 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -06001638 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001639 }
1640
1641 if (pipe(live_pipe) < 0) {
1642 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001643 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001644 }
1645
1646 pid = fork();
1647 if (pid < 0) {
1648 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001649 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001650 }
1651
1652 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001653 j = 0;
1654
Tom Zanussia0cccc22010-04-01 23:59:25 -05001655 dup2(live_pipe[1], 1);
1656 close(live_pipe[0]);
1657
Robert Richter317df652011-11-25 15:05:25 +01001658 if (is_top_script(argv[0])) {
1659 system_wide = true;
1660 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001661 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1662 err = -1;
1663 goto out;
1664 }
Robert Richter317df652011-11-25 15:05:25 +01001665 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001666
1667 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001668 if (!__argv) {
1669 pr_err("malloc failed\n");
1670 err = -ENOMEM;
1671 goto out;
1672 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001673
Tom Zanussib5b87312010-11-10 08:16:51 -06001674 __argv[j++] = "/bin/sh";
1675 __argv[j++] = rec_script_path;
1676 if (system_wide)
1677 __argv[j++] = "-a";
1678 __argv[j++] = "-q";
1679 __argv[j++] = "-o";
1680 __argv[j++] = "-";
1681 for (i = rep_args + 1; i < argc; i++)
1682 __argv[j++] = argv[i];
1683 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001684
1685 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001686 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001687 exit(-1);
1688 }
1689
1690 dup2(live_pipe[0], 0);
1691 close(live_pipe[1]);
1692
Tom Zanussib5b87312010-11-10 08:16:51 -06001693 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001694 if (!__argv) {
1695 pr_err("malloc failed\n");
1696 err = -ENOMEM;
1697 goto out;
1698 }
1699
Tom Zanussib5b87312010-11-10 08:16:51 -06001700 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001701 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001702 __argv[j++] = rep_script_path;
1703 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001704 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001705 __argv[j++] = "-i";
1706 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001707 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001708
1709 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001710 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001711 exit(-1);
1712 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001713
Tom Zanussib5b87312010-11-10 08:16:51 -06001714 if (rec_script_path)
1715 script_path = rec_script_path;
1716 if (rep_script_path)
1717 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001718
Tom Zanussib5b87312010-11-10 08:16:51 -06001719 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001720 j = 0;
1721
Robert Richter317df652011-11-25 15:05:25 +01001722 if (!rec_script_path)
1723 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06001724 else if (!system_wide) {
1725 if (have_cmd(argc - 1, &argv[1]) != 0) {
1726 err = -1;
1727 goto out;
1728 }
1729 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001730
1731 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001732 if (!__argv) {
1733 pr_err("malloc failed\n");
1734 err = -ENOMEM;
1735 goto out;
1736 }
1737
Tom Zanussib5b87312010-11-10 08:16:51 -06001738 __argv[j++] = "/bin/sh";
1739 __argv[j++] = script_path;
1740 if (system_wide)
1741 __argv[j++] = "-a";
1742 for (i = 2; i < argc; i++)
1743 __argv[j++] = argv[i];
1744 __argv[j++] = NULL;
1745
1746 execvp("/bin/sh", (char **)__argv);
1747 free(__argv);
1748 exit(-1);
1749 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001750
Tom Zanussicf4fee52010-03-03 01:04:33 -06001751 if (!script_name)
1752 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001753
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001754 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001755 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001756 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001757
Jiri Olsae90debd2013-12-09 11:02:50 +01001758 if (header || header_only) {
1759 perf_session__fprintf_info(session, stdout, show_full_info);
1760 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001761 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01001762 }
1763
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001764 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09001765 goto out_delete;
1766
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001767 script.session = session;
1768
Anton Blanchard5d67be92011-07-04 21:57:50 +10001769 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001770 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
1771 if (err < 0)
1772 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001773 }
1774
David Ahern1424dc92011-03-09 22:23:28 -07001775 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001776 symbol_conf.use_callchain = true;
1777 else
1778 symbol_conf.use_callchain = false;
1779
Tom Zanussi956ffd02009-11-25 01:15:46 -06001780 if (generate_script_lang) {
1781 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001782 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001783
David Ahern2c9e45f72011-03-17 10:03:21 -06001784 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001785 fprintf(stderr,
1786 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001787 err = -EINVAL;
1788 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07001789 }
1790
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001791 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06001792 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001793 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001794 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001795 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001796 }
1797
1798 err = fstat(input, &perf_stat);
1799 if (err < 0) {
1800 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001801 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001802 }
1803
1804 if (!perf_stat.st_size) {
1805 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001806 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001807 }
1808
1809 scripting_ops = script_spec__lookup(generate_script_lang);
1810 if (!scripting_ops) {
1811 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001812 err = -ENOENT;
1813 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001814 }
1815
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01001816 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001817 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001818 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001819 }
1820
1821 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001822 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001823 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001824 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001825 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001826 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001827 }
1828
David Ahern9cbdb702011-04-06 21:54:20 -06001829
1830 err = perf_session__check_output_opt(session);
1831 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001832 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06001833
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001834 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001835
Adrian Hunterd445dd22014-08-15 22:08:37 +03001836 flush_scripting();
1837
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001838out_delete:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001839 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001840
1841 if (script_started)
1842 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001843out:
1844 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001845}