blob: 7228d141a789d8d50dd4386df0e38e9bba7e9183 [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 Olsaf5fc1412013-10-15 16:27:32 +020019#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030020#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010021#include "util/cpumap.h"
22#include "util/thread_map.h"
23#include "util/stat.h"
Adrian Huntere2167082016-06-23 16:40:58 +030024#include "util/thread-stack.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100025#include <linux/bitmap.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030026#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030027#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010028#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010029#include "util/mem-events.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020030
Tom Zanussi956ffd02009-11-25 01:15:46 -060031static char const *script_name;
32static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020033static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020034static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020035static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070036static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090037static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010038static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030039static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030040static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100041static const char *cpu_list;
42static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010043static struct perf_stat_config stat_config;
Tom Zanussi956ffd02009-11-25 01:15:46 -060044
Adrian Hunter44cbe722015-09-25 16:15:50 +030045unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030046
David Ahern745f43e2011-03-09 22:23:26 -070047enum perf_output_field {
48 PERF_OUTPUT_COMM = 1U << 0,
49 PERF_OUTPUT_TID = 1U << 1,
50 PERF_OUTPUT_PID = 1U << 2,
51 PERF_OUTPUT_TIME = 1U << 3,
52 PERF_OUTPUT_CPU = 1U << 4,
53 PERF_OUTPUT_EVNAME = 1U << 5,
54 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060055 PERF_OUTPUT_IP = 1U << 7,
56 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060057 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060058 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090059 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020060 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaa2014-08-25 16:45:42 +020061 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020062 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020063 PERF_OUTPUT_BRSTACK = 1U << 15,
64 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010065 PERF_OUTPUT_DATA_SRC = 1U << 17,
66 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000067 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030068 PERF_OUTPUT_CALLINDENT = 1U << 20,
David Ahern745f43e2011-03-09 22:23:26 -070069};
70
71struct output_option {
72 const char *str;
73 enum perf_output_field field;
74} all_output_options[] = {
75 {.str = "comm", .field = PERF_OUTPUT_COMM},
76 {.str = "tid", .field = PERF_OUTPUT_TID},
77 {.str = "pid", .field = PERF_OUTPUT_PID},
78 {.str = "time", .field = PERF_OUTPUT_TIME},
79 {.str = "cpu", .field = PERF_OUTPUT_CPU},
80 {.str = "event", .field = PERF_OUTPUT_EVNAME},
81 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060082 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070083 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060084 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060085 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090086 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020087 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaa2014-08-25 16:45:42 +020088 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +020089 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +020090 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
91 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +010092 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
93 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +000094 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +030095 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
David Ahern745f43e2011-03-09 22:23:26 -070096};
97
98/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -060099static struct {
100 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600101 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400102 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600103 u64 fields;
104 u64 invalid_fields;
105} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700106
David Ahern2c9e45f72011-03-17 10:03:21 -0600107 [PERF_TYPE_HARDWARE] = {
108 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700109
David Ahern2c9e45f72011-03-17 10:03:21 -0600110 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
111 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600112 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200113 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
114 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600115
Wang Nan30372f02016-02-24 11:20:45 +0000116 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600117 },
118
119 [PERF_TYPE_SOFTWARE] = {
120 .user_set = false,
121
122 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
123 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600124 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200125 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000126 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600127
128 .invalid_fields = PERF_OUTPUT_TRACE,
129 },
130
131 [PERF_TYPE_TRACEPOINT] = {
132 .user_set = false,
133
134 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
135 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000136 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600137 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700138
139 [PERF_TYPE_RAW] = {
140 .user_set = false,
141
142 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
143 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600144 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200145 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100146 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
147 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700148
Wang Nan30372f02016-02-24 11:20:45 +0000149 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700150 },
Wang Nan27cfef02015-12-08 02:25:43 +0000151
152 [PERF_TYPE_BREAKPOINT] = {
153 .user_set = false,
154
155 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
156 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
157 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
158 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
159 PERF_OUTPUT_PERIOD,
160
Wang Nan30372f02016-02-24 11:20:45 +0000161 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000162 },
David Ahern1424dc92011-03-09 22:23:28 -0700163};
David Ahern745f43e2011-03-09 22:23:26 -0700164
David Ahern2c9e45f72011-03-17 10:03:21 -0600165static bool output_set_by_user(void)
166{
167 int j;
168 for (j = 0; j < PERF_TYPE_MAX; ++j) {
169 if (output[j].user_set)
170 return true;
171 }
172 return false;
173}
David Ahern745f43e2011-03-09 22:23:26 -0700174
David Ahern9cbdb702011-04-06 21:54:20 -0600175static const char *output_field2str(enum perf_output_field field)
176{
177 int i, imax = ARRAY_SIZE(all_output_options);
178 const char *str = "";
179
180 for (i = 0; i < imax; ++i) {
181 if (all_output_options[i].field == field) {
182 str = all_output_options[i].str;
183 break;
184 }
185 }
186 return str;
187}
188
David Ahern2c9e45f72011-03-17 10:03:21 -0600189#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700190
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300191static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
192 u64 sample_type, const char *sample_msg,
193 enum perf_output_field field,
194 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700195{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300196 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600197 int type = attr->type;
198 const char *evname;
199
200 if (attr->sample_type & sample_type)
201 return 0;
202
203 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300204 if (allow_user_set)
205 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300206 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600207 pr_err("Samples for '%s' event do not have %s attribute set. "
208 "Cannot print '%s' field.\n",
209 evname, sample_msg, output_field2str(field));
210 return -1;
211 }
212
213 /* user did not ask for it explicitly so remove from the default list */
214 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300215 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600216 pr_debug("Samples for '%s' event do not have %s attribute set. "
217 "Skipping '%s' field.\n",
218 evname, sample_msg, output_field2str(field));
219
220 return 0;
221}
222
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300223static int perf_evsel__check_stype(struct perf_evsel *evsel,
224 u64 sample_type, const char *sample_msg,
225 enum perf_output_field field)
226{
227 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
228 false);
229}
230
David Ahern9cbdb702011-04-06 21:54:20 -0600231static int perf_evsel__check_attr(struct perf_evsel *evsel,
232 struct perf_session *session)
233{
234 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300235 bool allow_user_set;
236
Jiri Olsae099eba2016-01-05 22:09:09 +0100237 if (perf_header__has_feat(&session->header, HEADER_STAT))
238 return 0;
239
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300240 allow_user_set = perf_header__has_feat(&session->header,
241 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600242
David Ahern1424dc92011-03-09 22:23:28 -0700243 if (PRINT_FIELD(TRACE) &&
244 !perf_session__has_traces(session, "record -R"))
245 return -EINVAL;
246
David Ahern787bef12011-05-27 14:28:43 -0600247 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300248 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
249 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700250 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700251 }
David Ahern7cec0922011-05-30 13:08:23 -0600252
253 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300254 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
255 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600256 return -EINVAL;
257
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100258 if (PRINT_FIELD(DATA_SRC) &&
259 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
260 PERF_OUTPUT_DATA_SRC))
261 return -EINVAL;
262
263 if (PRINT_FIELD(WEIGHT) &&
264 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
265 PERF_OUTPUT_WEIGHT))
266 return -EINVAL;
267
David Ahern7cec0922011-05-30 13:08:23 -0600268 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
269 pr_err("Display of symbols requested but neither sample IP nor "
270 "sample address\nis selected. Hence, no addresses to convert "
271 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600272 return -EINVAL;
273 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900274 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
275 pr_err("Display of offsets requested but symbol is not"
276 "selected.\n");
277 return -EINVAL;
278 }
David Ahern7cec0922011-05-30 13:08:23 -0600279 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
280 pr_err("Display of DSO requested but neither sample IP nor "
281 "sample address\nis selected. Hence, no addresses to convert "
282 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600283 return -EINVAL;
284 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200285 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
286 pr_err("Display of source line number requested but sample IP is not\n"
287 "selected. Hence, no address to lookup the source line number.\n");
288 return -EINVAL;
289 }
David Ahern1424dc92011-03-09 22:23:28 -0700290
291 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300292 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
293 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700294 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700295
296 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300297 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
298 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700299 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700300
301 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300302 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
303 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700304 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600305
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200306 if (PRINT_FIELD(PERIOD) &&
307 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
308 PERF_OUTPUT_PERIOD))
309 return -EINVAL;
310
Stephane Eranianfc36f942015-08-31 18:41:10 +0200311 if (PRINT_FIELD(IREGS) &&
312 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
313 PERF_OUTPUT_IREGS))
314 return -EINVAL;
315
David Ahern9cbdb702011-04-06 21:54:20 -0600316 return 0;
317}
318
Adrian Hunter7ea95722013-11-01 15:51:30 +0200319static void set_print_ip_opts(struct perf_event_attr *attr)
320{
321 unsigned int type = attr->type;
322
323 output[type].print_ip_opts = 0;
324 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300325 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200326
327 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300328 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200329
330 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300331 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200332
333 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300334 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200335
336 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300337 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200338}
339
David Ahern9cbdb702011-04-06 21:54:20 -0600340/*
341 * verify all user requested events exist and the samples
342 * have the expected data
343 */
344static int perf_session__check_output_opt(struct perf_session *session)
345{
He Kuang40f20e52016-05-16 04:51:19 +0000346 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600347 struct perf_evsel *evsel;
348
349 for (j = 0; j < PERF_TYPE_MAX; ++j) {
350 evsel = perf_session__find_first_evtype(session, j);
351
352 /*
353 * even if fields is set to 0 (ie., show nothing) event must
354 * exist if user explicitly includes it on the command line
355 */
356 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
357 pr_err("%s events do not exist. "
358 "Remove corresponding -f option to proceed.\n",
359 event_type(j));
360 return -1;
361 }
362
363 if (evsel && output[j].fields &&
364 perf_evsel__check_attr(evsel, session))
365 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400366
367 if (evsel == NULL)
368 continue;
369
Adrian Hunter7ea95722013-11-01 15:51:30 +0200370 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700371 }
372
Adrian Hunter98526ee2014-07-31 09:00:59 +0300373 if (!no_callchain) {
374 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000375 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300376
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300377 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000378 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300379 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
380 use_callchain = true;
381 break;
382 }
383 }
He Kuang71ac8992016-08-04 11:25:43 +0000384 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300385 symbol_conf.use_callchain = false;
386 }
387
David Ahern80b8b492013-11-19 21:07:37 -0700388 /*
389 * set default for tracepoints to print symbols only
390 * if callchains are present
391 */
392 if (symbol_conf.use_callchain &&
393 !output[PERF_TYPE_TRACEPOINT].user_set) {
394 struct perf_event_attr *attr;
395
396 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700397
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300398 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000399 if (evsel->attr.type != j)
400 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700401
He Kuang40f20e52016-05-16 04:51:19 +0000402 attr = &evsel->attr;
403
404 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
405 output[j].fields |= PERF_OUTPUT_IP;
406 output[j].fields |= PERF_OUTPUT_SYM;
407 output[j].fields |= PERF_OUTPUT_DSO;
408 set_print_ip_opts(attr);
409 goto out;
410 }
David Ahern80b8b492013-11-19 21:07:37 -0700411 }
412 }
413
414out:
David Ahern1424dc92011-03-09 22:23:28 -0700415 return 0;
416}
David Ahern745f43e2011-03-09 22:23:26 -0700417
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300418static void print_sample_iregs(struct perf_sample *sample,
Stephane Eranianfc36f942015-08-31 18:41:10 +0200419 struct perf_event_attr *attr)
420{
421 struct regs_dump *regs = &sample->intr_regs;
422 uint64_t mask = attr->sample_regs_intr;
423 unsigned i = 0, r;
424
425 if (!regs)
426 return;
427
428 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
429 u64 val = regs->regs[i++];
430 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
431 }
432}
433
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300434static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700435 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300436 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700437{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300438 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700439 unsigned long secs;
440 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700441 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700442
David Ahern745f43e2011-03-09 22:23:26 -0700443 if (PRINT_FIELD(COMM)) {
444 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200445 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600446 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200447 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700448 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200449 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700450 }
David Ahernc70c94b2011-03-09 22:23:25 -0700451
David Ahern745f43e2011-03-09 22:23:26 -0700452 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
453 printf("%5d/%-5d ", sample->pid, sample->tid);
454 else if (PRINT_FIELD(PID))
455 printf("%5d ", sample->pid);
456 else if (PRINT_FIELD(TID))
457 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700458
David Ahern745f43e2011-03-09 22:23:26 -0700459 if (PRINT_FIELD(CPU)) {
460 if (latency_format)
461 printf("%3d ", sample->cpu);
462 else
463 printf("[%03d] ", sample->cpu);
464 }
David Ahernc70c94b2011-03-09 22:23:25 -0700465
David Ahern745f43e2011-03-09 22:23:26 -0700466 if (PRINT_FIELD(TIME)) {
467 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300468 secs = nsecs / NSEC_PER_SEC;
469 nsecs -= secs * NSEC_PER_SEC;
470 usecs = nsecs / NSEC_PER_USEC;
Adrian Hunter83e19862015-09-25 16:15:36 +0300471 if (nanosecs)
472 printf("%5lu.%09llu: ", secs, nsecs);
473 else
474 printf("%5lu.%06lu: ", secs, usecs);
David Ahern745f43e2011-03-09 22:23:26 -0700475 }
David Ahernc70c94b2011-03-09 22:23:25 -0700476}
477
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200478static inline char
479mispred_str(struct branch_entry *br)
480{
481 if (!(br->flags.mispred || br->flags.predicted))
482 return '-';
483
484 return br->flags.predicted ? 'P' : 'M';
485}
486
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300487static void print_sample_brstack(struct perf_sample *sample)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200488{
489 struct branch_stack *br = sample->branch_stack;
490 u64 i;
491
492 if (!(br && br->nr))
493 return;
494
495 for (i = 0; i < br->nr; i++) {
496 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
497 br->entries[i].from,
498 br->entries[i].to,
499 mispred_str( br->entries + i),
500 br->entries[i].flags.in_tx? 'X' : '-',
501 br->entries[i].flags.abort? 'A' : '-',
502 br->entries[i].flags.cycles);
503 }
504}
505
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300506static void print_sample_brstacksym(struct perf_sample *sample,
507 struct thread *thread)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200508{
509 struct branch_stack *br = sample->branch_stack;
510 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200511 u64 i, from, to;
512
513 if (!(br && br->nr))
514 return;
515
516 for (i = 0; i < br->nr; i++) {
517
518 memset(&alf, 0, sizeof(alf));
519 memset(&alt, 0, sizeof(alt));
520 from = br->entries[i].from;
521 to = br->entries[i].to;
522
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300523 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200524 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300525 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200526
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300527 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200528 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300529 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200530
531 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
532 putchar('/');
533 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
534 printf("/%c/%c/%c/%d ",
535 mispred_str( br->entries + i),
536 br->entries[i].flags.in_tx? 'X' : '-',
537 br->entries[i].flags.abort? 'A' : '-',
538 br->entries[i].flags.cycles);
539 }
540}
541
542
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300543static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600544 struct thread *thread,
545 struct perf_event_attr *attr)
546{
547 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600548
549 printf("%16" PRIx64, sample->addr);
550
551 if (!sample_addr_correlates_sym(attr))
552 return;
553
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300554 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600555
556 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900557 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900558 if (PRINT_FIELD(SYMOFFSET))
559 symbol__fprintf_symname_offs(al.sym, &al, stdout);
560 else
561 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600562 }
563
564 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900565 printf(" (");
566 map__fprintf_dsoname(al.map, stdout);
567 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600568 }
569}
570
Adrian Huntere2167082016-06-23 16:40:58 +0300571static void print_sample_callindent(struct perf_sample *sample,
572 struct perf_evsel *evsel,
573 struct thread *thread,
574 struct addr_location *al)
575{
576 struct perf_event_attr *attr = &evsel->attr;
577 size_t depth = thread_stack__depth(thread);
578 struct addr_location addr_al;
579 const char *name = NULL;
580 static int spacing;
581 int len = 0;
582 u64 ip = 0;
583
584 /*
585 * The 'return' has already been popped off the stack so the depth has
586 * to be adjusted to match the 'call'.
587 */
588 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
589 depth += 1;
590
591 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
592 if (sample_addr_correlates_sym(attr)) {
593 thread__resolve(thread, &addr_al, sample);
594 if (addr_al.sym)
595 name = addr_al.sym->name;
596 else
597 ip = sample->addr;
598 } else {
599 ip = sample->addr;
600 }
601 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
602 if (al->sym)
603 name = al->sym->name;
604 else
605 ip = sample->ip;
606 }
607
608 if (name)
609 len = printf("%*s%s", (int)depth * 4, "", name);
610 else if (ip)
611 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
612
613 if (len < 0)
614 return;
615
616 /*
617 * Try to keep the output length from changing frequently so that the
618 * output lines up more nicely.
619 */
620 if (len > spacing || (len && len < spacing - 52))
621 spacing = round_up(len + 4, 32);
622
623 if (len < spacing)
624 printf("%*s", spacing - len, "");
625}
626
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300627static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +0900628 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200629 struct thread *thread,
630 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900631{
632 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300633 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900634
Adrian Huntere2167082016-06-23 16:40:58 +0300635 if (PRINT_FIELD(CALLINDENT))
636 print_sample_callindent(sample, evsel, thread, al);
637
Akihiro Nagai95582592012-01-30 13:43:09 +0900638 /* print branch_from information */
639 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300640 unsigned int print_opts = output[attr->type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -0700641 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300642
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300643 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700644 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300645 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700646 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300647
648 if (cursor == NULL) {
649 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300650 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300651 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300652 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300653 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300654 } else
655 putchar('\n');
656
657 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +0900658 }
659
Akihiro Nagai95582592012-01-30 13:43:09 +0900660 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300661 if (PRINT_FIELD(ADDR) ||
662 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300663 !output[attr->type].user_set)) {
664 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300665 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300666 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900667
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300668 if (print_srcline_last)
669 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
670
Akihiro Nagai95582592012-01-30 13:43:09 +0900671 printf("\n");
672}
673
Adrian Hunter055cd332016-06-23 16:40:56 +0300674static struct {
675 u32 flags;
676 const char *name;
677} sample_flags[] = {
678 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
679 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
680 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
681 {PERF_IP_FLAG_BRANCH, "jmp"},
682 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
683 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
684 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
685 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
686 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
687 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
688 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
689 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
690 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
691 {0, NULL}
692};
693
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300694static void print_sample_flags(u32 flags)
695{
696 const char *chars = PERF_IP_FLAG_CHARS;
697 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +0300698 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
699 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300700 char str[33];
701 int i, pos = 0;
702
Adrian Hunter055cd332016-06-23 16:40:56 +0300703 for (i = 0; sample_flags[i].name ; i++) {
704 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
705 name = sample_flags[i].name;
706 break;
707 }
708 }
709
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300710 for (i = 0; i < n; i++, flags >>= 1) {
711 if (flags & 1)
712 str[pos++] = chars[i];
713 }
714 for (; i < 32; i++, flags >>= 1) {
715 if (flags & 1)
716 str[pos++] = '?';
717 }
718 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +0300719
720 if (name)
721 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
722 else
723 printf(" %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300724}
725
Wang Nan30372f02016-02-24 11:20:45 +0000726struct printer_data {
727 int line_no;
728 bool hit_nul;
729 bool is_printable;
730};
731
732static void
733print_sample_bpf_output_printer(enum binary_printer_ops op,
734 unsigned int val,
735 void *extra)
736{
737 unsigned char ch = (unsigned char)val;
738 struct printer_data *printer_data = extra;
739
740 switch (op) {
741 case BINARY_PRINT_DATA_BEGIN:
742 printf("\n");
743 break;
744 case BINARY_PRINT_LINE_BEGIN:
745 printf("%17s", !printer_data->line_no ? "BPF output:" :
746 " ");
747 break;
748 case BINARY_PRINT_ADDR:
749 printf(" %04x:", val);
750 break;
751 case BINARY_PRINT_NUM_DATA:
752 printf(" %02x", val);
753 break;
754 case BINARY_PRINT_NUM_PAD:
755 printf(" ");
756 break;
757 case BINARY_PRINT_SEP:
758 printf(" ");
759 break;
760 case BINARY_PRINT_CHAR_DATA:
761 if (printer_data->hit_nul && ch)
762 printer_data->is_printable = false;
763
764 if (!isprint(ch)) {
765 printf("%c", '.');
766
767 if (!printer_data->is_printable)
768 break;
769
770 if (ch == '\0')
771 printer_data->hit_nul = true;
772 else
773 printer_data->is_printable = false;
774 } else {
775 printf("%c", ch);
776 }
777 break;
778 case BINARY_PRINT_CHAR_PAD:
779 printf(" ");
780 break;
781 case BINARY_PRINT_LINE_END:
782 printf("\n");
783 printer_data->line_no++;
784 break;
785 case BINARY_PRINT_DATA_END:
786 default:
787 break;
788 }
789}
790
791static void print_sample_bpf_output(struct perf_sample *sample)
792{
793 unsigned int nr_bytes = sample->raw_size;
794 struct printer_data printer_data = {0, false, true};
795
796 print_binary(sample->raw_data, nr_bytes, 8,
797 print_sample_bpf_output_printer, &printer_data);
798
799 if (printer_data.is_printable && printer_data.hit_nul)
800 printf("%17s \"%s\"\n", "BPF string:",
801 (char *)(sample->raw_data));
802}
803
Jiri Olsa809e9422015-11-26 18:55:21 +0100804struct perf_script {
805 struct perf_tool tool;
806 struct perf_session *session;
807 bool show_task_events;
808 bool show_mmap_events;
809 bool show_switch_events;
Jiri Olsacfc88742016-01-05 22:09:06 +0100810 bool allocated;
811 struct cpu_map *cpus;
812 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100813 int name_width;
Jiri Olsa809e9422015-11-26 18:55:21 +0100814};
815
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100816static int perf_evlist__max_name_len(struct perf_evlist *evlist)
817{
818 struct perf_evsel *evsel;
819 int max = 0;
820
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300821 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100822 int len = strlen(perf_evsel__name(evsel));
823
824 max = MAX(len, max);
825 }
826
827 return max;
828}
829
Jiri Olsac19ac912016-02-24 09:46:54 +0100830static size_t data_src__printf(u64 data_src)
831{
832 struct mem_info mi = { .data_src.val = data_src };
833 char decode[100];
834 char out[100];
835 static int maxlen;
836 int len;
837
838 perf_script__meminfo_scnprintf(decode, 100, &mi);
839
840 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
841 if (maxlen < len)
842 maxlen = len;
843
844 return printf("%-*s", maxlen, out);
845}
846
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300847static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +0100848 struct perf_sample *sample, struct perf_evsel *evsel,
849 struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700850{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300851 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300852 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700853
David Ahern2c9e45f72011-03-17 10:03:21 -0600854 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700855 return;
856
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300857 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700858
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200859 if (PRINT_FIELD(PERIOD))
860 printf("%10" PRIu64 " ", sample->period);
861
Namhyung Kime944d3d2013-11-18 14:34:52 +0900862 if (PRINT_FIELD(EVNAME)) {
863 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100864
865 if (!script->name_width)
866 script->name_width = perf_evlist__max_name_len(script->session->evlist);
867
868 printf("%*s: ", script->name_width,
869 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +0900870 }
871
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300872 if (print_flags)
873 print_sample_flags(sample->flags);
874
Akihiro Nagai95582592012-01-30 13:43:09 +0900875 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300876 print_sample_bts(sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900877 return;
878 }
879
David Ahern745f43e2011-03-09 22:23:26 -0700880 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300881 event_format__print(evsel->tp_format, sample->cpu,
882 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600883 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300884 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600885
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100886 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +0100887 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100888
889 if (PRINT_FIELD(WEIGHT))
890 printf("%16" PRIu64, sample->weight);
891
David Ahern787bef12011-05-27 14:28:43 -0600892 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -0700893 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -0400894
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -0300895 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700896 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300897 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700898 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300899
900 putchar(cursor ? '\n' : ' ');
901 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -0700902 }
903
Stephane Eranianfc36f942015-08-31 18:41:10 +0200904 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300905 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +0200906
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200907 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300908 print_sample_brstack(sample);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200909 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300910 print_sample_brstacksym(sample, thread);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200911
Wang Nan30372f02016-02-24 11:20:45 +0000912 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
913 print_sample_bpf_output(sample);
914
David Ahernc70c94b2011-03-09 22:23:25 -0700915 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700916}
917
Tom Zanussi956ffd02009-11-25 01:15:46 -0600918static struct scripting_ops *scripting_ops;
919
Jiri Olsa36e33c52016-01-06 11:49:56 +0100920static void __process_stat(struct perf_evsel *counter, u64 tstamp)
921{
922 int nthreads = thread_map__nr(counter->threads);
923 int ncpus = perf_evsel__nr_cpus(counter);
924 int cpu, thread;
925 static int header_printed;
926
927 if (counter->system_wide)
928 nthreads = 1;
929
930 if (!header_printed) {
931 printf("%3s %8s %15s %15s %15s %15s %s\n",
932 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
933 header_printed = 1;
934 }
935
936 for (thread = 0; thread < nthreads; thread++) {
937 for (cpu = 0; cpu < ncpus; cpu++) {
938 struct perf_counts_values *counts;
939
940 counts = perf_counts(counter->counts, cpu, thread);
941
942 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
943 counter->cpus->map[cpu],
944 thread_map__pid(counter->threads, thread),
945 counts->val,
946 counts->ena,
947 counts->run,
948 tstamp,
949 perf_evsel__name(counter));
950 }
951 }
952}
953
Jiri Olsae099eba2016-01-05 22:09:09 +0100954static void process_stat(struct perf_evsel *counter, u64 tstamp)
955{
956 if (scripting_ops && scripting_ops->process_stat)
957 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +0100958 else
959 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +0100960}
961
962static void process_stat_interval(u64 tstamp)
963{
964 if (scripting_ops && scripting_ops->process_stat_interval)
965 scripting_ops->process_stat_interval(tstamp);
966}
967
Tom Zanussi956ffd02009-11-25 01:15:46 -0600968static void setup_scripting(void)
969{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600970 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600971 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -0600972}
973
Adrian Hunterd445dd22014-08-15 22:08:37 +0300974static int flush_scripting(void)
975{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100976 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +0300977}
978
Tom Zanussi956ffd02009-11-25 01:15:46 -0600979static int cleanup_scripting(void)
980{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100981 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500982
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100983 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600984}
985
Jiri Olsa809e9422015-11-26 18:55:21 +0100986static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200987 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200988 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300989 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200990 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200991{
Jiri Olsa809e9422015-11-26 18:55:21 +0100992 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -0700993 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200994
David Ahern1424dc92011-03-09 22:23:28 -0700995 if (debug_mode) {
996 if (sample->time < last_timestamp) {
997 pr_err("Samples misordered, previous: %" PRIu64
998 " this: %" PRIu64 "\n", last_timestamp,
999 sample->time);
1000 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001001 }
David Ahern1424dc92011-03-09 22:23:28 -07001002 last_timestamp = sample->time;
1003 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001004 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001005
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001006 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001007 pr_err("problem processing %d event, skipping it.\n",
1008 event->header.type);
1009 return -1;
1010 }
1011
1012 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001013 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001014
Anton Blanchard5d67be92011-07-04 21:57:50 +10001015 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001016 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001017
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001018 if (scripting_ops)
1019 scripting_ops->process_event(event, sample, evsel, &al);
1020 else
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001021 process_event(scr, sample, evsel, &al);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001022
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001023out_put:
1024 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001025 return 0;
1026}
1027
Adrian Hunter7ea95722013-11-01 15:51:30 +02001028static int process_attr(struct perf_tool *tool, union perf_event *event,
1029 struct perf_evlist **pevlist)
1030{
1031 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1032 struct perf_evlist *evlist;
1033 struct perf_evsel *evsel, *pos;
1034 int err;
1035
1036 err = perf_event__process_attr(tool, event, pevlist);
1037 if (err)
1038 return err;
1039
1040 evlist = *pevlist;
1041 evsel = perf_evlist__last(*pevlist);
1042
1043 if (evsel->attr.type >= PERF_TYPE_MAX)
1044 return 0;
1045
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001046 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001047 if (pos->attr.type == evsel->attr.type && pos != evsel)
1048 return 0;
1049 }
1050
1051 set_print_ip_opts(&evsel->attr);
1052
Jiri Olsad2b5a312015-10-16 12:41:25 +02001053 if (evsel->attr.sample_type)
1054 err = perf_evsel__check_attr(evsel, scr->session);
1055
1056 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001057}
1058
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001059static int process_comm_event(struct perf_tool *tool,
1060 union perf_event *event,
1061 struct perf_sample *sample,
1062 struct machine *machine)
1063{
1064 struct thread *thread;
1065 struct perf_script *script = container_of(tool, struct perf_script, tool);
1066 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001067 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001068 int ret = -1;
1069
1070 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1071 if (thread == NULL) {
1072 pr_debug("problem processing COMM event, skipping it.\n");
1073 return -1;
1074 }
1075
1076 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1077 goto out;
1078
1079 if (!evsel->attr.sample_id_all) {
1080 sample->cpu = 0;
1081 sample->time = 0;
1082 sample->tid = event->comm.tid;
1083 sample->pid = event->comm.pid;
1084 }
1085 print_sample_start(sample, thread, evsel);
1086 perf_event__fprintf(event, stdout);
1087 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001088out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001089 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001090 return ret;
1091}
1092
1093static int process_fork_event(struct perf_tool *tool,
1094 union perf_event *event,
1095 struct perf_sample *sample,
1096 struct machine *machine)
1097{
1098 struct thread *thread;
1099 struct perf_script *script = container_of(tool, struct perf_script, tool);
1100 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001101 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001102
1103 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1104 return -1;
1105
1106 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1107 if (thread == NULL) {
1108 pr_debug("problem processing FORK event, skipping it.\n");
1109 return -1;
1110 }
1111
1112 if (!evsel->attr.sample_id_all) {
1113 sample->cpu = 0;
1114 sample->time = event->fork.time;
1115 sample->tid = event->fork.tid;
1116 sample->pid = event->fork.pid;
1117 }
1118 print_sample_start(sample, thread, evsel);
1119 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001120 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001121
1122 return 0;
1123}
1124static int process_exit_event(struct perf_tool *tool,
1125 union perf_event *event,
1126 struct perf_sample *sample,
1127 struct machine *machine)
1128{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001129 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001130 struct thread *thread;
1131 struct perf_script *script = container_of(tool, struct perf_script, tool);
1132 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001133 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001134
1135 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1136 if (thread == NULL) {
1137 pr_debug("problem processing EXIT event, skipping it.\n");
1138 return -1;
1139 }
1140
1141 if (!evsel->attr.sample_id_all) {
1142 sample->cpu = 0;
1143 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001144 sample->tid = event->fork.tid;
1145 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001146 }
1147 print_sample_start(sample, thread, evsel);
1148 perf_event__fprintf(event, stdout);
1149
1150 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001151 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001152
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001153 thread__put(thread);
1154 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001155}
1156
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001157static int process_mmap_event(struct perf_tool *tool,
1158 union perf_event *event,
1159 struct perf_sample *sample,
1160 struct machine *machine)
1161{
1162 struct thread *thread;
1163 struct perf_script *script = container_of(tool, struct perf_script, tool);
1164 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001165 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001166
1167 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1168 return -1;
1169
1170 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1171 if (thread == NULL) {
1172 pr_debug("problem processing MMAP event, skipping it.\n");
1173 return -1;
1174 }
1175
1176 if (!evsel->attr.sample_id_all) {
1177 sample->cpu = 0;
1178 sample->time = 0;
1179 sample->tid = event->mmap.tid;
1180 sample->pid = event->mmap.pid;
1181 }
1182 print_sample_start(sample, thread, evsel);
1183 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001184 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001185 return 0;
1186}
1187
1188static int process_mmap2_event(struct perf_tool *tool,
1189 union perf_event *event,
1190 struct perf_sample *sample,
1191 struct machine *machine)
1192{
1193 struct thread *thread;
1194 struct perf_script *script = container_of(tool, struct perf_script, tool);
1195 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001196 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001197
1198 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1199 return -1;
1200
1201 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1202 if (thread == NULL) {
1203 pr_debug("problem processing MMAP2 event, skipping it.\n");
1204 return -1;
1205 }
1206
1207 if (!evsel->attr.sample_id_all) {
1208 sample->cpu = 0;
1209 sample->time = 0;
1210 sample->tid = event->mmap2.tid;
1211 sample->pid = event->mmap2.pid;
1212 }
1213 print_sample_start(sample, thread, evsel);
1214 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001215 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001216 return 0;
1217}
1218
Adrian Hunter7c148982015-07-21 12:44:06 +03001219static int process_switch_event(struct perf_tool *tool,
1220 union perf_event *event,
1221 struct perf_sample *sample,
1222 struct machine *machine)
1223{
1224 struct thread *thread;
1225 struct perf_script *script = container_of(tool, struct perf_script, tool);
1226 struct perf_session *session = script->session;
1227 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1228
1229 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1230 return -1;
1231
1232 thread = machine__findnew_thread(machine, sample->pid,
1233 sample->tid);
1234 if (thread == NULL) {
1235 pr_debug("problem processing SWITCH event, skipping it.\n");
1236 return -1;
1237 }
1238
1239 print_sample_start(sample, thread, evsel);
1240 perf_event__fprintf(event, stdout);
1241 thread__put(thread);
1242 return 0;
1243}
1244
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001245static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001246{
1247 session_done = 1;
1248}
1249
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001250static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001251{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001252 int ret;
1253
Tom Zanussic239da32010-04-01 23:59:18 -05001254 signal(SIGINT, sig_handler);
1255
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001256 /* override event processing functions */
1257 if (script->show_task_events) {
1258 script->tool.comm = process_comm_event;
1259 script->tool.fork = process_fork_event;
1260 script->tool.exit = process_exit_event;
1261 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001262 if (script->show_mmap_events) {
1263 script->tool.mmap = process_mmap_event;
1264 script->tool.mmap2 = process_mmap2_event;
1265 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001266 if (script->show_switch_events)
1267 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001268
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001269 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001270
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001271 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001272 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001273
1274 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001275}
1276
Tom Zanussi956ffd02009-11-25 01:15:46 -06001277struct script_spec {
1278 struct list_head node;
1279 struct scripting_ops *ops;
1280 char spec[0];
1281};
1282
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001283static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001284
1285static struct script_spec *script_spec__new(const char *spec,
1286 struct scripting_ops *ops)
1287{
1288 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1289
1290 if (s != NULL) {
1291 strcpy(s->spec, spec);
1292 s->ops = ops;
1293 }
1294
1295 return s;
1296}
1297
Tom Zanussi956ffd02009-11-25 01:15:46 -06001298static void script_spec__add(struct script_spec *s)
1299{
1300 list_add_tail(&s->node, &script_specs);
1301}
1302
1303static struct script_spec *script_spec__find(const char *spec)
1304{
1305 struct script_spec *s;
1306
1307 list_for_each_entry(s, &script_specs, node)
1308 if (strcasecmp(s->spec, spec) == 0)
1309 return s;
1310 return NULL;
1311}
1312
Tom Zanussi956ffd02009-11-25 01:15:46 -06001313int script_spec_register(const char *spec, struct scripting_ops *ops)
1314{
1315 struct script_spec *s;
1316
1317 s = script_spec__find(spec);
1318 if (s)
1319 return -1;
1320
Taeung Song8560bae2016-02-26 00:13:10 +09001321 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001322 if (!s)
1323 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001324 else
1325 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001326
1327 return 0;
1328}
1329
1330static struct scripting_ops *script_spec__lookup(const char *spec)
1331{
1332 struct script_spec *s = script_spec__find(spec);
1333 if (!s)
1334 return NULL;
1335
1336 return s->ops;
1337}
1338
1339static void list_available_languages(void)
1340{
1341 struct script_spec *s;
1342
1343 fprintf(stderr, "\n");
1344 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001345 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001346
1347 list_for_each_entry(s, &script_specs, node)
1348 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1349
1350 fprintf(stderr, "\n");
1351}
1352
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001353static int parse_scriptname(const struct option *opt __maybe_unused,
1354 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001355{
1356 char spec[PATH_MAX];
1357 const char *script, *ext;
1358 int len;
1359
Tom Zanussif526d682010-01-27 02:27:52 -06001360 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001361 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001362 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001363 }
1364
1365 script = strchr(str, ':');
1366 if (script) {
1367 len = script - str;
1368 if (len >= PATH_MAX) {
1369 fprintf(stderr, "invalid language specifier");
1370 return -1;
1371 }
1372 strncpy(spec, str, len);
1373 spec[len] = '\0';
1374 scripting_ops = script_spec__lookup(spec);
1375 if (!scripting_ops) {
1376 fprintf(stderr, "invalid language specifier");
1377 return -1;
1378 }
1379 script++;
1380 } else {
1381 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001382 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001383 if (!ext) {
1384 fprintf(stderr, "invalid script extension");
1385 return -1;
1386 }
1387 scripting_ops = script_spec__lookup(++ext);
1388 if (!scripting_ops) {
1389 fprintf(stderr, "invalid script extension");
1390 return -1;
1391 }
1392 }
1393
1394 script_name = strdup(script);
1395
1396 return 0;
1397}
1398
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001399static int parse_output_fields(const struct option *opt __maybe_unused,
1400 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001401{
1402 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001403 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001404 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001405 int rc = 0;
1406 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001407 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001408
1409 if (!str)
1410 return -ENOMEM;
1411
David Ahern2c9e45f72011-03-17 10:03:21 -06001412 /* first word can state for which event type the user is specifying
1413 * the fields. If no type exists, the specified fields apply to all
1414 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001415 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001416 tok = strchr(str, ':');
1417 if (tok) {
1418 *tok = '\0';
1419 tok++;
1420 if (!strcmp(str, "hw"))
1421 type = PERF_TYPE_HARDWARE;
1422 else if (!strcmp(str, "sw"))
1423 type = PERF_TYPE_SOFTWARE;
1424 else if (!strcmp(str, "trace"))
1425 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001426 else if (!strcmp(str, "raw"))
1427 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001428 else if (!strcmp(str, "break"))
1429 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001430 else {
1431 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001432 rc = -EINVAL;
1433 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001434 }
1435
1436 if (output[type].user_set)
1437 pr_warning("Overriding previous field request for %s events.\n",
1438 event_type(type));
1439
1440 output[type].fields = 0;
1441 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001442 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001443
1444 } else {
1445 tok = str;
1446 if (strlen(str) == 0) {
1447 fprintf(stderr,
1448 "Cannot set fields to 'none' for all event types.\n");
1449 rc = -EINVAL;
1450 goto out;
1451 }
1452
1453 if (output_set_by_user())
1454 pr_warning("Overriding previous field request for all events.\n");
1455
1456 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1457 output[j].fields = 0;
1458 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001459 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001460 }
David Ahern1424dc92011-03-09 22:23:28 -07001461 }
1462
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001463 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001464 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001465 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001466 break;
David Ahern745f43e2011-03-09 22:23:26 -07001467 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001468 if (i == imax && strcmp(tok, "flags") == 0) {
1469 print_flags = true;
1470 continue;
1471 }
David Ahern745f43e2011-03-09 22:23:26 -07001472 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001473 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001474 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001475 goto out;
1476 }
1477
1478 if (type == -1) {
1479 /* add user option to all events types for
1480 * which it is valid
1481 */
1482 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1483 if (output[j].invalid_fields & all_output_options[i].field) {
1484 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1485 all_output_options[i].str, event_type(j));
1486 } else
1487 output[j].fields |= all_output_options[i].field;
1488 }
1489 } else {
1490 if (output[type].invalid_fields & all_output_options[i].field) {
1491 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1492 all_output_options[i].str, event_type(type));
1493
1494 rc = -EINVAL;
1495 goto out;
1496 }
1497 output[type].fields |= all_output_options[i].field;
1498 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001499 }
1500
1501 if (type >= 0) {
1502 if (output[type].fields == 0) {
1503 pr_debug("No fields requested for %s type. "
1504 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001505 }
David Ahern1424dc92011-03-09 22:23:28 -07001506 }
David Ahern745f43e2011-03-09 22:23:26 -07001507
David Ahern2c9e45f72011-03-17 10:03:21 -06001508out:
David Ahern745f43e2011-03-09 22:23:26 -07001509 free(str);
1510 return rc;
1511}
1512
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001513/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1514static int is_directory(const char *base_path, const struct dirent *dent)
1515{
1516 char path[PATH_MAX];
1517 struct stat st;
1518
1519 sprintf(path, "%s/%s", base_path, dent->d_name);
1520 if (stat(path, &st))
1521 return 0;
1522
1523 return S_ISDIR(st.st_mode);
1524}
1525
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001526#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1527 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1528 if ((lang_dirent->d_type == DT_DIR || \
1529 (lang_dirent->d_type == DT_UNKNOWN && \
1530 is_directory(scripts_path, lang_dirent))) && \
1531 (strcmp(lang_dirent->d_name, ".")) && \
1532 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001533
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001534#define for_each_script(lang_path, lang_dir, script_dirent) \
1535 while ((script_dirent = readdir(lang_dir)) != NULL) \
1536 if (script_dirent->d_type != DT_DIR && \
1537 (script_dirent->d_type != DT_UNKNOWN || \
1538 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001539
1540
1541#define RECORD_SUFFIX "-record"
1542#define REPORT_SUFFIX "-report"
1543
1544struct script_desc {
1545 struct list_head node;
1546 char *name;
1547 char *half_liner;
1548 char *args;
1549};
1550
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001551static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001552
1553static struct script_desc *script_desc__new(const char *name)
1554{
1555 struct script_desc *s = zalloc(sizeof(*s));
1556
Tom Zanussib5b87312010-11-10 08:16:51 -06001557 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001558 s->name = strdup(name);
1559
1560 return s;
1561}
1562
1563static void script_desc__delete(struct script_desc *s)
1564{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001565 zfree(&s->name);
1566 zfree(&s->half_liner);
1567 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001568 free(s);
1569}
1570
1571static void script_desc__add(struct script_desc *s)
1572{
1573 list_add_tail(&s->node, &script_descs);
1574}
1575
1576static struct script_desc *script_desc__find(const char *name)
1577{
1578 struct script_desc *s;
1579
1580 list_for_each_entry(s, &script_descs, node)
1581 if (strcasecmp(s->name, name) == 0)
1582 return s;
1583 return NULL;
1584}
1585
1586static struct script_desc *script_desc__findnew(const char *name)
1587{
1588 struct script_desc *s = script_desc__find(name);
1589
1590 if (s)
1591 return s;
1592
1593 s = script_desc__new(name);
1594 if (!s)
1595 goto out_delete_desc;
1596
1597 script_desc__add(s);
1598
1599 return s;
1600
1601out_delete_desc:
1602 script_desc__delete(s);
1603
1604 return NULL;
1605}
1606
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001607static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001608{
1609 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001610 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001611
1612 if (strlen(str) > suffix_len) {
1613 p = str + strlen(str) - suffix_len;
1614 if (!strncmp(p, suffix, suffix_len))
1615 return p;
1616 }
1617
1618 return NULL;
1619}
1620
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001621static int read_script_info(struct script_desc *desc, const char *filename)
1622{
1623 char line[BUFSIZ], *p;
1624 FILE *fp;
1625
1626 fp = fopen(filename, "r");
1627 if (!fp)
1628 return -1;
1629
1630 while (fgets(line, sizeof(line), fp)) {
1631 p = ltrim(line);
1632 if (strlen(p) == 0)
1633 continue;
1634 if (*p != '#')
1635 continue;
1636 p++;
1637 if (strlen(p) && *p == '!')
1638 continue;
1639
1640 p = ltrim(p);
1641 if (strlen(p) && p[strlen(p) - 1] == '\n')
1642 p[strlen(p) - 1] = '\0';
1643
1644 if (!strncmp(p, "description:", strlen("description:"))) {
1645 p += strlen("description:");
1646 desc->half_liner = strdup(ltrim(p));
1647 continue;
1648 }
1649
1650 if (!strncmp(p, "args:", strlen("args:"))) {
1651 p += strlen("args:");
1652 desc->args = strdup(ltrim(p));
1653 continue;
1654 }
1655 }
1656
1657 fclose(fp);
1658
1659 return 0;
1660}
1661
Robert Richter38efb532011-11-25 11:38:40 +01001662static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1663{
1664 char *script_root, *str;
1665
1666 script_root = strdup(script_dirent->d_name);
1667 if (!script_root)
1668 return NULL;
1669
1670 str = (char *)ends_with(script_root, suffix);
1671 if (!str) {
1672 free(script_root);
1673 return NULL;
1674 }
1675
1676 *str = '\0';
1677 return script_root;
1678}
1679
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001680static int list_available_scripts(const struct option *opt __maybe_unused,
1681 const char *s __maybe_unused,
1682 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001683{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001684 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001685 char scripts_path[MAXPATHLEN];
1686 DIR *scripts_dir, *lang_dir;
1687 char script_path[MAXPATHLEN];
1688 char lang_path[MAXPATHLEN];
1689 struct script_desc *desc;
1690 char first_half[BUFSIZ];
1691 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001692
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001693 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001694
1695 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00001696 if (!scripts_dir) {
1697 fprintf(stdout,
1698 "open(%s) failed.\n"
1699 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
1700 scripts_path);
1701 exit(-1);
1702 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001703
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001704 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001705 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001706 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001707 lang_dir = opendir(lang_path);
1708 if (!lang_dir)
1709 continue;
1710
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001711 for_each_script(lang_path, lang_dir, script_dirent) {
1712 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01001713 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001714 desc = script_desc__findnew(script_root);
1715 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001716 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001717 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001718 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001719 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001720 }
1721 }
1722
1723 fprintf(stdout, "List of available trace scripts:\n");
1724 list_for_each_entry(desc, &script_descs, node) {
1725 sprintf(first_half, "%s %s", desc->name,
1726 desc->args ? desc->args : "");
1727 fprintf(stdout, " %-36s %s\n", first_half,
1728 desc->half_liner ? desc->half_liner : "");
1729 }
1730
1731 exit(0);
1732}
1733
Feng Tange5f37052012-09-07 16:42:26 +08001734/*
Feng Tang49e639e2012-10-30 11:56:03 +08001735 * Some scripts specify the required events in their "xxx-record" file,
1736 * this function will check if the events in perf.data match those
1737 * mentioned in the "xxx-record".
1738 *
1739 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1740 * which is covered well now. And new parsing code should be added to
1741 * cover the future complexing formats like event groups etc.
1742 */
1743static int check_ev_match(char *dir_name, char *scriptname,
1744 struct perf_session *session)
1745{
1746 char filename[MAXPATHLEN], evname[128];
1747 char line[BUFSIZ], *p;
1748 struct perf_evsel *pos;
1749 int match, len;
1750 FILE *fp;
1751
1752 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1753
1754 fp = fopen(filename, "r");
1755 if (!fp)
1756 return -1;
1757
1758 while (fgets(line, sizeof(line), fp)) {
1759 p = ltrim(line);
1760 if (*p == '#')
1761 continue;
1762
1763 while (strlen(p)) {
1764 p = strstr(p, "-e");
1765 if (!p)
1766 break;
1767
1768 p += 2;
1769 p = ltrim(p);
1770 len = strcspn(p, " \t");
1771 if (!len)
1772 break;
1773
1774 snprintf(evname, len + 1, "%s", p);
1775
1776 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001777 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001778 if (!strcmp(perf_evsel__name(pos), evname)) {
1779 match = 1;
1780 break;
1781 }
1782 }
1783
1784 if (!match) {
1785 fclose(fp);
1786 return -1;
1787 }
1788 }
1789 }
1790
1791 fclose(fp);
1792 return 0;
1793}
1794
1795/*
Feng Tange5f37052012-09-07 16:42:26 +08001796 * Return -1 if none is found, otherwise the actual scripts number.
1797 *
1798 * Currently the only user of this function is the script browser, which
1799 * will list all statically runnable scripts, select one, execute it and
1800 * show the output in a perf browser.
1801 */
1802int find_scripts(char **scripts_array, char **scripts_path_array)
1803{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001804 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001805 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001806 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001807 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001808 struct perf_data_file file = {
1809 .path = input_name,
1810 .mode = PERF_DATA_MODE_READ,
1811 };
Feng Tange5f37052012-09-07 16:42:26 +08001812 char *temp;
1813 int i = 0;
1814
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001815 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001816 if (!session)
1817 return -1;
1818
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001819 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08001820
1821 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001822 if (!scripts_dir) {
1823 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001824 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001825 }
Feng Tange5f37052012-09-07 16:42:26 +08001826
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001827 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08001828 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001829 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08001830#ifdef NO_LIBPERL
1831 if (strstr(lang_path, "perl"))
1832 continue;
1833#endif
1834#ifdef NO_LIBPYTHON
1835 if (strstr(lang_path, "python"))
1836 continue;
1837#endif
1838
1839 lang_dir = opendir(lang_path);
1840 if (!lang_dir)
1841 continue;
1842
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001843 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08001844 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001845 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08001846 continue;
1847 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001848 script_dirent->d_name);
1849 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08001850 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001851 (temp - script_dirent->d_name) + 1,
1852 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001853
1854 if (check_ev_match(lang_path,
1855 scripts_array[i], session))
1856 continue;
1857
Feng Tange5f37052012-09-07 16:42:26 +08001858 i++;
1859 }
Feng Tang49e639e2012-10-30 11:56:03 +08001860 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001861 }
1862
Feng Tang49e639e2012-10-30 11:56:03 +08001863 closedir(scripts_dir);
1864 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001865 return i;
1866}
1867
Tom Zanussi38752942009-12-15 02:53:39 -06001868static char *get_script_path(const char *script_root, const char *suffix)
1869{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001870 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06001871 char scripts_path[MAXPATHLEN];
1872 char script_path[MAXPATHLEN];
1873 DIR *scripts_dir, *lang_dir;
1874 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001875 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001876
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001877 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06001878
1879 scripts_dir = opendir(scripts_path);
1880 if (!scripts_dir)
1881 return NULL;
1882
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001883 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06001884 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001885 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06001886 lang_dir = opendir(lang_path);
1887 if (!lang_dir)
1888 continue;
1889
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001890 for_each_script(lang_path, lang_dir, script_dirent) {
1891 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01001892 if (__script_root && !strcmp(script_root, __script_root)) {
1893 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001894 closedir(lang_dir);
1895 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001896 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001897 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001898 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001899 }
1900 free(__script_root);
1901 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001902 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001903 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001904 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001905
Robert Richter38efb532011-11-25 11:38:40 +01001906 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001907}
1908
Tom Zanussib5b87312010-11-10 08:16:51 -06001909static bool is_top_script(const char *script_path)
1910{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001911 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001912}
1913
1914static int has_required_arg(char *script_path)
1915{
1916 struct script_desc *desc;
1917 int n_args = 0;
1918 char *p;
1919
1920 desc = script_desc__new(NULL);
1921
1922 if (read_script_info(desc, script_path))
1923 goto out;
1924
1925 if (!desc->args)
1926 goto out;
1927
1928 for (p = desc->args; *p; p++)
1929 if (*p == '<')
1930 n_args++;
1931out:
1932 script_desc__delete(desc);
1933
1934 return n_args;
1935}
1936
David Ahernd54b1a92012-08-26 12:24:46 -06001937static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001938{
1939 char **__argv = malloc(sizeof(const char *) * argc);
1940
David Ahernd54b1a92012-08-26 12:24:46 -06001941 if (!__argv) {
1942 pr_err("malloc failed\n");
1943 return -1;
1944 }
1945
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001946 memcpy(__argv, argv, sizeof(const char *) * argc);
1947 argc = parse_options(argc, (const char **)__argv, record_options,
1948 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1949 free(__argv);
1950
David Ahernd54b1a92012-08-26 12:24:46 -06001951 system_wide = (argc == 0);
1952
1953 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001954}
1955
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001956static void script__setup_sample_type(struct perf_script *script)
1957{
1958 struct perf_session *session = script->session;
1959 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1960
1961 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1962 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1963 (sample_type & PERF_SAMPLE_STACK_USER))
1964 callchain_param.record_mode = CALLCHAIN_DWARF;
1965 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1966 callchain_param.record_mode = CALLCHAIN_LBR;
1967 else
1968 callchain_param.record_mode = CALLCHAIN_FP;
1969 }
1970}
1971
Jiri Olsae099eba2016-01-05 22:09:09 +01001972static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1973 union perf_event *event,
1974 struct perf_session *session)
1975{
1976 struct stat_round_event *round = &event->stat_round;
1977 struct perf_evsel *counter;
1978
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001979 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01001980 perf_stat_process_counter(&stat_config, counter);
1981 process_stat(counter, round->time);
1982 }
1983
1984 process_stat_interval(round->time);
1985 return 0;
1986}
1987
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01001988static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1989 union perf_event *event,
1990 struct perf_session *session __maybe_unused)
1991{
1992 perf_event__read_stat_config(&stat_config, &event->stat_config);
1993 return 0;
1994}
1995
Jiri Olsacfc88742016-01-05 22:09:06 +01001996static int set_maps(struct perf_script *script)
1997{
1998 struct perf_evlist *evlist = script->session->evlist;
1999
2000 if (!script->cpus || !script->threads)
2001 return 0;
2002
2003 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2004 return -EINVAL;
2005
2006 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2007
2008 if (perf_evlist__alloc_stats(evlist, true))
2009 return -ENOMEM;
2010
2011 script->allocated = true;
2012 return 0;
2013}
2014
2015static
2016int process_thread_map_event(struct perf_tool *tool,
2017 union perf_event *event,
2018 struct perf_session *session __maybe_unused)
2019{
2020 struct perf_script *script = container_of(tool, struct perf_script, tool);
2021
2022 if (script->threads) {
2023 pr_warning("Extra thread map event, ignoring.\n");
2024 return 0;
2025 }
2026
2027 script->threads = thread_map__new_event(&event->thread_map);
2028 if (!script->threads)
2029 return -ENOMEM;
2030
2031 return set_maps(script);
2032}
2033
2034static
2035int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2036 union perf_event *event,
2037 struct perf_session *session __maybe_unused)
2038{
2039 struct perf_script *script = container_of(tool, struct perf_script, tool);
2040
2041 if (script->cpus) {
2042 pr_warning("Extra cpu map event, ignoring.\n");
2043 return 0;
2044 }
2045
2046 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2047 if (!script->cpus)
2048 return -ENOMEM;
2049
2050 return set_maps(script);
2051}
2052
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002053int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002054{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002055 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002056 bool header = false;
2057 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002058 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002059 char *rec_script_path = NULL;
2060 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002061 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002062 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002063 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002064 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002065 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002066 struct perf_script script = {
2067 .tool = {
2068 .sample = process_sample_event,
2069 .mmap = perf_event__process_mmap,
2070 .mmap2 = perf_event__process_mmap2,
2071 .comm = perf_event__process_comm,
2072 .exit = perf_event__process_exit,
2073 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002074 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002075 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002076 .tracing_data = perf_event__process_tracing_data,
2077 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002078 .id_index = perf_event__process_id_index,
2079 .auxtrace_info = perf_event__process_auxtrace_info,
2080 .auxtrace = perf_event__process_auxtrace,
2081 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002082 .stat = perf_event__process_stat_event,
2083 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002084 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002085 .thread_map = process_thread_map_event,
2086 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002087 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002088 .ordering_requires_timestamps = true,
2089 },
2090 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002091 struct perf_data_file file = {
2092 .mode = PERF_DATA_MODE_READ,
2093 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002094 const struct option options[] = {
2095 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2096 "dump raw trace in ASCII"),
2097 OPT_INCR('v', "verbose", &verbose,
2098 "be more verbose (show symbol address, etc)"),
2099 OPT_BOOLEAN('L', "Latency", &latency_format,
2100 "show latency attributes (irqs/preemption disabled, etc)"),
2101 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2102 list_available_scripts),
2103 OPT_CALLBACK('s', "script", NULL, "name",
2104 "script file name (lang:script name, script name, or *)",
2105 parse_scriptname),
2106 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2107 "generate perf-script.xx script in specified language"),
2108 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2109 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2110 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002111 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2112 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002113 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2114 "file", "vmlinux pathname"),
2115 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2116 "file", "kallsyms pathname"),
2117 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2118 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002119 OPT_CALLBACK(0, "symfs", NULL, "directory",
2120 "Look for files with symbols relative to this directory",
2121 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002122 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002123 "comma separated output fields prepend with 'type:'. "
2124 "Valid types: hw,sw,trace,raw. "
2125 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Huntere2167082016-06-23 16:40:58 +03002126 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
Brendan Greggbcdc09a2016-08-03 02:47:49 +00002127 "bpf-output,callindent", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002128 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2129 "system-wide collection from all CPUs"),
2130 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2131 "only consider these symbols"),
2132 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2133 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2134 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002135 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2136 "only consider symbols in these pids"),
2137 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2138 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002139 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2140 "Set the maximum stack depth when parsing the callchain, "
2141 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002142 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002143 OPT_BOOLEAN('I', "show-info", &show_full_info,
2144 "display extended information from perf.data file"),
2145 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2146 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002147 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2148 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002149 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2150 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002151 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2152 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08002153 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002154 OPT_BOOLEAN(0, "ns", &nanosecs,
2155 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002156 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2157 "Instruction Tracing options",
2158 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002159 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2160 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002161 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2162 "Enable symbol demangling"),
2163 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2164 "Enable kernel symbol demangling"),
2165
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002166 OPT_END()
2167 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002168 const char * const script_subcommands[] = { "record", "report", NULL };
2169 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002170 "perf script [<options>]",
2171 "perf script [<options>] record <script> [<record-options>] <command>",
2172 "perf script [<options>] report <script> [script-args]",
2173 "perf script [<options>] <script> [<record-options>] <command>",
2174 "perf script [<options>] <top-script> [script-args]",
2175 NULL
2176 };
Tom Zanussi38752942009-12-15 02:53:39 -06002177
Tom Zanussib5b87312010-11-10 08:16:51 -06002178 setup_scripting();
2179
Yunlong Song40cae2b2015-03-18 21:35:54 +08002180 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002181 PARSE_OPT_STOP_AT_NON_OPTION);
2182
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002183 file.path = input_name;
2184
Tom Zanussib5b87312010-11-10 08:16:51 -06002185 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2186 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2187 if (!rec_script_path)
2188 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06002189 }
2190
Tom Zanussib5b87312010-11-10 08:16:51 -06002191 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2192 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2193 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002194 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002195 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002196 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002197 return -1;
2198 }
Tom Zanussi38752942009-12-15 02:53:39 -06002199 }
2200
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002201 if (itrace_synth_opts.callchain &&
2202 itrace_synth_opts.callchain_sz > scripting_max_stack)
2203 scripting_max_stack = itrace_synth_opts.callchain_sz;
2204
Ben Hutchings44e668c2010-10-10 16:10:03 +01002205 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002206 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002207
Tom Zanussib5b87312010-11-10 08:16:51 -06002208 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002209 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002210 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002211 pid_t pid;
2212
Tom Zanussib5b87312010-11-10 08:16:51 -06002213 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2214 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2215
2216 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002217 usage_with_options_msg(script_usage, options,
2218 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002219 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002220 }
2221
Tom Zanussib5b87312010-11-10 08:16:51 -06002222 if (is_top_script(argv[0])) {
2223 rep_args = argc - 1;
2224 } else {
2225 int rec_args;
2226
2227 rep_args = has_required_arg(rep_script_path);
2228 rec_args = (argc - 1) - rep_args;
2229 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002230 usage_with_options_msg(script_usage, options,
2231 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002232 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002233 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002234 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002235 }
2236
2237 if (pipe(live_pipe) < 0) {
2238 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002239 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002240 }
2241
2242 pid = fork();
2243 if (pid < 0) {
2244 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002245 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002246 }
2247
2248 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002249 j = 0;
2250
Tom Zanussia0cccc22010-04-01 23:59:25 -05002251 dup2(live_pipe[1], 1);
2252 close(live_pipe[0]);
2253
Robert Richter317df652011-11-25 15:05:25 +01002254 if (is_top_script(argv[0])) {
2255 system_wide = true;
2256 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002257 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2258 err = -1;
2259 goto out;
2260 }
Robert Richter317df652011-11-25 15:05:25 +01002261 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002262
2263 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002264 if (!__argv) {
2265 pr_err("malloc failed\n");
2266 err = -ENOMEM;
2267 goto out;
2268 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002269
Tom Zanussib5b87312010-11-10 08:16:51 -06002270 __argv[j++] = "/bin/sh";
2271 __argv[j++] = rec_script_path;
2272 if (system_wide)
2273 __argv[j++] = "-a";
2274 __argv[j++] = "-q";
2275 __argv[j++] = "-o";
2276 __argv[j++] = "-";
2277 for (i = rep_args + 1; i < argc; i++)
2278 __argv[j++] = argv[i];
2279 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002280
2281 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002282 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002283 exit(-1);
2284 }
2285
2286 dup2(live_pipe[0], 0);
2287 close(live_pipe[1]);
2288
Tom Zanussib5b87312010-11-10 08:16:51 -06002289 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002290 if (!__argv) {
2291 pr_err("malloc failed\n");
2292 err = -ENOMEM;
2293 goto out;
2294 }
2295
Tom Zanussib5b87312010-11-10 08:16:51 -06002296 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002297 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002298 __argv[j++] = rep_script_path;
2299 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002300 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002301 __argv[j++] = "-i";
2302 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002303 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002304
2305 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002306 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002307 exit(-1);
2308 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002309
Tom Zanussib5b87312010-11-10 08:16:51 -06002310 if (rec_script_path)
2311 script_path = rec_script_path;
2312 if (rep_script_path)
2313 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002314
Tom Zanussib5b87312010-11-10 08:16:51 -06002315 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002316 j = 0;
2317
Robert Richter317df652011-11-25 15:05:25 +01002318 if (!rec_script_path)
2319 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002320 else if (!system_wide) {
2321 if (have_cmd(argc - 1, &argv[1]) != 0) {
2322 err = -1;
2323 goto out;
2324 }
2325 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002326
2327 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002328 if (!__argv) {
2329 pr_err("malloc failed\n");
2330 err = -ENOMEM;
2331 goto out;
2332 }
2333
Tom Zanussib5b87312010-11-10 08:16:51 -06002334 __argv[j++] = "/bin/sh";
2335 __argv[j++] = script_path;
2336 if (system_wide)
2337 __argv[j++] = "-a";
2338 for (i = 2; i < argc; i++)
2339 __argv[j++] = argv[i];
2340 __argv[j++] = NULL;
2341
2342 execvp("/bin/sh", (char **)__argv);
2343 free(__argv);
2344 exit(-1);
2345 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002346
Tom Zanussicf4fee52010-03-03 01:04:33 -06002347 if (!script_name)
2348 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002349
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002350 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002351 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002352 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002353
Jiri Olsae90debd2013-12-09 11:02:50 +01002354 if (header || header_only) {
2355 perf_session__fprintf_info(session, stdout, show_full_info);
2356 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002357 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002358 }
2359
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002360 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002361 goto out_delete;
2362
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002363 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002364 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002365
Adrian Huntere2167082016-06-23 16:40:58 +03002366 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2367 itrace_synth_opts.thread_stack = true;
2368
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002369 session->itrace_synth_opts = &itrace_synth_opts;
2370
Anton Blanchard5d67be92011-07-04 21:57:50 +10002371 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002372 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2373 if (err < 0)
2374 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002375 }
2376
David Ahern1424dc92011-03-09 22:23:28 -07002377 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002378 symbol_conf.use_callchain = true;
2379 else
2380 symbol_conf.use_callchain = false;
2381
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002382 if (session->tevent.pevent &&
2383 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002384 machine__resolve_kernel_addr,
2385 &session->machines.host) < 0) {
2386 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2387 return -1;
2388 }
2389
Tom Zanussi956ffd02009-11-25 01:15:46 -06002390 if (generate_script_lang) {
2391 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002392 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002393
David Ahern2c9e45f72011-03-17 10:03:21 -06002394 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002395 fprintf(stderr,
2396 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002397 err = -EINVAL;
2398 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002399 }
2400
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002401 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002402 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002403 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002404 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002405 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002406 }
2407
2408 err = fstat(input, &perf_stat);
2409 if (err < 0) {
2410 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002411 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002412 }
2413
2414 if (!perf_stat.st_size) {
2415 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002416 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002417 }
2418
2419 scripting_ops = script_spec__lookup(generate_script_lang);
2420 if (!scripting_ops) {
2421 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002422 err = -ENOENT;
2423 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002424 }
2425
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002426 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002427 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002428 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002429 }
2430
2431 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002432 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002433 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002434 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002435 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002436 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002437 }
2438
David Ahern9cbdb702011-04-06 21:54:20 -06002439
2440 err = perf_session__check_output_opt(session);
2441 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002442 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002443
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002444 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002445
Adrian Hunterd445dd22014-08-15 22:08:37 +03002446 flush_scripting();
2447
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002448out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002449 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002450 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002451
2452 if (script_started)
2453 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002454out:
2455 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002456}