blob: 02a65d15c594785aeb9007e56b12edc4331f1ee7 [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#include "builtin.h"
2
Andrea Gelminib7eead82010-08-05 15:51:38 +02003#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02004#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02005#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06006#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02007#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06008#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +02009#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020010#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020011#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020012#include "util/symbol.h"
13#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010014#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020015#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070016#include "util/evlist.h"
17#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080018#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030020#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010021#include "util/cpumap.h"
22#include "util/thread_map.h"
23#include "util/stat.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100024#include <linux/bitmap.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010025#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010026#include "util/mem-events.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020027
Tom Zanussi956ffd02009-11-25 01:15:46 -060028static char const *script_name;
29static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020030static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020031static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020032static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070033static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090034static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010035static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030036static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030037static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100038static const char *cpu_list;
39static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010040static struct perf_stat_config stat_config;
Tom Zanussi956ffd02009-11-25 01:15:46 -060041
Adrian Hunter44cbe722015-09-25 16:15:50 +030042unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030043
David Ahern745f43e2011-03-09 22:23:26 -070044enum perf_output_field {
45 PERF_OUTPUT_COMM = 1U << 0,
46 PERF_OUTPUT_TID = 1U << 1,
47 PERF_OUTPUT_PID = 1U << 2,
48 PERF_OUTPUT_TIME = 1U << 3,
49 PERF_OUTPUT_CPU = 1U << 4,
50 PERF_OUTPUT_EVNAME = 1U << 5,
51 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060052 PERF_OUTPUT_IP = 1U << 7,
53 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060054 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060055 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090056 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020057 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020058 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020059 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020060 PERF_OUTPUT_BRSTACK = 1U << 15,
61 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010062 PERF_OUTPUT_DATA_SRC = 1U << 17,
63 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000064 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
David Ahern745f43e2011-03-09 22:23:26 -070065};
66
67struct output_option {
68 const char *str;
69 enum perf_output_field field;
70} all_output_options[] = {
71 {.str = "comm", .field = PERF_OUTPUT_COMM},
72 {.str = "tid", .field = PERF_OUTPUT_TID},
73 {.str = "pid", .field = PERF_OUTPUT_PID},
74 {.str = "time", .field = PERF_OUTPUT_TIME},
75 {.str = "cpu", .field = PERF_OUTPUT_CPU},
76 {.str = "event", .field = PERF_OUTPUT_EVNAME},
77 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060078 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070079 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060080 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060081 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090082 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020083 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +020084 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +020085 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +020086 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
87 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +010088 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
89 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +000090 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
David Ahern745f43e2011-03-09 22:23:26 -070091};
92
93/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -060094static struct {
95 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060096 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -040097 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -060098 u64 fields;
99 u64 invalid_fields;
100} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700101
David Ahern2c9e45f72011-03-17 10:03:21 -0600102 [PERF_TYPE_HARDWARE] = {
103 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700104
David Ahern2c9e45f72011-03-17 10:03:21 -0600105 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
106 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600107 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200108 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
109 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600110
Wang Nan30372f02016-02-24 11:20:45 +0000111 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600112 },
113
114 [PERF_TYPE_SOFTWARE] = {
115 .user_set = false,
116
117 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
118 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600119 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200120 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000121 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600122
123 .invalid_fields = PERF_OUTPUT_TRACE,
124 },
125
126 [PERF_TYPE_TRACEPOINT] = {
127 .user_set = false,
128
129 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
130 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000131 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600132 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700133
134 [PERF_TYPE_RAW] = {
135 .user_set = false,
136
137 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
138 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600139 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200140 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100141 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
142 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700143
Wang Nan30372f02016-02-24 11:20:45 +0000144 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700145 },
Wang Nan27cfef02015-12-08 02:25:43 +0000146
147 [PERF_TYPE_BREAKPOINT] = {
148 .user_set = false,
149
150 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
151 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
152 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
153 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
154 PERF_OUTPUT_PERIOD,
155
Wang Nan30372f02016-02-24 11:20:45 +0000156 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000157 },
David Ahern1424dc92011-03-09 22:23:28 -0700158};
David Ahern745f43e2011-03-09 22:23:26 -0700159
David Ahern2c9e45f72011-03-17 10:03:21 -0600160static bool output_set_by_user(void)
161{
162 int j;
163 for (j = 0; j < PERF_TYPE_MAX; ++j) {
164 if (output[j].user_set)
165 return true;
166 }
167 return false;
168}
David Ahern745f43e2011-03-09 22:23:26 -0700169
David Ahern9cbdb702011-04-06 21:54:20 -0600170static const char *output_field2str(enum perf_output_field field)
171{
172 int i, imax = ARRAY_SIZE(all_output_options);
173 const char *str = "";
174
175 for (i = 0; i < imax; ++i) {
176 if (all_output_options[i].field == field) {
177 str = all_output_options[i].str;
178 break;
179 }
180 }
181 return str;
182}
183
David Ahern2c9e45f72011-03-17 10:03:21 -0600184#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700185
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300186static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
187 u64 sample_type, const char *sample_msg,
188 enum perf_output_field field,
189 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700190{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300191 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600192 int type = attr->type;
193 const char *evname;
194
195 if (attr->sample_type & sample_type)
196 return 0;
197
198 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300199 if (allow_user_set)
200 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300201 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600202 pr_err("Samples for '%s' event do not have %s attribute set. "
203 "Cannot print '%s' field.\n",
204 evname, sample_msg, output_field2str(field));
205 return -1;
206 }
207
208 /* user did not ask for it explicitly so remove from the default list */
209 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300210 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600211 pr_debug("Samples for '%s' event do not have %s attribute set. "
212 "Skipping '%s' field.\n",
213 evname, sample_msg, output_field2str(field));
214
215 return 0;
216}
217
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300218static int perf_evsel__check_stype(struct perf_evsel *evsel,
219 u64 sample_type, const char *sample_msg,
220 enum perf_output_field field)
221{
222 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
223 false);
224}
225
David Ahern9cbdb702011-04-06 21:54:20 -0600226static int perf_evsel__check_attr(struct perf_evsel *evsel,
227 struct perf_session *session)
228{
229 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300230 bool allow_user_set;
231
Jiri Olsae099eba2016-01-05 22:09:09 +0100232 if (perf_header__has_feat(&session->header, HEADER_STAT))
233 return 0;
234
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300235 allow_user_set = perf_header__has_feat(&session->header,
236 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600237
David Ahern1424dc92011-03-09 22:23:28 -0700238 if (PRINT_FIELD(TRACE) &&
239 !perf_session__has_traces(session, "record -R"))
240 return -EINVAL;
241
David Ahern787bef12011-05-27 14:28:43 -0600242 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300243 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
244 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700245 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700246 }
David Ahern7cec0922011-05-30 13:08:23 -0600247
248 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300249 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
250 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600251 return -EINVAL;
252
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100253 if (PRINT_FIELD(DATA_SRC) &&
254 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
255 PERF_OUTPUT_DATA_SRC))
256 return -EINVAL;
257
258 if (PRINT_FIELD(WEIGHT) &&
259 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
260 PERF_OUTPUT_WEIGHT))
261 return -EINVAL;
262
David Ahern7cec0922011-05-30 13:08:23 -0600263 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
264 pr_err("Display of symbols requested but neither sample IP nor "
265 "sample address\nis selected. Hence, no addresses to convert "
266 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600267 return -EINVAL;
268 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900269 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
270 pr_err("Display of offsets requested but symbol is not"
271 "selected.\n");
272 return -EINVAL;
273 }
David Ahern7cec0922011-05-30 13:08:23 -0600274 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
275 pr_err("Display of DSO requested but neither sample IP nor "
276 "sample address\nis selected. Hence, no addresses to convert "
277 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600278 return -EINVAL;
279 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200280 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
281 pr_err("Display of source line number requested but sample IP is not\n"
282 "selected. Hence, no address to lookup the source line number.\n");
283 return -EINVAL;
284 }
David Ahern1424dc92011-03-09 22:23:28 -0700285
286 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300287 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
288 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700289 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700290
291 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300292 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
293 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700294 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700295
296 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300297 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
298 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700299 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600300
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200301 if (PRINT_FIELD(PERIOD) &&
302 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
303 PERF_OUTPUT_PERIOD))
304 return -EINVAL;
305
Stephane Eranianfc36f942015-08-31 18:41:10 +0200306 if (PRINT_FIELD(IREGS) &&
307 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
308 PERF_OUTPUT_IREGS))
309 return -EINVAL;
310
David Ahern9cbdb702011-04-06 21:54:20 -0600311 return 0;
312}
313
Adrian Hunter7ea95722013-11-01 15:51:30 +0200314static void set_print_ip_opts(struct perf_event_attr *attr)
315{
316 unsigned int type = attr->type;
317
318 output[type].print_ip_opts = 0;
319 if (PRINT_FIELD(IP))
320 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
321
322 if (PRINT_FIELD(SYM))
323 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
324
325 if (PRINT_FIELD(DSO))
326 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
327
328 if (PRINT_FIELD(SYMOFFSET))
329 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200330
331 if (PRINT_FIELD(SRCLINE))
332 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200333}
334
David Ahern9cbdb702011-04-06 21:54:20 -0600335/*
336 * verify all user requested events exist and the samples
337 * have the expected data
338 */
339static int perf_session__check_output_opt(struct perf_session *session)
340{
341 int j;
342 struct perf_evsel *evsel;
343
344 for (j = 0; j < PERF_TYPE_MAX; ++j) {
345 evsel = perf_session__find_first_evtype(session, j);
346
347 /*
348 * even if fields is set to 0 (ie., show nothing) event must
349 * exist if user explicitly includes it on the command line
350 */
351 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
352 pr_err("%s events do not exist. "
353 "Remove corresponding -f option to proceed.\n",
354 event_type(j));
355 return -1;
356 }
357
358 if (evsel && output[j].fields &&
359 perf_evsel__check_attr(evsel, session))
360 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400361
362 if (evsel == NULL)
363 continue;
364
Adrian Hunter7ea95722013-11-01 15:51:30 +0200365 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700366 }
367
Adrian Hunter98526ee2014-07-31 09:00:59 +0300368 if (!no_callchain) {
369 bool use_callchain = false;
370
371 evlist__for_each(session->evlist, evsel) {
372 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
373 use_callchain = true;
374 break;
375 }
376 }
377 if (!use_callchain)
378 symbol_conf.use_callchain = false;
379 }
380
David Ahern80b8b492013-11-19 21:07:37 -0700381 /*
382 * set default for tracepoints to print symbols only
383 * if callchains are present
384 */
385 if (symbol_conf.use_callchain &&
386 !output[PERF_TYPE_TRACEPOINT].user_set) {
387 struct perf_event_attr *attr;
388
389 j = PERF_TYPE_TRACEPOINT;
390 evsel = perf_session__find_first_evtype(session, j);
391 if (evsel == NULL)
392 goto out;
393
394 attr = &evsel->attr;
395
396 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
397 output[j].fields |= PERF_OUTPUT_IP;
398 output[j].fields |= PERF_OUTPUT_SYM;
399 output[j].fields |= PERF_OUTPUT_DSO;
400 set_print_ip_opts(attr);
401 }
402 }
403
404out:
David Ahern1424dc92011-03-09 22:23:28 -0700405 return 0;
406}
David Ahern745f43e2011-03-09 22:23:26 -0700407
Stephane Eranianfc36f942015-08-31 18:41:10 +0200408static void print_sample_iregs(union perf_event *event __maybe_unused,
409 struct perf_sample *sample,
410 struct thread *thread __maybe_unused,
411 struct perf_event_attr *attr)
412{
413 struct regs_dump *regs = &sample->intr_regs;
414 uint64_t mask = attr->sample_regs_intr;
415 unsigned i = 0, r;
416
417 if (!regs)
418 return;
419
420 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
421 u64 val = regs->regs[i++];
422 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
423 }
424}
425
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300426static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700427 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300428 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700429{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300430 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700431 unsigned long secs;
432 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700433 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700434
David Ahern745f43e2011-03-09 22:23:26 -0700435 if (PRINT_FIELD(COMM)) {
436 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200437 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600438 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200439 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700440 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200441 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700442 }
David Ahernc70c94b2011-03-09 22:23:25 -0700443
David Ahern745f43e2011-03-09 22:23:26 -0700444 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
445 printf("%5d/%-5d ", sample->pid, sample->tid);
446 else if (PRINT_FIELD(PID))
447 printf("%5d ", sample->pid);
448 else if (PRINT_FIELD(TID))
449 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700450
David Ahern745f43e2011-03-09 22:23:26 -0700451 if (PRINT_FIELD(CPU)) {
452 if (latency_format)
453 printf("%3d ", sample->cpu);
454 else
455 printf("[%03d] ", sample->cpu);
456 }
David Ahernc70c94b2011-03-09 22:23:25 -0700457
David Ahern745f43e2011-03-09 22:23:26 -0700458 if (PRINT_FIELD(TIME)) {
459 nsecs = sample->time;
460 secs = nsecs / NSECS_PER_SEC;
461 nsecs -= secs * NSECS_PER_SEC;
462 usecs = nsecs / NSECS_PER_USEC;
Adrian Hunter83e19862015-09-25 16:15:36 +0300463 if (nanosecs)
464 printf("%5lu.%09llu: ", secs, nsecs);
465 else
466 printf("%5lu.%06lu: ", secs, usecs);
David Ahern745f43e2011-03-09 22:23:26 -0700467 }
David Ahernc70c94b2011-03-09 22:23:25 -0700468}
469
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200470static inline char
471mispred_str(struct branch_entry *br)
472{
473 if (!(br->flags.mispred || br->flags.predicted))
474 return '-';
475
476 return br->flags.predicted ? 'P' : 'M';
477}
478
479static void print_sample_brstack(union perf_event *event __maybe_unused,
480 struct perf_sample *sample,
481 struct thread *thread __maybe_unused,
482 struct perf_event_attr *attr __maybe_unused)
483{
484 struct branch_stack *br = sample->branch_stack;
485 u64 i;
486
487 if (!(br && br->nr))
488 return;
489
490 for (i = 0; i < br->nr; i++) {
491 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
492 br->entries[i].from,
493 br->entries[i].to,
494 mispred_str( br->entries + i),
495 br->entries[i].flags.in_tx? 'X' : '-',
496 br->entries[i].flags.abort? 'A' : '-',
497 br->entries[i].flags.cycles);
498 }
499}
500
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300501static void print_sample_brstacksym(union perf_event *event __maybe_unused,
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200502 struct perf_sample *sample,
503 struct thread *thread __maybe_unused,
504 struct perf_event_attr *attr __maybe_unused)
505{
506 struct branch_stack *br = sample->branch_stack;
507 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200508 u64 i, from, to;
509
510 if (!(br && br->nr))
511 return;
512
513 for (i = 0; i < br->nr; i++) {
514
515 memset(&alf, 0, sizeof(alf));
516 memset(&alt, 0, sizeof(alt));
517 from = br->entries[i].from;
518 to = br->entries[i].to;
519
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300520 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200521 if (alf.map)
522 alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
523
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300524 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200525 if (alt.map)
526 alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
527
528 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
529 putchar('/');
530 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
531 printf("/%c/%c/%c/%d ",
532 mispred_str( br->entries + i),
533 br->entries[i].flags.in_tx? 'X' : '-',
534 br->entries[i].flags.abort? 'A' : '-',
535 br->entries[i].flags.cycles);
536 }
537}
538
539
David Ahern7cec0922011-05-30 13:08:23 -0600540static void print_sample_addr(union perf_event *event,
541 struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600542 struct thread *thread,
543 struct perf_event_attr *attr)
544{
545 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600546
547 printf("%16" PRIx64, sample->addr);
548
549 if (!sample_addr_correlates_sym(attr))
550 return;
551
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300552 perf_event__preprocess_sample_addr(event, sample, thread, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600553
554 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900555 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900556 if (PRINT_FIELD(SYMOFFSET))
557 symbol__fprintf_symname_offs(al.sym, &al, stdout);
558 else
559 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600560 }
561
562 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900563 printf(" (");
564 map__fprintf_dsoname(al.map, stdout);
565 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600566 }
567}
568
Akihiro Nagai95582592012-01-30 13:43:09 +0900569static void print_sample_bts(union perf_event *event,
570 struct perf_sample *sample,
571 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200572 struct thread *thread,
573 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900574{
575 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300576 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900577
578 /* print branch_from information */
579 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300580 unsigned int print_opts = output[attr->type].print_ip_opts;
581
582 if (symbol_conf.use_callchain && sample->callchain) {
Akihiro Nagai95582592012-01-30 13:43:09 +0900583 printf("\n");
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300584 } else {
585 printf(" ");
586 if (print_opts & PRINT_IP_OPT_SRCLINE) {
587 print_srcline_last = true;
588 print_opts &= ~PRINT_IP_OPT_SRCLINE;
589 }
590 }
591 perf_evsel__print_ip(evsel, sample, al, print_opts,
Adrian Hunter03cd1fe2015-09-25 16:15:49 +0300592 scripting_max_stack);
Akihiro Nagai95582592012-01-30 13:43:09 +0900593 }
594
Akihiro Nagai95582592012-01-30 13:43:09 +0900595 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300596 if (PRINT_FIELD(ADDR) ||
597 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300598 !output[attr->type].user_set)) {
599 printf(" => ");
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300600 print_sample_addr(event, sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300601 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900602
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300603 if (print_srcline_last)
604 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
605
Akihiro Nagai95582592012-01-30 13:43:09 +0900606 printf("\n");
607}
608
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300609static void print_sample_flags(u32 flags)
610{
611 const char *chars = PERF_IP_FLAG_CHARS;
612 const int n = strlen(PERF_IP_FLAG_CHARS);
613 char str[33];
614 int i, pos = 0;
615
616 for (i = 0; i < n; i++, flags >>= 1) {
617 if (flags & 1)
618 str[pos++] = chars[i];
619 }
620 for (; i < 32; i++, flags >>= 1) {
621 if (flags & 1)
622 str[pos++] = '?';
623 }
624 str[pos] = 0;
625 printf(" %-4s ", str);
626}
627
Wang Nan30372f02016-02-24 11:20:45 +0000628struct printer_data {
629 int line_no;
630 bool hit_nul;
631 bool is_printable;
632};
633
634static void
635print_sample_bpf_output_printer(enum binary_printer_ops op,
636 unsigned int val,
637 void *extra)
638{
639 unsigned char ch = (unsigned char)val;
640 struct printer_data *printer_data = extra;
641
642 switch (op) {
643 case BINARY_PRINT_DATA_BEGIN:
644 printf("\n");
645 break;
646 case BINARY_PRINT_LINE_BEGIN:
647 printf("%17s", !printer_data->line_no ? "BPF output:" :
648 " ");
649 break;
650 case BINARY_PRINT_ADDR:
651 printf(" %04x:", val);
652 break;
653 case BINARY_PRINT_NUM_DATA:
654 printf(" %02x", val);
655 break;
656 case BINARY_PRINT_NUM_PAD:
657 printf(" ");
658 break;
659 case BINARY_PRINT_SEP:
660 printf(" ");
661 break;
662 case BINARY_PRINT_CHAR_DATA:
663 if (printer_data->hit_nul && ch)
664 printer_data->is_printable = false;
665
666 if (!isprint(ch)) {
667 printf("%c", '.');
668
669 if (!printer_data->is_printable)
670 break;
671
672 if (ch == '\0')
673 printer_data->hit_nul = true;
674 else
675 printer_data->is_printable = false;
676 } else {
677 printf("%c", ch);
678 }
679 break;
680 case BINARY_PRINT_CHAR_PAD:
681 printf(" ");
682 break;
683 case BINARY_PRINT_LINE_END:
684 printf("\n");
685 printer_data->line_no++;
686 break;
687 case BINARY_PRINT_DATA_END:
688 default:
689 break;
690 }
691}
692
693static void print_sample_bpf_output(struct perf_sample *sample)
694{
695 unsigned int nr_bytes = sample->raw_size;
696 struct printer_data printer_data = {0, false, true};
697
698 print_binary(sample->raw_data, nr_bytes, 8,
699 print_sample_bpf_output_printer, &printer_data);
700
701 if (printer_data.is_printable && printer_data.hit_nul)
702 printf("%17s \"%s\"\n", "BPF string:",
703 (char *)(sample->raw_data));
704}
705
Jiri Olsa809e9422015-11-26 18:55:21 +0100706struct perf_script {
707 struct perf_tool tool;
708 struct perf_session *session;
709 bool show_task_events;
710 bool show_mmap_events;
711 bool show_switch_events;
Jiri Olsacfc88742016-01-05 22:09:06 +0100712 bool allocated;
713 struct cpu_map *cpus;
714 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100715 int name_width;
Jiri Olsa809e9422015-11-26 18:55:21 +0100716};
717
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100718static int perf_evlist__max_name_len(struct perf_evlist *evlist)
719{
720 struct perf_evsel *evsel;
721 int max = 0;
722
723 evlist__for_each(evlist, evsel) {
724 int len = strlen(perf_evsel__name(evsel));
725
726 max = MAX(len, max);
727 }
728
729 return max;
730}
731
Jiri Olsac19ac912016-02-24 09:46:54 +0100732static size_t data_src__printf(u64 data_src)
733{
734 struct mem_info mi = { .data_src.val = data_src };
735 char decode[100];
736 char out[100];
737 static int maxlen;
738 int len;
739
740 perf_script__meminfo_scnprintf(decode, 100, &mi);
741
742 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
743 if (maxlen < len)
744 maxlen = len;
745
746 return printf("%-*s", maxlen, out);
747}
748
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100749static void process_event(struct perf_script *script, union perf_event *event,
Jiri Olsa809e9422015-11-26 18:55:21 +0100750 struct perf_sample *sample, struct perf_evsel *evsel,
751 struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700752{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300753 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300754 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700755
David Ahern2c9e45f72011-03-17 10:03:21 -0600756 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700757 return;
758
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300759 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700760
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200761 if (PRINT_FIELD(PERIOD))
762 printf("%10" PRIu64 " ", sample->period);
763
Namhyung Kime944d3d2013-11-18 14:34:52 +0900764 if (PRINT_FIELD(EVNAME)) {
765 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100766
767 if (!script->name_width)
768 script->name_width = perf_evlist__max_name_len(script->session->evlist);
769
770 printf("%*s: ", script->name_width,
771 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +0900772 }
773
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300774 if (print_flags)
775 print_sample_flags(sample->flags);
776
Akihiro Nagai95582592012-01-30 13:43:09 +0900777 if (is_bts_event(attr)) {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300778 print_sample_bts(event, sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900779 return;
780 }
781
David Ahern745f43e2011-03-09 22:23:26 -0700782 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300783 event_format__print(evsel->tp_format, sample->cpu,
784 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600785 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300786 print_sample_addr(event, sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600787
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100788 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +0100789 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100790
791 if (PRINT_FIELD(WEIGHT))
792 printf("%16" PRIu64, sample->weight);
793
David Ahern787bef12011-05-27 14:28:43 -0600794 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700795 if (!symbol_conf.use_callchain)
796 printf(" ");
797 else
798 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400799
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300800 perf_evsel__print_ip(evsel, sample, al,
David Ahern307cbb92013-08-07 22:50:53 -0400801 output[attr->type].print_ip_opts,
Adrian Hunter03cd1fe2015-09-25 16:15:49 +0300802 scripting_max_stack);
David Ahernc0230b22011-03-09 22:23:27 -0700803 }
804
Stephane Eranianfc36f942015-08-31 18:41:10 +0200805 if (PRINT_FIELD(IREGS))
806 print_sample_iregs(event, sample, thread, attr);
807
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200808 if (PRINT_FIELD(BRSTACK))
809 print_sample_brstack(event, sample, thread, attr);
810 else if (PRINT_FIELD(BRSTACKSYM))
811 print_sample_brstacksym(event, sample, thread, attr);
812
Wang Nan30372f02016-02-24 11:20:45 +0000813 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
814 print_sample_bpf_output(sample);
815
David Ahernc70c94b2011-03-09 22:23:25 -0700816 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700817}
818
Tom Zanussi956ffd02009-11-25 01:15:46 -0600819static struct scripting_ops *scripting_ops;
820
Jiri Olsa36e33c52016-01-06 11:49:56 +0100821static void __process_stat(struct perf_evsel *counter, u64 tstamp)
822{
823 int nthreads = thread_map__nr(counter->threads);
824 int ncpus = perf_evsel__nr_cpus(counter);
825 int cpu, thread;
826 static int header_printed;
827
828 if (counter->system_wide)
829 nthreads = 1;
830
831 if (!header_printed) {
832 printf("%3s %8s %15s %15s %15s %15s %s\n",
833 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
834 header_printed = 1;
835 }
836
837 for (thread = 0; thread < nthreads; thread++) {
838 for (cpu = 0; cpu < ncpus; cpu++) {
839 struct perf_counts_values *counts;
840
841 counts = perf_counts(counter->counts, cpu, thread);
842
843 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
844 counter->cpus->map[cpu],
845 thread_map__pid(counter->threads, thread),
846 counts->val,
847 counts->ena,
848 counts->run,
849 tstamp,
850 perf_evsel__name(counter));
851 }
852 }
853}
854
Jiri Olsae099eba2016-01-05 22:09:09 +0100855static void process_stat(struct perf_evsel *counter, u64 tstamp)
856{
857 if (scripting_ops && scripting_ops->process_stat)
858 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +0100859 else
860 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +0100861}
862
863static void process_stat_interval(u64 tstamp)
864{
865 if (scripting_ops && scripting_ops->process_stat_interval)
866 scripting_ops->process_stat_interval(tstamp);
867}
868
Tom Zanussi956ffd02009-11-25 01:15:46 -0600869static void setup_scripting(void)
870{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600871 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600872 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -0600873}
874
Adrian Hunterd445dd22014-08-15 22:08:37 +0300875static int flush_scripting(void)
876{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100877 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +0300878}
879
Tom Zanussi956ffd02009-11-25 01:15:46 -0600880static int cleanup_scripting(void)
881{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100882 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500883
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100884 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600885}
886
Jiri Olsa809e9422015-11-26 18:55:21 +0100887static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200888 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200889 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300890 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200891 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200892{
Jiri Olsa809e9422015-11-26 18:55:21 +0100893 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -0700894 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200895
David Ahern1424dc92011-03-09 22:23:28 -0700896 if (debug_mode) {
897 if (sample->time < last_timestamp) {
898 pr_err("Samples misordered, previous: %" PRIu64
899 " this: %" PRIu64 "\n", last_timestamp,
900 sample->time);
901 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200902 }
David Ahern1424dc92011-03-09 22:23:28 -0700903 last_timestamp = sample->time;
904 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200905 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000906
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300907 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700908 pr_err("problem processing %d event, skipping it.\n",
909 event->header.type);
910 return -1;
911 }
912
913 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300914 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -0700915
Anton Blanchard5d67be92011-07-04 21:57:50 +1000916 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300917 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000918
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100919 if (scripting_ops)
920 scripting_ops->process_event(event, sample, evsel, &al);
921 else
Jiri Olsa809e9422015-11-26 18:55:21 +0100922 process_event(scr, event, sample, evsel, &al);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100923
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300924out_put:
925 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200926 return 0;
927}
928
Adrian Hunter7ea95722013-11-01 15:51:30 +0200929static int process_attr(struct perf_tool *tool, union perf_event *event,
930 struct perf_evlist **pevlist)
931{
932 struct perf_script *scr = container_of(tool, struct perf_script, tool);
933 struct perf_evlist *evlist;
934 struct perf_evsel *evsel, *pos;
935 int err;
936
937 err = perf_event__process_attr(tool, event, pevlist);
938 if (err)
939 return err;
940
941 evlist = *pevlist;
942 evsel = perf_evlist__last(*pevlist);
943
944 if (evsel->attr.type >= PERF_TYPE_MAX)
945 return 0;
946
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300947 evlist__for_each(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +0200948 if (pos->attr.type == evsel->attr.type && pos != evsel)
949 return 0;
950 }
951
952 set_print_ip_opts(&evsel->attr);
953
Jiri Olsad2b5a312015-10-16 12:41:25 +0200954 if (evsel->attr.sample_type)
955 err = perf_evsel__check_attr(evsel, scr->session);
956
957 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200958}
959
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900960static int process_comm_event(struct perf_tool *tool,
961 union perf_event *event,
962 struct perf_sample *sample,
963 struct machine *machine)
964{
965 struct thread *thread;
966 struct perf_script *script = container_of(tool, struct perf_script, tool);
967 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300968 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900969 int ret = -1;
970
971 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
972 if (thread == NULL) {
973 pr_debug("problem processing COMM event, skipping it.\n");
974 return -1;
975 }
976
977 if (perf_event__process_comm(tool, event, sample, machine) < 0)
978 goto out;
979
980 if (!evsel->attr.sample_id_all) {
981 sample->cpu = 0;
982 sample->time = 0;
983 sample->tid = event->comm.tid;
984 sample->pid = event->comm.pid;
985 }
986 print_sample_start(sample, thread, evsel);
987 perf_event__fprintf(event, stdout);
988 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900989out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300990 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900991 return ret;
992}
993
994static int process_fork_event(struct perf_tool *tool,
995 union perf_event *event,
996 struct perf_sample *sample,
997 struct machine *machine)
998{
999 struct thread *thread;
1000 struct perf_script *script = container_of(tool, struct perf_script, tool);
1001 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001002 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001003
1004 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1005 return -1;
1006
1007 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1008 if (thread == NULL) {
1009 pr_debug("problem processing FORK event, skipping it.\n");
1010 return -1;
1011 }
1012
1013 if (!evsel->attr.sample_id_all) {
1014 sample->cpu = 0;
1015 sample->time = event->fork.time;
1016 sample->tid = event->fork.tid;
1017 sample->pid = event->fork.pid;
1018 }
1019 print_sample_start(sample, thread, evsel);
1020 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001021 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001022
1023 return 0;
1024}
1025static int process_exit_event(struct perf_tool *tool,
1026 union perf_event *event,
1027 struct perf_sample *sample,
1028 struct machine *machine)
1029{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001030 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001031 struct thread *thread;
1032 struct perf_script *script = container_of(tool, struct perf_script, tool);
1033 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001034 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001035
1036 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1037 if (thread == NULL) {
1038 pr_debug("problem processing EXIT event, skipping it.\n");
1039 return -1;
1040 }
1041
1042 if (!evsel->attr.sample_id_all) {
1043 sample->cpu = 0;
1044 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001045 sample->tid = event->fork.tid;
1046 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001047 }
1048 print_sample_start(sample, thread, evsel);
1049 perf_event__fprintf(event, stdout);
1050
1051 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001052 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001053
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001054 thread__put(thread);
1055 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001056}
1057
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001058static int process_mmap_event(struct perf_tool *tool,
1059 union perf_event *event,
1060 struct perf_sample *sample,
1061 struct machine *machine)
1062{
1063 struct thread *thread;
1064 struct perf_script *script = container_of(tool, struct perf_script, tool);
1065 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001066 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001067
1068 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1069 return -1;
1070
1071 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1072 if (thread == NULL) {
1073 pr_debug("problem processing MMAP event, skipping it.\n");
1074 return -1;
1075 }
1076
1077 if (!evsel->attr.sample_id_all) {
1078 sample->cpu = 0;
1079 sample->time = 0;
1080 sample->tid = event->mmap.tid;
1081 sample->pid = event->mmap.pid;
1082 }
1083 print_sample_start(sample, thread, evsel);
1084 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001085 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001086 return 0;
1087}
1088
1089static int process_mmap2_event(struct perf_tool *tool,
1090 union perf_event *event,
1091 struct perf_sample *sample,
1092 struct machine *machine)
1093{
1094 struct thread *thread;
1095 struct perf_script *script = container_of(tool, struct perf_script, tool);
1096 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001097 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001098
1099 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1100 return -1;
1101
1102 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1103 if (thread == NULL) {
1104 pr_debug("problem processing MMAP2 event, skipping it.\n");
1105 return -1;
1106 }
1107
1108 if (!evsel->attr.sample_id_all) {
1109 sample->cpu = 0;
1110 sample->time = 0;
1111 sample->tid = event->mmap2.tid;
1112 sample->pid = event->mmap2.pid;
1113 }
1114 print_sample_start(sample, thread, evsel);
1115 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001116 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001117 return 0;
1118}
1119
Adrian Hunter7c148982015-07-21 12:44:06 +03001120static int process_switch_event(struct perf_tool *tool,
1121 union perf_event *event,
1122 struct perf_sample *sample,
1123 struct machine *machine)
1124{
1125 struct thread *thread;
1126 struct perf_script *script = container_of(tool, struct perf_script, tool);
1127 struct perf_session *session = script->session;
1128 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1129
1130 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1131 return -1;
1132
1133 thread = machine__findnew_thread(machine, sample->pid,
1134 sample->tid);
1135 if (thread == NULL) {
1136 pr_debug("problem processing SWITCH event, skipping it.\n");
1137 return -1;
1138 }
1139
1140 print_sample_start(sample, thread, evsel);
1141 perf_event__fprintf(event, stdout);
1142 thread__put(thread);
1143 return 0;
1144}
1145
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001146static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001147{
1148 session_done = 1;
1149}
1150
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001151static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001152{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001153 int ret;
1154
Tom Zanussic239da32010-04-01 23:59:18 -05001155 signal(SIGINT, sig_handler);
1156
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001157 /* override event processing functions */
1158 if (script->show_task_events) {
1159 script->tool.comm = process_comm_event;
1160 script->tool.fork = process_fork_event;
1161 script->tool.exit = process_exit_event;
1162 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001163 if (script->show_mmap_events) {
1164 script->tool.mmap = process_mmap_event;
1165 script->tool.mmap2 = process_mmap2_event;
1166 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001167 if (script->show_switch_events)
1168 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001169
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001170 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001171
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001172 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001173 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001174
1175 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001176}
1177
Tom Zanussi956ffd02009-11-25 01:15:46 -06001178struct script_spec {
1179 struct list_head node;
1180 struct scripting_ops *ops;
1181 char spec[0];
1182};
1183
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001184static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001185
1186static struct script_spec *script_spec__new(const char *spec,
1187 struct scripting_ops *ops)
1188{
1189 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1190
1191 if (s != NULL) {
1192 strcpy(s->spec, spec);
1193 s->ops = ops;
1194 }
1195
1196 return s;
1197}
1198
Tom Zanussi956ffd02009-11-25 01:15:46 -06001199static void script_spec__add(struct script_spec *s)
1200{
1201 list_add_tail(&s->node, &script_specs);
1202}
1203
1204static struct script_spec *script_spec__find(const char *spec)
1205{
1206 struct script_spec *s;
1207
1208 list_for_each_entry(s, &script_specs, node)
1209 if (strcasecmp(s->spec, spec) == 0)
1210 return s;
1211 return NULL;
1212}
1213
Tom Zanussi956ffd02009-11-25 01:15:46 -06001214int script_spec_register(const char *spec, struct scripting_ops *ops)
1215{
1216 struct script_spec *s;
1217
1218 s = script_spec__find(spec);
1219 if (s)
1220 return -1;
1221
Taeung Song8560bae2016-02-26 00:13:10 +09001222 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001223 if (!s)
1224 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001225 else
1226 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001227
1228 return 0;
1229}
1230
1231static struct scripting_ops *script_spec__lookup(const char *spec)
1232{
1233 struct script_spec *s = script_spec__find(spec);
1234 if (!s)
1235 return NULL;
1236
1237 return s->ops;
1238}
1239
1240static void list_available_languages(void)
1241{
1242 struct script_spec *s;
1243
1244 fprintf(stderr, "\n");
1245 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001246 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001247
1248 list_for_each_entry(s, &script_specs, node)
1249 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1250
1251 fprintf(stderr, "\n");
1252}
1253
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001254static int parse_scriptname(const struct option *opt __maybe_unused,
1255 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001256{
1257 char spec[PATH_MAX];
1258 const char *script, *ext;
1259 int len;
1260
Tom Zanussif526d682010-01-27 02:27:52 -06001261 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001262 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001263 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001264 }
1265
1266 script = strchr(str, ':');
1267 if (script) {
1268 len = script - str;
1269 if (len >= PATH_MAX) {
1270 fprintf(stderr, "invalid language specifier");
1271 return -1;
1272 }
1273 strncpy(spec, str, len);
1274 spec[len] = '\0';
1275 scripting_ops = script_spec__lookup(spec);
1276 if (!scripting_ops) {
1277 fprintf(stderr, "invalid language specifier");
1278 return -1;
1279 }
1280 script++;
1281 } else {
1282 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001283 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001284 if (!ext) {
1285 fprintf(stderr, "invalid script extension");
1286 return -1;
1287 }
1288 scripting_ops = script_spec__lookup(++ext);
1289 if (!scripting_ops) {
1290 fprintf(stderr, "invalid script extension");
1291 return -1;
1292 }
1293 }
1294
1295 script_name = strdup(script);
1296
1297 return 0;
1298}
1299
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001300static int parse_output_fields(const struct option *opt __maybe_unused,
1301 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001302{
1303 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001304 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001305 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001306 int rc = 0;
1307 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001308 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001309
1310 if (!str)
1311 return -ENOMEM;
1312
David Ahern2c9e45f72011-03-17 10:03:21 -06001313 /* first word can state for which event type the user is specifying
1314 * the fields. If no type exists, the specified fields apply to all
1315 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001316 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001317 tok = strchr(str, ':');
1318 if (tok) {
1319 *tok = '\0';
1320 tok++;
1321 if (!strcmp(str, "hw"))
1322 type = PERF_TYPE_HARDWARE;
1323 else if (!strcmp(str, "sw"))
1324 type = PERF_TYPE_SOFTWARE;
1325 else if (!strcmp(str, "trace"))
1326 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001327 else if (!strcmp(str, "raw"))
1328 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001329 else if (!strcmp(str, "break"))
1330 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001331 else {
1332 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001333 rc = -EINVAL;
1334 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001335 }
1336
1337 if (output[type].user_set)
1338 pr_warning("Overriding previous field request for %s events.\n",
1339 event_type(type));
1340
1341 output[type].fields = 0;
1342 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001343 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001344
1345 } else {
1346 tok = str;
1347 if (strlen(str) == 0) {
1348 fprintf(stderr,
1349 "Cannot set fields to 'none' for all event types.\n");
1350 rc = -EINVAL;
1351 goto out;
1352 }
1353
1354 if (output_set_by_user())
1355 pr_warning("Overriding previous field request for all events.\n");
1356
1357 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1358 output[j].fields = 0;
1359 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001360 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001361 }
David Ahern1424dc92011-03-09 22:23:28 -07001362 }
1363
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001364 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001365 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001366 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001367 break;
David Ahern745f43e2011-03-09 22:23:26 -07001368 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001369 if (i == imax && strcmp(tok, "flags") == 0) {
1370 print_flags = true;
1371 continue;
1372 }
David Ahern745f43e2011-03-09 22:23:26 -07001373 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001374 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001375 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001376 goto out;
1377 }
1378
1379 if (type == -1) {
1380 /* add user option to all events types for
1381 * which it is valid
1382 */
1383 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1384 if (output[j].invalid_fields & all_output_options[i].field) {
1385 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1386 all_output_options[i].str, event_type(j));
1387 } else
1388 output[j].fields |= all_output_options[i].field;
1389 }
1390 } else {
1391 if (output[type].invalid_fields & all_output_options[i].field) {
1392 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1393 all_output_options[i].str, event_type(type));
1394
1395 rc = -EINVAL;
1396 goto out;
1397 }
1398 output[type].fields |= all_output_options[i].field;
1399 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001400 }
1401
1402 if (type >= 0) {
1403 if (output[type].fields == 0) {
1404 pr_debug("No fields requested for %s type. "
1405 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001406 }
David Ahern1424dc92011-03-09 22:23:28 -07001407 }
David Ahern745f43e2011-03-09 22:23:26 -07001408
David Ahern2c9e45f72011-03-17 10:03:21 -06001409out:
David Ahern745f43e2011-03-09 22:23:26 -07001410 free(str);
1411 return rc;
1412}
1413
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001414/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1415static int is_directory(const char *base_path, const struct dirent *dent)
1416{
1417 char path[PATH_MAX];
1418 struct stat st;
1419
1420 sprintf(path, "%s/%s", base_path, dent->d_name);
1421 if (stat(path, &st))
1422 return 0;
1423
1424 return S_ISDIR(st.st_mode);
1425}
1426
1427#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001428 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1429 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001430 if ((lang_dirent.d_type == DT_DIR || \
1431 (lang_dirent.d_type == DT_UNKNOWN && \
1432 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001433 (strcmp(lang_dirent.d_name, ".")) && \
1434 (strcmp(lang_dirent.d_name, "..")))
1435
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001436#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001437 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1438 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001439 if (script_dirent.d_type != DT_DIR && \
1440 (script_dirent.d_type != DT_UNKNOWN || \
1441 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001442
1443
1444#define RECORD_SUFFIX "-record"
1445#define REPORT_SUFFIX "-report"
1446
1447struct script_desc {
1448 struct list_head node;
1449 char *name;
1450 char *half_liner;
1451 char *args;
1452};
1453
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001454static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001455
1456static struct script_desc *script_desc__new(const char *name)
1457{
1458 struct script_desc *s = zalloc(sizeof(*s));
1459
Tom Zanussib5b87312010-11-10 08:16:51 -06001460 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001461 s->name = strdup(name);
1462
1463 return s;
1464}
1465
1466static void script_desc__delete(struct script_desc *s)
1467{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001468 zfree(&s->name);
1469 zfree(&s->half_liner);
1470 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001471 free(s);
1472}
1473
1474static void script_desc__add(struct script_desc *s)
1475{
1476 list_add_tail(&s->node, &script_descs);
1477}
1478
1479static struct script_desc *script_desc__find(const char *name)
1480{
1481 struct script_desc *s;
1482
1483 list_for_each_entry(s, &script_descs, node)
1484 if (strcasecmp(s->name, name) == 0)
1485 return s;
1486 return NULL;
1487}
1488
1489static struct script_desc *script_desc__findnew(const char *name)
1490{
1491 struct script_desc *s = script_desc__find(name);
1492
1493 if (s)
1494 return s;
1495
1496 s = script_desc__new(name);
1497 if (!s)
1498 goto out_delete_desc;
1499
1500 script_desc__add(s);
1501
1502 return s;
1503
1504out_delete_desc:
1505 script_desc__delete(s);
1506
1507 return NULL;
1508}
1509
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001510static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001511{
1512 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001513 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001514
1515 if (strlen(str) > suffix_len) {
1516 p = str + strlen(str) - suffix_len;
1517 if (!strncmp(p, suffix, suffix_len))
1518 return p;
1519 }
1520
1521 return NULL;
1522}
1523
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001524static int read_script_info(struct script_desc *desc, const char *filename)
1525{
1526 char line[BUFSIZ], *p;
1527 FILE *fp;
1528
1529 fp = fopen(filename, "r");
1530 if (!fp)
1531 return -1;
1532
1533 while (fgets(line, sizeof(line), fp)) {
1534 p = ltrim(line);
1535 if (strlen(p) == 0)
1536 continue;
1537 if (*p != '#')
1538 continue;
1539 p++;
1540 if (strlen(p) && *p == '!')
1541 continue;
1542
1543 p = ltrim(p);
1544 if (strlen(p) && p[strlen(p) - 1] == '\n')
1545 p[strlen(p) - 1] = '\0';
1546
1547 if (!strncmp(p, "description:", strlen("description:"))) {
1548 p += strlen("description:");
1549 desc->half_liner = strdup(ltrim(p));
1550 continue;
1551 }
1552
1553 if (!strncmp(p, "args:", strlen("args:"))) {
1554 p += strlen("args:");
1555 desc->args = strdup(ltrim(p));
1556 continue;
1557 }
1558 }
1559
1560 fclose(fp);
1561
1562 return 0;
1563}
1564
Robert Richter38efb532011-11-25 11:38:40 +01001565static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1566{
1567 char *script_root, *str;
1568
1569 script_root = strdup(script_dirent->d_name);
1570 if (!script_root)
1571 return NULL;
1572
1573 str = (char *)ends_with(script_root, suffix);
1574 if (!str) {
1575 free(script_root);
1576 return NULL;
1577 }
1578
1579 *str = '\0';
1580 return script_root;
1581}
1582
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001583static int list_available_scripts(const struct option *opt __maybe_unused,
1584 const char *s __maybe_unused,
1585 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001586{
1587 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1588 char scripts_path[MAXPATHLEN];
1589 DIR *scripts_dir, *lang_dir;
1590 char script_path[MAXPATHLEN];
1591 char lang_path[MAXPATHLEN];
1592 struct script_desc *desc;
1593 char first_half[BUFSIZ];
1594 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001595
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001596 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001597
1598 scripts_dir = opendir(scripts_path);
1599 if (!scripts_dir)
1600 return -1;
1601
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001602 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001603 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1604 lang_dirent.d_name);
1605 lang_dir = opendir(lang_path);
1606 if (!lang_dir)
1607 continue;
1608
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001609 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001610 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1611 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001612 desc = script_desc__findnew(script_root);
1613 snprintf(script_path, MAXPATHLEN, "%s/%s",
1614 lang_path, script_dirent.d_name);
1615 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001616 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001617 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001618 }
1619 }
1620
1621 fprintf(stdout, "List of available trace scripts:\n");
1622 list_for_each_entry(desc, &script_descs, node) {
1623 sprintf(first_half, "%s %s", desc->name,
1624 desc->args ? desc->args : "");
1625 fprintf(stdout, " %-36s %s\n", first_half,
1626 desc->half_liner ? desc->half_liner : "");
1627 }
1628
1629 exit(0);
1630}
1631
Feng Tange5f37052012-09-07 16:42:26 +08001632/*
Feng Tang49e639e2012-10-30 11:56:03 +08001633 * Some scripts specify the required events in their "xxx-record" file,
1634 * this function will check if the events in perf.data match those
1635 * mentioned in the "xxx-record".
1636 *
1637 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1638 * which is covered well now. And new parsing code should be added to
1639 * cover the future complexing formats like event groups etc.
1640 */
1641static int check_ev_match(char *dir_name, char *scriptname,
1642 struct perf_session *session)
1643{
1644 char filename[MAXPATHLEN], evname[128];
1645 char line[BUFSIZ], *p;
1646 struct perf_evsel *pos;
1647 int match, len;
1648 FILE *fp;
1649
1650 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1651
1652 fp = fopen(filename, "r");
1653 if (!fp)
1654 return -1;
1655
1656 while (fgets(line, sizeof(line), fp)) {
1657 p = ltrim(line);
1658 if (*p == '#')
1659 continue;
1660
1661 while (strlen(p)) {
1662 p = strstr(p, "-e");
1663 if (!p)
1664 break;
1665
1666 p += 2;
1667 p = ltrim(p);
1668 len = strcspn(p, " \t");
1669 if (!len)
1670 break;
1671
1672 snprintf(evname, len + 1, "%s", p);
1673
1674 match = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001675 evlist__for_each(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001676 if (!strcmp(perf_evsel__name(pos), evname)) {
1677 match = 1;
1678 break;
1679 }
1680 }
1681
1682 if (!match) {
1683 fclose(fp);
1684 return -1;
1685 }
1686 }
1687 }
1688
1689 fclose(fp);
1690 return 0;
1691}
1692
1693/*
Feng Tange5f37052012-09-07 16:42:26 +08001694 * Return -1 if none is found, otherwise the actual scripts number.
1695 *
1696 * Currently the only user of this function is the script browser, which
1697 * will list all statically runnable scripts, select one, execute it and
1698 * show the output in a perf browser.
1699 */
1700int find_scripts(char **scripts_array, char **scripts_path_array)
1701{
1702 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001703 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001704 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001705 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001706 struct perf_data_file file = {
1707 .path = input_name,
1708 .mode = PERF_DATA_MODE_READ,
1709 };
Feng Tange5f37052012-09-07 16:42:26 +08001710 char *temp;
1711 int i = 0;
1712
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001713 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001714 if (!session)
1715 return -1;
1716
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001717 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08001718
1719 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001720 if (!scripts_dir) {
1721 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001722 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001723 }
Feng Tange5f37052012-09-07 16:42:26 +08001724
1725 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1726 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1727 lang_dirent.d_name);
1728#ifdef NO_LIBPERL
1729 if (strstr(lang_path, "perl"))
1730 continue;
1731#endif
1732#ifdef NO_LIBPYTHON
1733 if (strstr(lang_path, "python"))
1734 continue;
1735#endif
1736
1737 lang_dir = opendir(lang_path);
1738 if (!lang_dir)
1739 continue;
1740
1741 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1742 /* Skip those real time scripts: xxxtop.p[yl] */
1743 if (strstr(script_dirent.d_name, "top."))
1744 continue;
1745 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1746 script_dirent.d_name);
1747 temp = strchr(script_dirent.d_name, '.');
1748 snprintf(scripts_array[i],
1749 (temp - script_dirent.d_name) + 1,
1750 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001751
1752 if (check_ev_match(lang_path,
1753 scripts_array[i], session))
1754 continue;
1755
Feng Tange5f37052012-09-07 16:42:26 +08001756 i++;
1757 }
Feng Tang49e639e2012-10-30 11:56:03 +08001758 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001759 }
1760
Feng Tang49e639e2012-10-30 11:56:03 +08001761 closedir(scripts_dir);
1762 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001763 return i;
1764}
1765
Tom Zanussi38752942009-12-15 02:53:39 -06001766static char *get_script_path(const char *script_root, const char *suffix)
1767{
1768 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1769 char scripts_path[MAXPATHLEN];
1770 char script_path[MAXPATHLEN];
1771 DIR *scripts_dir, *lang_dir;
1772 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001773 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001774
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001775 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06001776
1777 scripts_dir = opendir(scripts_path);
1778 if (!scripts_dir)
1779 return NULL;
1780
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001781 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001782 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1783 lang_dirent.d_name);
1784 lang_dir = opendir(lang_path);
1785 if (!lang_dir)
1786 continue;
1787
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001788 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001789 __script_root = get_script_root(&script_dirent, suffix);
1790 if (__script_root && !strcmp(script_root, __script_root)) {
1791 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001792 closedir(lang_dir);
1793 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001794 snprintf(script_path, MAXPATHLEN, "%s/%s",
1795 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001796 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001797 }
1798 free(__script_root);
1799 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001800 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001801 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001802 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001803
Robert Richter38efb532011-11-25 11:38:40 +01001804 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001805}
1806
Tom Zanussib5b87312010-11-10 08:16:51 -06001807static bool is_top_script(const char *script_path)
1808{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001809 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001810}
1811
1812static int has_required_arg(char *script_path)
1813{
1814 struct script_desc *desc;
1815 int n_args = 0;
1816 char *p;
1817
1818 desc = script_desc__new(NULL);
1819
1820 if (read_script_info(desc, script_path))
1821 goto out;
1822
1823 if (!desc->args)
1824 goto out;
1825
1826 for (p = desc->args; *p; p++)
1827 if (*p == '<')
1828 n_args++;
1829out:
1830 script_desc__delete(desc);
1831
1832 return n_args;
1833}
1834
David Ahernd54b1a92012-08-26 12:24:46 -06001835static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001836{
1837 char **__argv = malloc(sizeof(const char *) * argc);
1838
David Ahernd54b1a92012-08-26 12:24:46 -06001839 if (!__argv) {
1840 pr_err("malloc failed\n");
1841 return -1;
1842 }
1843
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001844 memcpy(__argv, argv, sizeof(const char *) * argc);
1845 argc = parse_options(argc, (const char **)__argv, record_options,
1846 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1847 free(__argv);
1848
David Ahernd54b1a92012-08-26 12:24:46 -06001849 system_wide = (argc == 0);
1850
1851 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001852}
1853
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001854static void script__setup_sample_type(struct perf_script *script)
1855{
1856 struct perf_session *session = script->session;
1857 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1858
1859 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1860 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1861 (sample_type & PERF_SAMPLE_STACK_USER))
1862 callchain_param.record_mode = CALLCHAIN_DWARF;
1863 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1864 callchain_param.record_mode = CALLCHAIN_LBR;
1865 else
1866 callchain_param.record_mode = CALLCHAIN_FP;
1867 }
1868}
1869
Jiri Olsae099eba2016-01-05 22:09:09 +01001870static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1871 union perf_event *event,
1872 struct perf_session *session)
1873{
1874 struct stat_round_event *round = &event->stat_round;
1875 struct perf_evsel *counter;
1876
1877 evlist__for_each(session->evlist, counter) {
1878 perf_stat_process_counter(&stat_config, counter);
1879 process_stat(counter, round->time);
1880 }
1881
1882 process_stat_interval(round->time);
1883 return 0;
1884}
1885
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01001886static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1887 union perf_event *event,
1888 struct perf_session *session __maybe_unused)
1889{
1890 perf_event__read_stat_config(&stat_config, &event->stat_config);
1891 return 0;
1892}
1893
Jiri Olsacfc88742016-01-05 22:09:06 +01001894static int set_maps(struct perf_script *script)
1895{
1896 struct perf_evlist *evlist = script->session->evlist;
1897
1898 if (!script->cpus || !script->threads)
1899 return 0;
1900
1901 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
1902 return -EINVAL;
1903
1904 perf_evlist__set_maps(evlist, script->cpus, script->threads);
1905
1906 if (perf_evlist__alloc_stats(evlist, true))
1907 return -ENOMEM;
1908
1909 script->allocated = true;
1910 return 0;
1911}
1912
1913static
1914int process_thread_map_event(struct perf_tool *tool,
1915 union perf_event *event,
1916 struct perf_session *session __maybe_unused)
1917{
1918 struct perf_script *script = container_of(tool, struct perf_script, tool);
1919
1920 if (script->threads) {
1921 pr_warning("Extra thread map event, ignoring.\n");
1922 return 0;
1923 }
1924
1925 script->threads = thread_map__new_event(&event->thread_map);
1926 if (!script->threads)
1927 return -ENOMEM;
1928
1929 return set_maps(script);
1930}
1931
1932static
1933int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1934 union perf_event *event,
1935 struct perf_session *session __maybe_unused)
1936{
1937 struct perf_script *script = container_of(tool, struct perf_script, tool);
1938
1939 if (script->cpus) {
1940 pr_warning("Extra cpu map event, ignoring.\n");
1941 return 0;
1942 }
1943
1944 script->cpus = cpu_map__new_data(&event->cpu_map.data);
1945 if (!script->cpus)
1946 return -ENOMEM;
1947
1948 return set_maps(script);
1949}
1950
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001951int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001952{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001953 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01001954 bool header = false;
1955 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001956 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001957 char *rec_script_path = NULL;
1958 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001959 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001960 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06001961 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001962 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001963 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001964 struct perf_script script = {
1965 .tool = {
1966 .sample = process_sample_event,
1967 .mmap = perf_event__process_mmap,
1968 .mmap2 = perf_event__process_mmap2,
1969 .comm = perf_event__process_comm,
1970 .exit = perf_event__process_exit,
1971 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001972 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001973 .tracing_data = perf_event__process_tracing_data,
1974 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001975 .id_index = perf_event__process_id_index,
1976 .auxtrace_info = perf_event__process_auxtrace_info,
1977 .auxtrace = perf_event__process_auxtrace,
1978 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01001979 .stat = perf_event__process_stat_event,
1980 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01001981 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01001982 .thread_map = process_thread_map_event,
1983 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001984 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001985 .ordering_requires_timestamps = true,
1986 },
1987 };
Yunlong Song06af0f22015-04-02 21:47:16 +08001988 struct perf_data_file file = {
1989 .mode = PERF_DATA_MODE_READ,
1990 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001991 const struct option options[] = {
1992 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1993 "dump raw trace in ASCII"),
1994 OPT_INCR('v', "verbose", &verbose,
1995 "be more verbose (show symbol address, etc)"),
1996 OPT_BOOLEAN('L', "Latency", &latency_format,
1997 "show latency attributes (irqs/preemption disabled, etc)"),
1998 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1999 list_available_scripts),
2000 OPT_CALLBACK('s', "script", NULL, "name",
2001 "script file name (lang:script name, script name, or *)",
2002 parse_scriptname),
2003 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2004 "generate perf-script.xx script in specified language"),
2005 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2006 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2007 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002008 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2009 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002010 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2011 "file", "vmlinux pathname"),
2012 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2013 "file", "kallsyms pathname"),
2014 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2015 "When printing symbols do not display call chain"),
2016 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
2017 "Look for files with symbols relative to this directory"),
Yunlong Song06af0f22015-04-02 21:47:16 +08002018 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002019 "comma separated output fields prepend with 'type:'. "
2020 "Valid types: hw,sw,trace,raw. "
2021 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Stephane Eraniandc323ce2015-08-31 18:41:13 +02002022 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002023 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2024 "system-wide collection from all CPUs"),
2025 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2026 "only consider these symbols"),
2027 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2028 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2029 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002030 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2031 "only consider symbols in these pids"),
2032 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2033 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002034 OPT_BOOLEAN('I', "show-info", &show_full_info,
2035 "display extended information from perf.data file"),
2036 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2037 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002038 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2039 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002040 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2041 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002042 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2043 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08002044 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002045 OPT_BOOLEAN(0, "ns", &nanosecs,
2046 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002047 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2048 "Instruction Tracing options",
2049 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002050 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2051 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002052 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2053 "Enable symbol demangling"),
2054 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2055 "Enable kernel symbol demangling"),
2056
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002057 OPT_END()
2058 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002059 const char * const script_subcommands[] = { "record", "report", NULL };
2060 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002061 "perf script [<options>]",
2062 "perf script [<options>] record <script> [<record-options>] <command>",
2063 "perf script [<options>] report <script> [script-args]",
2064 "perf script [<options>] <script> [<record-options>] <command>",
2065 "perf script [<options>] <top-script> [script-args]",
2066 NULL
2067 };
Tom Zanussi38752942009-12-15 02:53:39 -06002068
Tom Zanussib5b87312010-11-10 08:16:51 -06002069 setup_scripting();
2070
Yunlong Song40cae2b2015-03-18 21:35:54 +08002071 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002072 PARSE_OPT_STOP_AT_NON_OPTION);
2073
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002074 file.path = input_name;
2075
Tom Zanussib5b87312010-11-10 08:16:51 -06002076 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2077 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2078 if (!rec_script_path)
2079 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06002080 }
2081
Tom Zanussib5b87312010-11-10 08:16:51 -06002082 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2083 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2084 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002085 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002086 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002087 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002088 return -1;
2089 }
Tom Zanussi38752942009-12-15 02:53:39 -06002090 }
2091
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002092 if (itrace_synth_opts.callchain &&
2093 itrace_synth_opts.callchain_sz > scripting_max_stack)
2094 scripting_max_stack = itrace_synth_opts.callchain_sz;
2095
Ben Hutchings44e668c2010-10-10 16:10:03 +01002096 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002097 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002098
Tom Zanussib5b87312010-11-10 08:16:51 -06002099 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002100 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002101 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002102 pid_t pid;
2103
Tom Zanussib5b87312010-11-10 08:16:51 -06002104 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2105 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2106
2107 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002108 usage_with_options_msg(script_usage, options,
2109 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002110 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002111 }
2112
Tom Zanussib5b87312010-11-10 08:16:51 -06002113 if (is_top_script(argv[0])) {
2114 rep_args = argc - 1;
2115 } else {
2116 int rec_args;
2117
2118 rep_args = has_required_arg(rep_script_path);
2119 rec_args = (argc - 1) - rep_args;
2120 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002121 usage_with_options_msg(script_usage, options,
2122 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002123 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002124 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002125 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002126 }
2127
2128 if (pipe(live_pipe) < 0) {
2129 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002130 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002131 }
2132
2133 pid = fork();
2134 if (pid < 0) {
2135 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002136 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002137 }
2138
2139 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002140 j = 0;
2141
Tom Zanussia0cccc22010-04-01 23:59:25 -05002142 dup2(live_pipe[1], 1);
2143 close(live_pipe[0]);
2144
Robert Richter317df652011-11-25 15:05:25 +01002145 if (is_top_script(argv[0])) {
2146 system_wide = true;
2147 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002148 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2149 err = -1;
2150 goto out;
2151 }
Robert Richter317df652011-11-25 15:05:25 +01002152 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002153
2154 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002155 if (!__argv) {
2156 pr_err("malloc failed\n");
2157 err = -ENOMEM;
2158 goto out;
2159 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002160
Tom Zanussib5b87312010-11-10 08:16:51 -06002161 __argv[j++] = "/bin/sh";
2162 __argv[j++] = rec_script_path;
2163 if (system_wide)
2164 __argv[j++] = "-a";
2165 __argv[j++] = "-q";
2166 __argv[j++] = "-o";
2167 __argv[j++] = "-";
2168 for (i = rep_args + 1; i < argc; i++)
2169 __argv[j++] = argv[i];
2170 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002171
2172 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002173 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002174 exit(-1);
2175 }
2176
2177 dup2(live_pipe[0], 0);
2178 close(live_pipe[1]);
2179
Tom Zanussib5b87312010-11-10 08:16:51 -06002180 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002181 if (!__argv) {
2182 pr_err("malloc failed\n");
2183 err = -ENOMEM;
2184 goto out;
2185 }
2186
Tom Zanussib5b87312010-11-10 08:16:51 -06002187 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002188 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002189 __argv[j++] = rep_script_path;
2190 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002191 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002192 __argv[j++] = "-i";
2193 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002194 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002195
2196 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002197 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002198 exit(-1);
2199 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002200
Tom Zanussib5b87312010-11-10 08:16:51 -06002201 if (rec_script_path)
2202 script_path = rec_script_path;
2203 if (rep_script_path)
2204 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002205
Tom Zanussib5b87312010-11-10 08:16:51 -06002206 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002207 j = 0;
2208
Robert Richter317df652011-11-25 15:05:25 +01002209 if (!rec_script_path)
2210 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002211 else if (!system_wide) {
2212 if (have_cmd(argc - 1, &argv[1]) != 0) {
2213 err = -1;
2214 goto out;
2215 }
2216 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002217
2218 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002219 if (!__argv) {
2220 pr_err("malloc failed\n");
2221 err = -ENOMEM;
2222 goto out;
2223 }
2224
Tom Zanussib5b87312010-11-10 08:16:51 -06002225 __argv[j++] = "/bin/sh";
2226 __argv[j++] = script_path;
2227 if (system_wide)
2228 __argv[j++] = "-a";
2229 for (i = 2; i < argc; i++)
2230 __argv[j++] = argv[i];
2231 __argv[j++] = NULL;
2232
2233 execvp("/bin/sh", (char **)__argv);
2234 free(__argv);
2235 exit(-1);
2236 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002237
Tom Zanussicf4fee52010-03-03 01:04:33 -06002238 if (!script_name)
2239 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002240
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002241 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002242 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002243 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002244
Jiri Olsae90debd2013-12-09 11:02:50 +01002245 if (header || header_only) {
2246 perf_session__fprintf_info(session, stdout, show_full_info);
2247 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002248 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002249 }
2250
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002251 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002252 goto out_delete;
2253
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002254 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002255 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002256
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002257 session->itrace_synth_opts = &itrace_synth_opts;
2258
Anton Blanchard5d67be92011-07-04 21:57:50 +10002259 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002260 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2261 if (err < 0)
2262 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002263 }
2264
David Ahern1424dc92011-03-09 22:23:28 -07002265 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002266 symbol_conf.use_callchain = true;
2267 else
2268 symbol_conf.use_callchain = false;
2269
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002270 if (session->tevent.pevent &&
2271 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002272 machine__resolve_kernel_addr,
2273 &session->machines.host) < 0) {
2274 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2275 return -1;
2276 }
2277
Tom Zanussi956ffd02009-11-25 01:15:46 -06002278 if (generate_script_lang) {
2279 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002280 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002281
David Ahern2c9e45f72011-03-17 10:03:21 -06002282 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002283 fprintf(stderr,
2284 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002285 err = -EINVAL;
2286 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002287 }
2288
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002289 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002290 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002291 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002292 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002293 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002294 }
2295
2296 err = fstat(input, &perf_stat);
2297 if (err < 0) {
2298 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002299 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002300 }
2301
2302 if (!perf_stat.st_size) {
2303 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002304 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002305 }
2306
2307 scripting_ops = script_spec__lookup(generate_script_lang);
2308 if (!scripting_ops) {
2309 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002310 err = -ENOENT;
2311 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002312 }
2313
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002314 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002315 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002316 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002317 }
2318
2319 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002320 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002321 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002322 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002323 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002324 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002325 }
2326
David Ahern9cbdb702011-04-06 21:54:20 -06002327
2328 err = perf_session__check_output_opt(session);
2329 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002330 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002331
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002332 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002333
Adrian Hunterd445dd22014-08-15 22:08:37 +03002334 flush_scripting();
2335
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002336out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002337 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002338 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002339
2340 if (script_started)
2341 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002342out:
2343 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002344}