blob: d05aec491cff22189d7fe763120884a639cd2cfb [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#include "builtin.h"
2
Andrea Gelminib7eead82010-08-05 15:51:38 +02003#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02004#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02005#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06006#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02007#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06008#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +02009#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020010#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020011#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020012#include "util/symbol.h"
13#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010014#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020015#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070016#include "util/evlist.h"
17#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080018#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030020#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010021#include "util/cpumap.h"
22#include "util/thread_map.h"
23#include "util/stat.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030024#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030025#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070026#include "util/time-utils.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030027#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100028#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030029#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030030#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030031#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010032#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010033#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080034#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030035#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030036#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030037#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030038#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030039#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030040#include <sys/types.h>
41#include <sys/stat.h>
42#include <unistd.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020043
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030044#include "sane_ctype.h"
45
Tom Zanussi956ffd02009-11-25 01:15:46 -060046static char const *script_name;
47static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020048static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020049static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020050static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070051static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090052static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010053static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030054static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030055static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100056static const char *cpu_list;
57static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010058static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080059static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060060
Adrian Hunter44cbe722015-09-25 16:15:50 +030061unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030062
David Ahern745f43e2011-03-09 22:23:26 -070063enum perf_output_field {
64 PERF_OUTPUT_COMM = 1U << 0,
65 PERF_OUTPUT_TID = 1U << 1,
66 PERF_OUTPUT_PID = 1U << 2,
67 PERF_OUTPUT_TIME = 1U << 3,
68 PERF_OUTPUT_CPU = 1U << 4,
69 PERF_OUTPUT_EVNAME = 1U << 5,
70 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060071 PERF_OUTPUT_IP = 1U << 7,
72 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060073 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060074 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090075 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020076 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020077 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020078 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020079 PERF_OUTPUT_BRSTACK = 1U << 15,
80 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010081 PERF_OUTPUT_DATA_SRC = 1U << 17,
82 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000083 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030084 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030085 PERF_OUTPUT_INSN = 1U << 21,
86 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080087 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
David Ahern745f43e2011-03-09 22:23:26 -070088};
89
90struct output_option {
91 const char *str;
92 enum perf_output_field field;
93} all_output_options[] = {
94 {.str = "comm", .field = PERF_OUTPUT_COMM},
95 {.str = "tid", .field = PERF_OUTPUT_TID},
96 {.str = "pid", .field = PERF_OUTPUT_PID},
97 {.str = "time", .field = PERF_OUTPUT_TIME},
98 {.str = "cpu", .field = PERF_OUTPUT_CPU},
99 {.str = "event", .field = PERF_OUTPUT_EVNAME},
100 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600101 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700102 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600103 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600104 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900105 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200106 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200107 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200108 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200109 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
110 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100111 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
112 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000113 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300114 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300115 {.str = "insn", .field = PERF_OUTPUT_INSN},
116 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800117 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
David Ahern745f43e2011-03-09 22:23:26 -0700118};
119
120/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600121static struct {
122 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600123 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400124 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600125 u64 fields;
126 u64 invalid_fields;
127} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700128
David Ahern2c9e45f72011-03-17 10:03:21 -0600129 [PERF_TYPE_HARDWARE] = {
130 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700131
David Ahern2c9e45f72011-03-17 10:03:21 -0600132 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
133 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600134 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200135 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
136 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600137
Wang Nan30372f02016-02-24 11:20:45 +0000138 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600139 },
140
141 [PERF_TYPE_SOFTWARE] = {
142 .user_set = false,
143
144 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
145 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600146 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200147 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000148 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600149
150 .invalid_fields = PERF_OUTPUT_TRACE,
151 },
152
153 [PERF_TYPE_TRACEPOINT] = {
154 .user_set = false,
155
156 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
157 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000158 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600159 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700160
161 [PERF_TYPE_RAW] = {
162 .user_set = false,
163
164 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
165 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600166 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200167 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100168 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
169 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700170
Wang Nan30372f02016-02-24 11:20:45 +0000171 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700172 },
Wang Nan27cfef02015-12-08 02:25:43 +0000173
174 [PERF_TYPE_BREAKPOINT] = {
175 .user_set = false,
176
177 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
178 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
179 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
180 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
181 PERF_OUTPUT_PERIOD,
182
Wang Nan30372f02016-02-24 11:20:45 +0000183 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000184 },
David Ahern1424dc92011-03-09 22:23:28 -0700185};
David Ahern745f43e2011-03-09 22:23:26 -0700186
David Ahern2c9e45f72011-03-17 10:03:21 -0600187static bool output_set_by_user(void)
188{
189 int j;
190 for (j = 0; j < PERF_TYPE_MAX; ++j) {
191 if (output[j].user_set)
192 return true;
193 }
194 return false;
195}
David Ahern745f43e2011-03-09 22:23:26 -0700196
David Ahern9cbdb702011-04-06 21:54:20 -0600197static const char *output_field2str(enum perf_output_field field)
198{
199 int i, imax = ARRAY_SIZE(all_output_options);
200 const char *str = "";
201
202 for (i = 0; i < imax; ++i) {
203 if (all_output_options[i].field == field) {
204 str = all_output_options[i].str;
205 break;
206 }
207 }
208 return str;
209}
210
David Ahern2c9e45f72011-03-17 10:03:21 -0600211#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700212
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300213static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
214 u64 sample_type, const char *sample_msg,
215 enum perf_output_field field,
216 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700217{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300218 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600219 int type = attr->type;
220 const char *evname;
221
222 if (attr->sample_type & sample_type)
223 return 0;
224
225 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300226 if (allow_user_set)
227 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300228 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600229 pr_err("Samples for '%s' event do not have %s attribute set. "
230 "Cannot print '%s' field.\n",
231 evname, sample_msg, output_field2str(field));
232 return -1;
233 }
234
235 /* user did not ask for it explicitly so remove from the default list */
236 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300237 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600238 pr_debug("Samples for '%s' event do not have %s attribute set. "
239 "Skipping '%s' field.\n",
240 evname, sample_msg, output_field2str(field));
241
242 return 0;
243}
244
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300245static int perf_evsel__check_stype(struct perf_evsel *evsel,
246 u64 sample_type, const char *sample_msg,
247 enum perf_output_field field)
248{
249 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
250 false);
251}
252
David Ahern9cbdb702011-04-06 21:54:20 -0600253static int perf_evsel__check_attr(struct perf_evsel *evsel,
254 struct perf_session *session)
255{
256 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300257 bool allow_user_set;
258
Jiri Olsae099eba2016-01-05 22:09:09 +0100259 if (perf_header__has_feat(&session->header, HEADER_STAT))
260 return 0;
261
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300262 allow_user_set = perf_header__has_feat(&session->header,
263 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600264
David Ahern1424dc92011-03-09 22:23:28 -0700265 if (PRINT_FIELD(TRACE) &&
266 !perf_session__has_traces(session, "record -R"))
267 return -EINVAL;
268
David Ahern787bef12011-05-27 14:28:43 -0600269 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300270 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
271 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700272 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700273 }
David Ahern7cec0922011-05-30 13:08:23 -0600274
275 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300276 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
277 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600278 return -EINVAL;
279
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100280 if (PRINT_FIELD(DATA_SRC) &&
281 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
282 PERF_OUTPUT_DATA_SRC))
283 return -EINVAL;
284
285 if (PRINT_FIELD(WEIGHT) &&
286 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
287 PERF_OUTPUT_WEIGHT))
288 return -EINVAL;
289
David Ahern7cec0922011-05-30 13:08:23 -0600290 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
291 pr_err("Display of symbols requested but neither sample IP nor "
292 "sample address\nis selected. Hence, no addresses to convert "
293 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600294 return -EINVAL;
295 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900296 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
297 pr_err("Display of offsets requested but symbol is not"
298 "selected.\n");
299 return -EINVAL;
300 }
David Ahern7cec0922011-05-30 13:08:23 -0600301 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
302 pr_err("Display of DSO requested but neither sample IP nor "
303 "sample address\nis selected. Hence, no addresses to convert "
304 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600305 return -EINVAL;
306 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200307 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
308 pr_err("Display of source line number requested but sample IP is not\n"
309 "selected. Hence, no address to lookup the source line number.\n");
310 return -EINVAL;
311 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800312 if (PRINT_FIELD(BRSTACKINSN) &&
313 !(perf_evlist__combined_branch_type(session->evlist) &
314 PERF_SAMPLE_BRANCH_ANY)) {
315 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
316 "Hint: run 'perf record -b ...'\n");
317 return -EINVAL;
318 }
David Ahern1424dc92011-03-09 22:23:28 -0700319 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300320 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
321 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700322 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700323
324 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300325 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
326 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700327 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700328
329 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300330 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
331 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700332 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600333
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200334 if (PRINT_FIELD(PERIOD) &&
335 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
336 PERF_OUTPUT_PERIOD))
337 return -EINVAL;
338
Stephane Eranianfc36f942015-08-31 18:41:10 +0200339 if (PRINT_FIELD(IREGS) &&
340 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
341 PERF_OUTPUT_IREGS))
342 return -EINVAL;
343
David Ahern9cbdb702011-04-06 21:54:20 -0600344 return 0;
345}
346
Adrian Hunter7ea95722013-11-01 15:51:30 +0200347static void set_print_ip_opts(struct perf_event_attr *attr)
348{
349 unsigned int type = attr->type;
350
351 output[type].print_ip_opts = 0;
352 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300353 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200354
355 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300356 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200357
358 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300359 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200360
361 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300362 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200363
364 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300365 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200366}
367
David Ahern9cbdb702011-04-06 21:54:20 -0600368/*
369 * verify all user requested events exist and the samples
370 * have the expected data
371 */
372static int perf_session__check_output_opt(struct perf_session *session)
373{
He Kuang40f20e52016-05-16 04:51:19 +0000374 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600375 struct perf_evsel *evsel;
376
377 for (j = 0; j < PERF_TYPE_MAX; ++j) {
378 evsel = perf_session__find_first_evtype(session, j);
379
380 /*
381 * even if fields is set to 0 (ie., show nothing) event must
382 * exist if user explicitly includes it on the command line
383 */
384 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
385 pr_err("%s events do not exist. "
386 "Remove corresponding -f option to proceed.\n",
387 event_type(j));
388 return -1;
389 }
390
391 if (evsel && output[j].fields &&
392 perf_evsel__check_attr(evsel, session))
393 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400394
395 if (evsel == NULL)
396 continue;
397
Adrian Hunter7ea95722013-11-01 15:51:30 +0200398 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700399 }
400
Adrian Hunter98526ee2014-07-31 09:00:59 +0300401 if (!no_callchain) {
402 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000403 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300404
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300405 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000406 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300407 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
408 use_callchain = true;
409 break;
410 }
411 }
He Kuang71ac8992016-08-04 11:25:43 +0000412 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300413 symbol_conf.use_callchain = false;
414 }
415
David Ahern80b8b492013-11-19 21:07:37 -0700416 /*
417 * set default for tracepoints to print symbols only
418 * if callchains are present
419 */
420 if (symbol_conf.use_callchain &&
421 !output[PERF_TYPE_TRACEPOINT].user_set) {
422 struct perf_event_attr *attr;
423
424 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700425
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300426 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000427 if (evsel->attr.type != j)
428 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700429
He Kuang40f20e52016-05-16 04:51:19 +0000430 attr = &evsel->attr;
431
432 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
433 output[j].fields |= PERF_OUTPUT_IP;
434 output[j].fields |= PERF_OUTPUT_SYM;
435 output[j].fields |= PERF_OUTPUT_DSO;
436 set_print_ip_opts(attr);
437 goto out;
438 }
David Ahern80b8b492013-11-19 21:07:37 -0700439 }
440 }
441
442out:
David Ahern1424dc92011-03-09 22:23:28 -0700443 return 0;
444}
David Ahern745f43e2011-03-09 22:23:26 -0700445
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300446static void print_sample_iregs(struct perf_sample *sample,
Stephane Eranianfc36f942015-08-31 18:41:10 +0200447 struct perf_event_attr *attr)
448{
449 struct regs_dump *regs = &sample->intr_regs;
450 uint64_t mask = attr->sample_regs_intr;
451 unsigned i = 0, r;
452
453 if (!regs)
454 return;
455
456 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
457 u64 val = regs->regs[i++];
458 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
459 }
460}
461
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300462static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700463 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300464 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700465{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300466 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700467 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700468 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700469
David Ahern745f43e2011-03-09 22:23:26 -0700470 if (PRINT_FIELD(COMM)) {
471 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200472 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600473 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200474 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700475 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200476 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700477 }
David Ahernc70c94b2011-03-09 22:23:25 -0700478
David Ahern745f43e2011-03-09 22:23:26 -0700479 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
480 printf("%5d/%-5d ", sample->pid, sample->tid);
481 else if (PRINT_FIELD(PID))
482 printf("%5d ", sample->pid);
483 else if (PRINT_FIELD(TID))
484 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700485
David Ahern745f43e2011-03-09 22:23:26 -0700486 if (PRINT_FIELD(CPU)) {
487 if (latency_format)
488 printf("%3d ", sample->cpu);
489 else
490 printf("[%03d] ", sample->cpu);
491 }
David Ahernc70c94b2011-03-09 22:23:25 -0700492
David Ahern745f43e2011-03-09 22:23:26 -0700493 if (PRINT_FIELD(TIME)) {
494 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300495 secs = nsecs / NSEC_PER_SEC;
496 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900497
Adrian Hunter83e19862015-09-25 16:15:36 +0300498 if (nanosecs)
499 printf("%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900500 else {
501 char sample_time[32];
502 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
503 printf("%12s: ", sample_time);
504 }
David Ahern745f43e2011-03-09 22:23:26 -0700505 }
David Ahernc70c94b2011-03-09 22:23:25 -0700506}
507
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200508static inline char
509mispred_str(struct branch_entry *br)
510{
511 if (!(br->flags.mispred || br->flags.predicted))
512 return '-';
513
514 return br->flags.predicted ? 'P' : 'M';
515}
516
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300517static void print_sample_brstack(struct perf_sample *sample)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200518{
519 struct branch_stack *br = sample->branch_stack;
520 u64 i;
521
522 if (!(br && br->nr))
523 return;
524
525 for (i = 0; i < br->nr; i++) {
526 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
527 br->entries[i].from,
528 br->entries[i].to,
529 mispred_str( br->entries + i),
530 br->entries[i].flags.in_tx? 'X' : '-',
531 br->entries[i].flags.abort? 'A' : '-',
532 br->entries[i].flags.cycles);
533 }
534}
535
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300536static void print_sample_brstacksym(struct perf_sample *sample,
537 struct thread *thread)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200538{
539 struct branch_stack *br = sample->branch_stack;
540 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200541 u64 i, from, to;
542
543 if (!(br && br->nr))
544 return;
545
546 for (i = 0; i < br->nr; i++) {
547
548 memset(&alf, 0, sizeof(alf));
549 memset(&alt, 0, sizeof(alt));
550 from = br->entries[i].from;
551 to = br->entries[i].to;
552
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300553 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200554 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300555 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200556
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300557 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200558 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300559 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200560
561 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
562 putchar('/');
563 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
564 printf("/%c/%c/%c/%d ",
565 mispred_str( br->entries + i),
566 br->entries[i].flags.in_tx? 'X' : '-',
567 br->entries[i].flags.abort? 'A' : '-',
568 br->entries[i].flags.cycles);
569 }
570}
571
Andi Kleen48d02a12017-02-23 15:46:34 -0800572#define MAXBB 16384UL
573
574static int grab_bb(u8 *buffer, u64 start, u64 end,
575 struct machine *machine, struct thread *thread,
576 bool *is64bit, u8 *cpumode, bool last)
577{
578 long offset, len;
579 struct addr_location al;
580 bool kernel;
581
582 if (!start || !end)
583 return 0;
584
585 kernel = machine__kernel_ip(machine, start);
586 if (kernel)
587 *cpumode = PERF_RECORD_MISC_KERNEL;
588 else
589 *cpumode = PERF_RECORD_MISC_USER;
590
591 /*
592 * Block overlaps between kernel and user.
593 * This can happen due to ring filtering
594 * On Intel CPUs the entry into the kernel is filtered,
595 * but the exit is not. Let the caller patch it up.
596 */
597 if (kernel != machine__kernel_ip(machine, end)) {
598 printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
599 start, end);
600 return -ENXIO;
601 }
602
603 memset(&al, 0, sizeof(al));
604 if (end - start > MAXBB - MAXINSN) {
605 if (last)
606 printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
607 else
608 printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
609 return 0;
610 }
611
612 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
613 if (!al.map || !al.map->dso) {
614 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
615 return 0;
616 }
617 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
618 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
619 return 0;
620 }
621
622 /* Load maps to ensure dso->is_64_bit has been updated */
623 map__load(al.map);
624
625 offset = al.map->map_ip(al.map, start);
626 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
627 end - start + MAXINSN);
628
629 *is64bit = al.map->dso->is_64_bit;
630 if (len <= 0)
631 printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
632 start, end);
633 return len;
634}
635
636static void print_jump(uint64_t ip, struct branch_entry *en,
637 struct perf_insn *x, u8 *inbuf, int len,
638 int insn)
639{
640 printf("\t%016" PRIx64 "\t%-30s\t#%s%s%s%s",
641 ip,
642 dump_insn(x, ip, inbuf, len, NULL),
643 en->flags.predicted ? " PRED" : "",
644 en->flags.mispred ? " MISPRED" : "",
645 en->flags.in_tx ? " INTX" : "",
646 en->flags.abort ? " ABORT" : "");
647 if (en->flags.cycles) {
648 printf(" %d cycles", en->flags.cycles);
649 if (insn)
650 printf(" %.2f IPC", (float)insn / en->flags.cycles);
651 }
652 putchar('\n');
653}
654
655static void print_ip_sym(struct thread *thread, u8 cpumode, int cpu,
656 uint64_t addr, struct symbol **lastsym,
657 struct perf_event_attr *attr)
658{
659 struct addr_location al;
660 int off;
661
662 memset(&al, 0, sizeof(al));
663
664 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
665 if (!al.map)
666 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
667 addr, &al);
668 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
669 return;
670
671 al.cpu = cpu;
672 al.sym = NULL;
673 if (al.map)
674 al.sym = map__find_symbol(al.map, al.addr);
675
676 if (!al.sym)
677 return;
678
679 if (al.addr < al.sym->end)
680 off = al.addr - al.sym->start;
681 else
682 off = al.addr - al.map->start - al.sym->start;
683 printf("\t%s", al.sym->name);
684 if (off)
685 printf("%+d", off);
686 putchar(':');
687 if (PRINT_FIELD(SRCLINE))
688 map__fprintf_srcline(al.map, al.addr, "\t", stdout);
689 putchar('\n');
690 *lastsym = al.sym;
691}
692
693static void print_sample_brstackinsn(struct perf_sample *sample,
694 struct thread *thread,
695 struct perf_event_attr *attr,
696 struct machine *machine)
697{
698 struct branch_stack *br = sample->branch_stack;
699 u64 start, end;
700 int i, insn, len, nr, ilen;
701 struct perf_insn x;
702 u8 buffer[MAXBB];
703 unsigned off;
704 struct symbol *lastsym = NULL;
705
706 if (!(br && br->nr))
707 return;
708 nr = br->nr;
709 if (max_blocks && nr > max_blocks + 1)
710 nr = max_blocks + 1;
711
712 x.thread = thread;
713 x.cpu = sample->cpu;
714
715 putchar('\n');
716
717 /* Handle first from jump, of which we don't know the entry. */
718 len = grab_bb(buffer, br->entries[nr-1].from,
719 br->entries[nr-1].from,
720 machine, thread, &x.is64bit, &x.cpumode, false);
721 if (len > 0) {
722 print_ip_sym(thread, x.cpumode, x.cpu,
723 br->entries[nr - 1].from, &lastsym, attr);
724 print_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
725 &x, buffer, len, 0);
726 }
727
728 /* Print all blocks */
729 for (i = nr - 2; i >= 0; i--) {
730 if (br->entries[i].from || br->entries[i].to)
731 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
732 br->entries[i].from,
733 br->entries[i].to);
734 start = br->entries[i + 1].to;
735 end = br->entries[i].from;
736
737 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
738 /* Patch up missing kernel transfers due to ring filters */
739 if (len == -ENXIO && i > 0) {
740 end = br->entries[--i].from;
741 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
742 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
743 }
744 if (len <= 0)
745 continue;
746
747 insn = 0;
748 for (off = 0;; off += ilen) {
749 uint64_t ip = start + off;
750
751 print_ip_sym(thread, x.cpumode, x.cpu, ip, &lastsym, attr);
752 if (ip == end) {
753 print_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn);
754 break;
755 } else {
756 printf("\t%016" PRIx64 "\t%s\n", ip,
757 dump_insn(&x, ip, buffer + off, len - off, &ilen));
758 if (ilen == 0)
759 break;
760 insn++;
761 }
762 }
763 }
764
765 /*
766 * Hit the branch? In this case we are already done, and the target
767 * has not been executed yet.
768 */
769 if (br->entries[0].from == sample->ip)
770 return;
771 if (br->entries[0].flags.abort)
772 return;
773
774 /*
775 * Print final block upto sample
776 */
777 start = br->entries[0].to;
778 end = sample->ip;
779 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
780 print_ip_sym(thread, x.cpumode, x.cpu, start, &lastsym, attr);
781 if (len <= 0) {
782 /* Print at least last IP if basic block did not work */
783 len = grab_bb(buffer, sample->ip, sample->ip,
784 machine, thread, &x.is64bit, &x.cpumode, false);
785 if (len <= 0)
786 return;
787
788 printf("\t%016" PRIx64 "\t%s\n", sample->ip,
789 dump_insn(&x, sample->ip, buffer, len, NULL));
790 return;
791 }
792 for (off = 0; off <= end - start; off += ilen) {
793 printf("\t%016" PRIx64 "\t%s\n", start + off,
794 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
795 if (ilen == 0)
796 break;
797 }
798}
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200799
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300800static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600801 struct thread *thread,
802 struct perf_event_attr *attr)
803{
804 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600805
806 printf("%16" PRIx64, sample->addr);
807
808 if (!sample_addr_correlates_sym(attr))
809 return;
810
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300811 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600812
813 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900814 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900815 if (PRINT_FIELD(SYMOFFSET))
816 symbol__fprintf_symname_offs(al.sym, &al, stdout);
817 else
818 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600819 }
820
821 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900822 printf(" (");
823 map__fprintf_dsoname(al.map, stdout);
824 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600825 }
826}
827
Adrian Huntere2167082016-06-23 16:40:58 +0300828static void print_sample_callindent(struct perf_sample *sample,
829 struct perf_evsel *evsel,
830 struct thread *thread,
831 struct addr_location *al)
832{
833 struct perf_event_attr *attr = &evsel->attr;
834 size_t depth = thread_stack__depth(thread);
835 struct addr_location addr_al;
836 const char *name = NULL;
837 static int spacing;
838 int len = 0;
839 u64 ip = 0;
840
841 /*
842 * The 'return' has already been popped off the stack so the depth has
843 * to be adjusted to match the 'call'.
844 */
845 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
846 depth += 1;
847
848 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
849 if (sample_addr_correlates_sym(attr)) {
850 thread__resolve(thread, &addr_al, sample);
851 if (addr_al.sym)
852 name = addr_al.sym->name;
853 else
854 ip = sample->addr;
855 } else {
856 ip = sample->addr;
857 }
858 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
859 if (al->sym)
860 name = al->sym->name;
861 else
862 ip = sample->ip;
863 }
864
865 if (name)
866 len = printf("%*s%s", (int)depth * 4, "", name);
867 else if (ip)
868 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
869
870 if (len < 0)
871 return;
872
873 /*
874 * Try to keep the output length from changing frequently so that the
875 * output lines up more nicely.
876 */
877 if (len > spacing || (len && len < spacing - 52))
878 spacing = round_up(len + 4, 32);
879
880 if (len < spacing)
881 printf("%*s", spacing - len, "");
882}
883
Andi Kleen224e2c92016-10-07 16:42:27 +0300884static void print_insn(struct perf_sample *sample,
Andi Kleen48d02a12017-02-23 15:46:34 -0800885 struct perf_event_attr *attr,
886 struct thread *thread,
887 struct machine *machine)
Andi Kleen224e2c92016-10-07 16:42:27 +0300888{
889 if (PRINT_FIELD(INSNLEN))
890 printf(" ilen: %d", sample->insn_len);
891 if (PRINT_FIELD(INSN)) {
892 int i;
893
894 printf(" insn:");
895 for (i = 0; i < sample->insn_len; i++)
896 printf(" %02x", (unsigned char)sample->insn[i]);
897 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800898 if (PRINT_FIELD(BRSTACKINSN))
899 print_sample_brstackinsn(sample, thread, attr, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300900}
901
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300902static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +0900903 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200904 struct thread *thread,
Andi Kleen48d02a12017-02-23 15:46:34 -0800905 struct addr_location *al,
906 struct machine *machine)
Akihiro Nagai95582592012-01-30 13:43:09 +0900907{
908 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300909 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900910
Adrian Huntere2167082016-06-23 16:40:58 +0300911 if (PRINT_FIELD(CALLINDENT))
912 print_sample_callindent(sample, evsel, thread, al);
913
Akihiro Nagai95582592012-01-30 13:43:09 +0900914 /* print branch_from information */
915 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300916 unsigned int print_opts = output[attr->type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -0700917 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300918
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300919 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700920 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300921 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700922 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300923
924 if (cursor == NULL) {
925 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300926 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300927 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300928 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300929 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300930 } else
931 putchar('\n');
932
933 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +0900934 }
935
Akihiro Nagai95582592012-01-30 13:43:09 +0900936 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300937 if (PRINT_FIELD(ADDR) ||
938 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300939 !output[attr->type].user_set)) {
940 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300941 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300942 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900943
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300944 if (print_srcline_last)
945 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
946
Andi Kleen48d02a12017-02-23 15:46:34 -0800947 print_insn(sample, attr, thread, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300948
Akihiro Nagai95582592012-01-30 13:43:09 +0900949 printf("\n");
950}
951
Adrian Hunter055cd332016-06-23 16:40:56 +0300952static struct {
953 u32 flags;
954 const char *name;
955} sample_flags[] = {
956 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
957 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
958 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
959 {PERF_IP_FLAG_BRANCH, "jmp"},
960 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
961 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
962 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
963 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
964 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
965 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
966 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
967 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
968 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
969 {0, NULL}
970};
971
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300972static void print_sample_flags(u32 flags)
973{
974 const char *chars = PERF_IP_FLAG_CHARS;
975 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +0300976 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
977 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300978 char str[33];
979 int i, pos = 0;
980
Adrian Hunter055cd332016-06-23 16:40:56 +0300981 for (i = 0; sample_flags[i].name ; i++) {
982 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
983 name = sample_flags[i].name;
984 break;
985 }
986 }
987
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300988 for (i = 0; i < n; i++, flags >>= 1) {
989 if (flags & 1)
990 str[pos++] = chars[i];
991 }
992 for (; i < 32; i++, flags >>= 1) {
993 if (flags & 1)
994 str[pos++] = '?';
995 }
996 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +0300997
998 if (name)
999 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
1000 else
1001 printf(" %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001002}
1003
Wang Nan30372f02016-02-24 11:20:45 +00001004struct printer_data {
1005 int line_no;
1006 bool hit_nul;
1007 bool is_printable;
1008};
1009
1010static void
1011print_sample_bpf_output_printer(enum binary_printer_ops op,
1012 unsigned int val,
1013 void *extra)
1014{
1015 unsigned char ch = (unsigned char)val;
1016 struct printer_data *printer_data = extra;
1017
1018 switch (op) {
1019 case BINARY_PRINT_DATA_BEGIN:
1020 printf("\n");
1021 break;
1022 case BINARY_PRINT_LINE_BEGIN:
1023 printf("%17s", !printer_data->line_no ? "BPF output:" :
1024 " ");
1025 break;
1026 case BINARY_PRINT_ADDR:
1027 printf(" %04x:", val);
1028 break;
1029 case BINARY_PRINT_NUM_DATA:
1030 printf(" %02x", val);
1031 break;
1032 case BINARY_PRINT_NUM_PAD:
1033 printf(" ");
1034 break;
1035 case BINARY_PRINT_SEP:
1036 printf(" ");
1037 break;
1038 case BINARY_PRINT_CHAR_DATA:
1039 if (printer_data->hit_nul && ch)
1040 printer_data->is_printable = false;
1041
1042 if (!isprint(ch)) {
1043 printf("%c", '.');
1044
1045 if (!printer_data->is_printable)
1046 break;
1047
1048 if (ch == '\0')
1049 printer_data->hit_nul = true;
1050 else
1051 printer_data->is_printable = false;
1052 } else {
1053 printf("%c", ch);
1054 }
1055 break;
1056 case BINARY_PRINT_CHAR_PAD:
1057 printf(" ");
1058 break;
1059 case BINARY_PRINT_LINE_END:
1060 printf("\n");
1061 printer_data->line_no++;
1062 break;
1063 case BINARY_PRINT_DATA_END:
1064 default:
1065 break;
1066 }
1067}
1068
1069static void print_sample_bpf_output(struct perf_sample *sample)
1070{
1071 unsigned int nr_bytes = sample->raw_size;
1072 struct printer_data printer_data = {0, false, true};
1073
1074 print_binary(sample->raw_data, nr_bytes, 8,
1075 print_sample_bpf_output_printer, &printer_data);
1076
1077 if (printer_data.is_printable && printer_data.hit_nul)
1078 printf("%17s \"%s\"\n", "BPF string:",
1079 (char *)(sample->raw_data));
1080}
1081
Jiri Olsa809e9422015-11-26 18:55:21 +01001082struct perf_script {
1083 struct perf_tool tool;
1084 struct perf_session *session;
1085 bool show_task_events;
1086 bool show_mmap_events;
1087 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301088 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001089 bool allocated;
1090 struct cpu_map *cpus;
1091 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001092 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001093 const char *time_str;
1094 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001095};
1096
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001097static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1098{
1099 struct perf_evsel *evsel;
1100 int max = 0;
1101
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001102 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001103 int len = strlen(perf_evsel__name(evsel));
1104
1105 max = MAX(len, max);
1106 }
1107
1108 return max;
1109}
1110
Jiri Olsac19ac912016-02-24 09:46:54 +01001111static size_t data_src__printf(u64 data_src)
1112{
1113 struct mem_info mi = { .data_src.val = data_src };
1114 char decode[100];
1115 char out[100];
1116 static int maxlen;
1117 int len;
1118
1119 perf_script__meminfo_scnprintf(decode, 100, &mi);
1120
1121 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1122 if (maxlen < len)
1123 maxlen = len;
1124
1125 return printf("%-*s", maxlen, out);
1126}
1127
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001128static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001129 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001130 struct addr_location *al,
1131 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001132{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001133 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001134 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -07001135
David Ahern2c9e45f72011-03-17 10:03:21 -06001136 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001137 return;
1138
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001139 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -07001140
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001141 if (PRINT_FIELD(PERIOD))
1142 printf("%10" PRIu64 " ", sample->period);
1143
Namhyung Kime944d3d2013-11-18 14:34:52 +09001144 if (PRINT_FIELD(EVNAME)) {
1145 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001146
1147 if (!script->name_width)
1148 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1149
1150 printf("%*s: ", script->name_width,
1151 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001152 }
1153
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001154 if (print_flags)
1155 print_sample_flags(sample->flags);
1156
Akihiro Nagai95582592012-01-30 13:43:09 +09001157 if (is_bts_event(attr)) {
Andi Kleen48d02a12017-02-23 15:46:34 -08001158 print_sample_bts(sample, evsel, thread, al, machine);
Akihiro Nagai95582592012-01-30 13:43:09 +09001159 return;
1160 }
1161
David Ahern745f43e2011-03-09 22:23:26 -07001162 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001163 event_format__print(evsel->tp_format, sample->cpu,
1164 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -06001165 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001166 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -06001167
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001168 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +01001169 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001170
1171 if (PRINT_FIELD(WEIGHT))
1172 printf("%16" PRIu64, sample->weight);
1173
David Ahern787bef12011-05-27 14:28:43 -06001174 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001175 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001176
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001177 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001178 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001179 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001180 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001181
1182 putchar(cursor ? '\n' : ' ');
1183 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001184 }
1185
Stephane Eranianfc36f942015-08-31 18:41:10 +02001186 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001187 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001188
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001189 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001190 print_sample_brstack(sample);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001191 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001192 print_sample_brstacksym(sample, thread);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001193
Wang Nan30372f02016-02-24 11:20:45 +00001194 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1195 print_sample_bpf_output(sample);
Andi Kleen48d02a12017-02-23 15:46:34 -08001196 print_insn(sample, attr, thread, machine);
David Ahernc70c94b2011-03-09 22:23:25 -07001197 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001198}
1199
Tom Zanussi956ffd02009-11-25 01:15:46 -06001200static struct scripting_ops *scripting_ops;
1201
Jiri Olsa36e33c52016-01-06 11:49:56 +01001202static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1203{
1204 int nthreads = thread_map__nr(counter->threads);
1205 int ncpus = perf_evsel__nr_cpus(counter);
1206 int cpu, thread;
1207 static int header_printed;
1208
1209 if (counter->system_wide)
1210 nthreads = 1;
1211
1212 if (!header_printed) {
1213 printf("%3s %8s %15s %15s %15s %15s %s\n",
1214 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1215 header_printed = 1;
1216 }
1217
1218 for (thread = 0; thread < nthreads; thread++) {
1219 for (cpu = 0; cpu < ncpus; cpu++) {
1220 struct perf_counts_values *counts;
1221
1222 counts = perf_counts(counter->counts, cpu, thread);
1223
1224 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1225 counter->cpus->map[cpu],
1226 thread_map__pid(counter->threads, thread),
1227 counts->val,
1228 counts->ena,
1229 counts->run,
1230 tstamp,
1231 perf_evsel__name(counter));
1232 }
1233 }
1234}
1235
Jiri Olsae099eba2016-01-05 22:09:09 +01001236static void process_stat(struct perf_evsel *counter, u64 tstamp)
1237{
1238 if (scripting_ops && scripting_ops->process_stat)
1239 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001240 else
1241 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001242}
1243
1244static void process_stat_interval(u64 tstamp)
1245{
1246 if (scripting_ops && scripting_ops->process_stat_interval)
1247 scripting_ops->process_stat_interval(tstamp);
1248}
1249
Tom Zanussi956ffd02009-11-25 01:15:46 -06001250static void setup_scripting(void)
1251{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001252 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001253 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001254}
1255
Adrian Hunterd445dd22014-08-15 22:08:37 +03001256static int flush_scripting(void)
1257{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001258 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001259}
1260
Tom Zanussi956ffd02009-11-25 01:15:46 -06001261static int cleanup_scripting(void)
1262{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001263 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001264
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001265 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001266}
1267
Jiri Olsa809e9422015-11-26 18:55:21 +01001268static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001269 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001270 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001271 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001272 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001273{
Jiri Olsa809e9422015-11-26 18:55:21 +01001274 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001275 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001276
David Aherna91f4c42016-11-29 10:15:43 -07001277 if (perf_time__skip_sample(&scr->ptime, sample->time))
1278 return 0;
1279
David Ahern1424dc92011-03-09 22:23:28 -07001280 if (debug_mode) {
1281 if (sample->time < last_timestamp) {
1282 pr_err("Samples misordered, previous: %" PRIu64
1283 " this: %" PRIu64 "\n", last_timestamp,
1284 sample->time);
1285 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001286 }
David Ahern1424dc92011-03-09 22:23:28 -07001287 last_timestamp = sample->time;
1288 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001289 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001290
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001291 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001292 pr_err("problem processing %d event, skipping it.\n",
1293 event->header.type);
1294 return -1;
1295 }
1296
1297 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001298 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001299
Anton Blanchard5d67be92011-07-04 21:57:50 +10001300 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001301 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001302
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001303 if (scripting_ops)
1304 scripting_ops->process_event(event, sample, evsel, &al);
1305 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001306 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001307
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001308out_put:
1309 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001310 return 0;
1311}
1312
Adrian Hunter7ea95722013-11-01 15:51:30 +02001313static int process_attr(struct perf_tool *tool, union perf_event *event,
1314 struct perf_evlist **pevlist)
1315{
1316 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1317 struct perf_evlist *evlist;
1318 struct perf_evsel *evsel, *pos;
1319 int err;
1320
1321 err = perf_event__process_attr(tool, event, pevlist);
1322 if (err)
1323 return err;
1324
1325 evlist = *pevlist;
1326 evsel = perf_evlist__last(*pevlist);
1327
1328 if (evsel->attr.type >= PERF_TYPE_MAX)
1329 return 0;
1330
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001331 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001332 if (pos->attr.type == evsel->attr.type && pos != evsel)
1333 return 0;
1334 }
1335
1336 set_print_ip_opts(&evsel->attr);
1337
Jiri Olsad2b5a312015-10-16 12:41:25 +02001338 if (evsel->attr.sample_type)
1339 err = perf_evsel__check_attr(evsel, scr->session);
1340
1341 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001342}
1343
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001344static int process_comm_event(struct perf_tool *tool,
1345 union perf_event *event,
1346 struct perf_sample *sample,
1347 struct machine *machine)
1348{
1349 struct thread *thread;
1350 struct perf_script *script = container_of(tool, struct perf_script, tool);
1351 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001352 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001353 int ret = -1;
1354
1355 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1356 if (thread == NULL) {
1357 pr_debug("problem processing COMM event, skipping it.\n");
1358 return -1;
1359 }
1360
1361 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1362 goto out;
1363
1364 if (!evsel->attr.sample_id_all) {
1365 sample->cpu = 0;
1366 sample->time = 0;
1367 sample->tid = event->comm.tid;
1368 sample->pid = event->comm.pid;
1369 }
1370 print_sample_start(sample, thread, evsel);
1371 perf_event__fprintf(event, stdout);
1372 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001373out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001374 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001375 return ret;
1376}
1377
Hari Bathini96a44bb2017-03-08 02:12:06 +05301378static int process_namespaces_event(struct perf_tool *tool,
1379 union perf_event *event,
1380 struct perf_sample *sample,
1381 struct machine *machine)
1382{
1383 struct thread *thread;
1384 struct perf_script *script = container_of(tool, struct perf_script, tool);
1385 struct perf_session *session = script->session;
1386 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1387 int ret = -1;
1388
1389 thread = machine__findnew_thread(machine, event->namespaces.pid,
1390 event->namespaces.tid);
1391 if (thread == NULL) {
1392 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1393 return -1;
1394 }
1395
1396 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1397 goto out;
1398
1399 if (!evsel->attr.sample_id_all) {
1400 sample->cpu = 0;
1401 sample->time = 0;
1402 sample->tid = event->namespaces.tid;
1403 sample->pid = event->namespaces.pid;
1404 }
1405 print_sample_start(sample, thread, evsel);
1406 perf_event__fprintf(event, stdout);
1407 ret = 0;
1408out:
1409 thread__put(thread);
1410 return ret;
1411}
1412
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001413static int process_fork_event(struct perf_tool *tool,
1414 union perf_event *event,
1415 struct perf_sample *sample,
1416 struct machine *machine)
1417{
1418 struct thread *thread;
1419 struct perf_script *script = container_of(tool, struct perf_script, tool);
1420 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001421 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001422
1423 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1424 return -1;
1425
1426 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1427 if (thread == NULL) {
1428 pr_debug("problem processing FORK event, skipping it.\n");
1429 return -1;
1430 }
1431
1432 if (!evsel->attr.sample_id_all) {
1433 sample->cpu = 0;
1434 sample->time = event->fork.time;
1435 sample->tid = event->fork.tid;
1436 sample->pid = event->fork.pid;
1437 }
1438 print_sample_start(sample, thread, evsel);
1439 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001440 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001441
1442 return 0;
1443}
1444static int process_exit_event(struct perf_tool *tool,
1445 union perf_event *event,
1446 struct perf_sample *sample,
1447 struct machine *machine)
1448{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001449 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001450 struct thread *thread;
1451 struct perf_script *script = container_of(tool, struct perf_script, tool);
1452 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001453 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001454
1455 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1456 if (thread == NULL) {
1457 pr_debug("problem processing EXIT event, skipping it.\n");
1458 return -1;
1459 }
1460
1461 if (!evsel->attr.sample_id_all) {
1462 sample->cpu = 0;
1463 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001464 sample->tid = event->fork.tid;
1465 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001466 }
1467 print_sample_start(sample, thread, evsel);
1468 perf_event__fprintf(event, stdout);
1469
1470 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001471 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001472
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001473 thread__put(thread);
1474 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001475}
1476
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001477static int process_mmap_event(struct perf_tool *tool,
1478 union perf_event *event,
1479 struct perf_sample *sample,
1480 struct machine *machine)
1481{
1482 struct thread *thread;
1483 struct perf_script *script = container_of(tool, struct perf_script, tool);
1484 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001485 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001486
1487 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1488 return -1;
1489
1490 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1491 if (thread == NULL) {
1492 pr_debug("problem processing MMAP event, skipping it.\n");
1493 return -1;
1494 }
1495
1496 if (!evsel->attr.sample_id_all) {
1497 sample->cpu = 0;
1498 sample->time = 0;
1499 sample->tid = event->mmap.tid;
1500 sample->pid = event->mmap.pid;
1501 }
1502 print_sample_start(sample, thread, evsel);
1503 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001504 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001505 return 0;
1506}
1507
1508static int process_mmap2_event(struct perf_tool *tool,
1509 union perf_event *event,
1510 struct perf_sample *sample,
1511 struct machine *machine)
1512{
1513 struct thread *thread;
1514 struct perf_script *script = container_of(tool, struct perf_script, tool);
1515 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001516 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001517
1518 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1519 return -1;
1520
1521 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1522 if (thread == NULL) {
1523 pr_debug("problem processing MMAP2 event, skipping it.\n");
1524 return -1;
1525 }
1526
1527 if (!evsel->attr.sample_id_all) {
1528 sample->cpu = 0;
1529 sample->time = 0;
1530 sample->tid = event->mmap2.tid;
1531 sample->pid = event->mmap2.pid;
1532 }
1533 print_sample_start(sample, thread, evsel);
1534 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001535 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001536 return 0;
1537}
1538
Adrian Hunter7c148982015-07-21 12:44:06 +03001539static int process_switch_event(struct perf_tool *tool,
1540 union perf_event *event,
1541 struct perf_sample *sample,
1542 struct machine *machine)
1543{
1544 struct thread *thread;
1545 struct perf_script *script = container_of(tool, struct perf_script, tool);
1546 struct perf_session *session = script->session;
1547 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1548
1549 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1550 return -1;
1551
1552 thread = machine__findnew_thread(machine, sample->pid,
1553 sample->tid);
1554 if (thread == NULL) {
1555 pr_debug("problem processing SWITCH event, skipping it.\n");
1556 return -1;
1557 }
1558
1559 print_sample_start(sample, thread, evsel);
1560 perf_event__fprintf(event, stdout);
1561 thread__put(thread);
1562 return 0;
1563}
1564
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001565static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001566{
1567 session_done = 1;
1568}
1569
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001570static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001571{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001572 int ret;
1573
Tom Zanussic239da32010-04-01 23:59:18 -05001574 signal(SIGINT, sig_handler);
1575
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001576 /* override event processing functions */
1577 if (script->show_task_events) {
1578 script->tool.comm = process_comm_event;
1579 script->tool.fork = process_fork_event;
1580 script->tool.exit = process_exit_event;
1581 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001582 if (script->show_mmap_events) {
1583 script->tool.mmap = process_mmap_event;
1584 script->tool.mmap2 = process_mmap2_event;
1585 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001586 if (script->show_switch_events)
1587 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301588 if (script->show_namespace_events)
1589 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001590
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001591 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001592
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001593 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001594 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001595
1596 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001597}
1598
Tom Zanussi956ffd02009-11-25 01:15:46 -06001599struct script_spec {
1600 struct list_head node;
1601 struct scripting_ops *ops;
1602 char spec[0];
1603};
1604
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001605static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001606
1607static struct script_spec *script_spec__new(const char *spec,
1608 struct scripting_ops *ops)
1609{
1610 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1611
1612 if (s != NULL) {
1613 strcpy(s->spec, spec);
1614 s->ops = ops;
1615 }
1616
1617 return s;
1618}
1619
Tom Zanussi956ffd02009-11-25 01:15:46 -06001620static void script_spec__add(struct script_spec *s)
1621{
1622 list_add_tail(&s->node, &script_specs);
1623}
1624
1625static struct script_spec *script_spec__find(const char *spec)
1626{
1627 struct script_spec *s;
1628
1629 list_for_each_entry(s, &script_specs, node)
1630 if (strcasecmp(s->spec, spec) == 0)
1631 return s;
1632 return NULL;
1633}
1634
Tom Zanussi956ffd02009-11-25 01:15:46 -06001635int script_spec_register(const char *spec, struct scripting_ops *ops)
1636{
1637 struct script_spec *s;
1638
1639 s = script_spec__find(spec);
1640 if (s)
1641 return -1;
1642
Taeung Song8560bae2016-02-26 00:13:10 +09001643 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001644 if (!s)
1645 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001646 else
1647 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001648
1649 return 0;
1650}
1651
1652static struct scripting_ops *script_spec__lookup(const char *spec)
1653{
1654 struct script_spec *s = script_spec__find(spec);
1655 if (!s)
1656 return NULL;
1657
1658 return s->ops;
1659}
1660
1661static void list_available_languages(void)
1662{
1663 struct script_spec *s;
1664
1665 fprintf(stderr, "\n");
1666 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001667 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001668
1669 list_for_each_entry(s, &script_specs, node)
1670 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1671
1672 fprintf(stderr, "\n");
1673}
1674
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001675static int parse_scriptname(const struct option *opt __maybe_unused,
1676 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001677{
1678 char spec[PATH_MAX];
1679 const char *script, *ext;
1680 int len;
1681
Tom Zanussif526d682010-01-27 02:27:52 -06001682 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001683 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001684 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001685 }
1686
1687 script = strchr(str, ':');
1688 if (script) {
1689 len = script - str;
1690 if (len >= PATH_MAX) {
1691 fprintf(stderr, "invalid language specifier");
1692 return -1;
1693 }
1694 strncpy(spec, str, len);
1695 spec[len] = '\0';
1696 scripting_ops = script_spec__lookup(spec);
1697 if (!scripting_ops) {
1698 fprintf(stderr, "invalid language specifier");
1699 return -1;
1700 }
1701 script++;
1702 } else {
1703 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001704 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001705 if (!ext) {
1706 fprintf(stderr, "invalid script extension");
1707 return -1;
1708 }
1709 scripting_ops = script_spec__lookup(++ext);
1710 if (!scripting_ops) {
1711 fprintf(stderr, "invalid script extension");
1712 return -1;
1713 }
1714 }
1715
1716 script_name = strdup(script);
1717
1718 return 0;
1719}
1720
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001721static int parse_output_fields(const struct option *opt __maybe_unused,
1722 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001723{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001724 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001725 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001726 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001727 int rc = 0;
1728 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001729 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001730
1731 if (!str)
1732 return -ENOMEM;
1733
David Ahern2c9e45f72011-03-17 10:03:21 -06001734 /* first word can state for which event type the user is specifying
1735 * the fields. If no type exists, the specified fields apply to all
1736 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001737 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001738 tok = strchr(str, ':');
1739 if (tok) {
1740 *tok = '\0';
1741 tok++;
1742 if (!strcmp(str, "hw"))
1743 type = PERF_TYPE_HARDWARE;
1744 else if (!strcmp(str, "sw"))
1745 type = PERF_TYPE_SOFTWARE;
1746 else if (!strcmp(str, "trace"))
1747 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001748 else if (!strcmp(str, "raw"))
1749 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001750 else if (!strcmp(str, "break"))
1751 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001752 else {
1753 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001754 rc = -EINVAL;
1755 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001756 }
1757
1758 if (output[type].user_set)
1759 pr_warning("Overriding previous field request for %s events.\n",
1760 event_type(type));
1761
1762 output[type].fields = 0;
1763 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001764 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001765
1766 } else {
1767 tok = str;
1768 if (strlen(str) == 0) {
1769 fprintf(stderr,
1770 "Cannot set fields to 'none' for all event types.\n");
1771 rc = -EINVAL;
1772 goto out;
1773 }
1774
1775 if (output_set_by_user())
1776 pr_warning("Overriding previous field request for all events.\n");
1777
1778 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1779 output[j].fields = 0;
1780 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001781 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001782 }
David Ahern1424dc92011-03-09 22:23:28 -07001783 }
1784
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001785 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
David Ahern745f43e2011-03-09 22:23:26 -07001786 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001787 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001788 break;
David Ahern745f43e2011-03-09 22:23:26 -07001789 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001790 if (i == imax && strcmp(tok, "flags") == 0) {
1791 print_flags = true;
1792 continue;
1793 }
David Ahern745f43e2011-03-09 22:23:26 -07001794 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001795 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001796 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001797 goto out;
1798 }
1799
1800 if (type == -1) {
1801 /* add user option to all events types for
1802 * which it is valid
1803 */
1804 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1805 if (output[j].invalid_fields & all_output_options[i].field) {
1806 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1807 all_output_options[i].str, event_type(j));
1808 } else
1809 output[j].fields |= all_output_options[i].field;
1810 }
1811 } else {
1812 if (output[type].invalid_fields & all_output_options[i].field) {
1813 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1814 all_output_options[i].str, event_type(type));
1815
1816 rc = -EINVAL;
1817 goto out;
1818 }
1819 output[type].fields |= all_output_options[i].field;
1820 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001821 }
1822
1823 if (type >= 0) {
1824 if (output[type].fields == 0) {
1825 pr_debug("No fields requested for %s type. "
1826 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001827 }
David Ahern1424dc92011-03-09 22:23:28 -07001828 }
David Ahern745f43e2011-03-09 22:23:26 -07001829
David Ahern2c9e45f72011-03-17 10:03:21 -06001830out:
David Ahern745f43e2011-03-09 22:23:26 -07001831 free(str);
1832 return rc;
1833}
1834
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001835/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1836static int is_directory(const char *base_path, const struct dirent *dent)
1837{
1838 char path[PATH_MAX];
1839 struct stat st;
1840
1841 sprintf(path, "%s/%s", base_path, dent->d_name);
1842 if (stat(path, &st))
1843 return 0;
1844
1845 return S_ISDIR(st.st_mode);
1846}
1847
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001848#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1849 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1850 if ((lang_dirent->d_type == DT_DIR || \
1851 (lang_dirent->d_type == DT_UNKNOWN && \
1852 is_directory(scripts_path, lang_dirent))) && \
1853 (strcmp(lang_dirent->d_name, ".")) && \
1854 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001855
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001856#define for_each_script(lang_path, lang_dir, script_dirent) \
1857 while ((script_dirent = readdir(lang_dir)) != NULL) \
1858 if (script_dirent->d_type != DT_DIR && \
1859 (script_dirent->d_type != DT_UNKNOWN || \
1860 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001861
1862
1863#define RECORD_SUFFIX "-record"
1864#define REPORT_SUFFIX "-report"
1865
1866struct script_desc {
1867 struct list_head node;
1868 char *name;
1869 char *half_liner;
1870 char *args;
1871};
1872
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001873static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001874
1875static struct script_desc *script_desc__new(const char *name)
1876{
1877 struct script_desc *s = zalloc(sizeof(*s));
1878
Tom Zanussib5b87312010-11-10 08:16:51 -06001879 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001880 s->name = strdup(name);
1881
1882 return s;
1883}
1884
1885static void script_desc__delete(struct script_desc *s)
1886{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001887 zfree(&s->name);
1888 zfree(&s->half_liner);
1889 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001890 free(s);
1891}
1892
1893static void script_desc__add(struct script_desc *s)
1894{
1895 list_add_tail(&s->node, &script_descs);
1896}
1897
1898static struct script_desc *script_desc__find(const char *name)
1899{
1900 struct script_desc *s;
1901
1902 list_for_each_entry(s, &script_descs, node)
1903 if (strcasecmp(s->name, name) == 0)
1904 return s;
1905 return NULL;
1906}
1907
1908static struct script_desc *script_desc__findnew(const char *name)
1909{
1910 struct script_desc *s = script_desc__find(name);
1911
1912 if (s)
1913 return s;
1914
1915 s = script_desc__new(name);
1916 if (!s)
1917 goto out_delete_desc;
1918
1919 script_desc__add(s);
1920
1921 return s;
1922
1923out_delete_desc:
1924 script_desc__delete(s);
1925
1926 return NULL;
1927}
1928
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001929static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001930{
1931 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001932 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001933
1934 if (strlen(str) > suffix_len) {
1935 p = str + strlen(str) - suffix_len;
1936 if (!strncmp(p, suffix, suffix_len))
1937 return p;
1938 }
1939
1940 return NULL;
1941}
1942
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001943static int read_script_info(struct script_desc *desc, const char *filename)
1944{
1945 char line[BUFSIZ], *p;
1946 FILE *fp;
1947
1948 fp = fopen(filename, "r");
1949 if (!fp)
1950 return -1;
1951
1952 while (fgets(line, sizeof(line), fp)) {
1953 p = ltrim(line);
1954 if (strlen(p) == 0)
1955 continue;
1956 if (*p != '#')
1957 continue;
1958 p++;
1959 if (strlen(p) && *p == '!')
1960 continue;
1961
1962 p = ltrim(p);
1963 if (strlen(p) && p[strlen(p) - 1] == '\n')
1964 p[strlen(p) - 1] = '\0';
1965
1966 if (!strncmp(p, "description:", strlen("description:"))) {
1967 p += strlen("description:");
1968 desc->half_liner = strdup(ltrim(p));
1969 continue;
1970 }
1971
1972 if (!strncmp(p, "args:", strlen("args:"))) {
1973 p += strlen("args:");
1974 desc->args = strdup(ltrim(p));
1975 continue;
1976 }
1977 }
1978
1979 fclose(fp);
1980
1981 return 0;
1982}
1983
Robert Richter38efb532011-11-25 11:38:40 +01001984static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1985{
1986 char *script_root, *str;
1987
1988 script_root = strdup(script_dirent->d_name);
1989 if (!script_root)
1990 return NULL;
1991
1992 str = (char *)ends_with(script_root, suffix);
1993 if (!str) {
1994 free(script_root);
1995 return NULL;
1996 }
1997
1998 *str = '\0';
1999 return script_root;
2000}
2001
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002002static int list_available_scripts(const struct option *opt __maybe_unused,
2003 const char *s __maybe_unused,
2004 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002005{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002006 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002007 char scripts_path[MAXPATHLEN];
2008 DIR *scripts_dir, *lang_dir;
2009 char script_path[MAXPATHLEN];
2010 char lang_path[MAXPATHLEN];
2011 struct script_desc *desc;
2012 char first_half[BUFSIZ];
2013 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002014
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002015 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002016
2017 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002018 if (!scripts_dir) {
2019 fprintf(stdout,
2020 "open(%s) failed.\n"
2021 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2022 scripts_path);
2023 exit(-1);
2024 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002025
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002026 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002027 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002028 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002029 lang_dir = opendir(lang_path);
2030 if (!lang_dir)
2031 continue;
2032
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002033 for_each_script(lang_path, lang_dir, script_dirent) {
2034 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002035 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002036 desc = script_desc__findnew(script_root);
2037 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002038 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002039 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002040 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002041 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002042 }
2043 }
2044
2045 fprintf(stdout, "List of available trace scripts:\n");
2046 list_for_each_entry(desc, &script_descs, node) {
2047 sprintf(first_half, "%s %s", desc->name,
2048 desc->args ? desc->args : "");
2049 fprintf(stdout, " %-36s %s\n", first_half,
2050 desc->half_liner ? desc->half_liner : "");
2051 }
2052
2053 exit(0);
2054}
2055
Feng Tange5f37052012-09-07 16:42:26 +08002056/*
Feng Tang49e639e2012-10-30 11:56:03 +08002057 * Some scripts specify the required events in their "xxx-record" file,
2058 * this function will check if the events in perf.data match those
2059 * mentioned in the "xxx-record".
2060 *
2061 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2062 * which is covered well now. And new parsing code should be added to
2063 * cover the future complexing formats like event groups etc.
2064 */
2065static int check_ev_match(char *dir_name, char *scriptname,
2066 struct perf_session *session)
2067{
2068 char filename[MAXPATHLEN], evname[128];
2069 char line[BUFSIZ], *p;
2070 struct perf_evsel *pos;
2071 int match, len;
2072 FILE *fp;
2073
2074 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2075
2076 fp = fopen(filename, "r");
2077 if (!fp)
2078 return -1;
2079
2080 while (fgets(line, sizeof(line), fp)) {
2081 p = ltrim(line);
2082 if (*p == '#')
2083 continue;
2084
2085 while (strlen(p)) {
2086 p = strstr(p, "-e");
2087 if (!p)
2088 break;
2089
2090 p += 2;
2091 p = ltrim(p);
2092 len = strcspn(p, " \t");
2093 if (!len)
2094 break;
2095
2096 snprintf(evname, len + 1, "%s", p);
2097
2098 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002099 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002100 if (!strcmp(perf_evsel__name(pos), evname)) {
2101 match = 1;
2102 break;
2103 }
2104 }
2105
2106 if (!match) {
2107 fclose(fp);
2108 return -1;
2109 }
2110 }
2111 }
2112
2113 fclose(fp);
2114 return 0;
2115}
2116
2117/*
Feng Tange5f37052012-09-07 16:42:26 +08002118 * Return -1 if none is found, otherwise the actual scripts number.
2119 *
2120 * Currently the only user of this function is the script browser, which
2121 * will list all statically runnable scripts, select one, execute it and
2122 * show the output in a perf browser.
2123 */
2124int find_scripts(char **scripts_array, char **scripts_path_array)
2125{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002126 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002127 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002128 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002129 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002130 struct perf_data_file file = {
2131 .path = input_name,
2132 .mode = PERF_DATA_MODE_READ,
2133 };
Feng Tange5f37052012-09-07 16:42:26 +08002134 char *temp;
2135 int i = 0;
2136
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002137 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002138 if (!session)
2139 return -1;
2140
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002141 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002142
2143 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002144 if (!scripts_dir) {
2145 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002146 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002147 }
Feng Tange5f37052012-09-07 16:42:26 +08002148
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002149 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002150 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002151 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002152#ifdef NO_LIBPERL
2153 if (strstr(lang_path, "perl"))
2154 continue;
2155#endif
2156#ifdef NO_LIBPYTHON
2157 if (strstr(lang_path, "python"))
2158 continue;
2159#endif
2160
2161 lang_dir = opendir(lang_path);
2162 if (!lang_dir)
2163 continue;
2164
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002165 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002166 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002167 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002168 continue;
2169 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002170 script_dirent->d_name);
2171 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002172 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002173 (temp - script_dirent->d_name) + 1,
2174 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002175
2176 if (check_ev_match(lang_path,
2177 scripts_array[i], session))
2178 continue;
2179
Feng Tange5f37052012-09-07 16:42:26 +08002180 i++;
2181 }
Feng Tang49e639e2012-10-30 11:56:03 +08002182 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002183 }
2184
Feng Tang49e639e2012-10-30 11:56:03 +08002185 closedir(scripts_dir);
2186 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002187 return i;
2188}
2189
Tom Zanussi38752942009-12-15 02:53:39 -06002190static char *get_script_path(const char *script_root, const char *suffix)
2191{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002192 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002193 char scripts_path[MAXPATHLEN];
2194 char script_path[MAXPATHLEN];
2195 DIR *scripts_dir, *lang_dir;
2196 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002197 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002198
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002199 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002200
2201 scripts_dir = opendir(scripts_path);
2202 if (!scripts_dir)
2203 return NULL;
2204
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002205 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002206 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002207 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002208 lang_dir = opendir(lang_path);
2209 if (!lang_dir)
2210 continue;
2211
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002212 for_each_script(lang_path, lang_dir, script_dirent) {
2213 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002214 if (__script_root && !strcmp(script_root, __script_root)) {
2215 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002216 closedir(lang_dir);
2217 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002218 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002219 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002220 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002221 }
2222 free(__script_root);
2223 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002224 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002225 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002226 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002227
Robert Richter38efb532011-11-25 11:38:40 +01002228 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002229}
2230
Tom Zanussib5b87312010-11-10 08:16:51 -06002231static bool is_top_script(const char *script_path)
2232{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002233 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002234}
2235
2236static int has_required_arg(char *script_path)
2237{
2238 struct script_desc *desc;
2239 int n_args = 0;
2240 char *p;
2241
2242 desc = script_desc__new(NULL);
2243
2244 if (read_script_info(desc, script_path))
2245 goto out;
2246
2247 if (!desc->args)
2248 goto out;
2249
2250 for (p = desc->args; *p; p++)
2251 if (*p == '<')
2252 n_args++;
2253out:
2254 script_desc__delete(desc);
2255
2256 return n_args;
2257}
2258
David Ahernd54b1a92012-08-26 12:24:46 -06002259static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002260{
2261 char **__argv = malloc(sizeof(const char *) * argc);
2262
David Ahernd54b1a92012-08-26 12:24:46 -06002263 if (!__argv) {
2264 pr_err("malloc failed\n");
2265 return -1;
2266 }
2267
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002268 memcpy(__argv, argv, sizeof(const char *) * argc);
2269 argc = parse_options(argc, (const char **)__argv, record_options,
2270 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2271 free(__argv);
2272
David Ahernd54b1a92012-08-26 12:24:46 -06002273 system_wide = (argc == 0);
2274
2275 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002276}
2277
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002278static void script__setup_sample_type(struct perf_script *script)
2279{
2280 struct perf_session *session = script->session;
2281 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2282
2283 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2284 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2285 (sample_type & PERF_SAMPLE_STACK_USER))
2286 callchain_param.record_mode = CALLCHAIN_DWARF;
2287 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2288 callchain_param.record_mode = CALLCHAIN_LBR;
2289 else
2290 callchain_param.record_mode = CALLCHAIN_FP;
2291 }
2292}
2293
Jiri Olsae099eba2016-01-05 22:09:09 +01002294static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2295 union perf_event *event,
2296 struct perf_session *session)
2297{
2298 struct stat_round_event *round = &event->stat_round;
2299 struct perf_evsel *counter;
2300
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002301 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002302 perf_stat_process_counter(&stat_config, counter);
2303 process_stat(counter, round->time);
2304 }
2305
2306 process_stat_interval(round->time);
2307 return 0;
2308}
2309
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002310static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2311 union perf_event *event,
2312 struct perf_session *session __maybe_unused)
2313{
2314 perf_event__read_stat_config(&stat_config, &event->stat_config);
2315 return 0;
2316}
2317
Jiri Olsacfc88742016-01-05 22:09:06 +01002318static int set_maps(struct perf_script *script)
2319{
2320 struct perf_evlist *evlist = script->session->evlist;
2321
2322 if (!script->cpus || !script->threads)
2323 return 0;
2324
2325 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2326 return -EINVAL;
2327
2328 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2329
2330 if (perf_evlist__alloc_stats(evlist, true))
2331 return -ENOMEM;
2332
2333 script->allocated = true;
2334 return 0;
2335}
2336
2337static
2338int process_thread_map_event(struct perf_tool *tool,
2339 union perf_event *event,
2340 struct perf_session *session __maybe_unused)
2341{
2342 struct perf_script *script = container_of(tool, struct perf_script, tool);
2343
2344 if (script->threads) {
2345 pr_warning("Extra thread map event, ignoring.\n");
2346 return 0;
2347 }
2348
2349 script->threads = thread_map__new_event(&event->thread_map);
2350 if (!script->threads)
2351 return -ENOMEM;
2352
2353 return set_maps(script);
2354}
2355
2356static
2357int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2358 union perf_event *event,
2359 struct perf_session *session __maybe_unused)
2360{
2361 struct perf_script *script = container_of(tool, struct perf_script, tool);
2362
2363 if (script->cpus) {
2364 pr_warning("Extra cpu map event, ignoring.\n");
2365 return 0;
2366 }
2367
2368 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2369 if (!script->cpus)
2370 return -ENOMEM;
2371
2372 return set_maps(script);
2373}
2374
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002375int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002376{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002377 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002378 bool header = false;
2379 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002380 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002381 char *rec_script_path = NULL;
2382 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002383 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002384 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002385 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002386 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002387 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002388 struct perf_script script = {
2389 .tool = {
2390 .sample = process_sample_event,
2391 .mmap = perf_event__process_mmap,
2392 .mmap2 = perf_event__process_mmap2,
2393 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302394 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002395 .exit = perf_event__process_exit,
2396 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002397 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002398 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002399 .tracing_data = perf_event__process_tracing_data,
2400 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002401 .id_index = perf_event__process_id_index,
2402 .auxtrace_info = perf_event__process_auxtrace_info,
2403 .auxtrace = perf_event__process_auxtrace,
2404 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002405 .stat = perf_event__process_stat_event,
2406 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002407 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002408 .thread_map = process_thread_map_event,
2409 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002410 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002411 .ordering_requires_timestamps = true,
2412 },
2413 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002414 struct perf_data_file file = {
2415 .mode = PERF_DATA_MODE_READ,
2416 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002417 const struct option options[] = {
2418 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2419 "dump raw trace in ASCII"),
2420 OPT_INCR('v', "verbose", &verbose,
2421 "be more verbose (show symbol address, etc)"),
2422 OPT_BOOLEAN('L', "Latency", &latency_format,
2423 "show latency attributes (irqs/preemption disabled, etc)"),
2424 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2425 list_available_scripts),
2426 OPT_CALLBACK('s', "script", NULL, "name",
2427 "script file name (lang:script name, script name, or *)",
2428 parse_scriptname),
2429 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2430 "generate perf-script.xx script in specified language"),
2431 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2432 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2433 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002434 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2435 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002436 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2437 "file", "vmlinux pathname"),
2438 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2439 "file", "kallsyms pathname"),
2440 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2441 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002442 OPT_CALLBACK(0, "symfs", NULL, "directory",
2443 "Look for files with symbols relative to this directory",
2444 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002445 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002446 "comma separated output fields prepend with 'type:'. "
2447 "Valid types: hw,sw,trace,raw. "
2448 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Huntere2167082016-06-23 16:40:58 +03002449 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
Andi Kleen48d02a12017-02-23 15:46:34 -08002450 "bpf-output,callindent,insn,insnlen,brstackinsn",
2451 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002452 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2453 "system-wide collection from all CPUs"),
2454 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2455 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002456 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2457 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002458 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2459 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2460 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002461 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2462 "only consider symbols in these pids"),
2463 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2464 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002465 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2466 "Set the maximum stack depth when parsing the callchain, "
2467 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002468 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002469 OPT_BOOLEAN('I', "show-info", &show_full_info,
2470 "display extended information from perf.data file"),
2471 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2472 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002473 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2474 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002475 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2476 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002477 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2478 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302479 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2480 "Show namespace events (if recorded)"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002481 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002482 OPT_INTEGER(0, "max-blocks", &max_blocks,
2483 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002484 OPT_BOOLEAN(0, "ns", &nanosecs,
2485 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002486 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2487 "Instruction Tracing options",
2488 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002489 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2490 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002491 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2492 "Enable symbol demangling"),
2493 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2494 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002495 OPT_STRING(0, "time", &script.time_str, "str",
2496 "Time span of interest (start,stop)"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002497 OPT_END()
2498 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002499 const char * const script_subcommands[] = { "record", "report", NULL };
2500 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002501 "perf script [<options>]",
2502 "perf script [<options>] record <script> [<record-options>] <command>",
2503 "perf script [<options>] report <script> [script-args]",
2504 "perf script [<options>] <script> [<record-options>] <command>",
2505 "perf script [<options>] <top-script> [script-args]",
2506 NULL
2507 };
Tom Zanussi38752942009-12-15 02:53:39 -06002508
Tom Zanussib5b87312010-11-10 08:16:51 -06002509 setup_scripting();
2510
Yunlong Song40cae2b2015-03-18 21:35:54 +08002511 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002512 PARSE_OPT_STOP_AT_NON_OPTION);
2513
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002514 file.path = input_name;
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002515 file.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002516
Tom Zanussib5b87312010-11-10 08:16:51 -06002517 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2518 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2519 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002520 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002521 }
2522
Tom Zanussib5b87312010-11-10 08:16:51 -06002523 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2524 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2525 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002526 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002527 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002528 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002529 return -1;
2530 }
Tom Zanussi38752942009-12-15 02:53:39 -06002531 }
2532
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002533 if (itrace_synth_opts.callchain &&
2534 itrace_synth_opts.callchain_sz > scripting_max_stack)
2535 scripting_max_stack = itrace_synth_opts.callchain_sz;
2536
Ben Hutchings44e668c2010-10-10 16:10:03 +01002537 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002538 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002539
Tom Zanussib5b87312010-11-10 08:16:51 -06002540 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002541 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002542 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002543 pid_t pid;
2544
Tom Zanussib5b87312010-11-10 08:16:51 -06002545 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2546 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2547
2548 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002549 usage_with_options_msg(script_usage, options,
2550 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002551 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002552 }
2553
Tom Zanussib5b87312010-11-10 08:16:51 -06002554 if (is_top_script(argv[0])) {
2555 rep_args = argc - 1;
2556 } else {
2557 int rec_args;
2558
2559 rep_args = has_required_arg(rep_script_path);
2560 rec_args = (argc - 1) - rep_args;
2561 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002562 usage_with_options_msg(script_usage, options,
2563 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002564 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002565 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002566 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002567 }
2568
2569 if (pipe(live_pipe) < 0) {
2570 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002571 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002572 }
2573
2574 pid = fork();
2575 if (pid < 0) {
2576 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002577 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002578 }
2579
2580 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002581 j = 0;
2582
Tom Zanussia0cccc22010-04-01 23:59:25 -05002583 dup2(live_pipe[1], 1);
2584 close(live_pipe[0]);
2585
Robert Richter317df652011-11-25 15:05:25 +01002586 if (is_top_script(argv[0])) {
2587 system_wide = true;
2588 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002589 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2590 err = -1;
2591 goto out;
2592 }
Robert Richter317df652011-11-25 15:05:25 +01002593 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002594
2595 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002596 if (!__argv) {
2597 pr_err("malloc failed\n");
2598 err = -ENOMEM;
2599 goto out;
2600 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002601
Tom Zanussib5b87312010-11-10 08:16:51 -06002602 __argv[j++] = "/bin/sh";
2603 __argv[j++] = rec_script_path;
2604 if (system_wide)
2605 __argv[j++] = "-a";
2606 __argv[j++] = "-q";
2607 __argv[j++] = "-o";
2608 __argv[j++] = "-";
2609 for (i = rep_args + 1; i < argc; i++)
2610 __argv[j++] = argv[i];
2611 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002612
2613 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002614 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002615 exit(-1);
2616 }
2617
2618 dup2(live_pipe[0], 0);
2619 close(live_pipe[1]);
2620
Tom Zanussib5b87312010-11-10 08:16:51 -06002621 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002622 if (!__argv) {
2623 pr_err("malloc failed\n");
2624 err = -ENOMEM;
2625 goto out;
2626 }
2627
Tom Zanussib5b87312010-11-10 08:16:51 -06002628 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002629 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002630 __argv[j++] = rep_script_path;
2631 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002632 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002633 __argv[j++] = "-i";
2634 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002635 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002636
2637 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002638 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002639 exit(-1);
2640 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002641
Tom Zanussib5b87312010-11-10 08:16:51 -06002642 if (rec_script_path)
2643 script_path = rec_script_path;
2644 if (rep_script_path)
2645 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002646
Tom Zanussib5b87312010-11-10 08:16:51 -06002647 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002648 j = 0;
2649
Robert Richter317df652011-11-25 15:05:25 +01002650 if (!rec_script_path)
2651 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002652 else if (!system_wide) {
2653 if (have_cmd(argc - 1, &argv[1]) != 0) {
2654 err = -1;
2655 goto out;
2656 }
2657 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002658
2659 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002660 if (!__argv) {
2661 pr_err("malloc failed\n");
2662 err = -ENOMEM;
2663 goto out;
2664 }
2665
Tom Zanussib5b87312010-11-10 08:16:51 -06002666 __argv[j++] = "/bin/sh";
2667 __argv[j++] = script_path;
2668 if (system_wide)
2669 __argv[j++] = "-a";
2670 for (i = 2; i < argc; i++)
2671 __argv[j++] = argv[i];
2672 __argv[j++] = NULL;
2673
2674 execvp("/bin/sh", (char **)__argv);
2675 free(__argv);
2676 exit(-1);
2677 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002678
Tom Zanussicf4fee52010-03-03 01:04:33 -06002679 if (!script_name)
2680 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002681
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002682 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002683 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002684 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002685
Jiri Olsae90debd2013-12-09 11:02:50 +01002686 if (header || header_only) {
2687 perf_session__fprintf_info(session, stdout, show_full_info);
2688 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002689 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002690 }
2691
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002692 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002693 goto out_delete;
2694
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002695 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002696 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002697
Adrian Huntere2167082016-06-23 16:40:58 +03002698 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2699 itrace_synth_opts.thread_stack = true;
2700
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002701 session->itrace_synth_opts = &itrace_synth_opts;
2702
Anton Blanchard5d67be92011-07-04 21:57:50 +10002703 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002704 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2705 if (err < 0)
2706 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002707 }
2708
David Ahern1424dc92011-03-09 22:23:28 -07002709 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002710 symbol_conf.use_callchain = true;
2711 else
2712 symbol_conf.use_callchain = false;
2713
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002714 if (session->tevent.pevent &&
2715 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002716 machine__resolve_kernel_addr,
2717 &session->machines.host) < 0) {
2718 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2719 return -1;
2720 }
2721
Tom Zanussi956ffd02009-11-25 01:15:46 -06002722 if (generate_script_lang) {
2723 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002724 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002725
David Ahern2c9e45f72011-03-17 10:03:21 -06002726 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002727 fprintf(stderr,
2728 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002729 err = -EINVAL;
2730 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002731 }
2732
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002733 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002734 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002735 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002736 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002737 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002738 }
2739
2740 err = fstat(input, &perf_stat);
2741 if (err < 0) {
2742 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002743 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002744 }
2745
2746 if (!perf_stat.st_size) {
2747 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002748 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002749 }
2750
2751 scripting_ops = script_spec__lookup(generate_script_lang);
2752 if (!scripting_ops) {
2753 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002754 err = -ENOENT;
2755 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002756 }
2757
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002758 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002759 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002760 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002761 }
2762
2763 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002764 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002765 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002766 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002767 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002768 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002769 }
2770
David Ahern9cbdb702011-04-06 21:54:20 -06002771
2772 err = perf_session__check_output_opt(session);
2773 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002774 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002775
David Aherna91f4c42016-11-29 10:15:43 -07002776 /* needs to be parsed after looking up reference time */
2777 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
2778 pr_err("Invalid time string\n");
2779 return -EINVAL;
2780 }
2781
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002782 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002783
Adrian Hunterd445dd22014-08-15 22:08:37 +03002784 flush_scripting();
2785
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002786out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002787 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002788 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002789
2790 if (script_started)
2791 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002792out:
2793 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002794}