blob: afa84debc5c4df6645636cf33c050b084747fc8e [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;
Andi Kleen36ce5652017-06-02 08:48:10 -07001730 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07001731
1732 if (!str)
1733 return -ENOMEM;
1734
David Ahern2c9e45f72011-03-17 10:03:21 -06001735 /* first word can state for which event type the user is specifying
1736 * the fields. If no type exists, the specified fields apply to all
1737 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001738 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001739 tok = strchr(str, ':');
1740 if (tok) {
1741 *tok = '\0';
1742 tok++;
1743 if (!strcmp(str, "hw"))
1744 type = PERF_TYPE_HARDWARE;
1745 else if (!strcmp(str, "sw"))
1746 type = PERF_TYPE_SOFTWARE;
1747 else if (!strcmp(str, "trace"))
1748 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001749 else if (!strcmp(str, "raw"))
1750 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001751 else if (!strcmp(str, "break"))
1752 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001753 else {
1754 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001755 rc = -EINVAL;
1756 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001757 }
1758
1759 if (output[type].user_set)
1760 pr_warning("Overriding previous field request for %s events.\n",
1761 event_type(type));
1762
1763 output[type].fields = 0;
1764 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001765 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001766
1767 } else {
1768 tok = str;
1769 if (strlen(str) == 0) {
1770 fprintf(stderr,
1771 "Cannot set fields to 'none' for all event types.\n");
1772 rc = -EINVAL;
1773 goto out;
1774 }
1775
Andi Kleen36ce5652017-06-02 08:48:10 -07001776 /* Don't override defaults for +- */
1777 if (strchr(str, '+') || strchr(str, '-'))
1778 goto parse;
1779
David Ahern2c9e45f72011-03-17 10:03:21 -06001780 if (output_set_by_user())
1781 pr_warning("Overriding previous field request for all events.\n");
1782
1783 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1784 output[j].fields = 0;
1785 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001786 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001787 }
David Ahern1424dc92011-03-09 22:23:28 -07001788 }
1789
Andi Kleen36ce5652017-06-02 08:48:10 -07001790parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001791 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07001792 if (*tok == '+') {
1793 if (change == SET)
1794 goto out_badmix;
1795 change = ADD;
1796 tok++;
1797 } else if (*tok == '-') {
1798 if (change == SET)
1799 goto out_badmix;
1800 change = REMOVE;
1801 tok++;
1802 } else {
1803 if (change != SET && change != DEFAULT)
1804 goto out_badmix;
1805 change = SET;
1806 }
1807
David Ahern745f43e2011-03-09 22:23:26 -07001808 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001809 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001810 break;
David Ahern745f43e2011-03-09 22:23:26 -07001811 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001812 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07001813 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001814 continue;
1815 }
David Ahern745f43e2011-03-09 22:23:26 -07001816 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001817 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001818 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001819 goto out;
1820 }
1821
1822 if (type == -1) {
1823 /* add user option to all events types for
1824 * which it is valid
1825 */
1826 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1827 if (output[j].invalid_fields & all_output_options[i].field) {
1828 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1829 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07001830 } else {
1831 if (change == REMOVE)
1832 output[j].fields &= ~all_output_options[i].field;
1833 else
1834 output[j].fields |= all_output_options[i].field;
1835 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001836 }
1837 } else {
1838 if (output[type].invalid_fields & all_output_options[i].field) {
1839 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1840 all_output_options[i].str, event_type(type));
1841
1842 rc = -EINVAL;
1843 goto out;
1844 }
1845 output[type].fields |= all_output_options[i].field;
1846 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001847 }
1848
1849 if (type >= 0) {
1850 if (output[type].fields == 0) {
1851 pr_debug("No fields requested for %s type. "
1852 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001853 }
David Ahern1424dc92011-03-09 22:23:28 -07001854 }
Andi Kleen36ce5652017-06-02 08:48:10 -07001855 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07001856
Andi Kleen36ce5652017-06-02 08:48:10 -07001857out_badmix:
1858 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
1859 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001860out:
David Ahern745f43e2011-03-09 22:23:26 -07001861 free(str);
1862 return rc;
1863}
1864
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001865/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1866static int is_directory(const char *base_path, const struct dirent *dent)
1867{
1868 char path[PATH_MAX];
1869 struct stat st;
1870
1871 sprintf(path, "%s/%s", base_path, dent->d_name);
1872 if (stat(path, &st))
1873 return 0;
1874
1875 return S_ISDIR(st.st_mode);
1876}
1877
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001878#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1879 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1880 if ((lang_dirent->d_type == DT_DIR || \
1881 (lang_dirent->d_type == DT_UNKNOWN && \
1882 is_directory(scripts_path, lang_dirent))) && \
1883 (strcmp(lang_dirent->d_name, ".")) && \
1884 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001885
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001886#define for_each_script(lang_path, lang_dir, script_dirent) \
1887 while ((script_dirent = readdir(lang_dir)) != NULL) \
1888 if (script_dirent->d_type != DT_DIR && \
1889 (script_dirent->d_type != DT_UNKNOWN || \
1890 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001891
1892
1893#define RECORD_SUFFIX "-record"
1894#define REPORT_SUFFIX "-report"
1895
1896struct script_desc {
1897 struct list_head node;
1898 char *name;
1899 char *half_liner;
1900 char *args;
1901};
1902
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001903static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001904
1905static struct script_desc *script_desc__new(const char *name)
1906{
1907 struct script_desc *s = zalloc(sizeof(*s));
1908
Tom Zanussib5b87312010-11-10 08:16:51 -06001909 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001910 s->name = strdup(name);
1911
1912 return s;
1913}
1914
1915static void script_desc__delete(struct script_desc *s)
1916{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001917 zfree(&s->name);
1918 zfree(&s->half_liner);
1919 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001920 free(s);
1921}
1922
1923static void script_desc__add(struct script_desc *s)
1924{
1925 list_add_tail(&s->node, &script_descs);
1926}
1927
1928static struct script_desc *script_desc__find(const char *name)
1929{
1930 struct script_desc *s;
1931
1932 list_for_each_entry(s, &script_descs, node)
1933 if (strcasecmp(s->name, name) == 0)
1934 return s;
1935 return NULL;
1936}
1937
1938static struct script_desc *script_desc__findnew(const char *name)
1939{
1940 struct script_desc *s = script_desc__find(name);
1941
1942 if (s)
1943 return s;
1944
1945 s = script_desc__new(name);
1946 if (!s)
1947 goto out_delete_desc;
1948
1949 script_desc__add(s);
1950
1951 return s;
1952
1953out_delete_desc:
1954 script_desc__delete(s);
1955
1956 return NULL;
1957}
1958
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001959static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001960{
1961 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001962 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001963
1964 if (strlen(str) > suffix_len) {
1965 p = str + strlen(str) - suffix_len;
1966 if (!strncmp(p, suffix, suffix_len))
1967 return p;
1968 }
1969
1970 return NULL;
1971}
1972
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001973static int read_script_info(struct script_desc *desc, const char *filename)
1974{
1975 char line[BUFSIZ], *p;
1976 FILE *fp;
1977
1978 fp = fopen(filename, "r");
1979 if (!fp)
1980 return -1;
1981
1982 while (fgets(line, sizeof(line), fp)) {
1983 p = ltrim(line);
1984 if (strlen(p) == 0)
1985 continue;
1986 if (*p != '#')
1987 continue;
1988 p++;
1989 if (strlen(p) && *p == '!')
1990 continue;
1991
1992 p = ltrim(p);
1993 if (strlen(p) && p[strlen(p) - 1] == '\n')
1994 p[strlen(p) - 1] = '\0';
1995
1996 if (!strncmp(p, "description:", strlen("description:"))) {
1997 p += strlen("description:");
1998 desc->half_liner = strdup(ltrim(p));
1999 continue;
2000 }
2001
2002 if (!strncmp(p, "args:", strlen("args:"))) {
2003 p += strlen("args:");
2004 desc->args = strdup(ltrim(p));
2005 continue;
2006 }
2007 }
2008
2009 fclose(fp);
2010
2011 return 0;
2012}
2013
Robert Richter38efb532011-11-25 11:38:40 +01002014static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2015{
2016 char *script_root, *str;
2017
2018 script_root = strdup(script_dirent->d_name);
2019 if (!script_root)
2020 return NULL;
2021
2022 str = (char *)ends_with(script_root, suffix);
2023 if (!str) {
2024 free(script_root);
2025 return NULL;
2026 }
2027
2028 *str = '\0';
2029 return script_root;
2030}
2031
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002032static int list_available_scripts(const struct option *opt __maybe_unused,
2033 const char *s __maybe_unused,
2034 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002035{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002036 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002037 char scripts_path[MAXPATHLEN];
2038 DIR *scripts_dir, *lang_dir;
2039 char script_path[MAXPATHLEN];
2040 char lang_path[MAXPATHLEN];
2041 struct script_desc *desc;
2042 char first_half[BUFSIZ];
2043 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002044
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002045 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002046
2047 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002048 if (!scripts_dir) {
2049 fprintf(stdout,
2050 "open(%s) failed.\n"
2051 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2052 scripts_path);
2053 exit(-1);
2054 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002055
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002056 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002057 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002058 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002059 lang_dir = opendir(lang_path);
2060 if (!lang_dir)
2061 continue;
2062
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002063 for_each_script(lang_path, lang_dir, script_dirent) {
2064 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002065 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002066 desc = script_desc__findnew(script_root);
2067 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002068 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002069 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002070 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002071 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002072 }
2073 }
2074
2075 fprintf(stdout, "List of available trace scripts:\n");
2076 list_for_each_entry(desc, &script_descs, node) {
2077 sprintf(first_half, "%s %s", desc->name,
2078 desc->args ? desc->args : "");
2079 fprintf(stdout, " %-36s %s\n", first_half,
2080 desc->half_liner ? desc->half_liner : "");
2081 }
2082
2083 exit(0);
2084}
2085
Feng Tange5f37052012-09-07 16:42:26 +08002086/*
Feng Tang49e639e2012-10-30 11:56:03 +08002087 * Some scripts specify the required events in their "xxx-record" file,
2088 * this function will check if the events in perf.data match those
2089 * mentioned in the "xxx-record".
2090 *
2091 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2092 * which is covered well now. And new parsing code should be added to
2093 * cover the future complexing formats like event groups etc.
2094 */
2095static int check_ev_match(char *dir_name, char *scriptname,
2096 struct perf_session *session)
2097{
2098 char filename[MAXPATHLEN], evname[128];
2099 char line[BUFSIZ], *p;
2100 struct perf_evsel *pos;
2101 int match, len;
2102 FILE *fp;
2103
2104 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2105
2106 fp = fopen(filename, "r");
2107 if (!fp)
2108 return -1;
2109
2110 while (fgets(line, sizeof(line), fp)) {
2111 p = ltrim(line);
2112 if (*p == '#')
2113 continue;
2114
2115 while (strlen(p)) {
2116 p = strstr(p, "-e");
2117 if (!p)
2118 break;
2119
2120 p += 2;
2121 p = ltrim(p);
2122 len = strcspn(p, " \t");
2123 if (!len)
2124 break;
2125
2126 snprintf(evname, len + 1, "%s", p);
2127
2128 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002129 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002130 if (!strcmp(perf_evsel__name(pos), evname)) {
2131 match = 1;
2132 break;
2133 }
2134 }
2135
2136 if (!match) {
2137 fclose(fp);
2138 return -1;
2139 }
2140 }
2141 }
2142
2143 fclose(fp);
2144 return 0;
2145}
2146
2147/*
Feng Tange5f37052012-09-07 16:42:26 +08002148 * Return -1 if none is found, otherwise the actual scripts number.
2149 *
2150 * Currently the only user of this function is the script browser, which
2151 * will list all statically runnable scripts, select one, execute it and
2152 * show the output in a perf browser.
2153 */
2154int find_scripts(char **scripts_array, char **scripts_path_array)
2155{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002156 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002157 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002158 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002159 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002160 struct perf_data_file file = {
2161 .path = input_name,
2162 .mode = PERF_DATA_MODE_READ,
2163 };
Feng Tange5f37052012-09-07 16:42:26 +08002164 char *temp;
2165 int i = 0;
2166
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002167 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002168 if (!session)
2169 return -1;
2170
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002171 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002172
2173 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002174 if (!scripts_dir) {
2175 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002176 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002177 }
Feng Tange5f37052012-09-07 16:42:26 +08002178
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002179 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002180 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002181 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002182#ifdef NO_LIBPERL
2183 if (strstr(lang_path, "perl"))
2184 continue;
2185#endif
2186#ifdef NO_LIBPYTHON
2187 if (strstr(lang_path, "python"))
2188 continue;
2189#endif
2190
2191 lang_dir = opendir(lang_path);
2192 if (!lang_dir)
2193 continue;
2194
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002195 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002196 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002197 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002198 continue;
2199 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002200 script_dirent->d_name);
2201 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002202 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002203 (temp - script_dirent->d_name) + 1,
2204 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002205
2206 if (check_ev_match(lang_path,
2207 scripts_array[i], session))
2208 continue;
2209
Feng Tange5f37052012-09-07 16:42:26 +08002210 i++;
2211 }
Feng Tang49e639e2012-10-30 11:56:03 +08002212 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002213 }
2214
Feng Tang49e639e2012-10-30 11:56:03 +08002215 closedir(scripts_dir);
2216 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002217 return i;
2218}
2219
Tom Zanussi38752942009-12-15 02:53:39 -06002220static char *get_script_path(const char *script_root, const char *suffix)
2221{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002222 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002223 char scripts_path[MAXPATHLEN];
2224 char script_path[MAXPATHLEN];
2225 DIR *scripts_dir, *lang_dir;
2226 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002227 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002228
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002229 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002230
2231 scripts_dir = opendir(scripts_path);
2232 if (!scripts_dir)
2233 return NULL;
2234
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002235 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002236 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002237 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002238 lang_dir = opendir(lang_path);
2239 if (!lang_dir)
2240 continue;
2241
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002242 for_each_script(lang_path, lang_dir, script_dirent) {
2243 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002244 if (__script_root && !strcmp(script_root, __script_root)) {
2245 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002246 closedir(lang_dir);
2247 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002248 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002249 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002250 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002251 }
2252 free(__script_root);
2253 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002254 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002255 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002256 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002257
Robert Richter38efb532011-11-25 11:38:40 +01002258 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002259}
2260
Tom Zanussib5b87312010-11-10 08:16:51 -06002261static bool is_top_script(const char *script_path)
2262{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002263 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002264}
2265
2266static int has_required_arg(char *script_path)
2267{
2268 struct script_desc *desc;
2269 int n_args = 0;
2270 char *p;
2271
2272 desc = script_desc__new(NULL);
2273
2274 if (read_script_info(desc, script_path))
2275 goto out;
2276
2277 if (!desc->args)
2278 goto out;
2279
2280 for (p = desc->args; *p; p++)
2281 if (*p == '<')
2282 n_args++;
2283out:
2284 script_desc__delete(desc);
2285
2286 return n_args;
2287}
2288
David Ahernd54b1a92012-08-26 12:24:46 -06002289static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002290{
2291 char **__argv = malloc(sizeof(const char *) * argc);
2292
David Ahernd54b1a92012-08-26 12:24:46 -06002293 if (!__argv) {
2294 pr_err("malloc failed\n");
2295 return -1;
2296 }
2297
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002298 memcpy(__argv, argv, sizeof(const char *) * argc);
2299 argc = parse_options(argc, (const char **)__argv, record_options,
2300 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2301 free(__argv);
2302
David Ahernd54b1a92012-08-26 12:24:46 -06002303 system_wide = (argc == 0);
2304
2305 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002306}
2307
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002308static void script__setup_sample_type(struct perf_script *script)
2309{
2310 struct perf_session *session = script->session;
2311 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2312
2313 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2314 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2315 (sample_type & PERF_SAMPLE_STACK_USER))
2316 callchain_param.record_mode = CALLCHAIN_DWARF;
2317 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2318 callchain_param.record_mode = CALLCHAIN_LBR;
2319 else
2320 callchain_param.record_mode = CALLCHAIN_FP;
2321 }
2322}
2323
Jiri Olsae099eba2016-01-05 22:09:09 +01002324static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2325 union perf_event *event,
2326 struct perf_session *session)
2327{
2328 struct stat_round_event *round = &event->stat_round;
2329 struct perf_evsel *counter;
2330
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002331 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002332 perf_stat_process_counter(&stat_config, counter);
2333 process_stat(counter, round->time);
2334 }
2335
2336 process_stat_interval(round->time);
2337 return 0;
2338}
2339
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002340static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2341 union perf_event *event,
2342 struct perf_session *session __maybe_unused)
2343{
2344 perf_event__read_stat_config(&stat_config, &event->stat_config);
2345 return 0;
2346}
2347
Jiri Olsacfc88742016-01-05 22:09:06 +01002348static int set_maps(struct perf_script *script)
2349{
2350 struct perf_evlist *evlist = script->session->evlist;
2351
2352 if (!script->cpus || !script->threads)
2353 return 0;
2354
2355 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2356 return -EINVAL;
2357
2358 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2359
2360 if (perf_evlist__alloc_stats(evlist, true))
2361 return -ENOMEM;
2362
2363 script->allocated = true;
2364 return 0;
2365}
2366
2367static
2368int process_thread_map_event(struct perf_tool *tool,
2369 union perf_event *event,
2370 struct perf_session *session __maybe_unused)
2371{
2372 struct perf_script *script = container_of(tool, struct perf_script, tool);
2373
2374 if (script->threads) {
2375 pr_warning("Extra thread map event, ignoring.\n");
2376 return 0;
2377 }
2378
2379 script->threads = thread_map__new_event(&event->thread_map);
2380 if (!script->threads)
2381 return -ENOMEM;
2382
2383 return set_maps(script);
2384}
2385
2386static
2387int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2388 union perf_event *event,
2389 struct perf_session *session __maybe_unused)
2390{
2391 struct perf_script *script = container_of(tool, struct perf_script, tool);
2392
2393 if (script->cpus) {
2394 pr_warning("Extra cpu map event, ignoring.\n");
2395 return 0;
2396 }
2397
2398 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2399 if (!script->cpus)
2400 return -ENOMEM;
2401
2402 return set_maps(script);
2403}
2404
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002405int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002406{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002407 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002408 bool header = false;
2409 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002410 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002411 char *rec_script_path = NULL;
2412 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002413 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002414 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002415 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002416 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002417 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002418 struct perf_script script = {
2419 .tool = {
2420 .sample = process_sample_event,
2421 .mmap = perf_event__process_mmap,
2422 .mmap2 = perf_event__process_mmap2,
2423 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302424 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002425 .exit = perf_event__process_exit,
2426 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002427 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002428 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002429 .tracing_data = perf_event__process_tracing_data,
2430 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002431 .id_index = perf_event__process_id_index,
2432 .auxtrace_info = perf_event__process_auxtrace_info,
2433 .auxtrace = perf_event__process_auxtrace,
2434 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002435 .stat = perf_event__process_stat_event,
2436 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002437 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002438 .thread_map = process_thread_map_event,
2439 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002440 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002441 .ordering_requires_timestamps = true,
2442 },
2443 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002444 struct perf_data_file file = {
2445 .mode = PERF_DATA_MODE_READ,
2446 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002447 const struct option options[] = {
2448 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2449 "dump raw trace in ASCII"),
2450 OPT_INCR('v', "verbose", &verbose,
2451 "be more verbose (show symbol address, etc)"),
2452 OPT_BOOLEAN('L', "Latency", &latency_format,
2453 "show latency attributes (irqs/preemption disabled, etc)"),
2454 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2455 list_available_scripts),
2456 OPT_CALLBACK('s', "script", NULL, "name",
2457 "script file name (lang:script name, script name, or *)",
2458 parse_scriptname),
2459 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2460 "generate perf-script.xx script in specified language"),
2461 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2462 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2463 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002464 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2465 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002466 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2467 "file", "vmlinux pathname"),
2468 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2469 "file", "kallsyms pathname"),
2470 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2471 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002472 OPT_CALLBACK(0, "symfs", NULL, "directory",
2473 "Look for files with symbols relative to this directory",
2474 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002475 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002476 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07002477 "+field to add and -field to remove."
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002478 "Valid types: hw,sw,trace,raw. "
2479 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Huntere2167082016-06-23 16:40:58 +03002480 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
Andi Kleen48d02a12017-02-23 15:46:34 -08002481 "bpf-output,callindent,insn,insnlen,brstackinsn",
2482 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002483 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2484 "system-wide collection from all CPUs"),
2485 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2486 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002487 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2488 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002489 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2490 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2491 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002492 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2493 "only consider symbols in these pids"),
2494 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2495 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002496 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2497 "Set the maximum stack depth when parsing the callchain, "
2498 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002499 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002500 OPT_BOOLEAN('I', "show-info", &show_full_info,
2501 "display extended information from perf.data file"),
2502 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2503 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002504 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2505 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002506 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2507 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002508 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2509 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302510 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2511 "Show namespace events (if recorded)"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002512 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002513 OPT_INTEGER(0, "max-blocks", &max_blocks,
2514 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002515 OPT_BOOLEAN(0, "ns", &nanosecs,
2516 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002517 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2518 "Instruction Tracing options",
2519 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002520 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2521 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002522 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2523 "Enable symbol demangling"),
2524 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2525 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002526 OPT_STRING(0, "time", &script.time_str, "str",
2527 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09002528 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2529 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002530 OPT_END()
2531 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002532 const char * const script_subcommands[] = { "record", "report", NULL };
2533 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002534 "perf script [<options>]",
2535 "perf script [<options>] record <script> [<record-options>] <command>",
2536 "perf script [<options>] report <script> [script-args]",
2537 "perf script [<options>] <script> [<record-options>] <command>",
2538 "perf script [<options>] <top-script> [script-args]",
2539 NULL
2540 };
Tom Zanussi38752942009-12-15 02:53:39 -06002541
Tom Zanussib5b87312010-11-10 08:16:51 -06002542 setup_scripting();
2543
Yunlong Song40cae2b2015-03-18 21:35:54 +08002544 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002545 PARSE_OPT_STOP_AT_NON_OPTION);
2546
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002547 file.path = input_name;
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002548 file.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002549
Tom Zanussib5b87312010-11-10 08:16:51 -06002550 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2551 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2552 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002553 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002554 }
2555
Tom Zanussib5b87312010-11-10 08:16:51 -06002556 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2557 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2558 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002559 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002560 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002561 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002562 return -1;
2563 }
Tom Zanussi38752942009-12-15 02:53:39 -06002564 }
2565
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002566 if (itrace_synth_opts.callchain &&
2567 itrace_synth_opts.callchain_sz > scripting_max_stack)
2568 scripting_max_stack = itrace_synth_opts.callchain_sz;
2569
Ben Hutchings44e668c2010-10-10 16:10:03 +01002570 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002571 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002572
Tom Zanussib5b87312010-11-10 08:16:51 -06002573 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002574 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002575 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002576 pid_t pid;
2577
Tom Zanussib5b87312010-11-10 08:16:51 -06002578 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2579 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2580
2581 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002582 usage_with_options_msg(script_usage, options,
2583 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002584 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002585 }
2586
Tom Zanussib5b87312010-11-10 08:16:51 -06002587 if (is_top_script(argv[0])) {
2588 rep_args = argc - 1;
2589 } else {
2590 int rec_args;
2591
2592 rep_args = has_required_arg(rep_script_path);
2593 rec_args = (argc - 1) - rep_args;
2594 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002595 usage_with_options_msg(script_usage, options,
2596 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002597 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002598 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002599 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002600 }
2601
2602 if (pipe(live_pipe) < 0) {
2603 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002604 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002605 }
2606
2607 pid = fork();
2608 if (pid < 0) {
2609 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002610 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002611 }
2612
2613 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002614 j = 0;
2615
Tom Zanussia0cccc22010-04-01 23:59:25 -05002616 dup2(live_pipe[1], 1);
2617 close(live_pipe[0]);
2618
Robert Richter317df652011-11-25 15:05:25 +01002619 if (is_top_script(argv[0])) {
2620 system_wide = true;
2621 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002622 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2623 err = -1;
2624 goto out;
2625 }
Robert Richter317df652011-11-25 15:05:25 +01002626 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002627
2628 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002629 if (!__argv) {
2630 pr_err("malloc failed\n");
2631 err = -ENOMEM;
2632 goto out;
2633 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002634
Tom Zanussib5b87312010-11-10 08:16:51 -06002635 __argv[j++] = "/bin/sh";
2636 __argv[j++] = rec_script_path;
2637 if (system_wide)
2638 __argv[j++] = "-a";
2639 __argv[j++] = "-q";
2640 __argv[j++] = "-o";
2641 __argv[j++] = "-";
2642 for (i = rep_args + 1; i < argc; i++)
2643 __argv[j++] = argv[i];
2644 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002645
2646 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002647 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002648 exit(-1);
2649 }
2650
2651 dup2(live_pipe[0], 0);
2652 close(live_pipe[1]);
2653
Tom Zanussib5b87312010-11-10 08:16:51 -06002654 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002655 if (!__argv) {
2656 pr_err("malloc failed\n");
2657 err = -ENOMEM;
2658 goto out;
2659 }
2660
Tom Zanussib5b87312010-11-10 08:16:51 -06002661 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002662 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002663 __argv[j++] = rep_script_path;
2664 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002665 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002666 __argv[j++] = "-i";
2667 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002668 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002669
2670 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002671 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002672 exit(-1);
2673 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002674
Tom Zanussib5b87312010-11-10 08:16:51 -06002675 if (rec_script_path)
2676 script_path = rec_script_path;
2677 if (rep_script_path)
2678 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002679
Tom Zanussib5b87312010-11-10 08:16:51 -06002680 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002681 j = 0;
2682
Robert Richter317df652011-11-25 15:05:25 +01002683 if (!rec_script_path)
2684 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002685 else if (!system_wide) {
2686 if (have_cmd(argc - 1, &argv[1]) != 0) {
2687 err = -1;
2688 goto out;
2689 }
2690 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002691
2692 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002693 if (!__argv) {
2694 pr_err("malloc failed\n");
2695 err = -ENOMEM;
2696 goto out;
2697 }
2698
Tom Zanussib5b87312010-11-10 08:16:51 -06002699 __argv[j++] = "/bin/sh";
2700 __argv[j++] = script_path;
2701 if (system_wide)
2702 __argv[j++] = "-a";
2703 for (i = 2; i < argc; i++)
2704 __argv[j++] = argv[i];
2705 __argv[j++] = NULL;
2706
2707 execvp("/bin/sh", (char **)__argv);
2708 free(__argv);
2709 exit(-1);
2710 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002711
Tom Zanussicf4fee52010-03-03 01:04:33 -06002712 if (!script_name)
2713 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002714
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002715 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002716 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002717 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002718
Jiri Olsae90debd2013-12-09 11:02:50 +01002719 if (header || header_only) {
2720 perf_session__fprintf_info(session, stdout, show_full_info);
2721 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002722 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002723 }
2724
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002725 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002726 goto out_delete;
2727
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002728 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002729 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002730
Adrian Huntere2167082016-06-23 16:40:58 +03002731 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2732 itrace_synth_opts.thread_stack = true;
2733
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002734 session->itrace_synth_opts = &itrace_synth_opts;
2735
Anton Blanchard5d67be92011-07-04 21:57:50 +10002736 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002737 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2738 if (err < 0)
2739 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002740 }
2741
David Ahern1424dc92011-03-09 22:23:28 -07002742 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002743 symbol_conf.use_callchain = true;
2744 else
2745 symbol_conf.use_callchain = false;
2746
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002747 if (session->tevent.pevent &&
2748 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002749 machine__resolve_kernel_addr,
2750 &session->machines.host) < 0) {
2751 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2752 return -1;
2753 }
2754
Tom Zanussi956ffd02009-11-25 01:15:46 -06002755 if (generate_script_lang) {
2756 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002757 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002758
David Ahern2c9e45f72011-03-17 10:03:21 -06002759 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002760 fprintf(stderr,
2761 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002762 err = -EINVAL;
2763 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002764 }
2765
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002766 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002767 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002768 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002769 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002770 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002771 }
2772
2773 err = fstat(input, &perf_stat);
2774 if (err < 0) {
2775 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002776 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002777 }
2778
2779 if (!perf_stat.st_size) {
2780 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002781 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002782 }
2783
2784 scripting_ops = script_spec__lookup(generate_script_lang);
2785 if (!scripting_ops) {
2786 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002787 err = -ENOENT;
2788 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002789 }
2790
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002791 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002792 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002793 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002794 }
2795
2796 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002797 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002798 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002799 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002800 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002801 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002802 }
2803
David Ahern9cbdb702011-04-06 21:54:20 -06002804
2805 err = perf_session__check_output_opt(session);
2806 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002807 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002808
David Aherna91f4c42016-11-29 10:15:43 -07002809 /* needs to be parsed after looking up reference time */
2810 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
2811 pr_err("Invalid time string\n");
2812 return -EINVAL;
2813 }
2814
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002815 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002816
Adrian Hunterd445dd22014-08-15 22:08:37 +03002817 flush_scripting();
2818
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002819out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002820 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002821 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002822
2823 if (script_started)
2824 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002825out:
2826 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002827}