blob: 3c21089f52736af7162d717bad51487317479cf9 [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 }
Mark Santaniello55b9b502017-06-19 09:38:24 -0700301 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR) &&
302 !PRINT_FIELD(BRSTACK) && !PRINT_FIELD(BRSTACKSYM)) {
303 pr_err("Display of DSO requested but none of sample IP, sample address, "
304 "brstack\nor brstacksym are selected. Hence, no addresses to "
305 "convert to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600306 return -EINVAL;
307 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200308 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
309 pr_err("Display of source line number requested but sample IP is not\n"
310 "selected. Hence, no address to lookup the source line number.\n");
311 return -EINVAL;
312 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800313 if (PRINT_FIELD(BRSTACKINSN) &&
314 !(perf_evlist__combined_branch_type(session->evlist) &
315 PERF_SAMPLE_BRANCH_ANY)) {
316 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
317 "Hint: run 'perf record -b ...'\n");
318 return -EINVAL;
319 }
David Ahern1424dc92011-03-09 22:23:28 -0700320 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300321 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
322 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700323 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700324
325 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300326 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
327 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700328 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700329
330 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300331 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
332 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700333 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600334
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200335 if (PRINT_FIELD(PERIOD) &&
336 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
337 PERF_OUTPUT_PERIOD))
338 return -EINVAL;
339
Stephane Eranianfc36f942015-08-31 18:41:10 +0200340 if (PRINT_FIELD(IREGS) &&
341 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
342 PERF_OUTPUT_IREGS))
343 return -EINVAL;
344
David Ahern9cbdb702011-04-06 21:54:20 -0600345 return 0;
346}
347
Adrian Hunter7ea95722013-11-01 15:51:30 +0200348static void set_print_ip_opts(struct perf_event_attr *attr)
349{
350 unsigned int type = attr->type;
351
352 output[type].print_ip_opts = 0;
353 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300354 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200355
356 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300357 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200358
359 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300360 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200361
362 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300363 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200364
365 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300366 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200367}
368
David Ahern9cbdb702011-04-06 21:54:20 -0600369/*
370 * verify all user requested events exist and the samples
371 * have the expected data
372 */
373static int perf_session__check_output_opt(struct perf_session *session)
374{
He Kuang40f20e52016-05-16 04:51:19 +0000375 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600376 struct perf_evsel *evsel;
377
378 for (j = 0; j < PERF_TYPE_MAX; ++j) {
379 evsel = perf_session__find_first_evtype(session, j);
380
381 /*
382 * even if fields is set to 0 (ie., show nothing) event must
383 * exist if user explicitly includes it on the command line
384 */
385 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
386 pr_err("%s events do not exist. "
387 "Remove corresponding -f option to proceed.\n",
388 event_type(j));
389 return -1;
390 }
391
392 if (evsel && output[j].fields &&
393 perf_evsel__check_attr(evsel, session))
394 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400395
396 if (evsel == NULL)
397 continue;
398
Adrian Hunter7ea95722013-11-01 15:51:30 +0200399 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700400 }
401
Adrian Hunter98526ee2014-07-31 09:00:59 +0300402 if (!no_callchain) {
403 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000404 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300405
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300406 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000407 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300408 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
409 use_callchain = true;
410 break;
411 }
412 }
He Kuang71ac8992016-08-04 11:25:43 +0000413 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300414 symbol_conf.use_callchain = false;
415 }
416
David Ahern80b8b492013-11-19 21:07:37 -0700417 /*
418 * set default for tracepoints to print symbols only
419 * if callchains are present
420 */
421 if (symbol_conf.use_callchain &&
422 !output[PERF_TYPE_TRACEPOINT].user_set) {
423 struct perf_event_attr *attr;
424
425 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700426
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300427 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000428 if (evsel->attr.type != j)
429 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700430
He Kuang40f20e52016-05-16 04:51:19 +0000431 attr = &evsel->attr;
432
433 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
434 output[j].fields |= PERF_OUTPUT_IP;
435 output[j].fields |= PERF_OUTPUT_SYM;
436 output[j].fields |= PERF_OUTPUT_DSO;
437 set_print_ip_opts(attr);
438 goto out;
439 }
David Ahern80b8b492013-11-19 21:07:37 -0700440 }
441 }
442
443out:
David Ahern1424dc92011-03-09 22:23:28 -0700444 return 0;
445}
David Ahern745f43e2011-03-09 22:23:26 -0700446
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300447static void print_sample_iregs(struct perf_sample *sample,
Stephane Eranianfc36f942015-08-31 18:41:10 +0200448 struct perf_event_attr *attr)
449{
450 struct regs_dump *regs = &sample->intr_regs;
451 uint64_t mask = attr->sample_regs_intr;
452 unsigned i = 0, r;
453
454 if (!regs)
455 return;
456
457 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
458 u64 val = regs->regs[i++];
459 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
460 }
461}
462
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300463static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700464 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300465 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700466{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300467 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700468 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700469 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700470
David Ahern745f43e2011-03-09 22:23:26 -0700471 if (PRINT_FIELD(COMM)) {
472 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200473 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600474 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200475 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700476 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200477 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700478 }
David Ahernc70c94b2011-03-09 22:23:25 -0700479
David Ahern745f43e2011-03-09 22:23:26 -0700480 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
481 printf("%5d/%-5d ", sample->pid, sample->tid);
482 else if (PRINT_FIELD(PID))
483 printf("%5d ", sample->pid);
484 else if (PRINT_FIELD(TID))
485 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700486
David Ahern745f43e2011-03-09 22:23:26 -0700487 if (PRINT_FIELD(CPU)) {
488 if (latency_format)
489 printf("%3d ", sample->cpu);
490 else
491 printf("[%03d] ", sample->cpu);
492 }
David Ahernc70c94b2011-03-09 22:23:25 -0700493
David Ahern745f43e2011-03-09 22:23:26 -0700494 if (PRINT_FIELD(TIME)) {
495 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300496 secs = nsecs / NSEC_PER_SEC;
497 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900498
Adrian Hunter83e19862015-09-25 16:15:36 +0300499 if (nanosecs)
500 printf("%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900501 else {
502 char sample_time[32];
503 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
504 printf("%12s: ", sample_time);
505 }
David Ahern745f43e2011-03-09 22:23:26 -0700506 }
David Ahernc70c94b2011-03-09 22:23:25 -0700507}
508
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200509static inline char
510mispred_str(struct branch_entry *br)
511{
512 if (!(br->flags.mispred || br->flags.predicted))
513 return '-';
514
515 return br->flags.predicted ? 'P' : 'M';
516}
517
Mark Santaniello55b9b502017-06-19 09:38:24 -0700518static void print_sample_brstack(struct perf_sample *sample,
519 struct thread *thread,
520 struct perf_event_attr *attr)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200521{
522 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700523 struct addr_location alf, alt;
524 u64 i, from, to;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200525
526 if (!(br && br->nr))
527 return;
528
529 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700530 from = br->entries[i].from;
531 to = br->entries[i].to;
532
533 if (PRINT_FIELD(DSO)) {
534 memset(&alf, 0, sizeof(alf));
535 memset(&alt, 0, sizeof(alt));
536 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
537 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
538 }
539
540 printf("0x%"PRIx64, from);
541 if (PRINT_FIELD(DSO)) {
542 printf("(");
543 map__fprintf_dsoname(alf.map, stdout);
544 printf(")");
545 }
546
547 printf("/0x%"PRIx64, to);
548 if (PRINT_FIELD(DSO)) {
549 printf("(");
550 map__fprintf_dsoname(alt.map, stdout);
551 printf(")");
552 }
553
554 printf("/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200555 mispred_str( br->entries + i),
556 br->entries[i].flags.in_tx? 'X' : '-',
557 br->entries[i].flags.abort? 'A' : '-',
558 br->entries[i].flags.cycles);
559 }
560}
561
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300562static void print_sample_brstacksym(struct perf_sample *sample,
Mark Santaniello55b9b502017-06-19 09:38:24 -0700563 struct thread *thread,
564 struct perf_event_attr *attr)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200565{
566 struct branch_stack *br = sample->branch_stack;
567 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200568 u64 i, from, to;
569
570 if (!(br && br->nr))
571 return;
572
573 for (i = 0; i < br->nr; i++) {
574
575 memset(&alf, 0, sizeof(alf));
576 memset(&alt, 0, sizeof(alt));
577 from = br->entries[i].from;
578 to = br->entries[i].to;
579
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300580 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200581 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300582 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200583
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300584 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200585 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300586 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200587
588 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700589 if (PRINT_FIELD(DSO)) {
590 printf("(");
591 map__fprintf_dsoname(alf.map, stdout);
592 printf(")");
593 }
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200594 putchar('/');
595 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700596 if (PRINT_FIELD(DSO)) {
597 printf("(");
598 map__fprintf_dsoname(alt.map, stdout);
599 printf(")");
600 }
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200601 printf("/%c/%c/%c/%d ",
602 mispred_str( br->entries + i),
603 br->entries[i].flags.in_tx? 'X' : '-',
604 br->entries[i].flags.abort? 'A' : '-',
605 br->entries[i].flags.cycles);
606 }
607}
608
Andi Kleen48d02a12017-02-23 15:46:34 -0800609#define MAXBB 16384UL
610
611static int grab_bb(u8 *buffer, u64 start, u64 end,
612 struct machine *machine, struct thread *thread,
613 bool *is64bit, u8 *cpumode, bool last)
614{
615 long offset, len;
616 struct addr_location al;
617 bool kernel;
618
619 if (!start || !end)
620 return 0;
621
622 kernel = machine__kernel_ip(machine, start);
623 if (kernel)
624 *cpumode = PERF_RECORD_MISC_KERNEL;
625 else
626 *cpumode = PERF_RECORD_MISC_USER;
627
628 /*
629 * Block overlaps between kernel and user.
630 * This can happen due to ring filtering
631 * On Intel CPUs the entry into the kernel is filtered,
632 * but the exit is not. Let the caller patch it up.
633 */
634 if (kernel != machine__kernel_ip(machine, end)) {
635 printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
636 start, end);
637 return -ENXIO;
638 }
639
640 memset(&al, 0, sizeof(al));
641 if (end - start > MAXBB - MAXINSN) {
642 if (last)
643 printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
644 else
645 printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
646 return 0;
647 }
648
649 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
650 if (!al.map || !al.map->dso) {
651 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
652 return 0;
653 }
654 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
655 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
656 return 0;
657 }
658
659 /* Load maps to ensure dso->is_64_bit has been updated */
660 map__load(al.map);
661
662 offset = al.map->map_ip(al.map, start);
663 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
664 end - start + MAXINSN);
665
666 *is64bit = al.map->dso->is_64_bit;
667 if (len <= 0)
668 printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
669 start, end);
670 return len;
671}
672
673static void print_jump(uint64_t ip, struct branch_entry *en,
674 struct perf_insn *x, u8 *inbuf, int len,
675 int insn)
676{
677 printf("\t%016" PRIx64 "\t%-30s\t#%s%s%s%s",
678 ip,
679 dump_insn(x, ip, inbuf, len, NULL),
680 en->flags.predicted ? " PRED" : "",
681 en->flags.mispred ? " MISPRED" : "",
682 en->flags.in_tx ? " INTX" : "",
683 en->flags.abort ? " ABORT" : "");
684 if (en->flags.cycles) {
685 printf(" %d cycles", en->flags.cycles);
686 if (insn)
687 printf(" %.2f IPC", (float)insn / en->flags.cycles);
688 }
689 putchar('\n');
690}
691
692static void print_ip_sym(struct thread *thread, u8 cpumode, int cpu,
693 uint64_t addr, struct symbol **lastsym,
694 struct perf_event_attr *attr)
695{
696 struct addr_location al;
697 int off;
698
699 memset(&al, 0, sizeof(al));
700
701 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
702 if (!al.map)
703 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
704 addr, &al);
705 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
706 return;
707
708 al.cpu = cpu;
709 al.sym = NULL;
710 if (al.map)
711 al.sym = map__find_symbol(al.map, al.addr);
712
713 if (!al.sym)
714 return;
715
716 if (al.addr < al.sym->end)
717 off = al.addr - al.sym->start;
718 else
719 off = al.addr - al.map->start - al.sym->start;
720 printf("\t%s", al.sym->name);
721 if (off)
722 printf("%+d", off);
723 putchar(':');
724 if (PRINT_FIELD(SRCLINE))
725 map__fprintf_srcline(al.map, al.addr, "\t", stdout);
726 putchar('\n');
727 *lastsym = al.sym;
728}
729
730static void print_sample_brstackinsn(struct perf_sample *sample,
731 struct thread *thread,
732 struct perf_event_attr *attr,
733 struct machine *machine)
734{
735 struct branch_stack *br = sample->branch_stack;
736 u64 start, end;
737 int i, insn, len, nr, ilen;
738 struct perf_insn x;
739 u8 buffer[MAXBB];
740 unsigned off;
741 struct symbol *lastsym = NULL;
742
743 if (!(br && br->nr))
744 return;
745 nr = br->nr;
746 if (max_blocks && nr > max_blocks + 1)
747 nr = max_blocks + 1;
748
749 x.thread = thread;
750 x.cpu = sample->cpu;
751
752 putchar('\n');
753
754 /* Handle first from jump, of which we don't know the entry. */
755 len = grab_bb(buffer, br->entries[nr-1].from,
756 br->entries[nr-1].from,
757 machine, thread, &x.is64bit, &x.cpumode, false);
758 if (len > 0) {
759 print_ip_sym(thread, x.cpumode, x.cpu,
760 br->entries[nr - 1].from, &lastsym, attr);
761 print_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
762 &x, buffer, len, 0);
763 }
764
765 /* Print all blocks */
766 for (i = nr - 2; i >= 0; i--) {
767 if (br->entries[i].from || br->entries[i].to)
768 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
769 br->entries[i].from,
770 br->entries[i].to);
771 start = br->entries[i + 1].to;
772 end = br->entries[i].from;
773
774 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
775 /* Patch up missing kernel transfers due to ring filters */
776 if (len == -ENXIO && i > 0) {
777 end = br->entries[--i].from;
778 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
779 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
780 }
781 if (len <= 0)
782 continue;
783
784 insn = 0;
785 for (off = 0;; off += ilen) {
786 uint64_t ip = start + off;
787
788 print_ip_sym(thread, x.cpumode, x.cpu, ip, &lastsym, attr);
789 if (ip == end) {
790 print_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn);
791 break;
792 } else {
793 printf("\t%016" PRIx64 "\t%s\n", ip,
794 dump_insn(&x, ip, buffer + off, len - off, &ilen));
795 if (ilen == 0)
796 break;
797 insn++;
798 }
799 }
800 }
801
802 /*
803 * Hit the branch? In this case we are already done, and the target
804 * has not been executed yet.
805 */
806 if (br->entries[0].from == sample->ip)
807 return;
808 if (br->entries[0].flags.abort)
809 return;
810
811 /*
812 * Print final block upto sample
813 */
814 start = br->entries[0].to;
815 end = sample->ip;
816 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
817 print_ip_sym(thread, x.cpumode, x.cpu, start, &lastsym, attr);
818 if (len <= 0) {
819 /* Print at least last IP if basic block did not work */
820 len = grab_bb(buffer, sample->ip, sample->ip,
821 machine, thread, &x.is64bit, &x.cpumode, false);
822 if (len <= 0)
823 return;
824
825 printf("\t%016" PRIx64 "\t%s\n", sample->ip,
826 dump_insn(&x, sample->ip, buffer, len, NULL));
827 return;
828 }
829 for (off = 0; off <= end - start; off += ilen) {
830 printf("\t%016" PRIx64 "\t%s\n", start + off,
831 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
832 if (ilen == 0)
833 break;
834 }
835}
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200836
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300837static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600838 struct thread *thread,
839 struct perf_event_attr *attr)
840{
841 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600842
843 printf("%16" PRIx64, sample->addr);
844
845 if (!sample_addr_correlates_sym(attr))
846 return;
847
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300848 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600849
850 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900851 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900852 if (PRINT_FIELD(SYMOFFSET))
853 symbol__fprintf_symname_offs(al.sym, &al, stdout);
854 else
855 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600856 }
857
858 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900859 printf(" (");
860 map__fprintf_dsoname(al.map, stdout);
861 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600862 }
863}
864
Adrian Huntere2167082016-06-23 16:40:58 +0300865static void print_sample_callindent(struct perf_sample *sample,
866 struct perf_evsel *evsel,
867 struct thread *thread,
868 struct addr_location *al)
869{
870 struct perf_event_attr *attr = &evsel->attr;
871 size_t depth = thread_stack__depth(thread);
872 struct addr_location addr_al;
873 const char *name = NULL;
874 static int spacing;
875 int len = 0;
876 u64 ip = 0;
877
878 /*
879 * The 'return' has already been popped off the stack so the depth has
880 * to be adjusted to match the 'call'.
881 */
882 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
883 depth += 1;
884
885 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
886 if (sample_addr_correlates_sym(attr)) {
887 thread__resolve(thread, &addr_al, sample);
888 if (addr_al.sym)
889 name = addr_al.sym->name;
890 else
891 ip = sample->addr;
892 } else {
893 ip = sample->addr;
894 }
895 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
896 if (al->sym)
897 name = al->sym->name;
898 else
899 ip = sample->ip;
900 }
901
902 if (name)
903 len = printf("%*s%s", (int)depth * 4, "", name);
904 else if (ip)
905 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
906
907 if (len < 0)
908 return;
909
910 /*
911 * Try to keep the output length from changing frequently so that the
912 * output lines up more nicely.
913 */
914 if (len > spacing || (len && len < spacing - 52))
915 spacing = round_up(len + 4, 32);
916
917 if (len < spacing)
918 printf("%*s", spacing - len, "");
919}
920
Andi Kleen224e2c92016-10-07 16:42:27 +0300921static void print_insn(struct perf_sample *sample,
Andi Kleen48d02a12017-02-23 15:46:34 -0800922 struct perf_event_attr *attr,
923 struct thread *thread,
924 struct machine *machine)
Andi Kleen224e2c92016-10-07 16:42:27 +0300925{
926 if (PRINT_FIELD(INSNLEN))
927 printf(" ilen: %d", sample->insn_len);
928 if (PRINT_FIELD(INSN)) {
929 int i;
930
931 printf(" insn:");
932 for (i = 0; i < sample->insn_len; i++)
933 printf(" %02x", (unsigned char)sample->insn[i]);
934 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800935 if (PRINT_FIELD(BRSTACKINSN))
936 print_sample_brstackinsn(sample, thread, attr, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300937}
938
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300939static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +0900940 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200941 struct thread *thread,
Andi Kleen48d02a12017-02-23 15:46:34 -0800942 struct addr_location *al,
943 struct machine *machine)
Akihiro Nagai95582592012-01-30 13:43:09 +0900944{
945 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300946 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900947
Adrian Huntere2167082016-06-23 16:40:58 +0300948 if (PRINT_FIELD(CALLINDENT))
949 print_sample_callindent(sample, evsel, thread, al);
950
Akihiro Nagai95582592012-01-30 13:43:09 +0900951 /* print branch_from information */
952 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300953 unsigned int print_opts = output[attr->type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -0700954 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300955
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300956 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700957 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300958 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700959 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300960
961 if (cursor == NULL) {
962 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300963 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300964 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300965 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300966 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300967 } else
968 putchar('\n');
969
970 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +0900971 }
972
Akihiro Nagai95582592012-01-30 13:43:09 +0900973 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300974 if (PRINT_FIELD(ADDR) ||
975 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300976 !output[attr->type].user_set)) {
977 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300978 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300979 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900980
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300981 if (print_srcline_last)
982 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
983
Andi Kleen48d02a12017-02-23 15:46:34 -0800984 print_insn(sample, attr, thread, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300985
Akihiro Nagai95582592012-01-30 13:43:09 +0900986 printf("\n");
987}
988
Adrian Hunter055cd332016-06-23 16:40:56 +0300989static struct {
990 u32 flags;
991 const char *name;
992} sample_flags[] = {
993 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
994 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
995 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
996 {PERF_IP_FLAG_BRANCH, "jmp"},
997 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
998 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
999 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1000 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1001 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1002 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1003 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1004 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1005 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1006 {0, NULL}
1007};
1008
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001009static void print_sample_flags(u32 flags)
1010{
1011 const char *chars = PERF_IP_FLAG_CHARS;
1012 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001013 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1014 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001015 char str[33];
1016 int i, pos = 0;
1017
Adrian Hunter055cd332016-06-23 16:40:56 +03001018 for (i = 0; sample_flags[i].name ; i++) {
1019 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1020 name = sample_flags[i].name;
1021 break;
1022 }
1023 }
1024
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001025 for (i = 0; i < n; i++, flags >>= 1) {
1026 if (flags & 1)
1027 str[pos++] = chars[i];
1028 }
1029 for (; i < 32; i++, flags >>= 1) {
1030 if (flags & 1)
1031 str[pos++] = '?';
1032 }
1033 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001034
1035 if (name)
1036 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
1037 else
1038 printf(" %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001039}
1040
Wang Nan30372f02016-02-24 11:20:45 +00001041struct printer_data {
1042 int line_no;
1043 bool hit_nul;
1044 bool is_printable;
1045};
1046
1047static void
1048print_sample_bpf_output_printer(enum binary_printer_ops op,
1049 unsigned int val,
1050 void *extra)
1051{
1052 unsigned char ch = (unsigned char)val;
1053 struct printer_data *printer_data = extra;
1054
1055 switch (op) {
1056 case BINARY_PRINT_DATA_BEGIN:
1057 printf("\n");
1058 break;
1059 case BINARY_PRINT_LINE_BEGIN:
1060 printf("%17s", !printer_data->line_no ? "BPF output:" :
1061 " ");
1062 break;
1063 case BINARY_PRINT_ADDR:
1064 printf(" %04x:", val);
1065 break;
1066 case BINARY_PRINT_NUM_DATA:
1067 printf(" %02x", val);
1068 break;
1069 case BINARY_PRINT_NUM_PAD:
1070 printf(" ");
1071 break;
1072 case BINARY_PRINT_SEP:
1073 printf(" ");
1074 break;
1075 case BINARY_PRINT_CHAR_DATA:
1076 if (printer_data->hit_nul && ch)
1077 printer_data->is_printable = false;
1078
1079 if (!isprint(ch)) {
1080 printf("%c", '.');
1081
1082 if (!printer_data->is_printable)
1083 break;
1084
1085 if (ch == '\0')
1086 printer_data->hit_nul = true;
1087 else
1088 printer_data->is_printable = false;
1089 } else {
1090 printf("%c", ch);
1091 }
1092 break;
1093 case BINARY_PRINT_CHAR_PAD:
1094 printf(" ");
1095 break;
1096 case BINARY_PRINT_LINE_END:
1097 printf("\n");
1098 printer_data->line_no++;
1099 break;
1100 case BINARY_PRINT_DATA_END:
1101 default:
1102 break;
1103 }
1104}
1105
1106static void print_sample_bpf_output(struct perf_sample *sample)
1107{
1108 unsigned int nr_bytes = sample->raw_size;
1109 struct printer_data printer_data = {0, false, true};
1110
1111 print_binary(sample->raw_data, nr_bytes, 8,
1112 print_sample_bpf_output_printer, &printer_data);
1113
1114 if (printer_data.is_printable && printer_data.hit_nul)
1115 printf("%17s \"%s\"\n", "BPF string:",
1116 (char *)(sample->raw_data));
1117}
1118
Jiri Olsa809e9422015-11-26 18:55:21 +01001119struct perf_script {
1120 struct perf_tool tool;
1121 struct perf_session *session;
1122 bool show_task_events;
1123 bool show_mmap_events;
1124 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301125 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001126 bool allocated;
1127 struct cpu_map *cpus;
1128 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001129 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001130 const char *time_str;
1131 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001132};
1133
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001134static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1135{
1136 struct perf_evsel *evsel;
1137 int max = 0;
1138
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001139 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001140 int len = strlen(perf_evsel__name(evsel));
1141
1142 max = MAX(len, max);
1143 }
1144
1145 return max;
1146}
1147
Jiri Olsac19ac912016-02-24 09:46:54 +01001148static size_t data_src__printf(u64 data_src)
1149{
1150 struct mem_info mi = { .data_src.val = data_src };
1151 char decode[100];
1152 char out[100];
1153 static int maxlen;
1154 int len;
1155
1156 perf_script__meminfo_scnprintf(decode, 100, &mi);
1157
1158 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1159 if (maxlen < len)
1160 maxlen = len;
1161
1162 return printf("%-*s", maxlen, out);
1163}
1164
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001165static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001166 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001167 struct addr_location *al,
1168 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001169{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001170 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001171 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -07001172
David Ahern2c9e45f72011-03-17 10:03:21 -06001173 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001174 return;
1175
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001176 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -07001177
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001178 if (PRINT_FIELD(PERIOD))
1179 printf("%10" PRIu64 " ", sample->period);
1180
Namhyung Kime944d3d2013-11-18 14:34:52 +09001181 if (PRINT_FIELD(EVNAME)) {
1182 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001183
1184 if (!script->name_width)
1185 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1186
1187 printf("%*s: ", script->name_width,
1188 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001189 }
1190
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001191 if (print_flags)
1192 print_sample_flags(sample->flags);
1193
Akihiro Nagai95582592012-01-30 13:43:09 +09001194 if (is_bts_event(attr)) {
Andi Kleen48d02a12017-02-23 15:46:34 -08001195 print_sample_bts(sample, evsel, thread, al, machine);
Akihiro Nagai95582592012-01-30 13:43:09 +09001196 return;
1197 }
1198
David Ahern745f43e2011-03-09 22:23:26 -07001199 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001200 event_format__print(evsel->tp_format, sample->cpu,
1201 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -06001202 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001203 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -06001204
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001205 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +01001206 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001207
1208 if (PRINT_FIELD(WEIGHT))
1209 printf("%16" PRIu64, sample->weight);
1210
David Ahern787bef12011-05-27 14:28:43 -06001211 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001212 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001213
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001214 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001215 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001216 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001217 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001218
1219 putchar(cursor ? '\n' : ' ');
1220 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001221 }
1222
Stephane Eranianfc36f942015-08-31 18:41:10 +02001223 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001224 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001225
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001226 if (PRINT_FIELD(BRSTACK))
Mark Santaniello55b9b502017-06-19 09:38:24 -07001227 print_sample_brstack(sample, thread, attr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001228 else if (PRINT_FIELD(BRSTACKSYM))
Mark Santaniello55b9b502017-06-19 09:38:24 -07001229 print_sample_brstacksym(sample, thread, attr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001230
Wang Nan30372f02016-02-24 11:20:45 +00001231 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1232 print_sample_bpf_output(sample);
Andi Kleen48d02a12017-02-23 15:46:34 -08001233 print_insn(sample, attr, thread, machine);
David Ahernc70c94b2011-03-09 22:23:25 -07001234 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001235}
1236
Tom Zanussi956ffd02009-11-25 01:15:46 -06001237static struct scripting_ops *scripting_ops;
1238
Jiri Olsa36e33c52016-01-06 11:49:56 +01001239static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1240{
1241 int nthreads = thread_map__nr(counter->threads);
1242 int ncpus = perf_evsel__nr_cpus(counter);
1243 int cpu, thread;
1244 static int header_printed;
1245
1246 if (counter->system_wide)
1247 nthreads = 1;
1248
1249 if (!header_printed) {
1250 printf("%3s %8s %15s %15s %15s %15s %s\n",
1251 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1252 header_printed = 1;
1253 }
1254
1255 for (thread = 0; thread < nthreads; thread++) {
1256 for (cpu = 0; cpu < ncpus; cpu++) {
1257 struct perf_counts_values *counts;
1258
1259 counts = perf_counts(counter->counts, cpu, thread);
1260
1261 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1262 counter->cpus->map[cpu],
1263 thread_map__pid(counter->threads, thread),
1264 counts->val,
1265 counts->ena,
1266 counts->run,
1267 tstamp,
1268 perf_evsel__name(counter));
1269 }
1270 }
1271}
1272
Jiri Olsae099eba2016-01-05 22:09:09 +01001273static void process_stat(struct perf_evsel *counter, u64 tstamp)
1274{
1275 if (scripting_ops && scripting_ops->process_stat)
1276 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001277 else
1278 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001279}
1280
1281static void process_stat_interval(u64 tstamp)
1282{
1283 if (scripting_ops && scripting_ops->process_stat_interval)
1284 scripting_ops->process_stat_interval(tstamp);
1285}
1286
Tom Zanussi956ffd02009-11-25 01:15:46 -06001287static void setup_scripting(void)
1288{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001289 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001290 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001291}
1292
Adrian Hunterd445dd22014-08-15 22:08:37 +03001293static int flush_scripting(void)
1294{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001295 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001296}
1297
Tom Zanussi956ffd02009-11-25 01:15:46 -06001298static int cleanup_scripting(void)
1299{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001300 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001301
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001302 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001303}
1304
Jiri Olsa809e9422015-11-26 18:55:21 +01001305static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001306 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001307 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001308 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001309 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001310{
Jiri Olsa809e9422015-11-26 18:55:21 +01001311 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001312 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001313
David Aherna91f4c42016-11-29 10:15:43 -07001314 if (perf_time__skip_sample(&scr->ptime, sample->time))
1315 return 0;
1316
David Ahern1424dc92011-03-09 22:23:28 -07001317 if (debug_mode) {
1318 if (sample->time < last_timestamp) {
1319 pr_err("Samples misordered, previous: %" PRIu64
1320 " this: %" PRIu64 "\n", last_timestamp,
1321 sample->time);
1322 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001323 }
David Ahern1424dc92011-03-09 22:23:28 -07001324 last_timestamp = sample->time;
1325 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001326 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001327
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001328 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001329 pr_err("problem processing %d event, skipping it.\n",
1330 event->header.type);
1331 return -1;
1332 }
1333
1334 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001335 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001336
Anton Blanchard5d67be92011-07-04 21:57:50 +10001337 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001338 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001339
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001340 if (scripting_ops)
1341 scripting_ops->process_event(event, sample, evsel, &al);
1342 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001343 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001344
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001345out_put:
1346 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001347 return 0;
1348}
1349
Adrian Hunter7ea95722013-11-01 15:51:30 +02001350static int process_attr(struct perf_tool *tool, union perf_event *event,
1351 struct perf_evlist **pevlist)
1352{
1353 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1354 struct perf_evlist *evlist;
1355 struct perf_evsel *evsel, *pos;
1356 int err;
1357
1358 err = perf_event__process_attr(tool, event, pevlist);
1359 if (err)
1360 return err;
1361
1362 evlist = *pevlist;
1363 evsel = perf_evlist__last(*pevlist);
1364
1365 if (evsel->attr.type >= PERF_TYPE_MAX)
1366 return 0;
1367
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001368 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001369 if (pos->attr.type == evsel->attr.type && pos != evsel)
1370 return 0;
1371 }
1372
1373 set_print_ip_opts(&evsel->attr);
1374
Jiri Olsad2b5a312015-10-16 12:41:25 +02001375 if (evsel->attr.sample_type)
1376 err = perf_evsel__check_attr(evsel, scr->session);
1377
1378 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001379}
1380
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001381static int process_comm_event(struct perf_tool *tool,
1382 union perf_event *event,
1383 struct perf_sample *sample,
1384 struct machine *machine)
1385{
1386 struct thread *thread;
1387 struct perf_script *script = container_of(tool, struct perf_script, tool);
1388 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001389 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001390 int ret = -1;
1391
1392 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1393 if (thread == NULL) {
1394 pr_debug("problem processing COMM event, skipping it.\n");
1395 return -1;
1396 }
1397
1398 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1399 goto out;
1400
1401 if (!evsel->attr.sample_id_all) {
1402 sample->cpu = 0;
1403 sample->time = 0;
1404 sample->tid = event->comm.tid;
1405 sample->pid = event->comm.pid;
1406 }
1407 print_sample_start(sample, thread, evsel);
1408 perf_event__fprintf(event, stdout);
1409 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001410out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001411 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001412 return ret;
1413}
1414
Hari Bathini96a44bb2017-03-08 02:12:06 +05301415static int process_namespaces_event(struct perf_tool *tool,
1416 union perf_event *event,
1417 struct perf_sample *sample,
1418 struct machine *machine)
1419{
1420 struct thread *thread;
1421 struct perf_script *script = container_of(tool, struct perf_script, tool);
1422 struct perf_session *session = script->session;
1423 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1424 int ret = -1;
1425
1426 thread = machine__findnew_thread(machine, event->namespaces.pid,
1427 event->namespaces.tid);
1428 if (thread == NULL) {
1429 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1430 return -1;
1431 }
1432
1433 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1434 goto out;
1435
1436 if (!evsel->attr.sample_id_all) {
1437 sample->cpu = 0;
1438 sample->time = 0;
1439 sample->tid = event->namespaces.tid;
1440 sample->pid = event->namespaces.pid;
1441 }
1442 print_sample_start(sample, thread, evsel);
1443 perf_event__fprintf(event, stdout);
1444 ret = 0;
1445out:
1446 thread__put(thread);
1447 return ret;
1448}
1449
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001450static int process_fork_event(struct perf_tool *tool,
1451 union perf_event *event,
1452 struct perf_sample *sample,
1453 struct machine *machine)
1454{
1455 struct thread *thread;
1456 struct perf_script *script = container_of(tool, struct perf_script, tool);
1457 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001458 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001459
1460 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1461 return -1;
1462
1463 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1464 if (thread == NULL) {
1465 pr_debug("problem processing FORK event, skipping it.\n");
1466 return -1;
1467 }
1468
1469 if (!evsel->attr.sample_id_all) {
1470 sample->cpu = 0;
1471 sample->time = event->fork.time;
1472 sample->tid = event->fork.tid;
1473 sample->pid = event->fork.pid;
1474 }
1475 print_sample_start(sample, thread, evsel);
1476 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001477 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001478
1479 return 0;
1480}
1481static int process_exit_event(struct perf_tool *tool,
1482 union perf_event *event,
1483 struct perf_sample *sample,
1484 struct machine *machine)
1485{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001486 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001487 struct thread *thread;
1488 struct perf_script *script = container_of(tool, struct perf_script, tool);
1489 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001490 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001491
1492 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1493 if (thread == NULL) {
1494 pr_debug("problem processing EXIT event, skipping it.\n");
1495 return -1;
1496 }
1497
1498 if (!evsel->attr.sample_id_all) {
1499 sample->cpu = 0;
1500 sample->time = 0;
Adrian Hunter53ff6bc32015-08-18 12:07:05 +03001501 sample->tid = event->fork.tid;
1502 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001503 }
1504 print_sample_start(sample, thread, evsel);
1505 perf_event__fprintf(event, stdout);
1506
1507 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001508 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001509
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001510 thread__put(thread);
1511 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001512}
1513
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001514static int process_mmap_event(struct perf_tool *tool,
1515 union perf_event *event,
1516 struct perf_sample *sample,
1517 struct machine *machine)
1518{
1519 struct thread *thread;
1520 struct perf_script *script = container_of(tool, struct perf_script, tool);
1521 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001522 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001523
1524 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1525 return -1;
1526
1527 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1528 if (thread == NULL) {
1529 pr_debug("problem processing MMAP event, skipping it.\n");
1530 return -1;
1531 }
1532
1533 if (!evsel->attr.sample_id_all) {
1534 sample->cpu = 0;
1535 sample->time = 0;
1536 sample->tid = event->mmap.tid;
1537 sample->pid = event->mmap.pid;
1538 }
1539 print_sample_start(sample, thread, evsel);
1540 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001541 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001542 return 0;
1543}
1544
1545static int process_mmap2_event(struct perf_tool *tool,
1546 union perf_event *event,
1547 struct perf_sample *sample,
1548 struct machine *machine)
1549{
1550 struct thread *thread;
1551 struct perf_script *script = container_of(tool, struct perf_script, tool);
1552 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001553 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001554
1555 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1556 return -1;
1557
1558 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1559 if (thread == NULL) {
1560 pr_debug("problem processing MMAP2 event, skipping it.\n");
1561 return -1;
1562 }
1563
1564 if (!evsel->attr.sample_id_all) {
1565 sample->cpu = 0;
1566 sample->time = 0;
1567 sample->tid = event->mmap2.tid;
1568 sample->pid = event->mmap2.pid;
1569 }
1570 print_sample_start(sample, thread, evsel);
1571 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001572 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001573 return 0;
1574}
1575
Adrian Hunter7c148982015-07-21 12:44:06 +03001576static int process_switch_event(struct perf_tool *tool,
1577 union perf_event *event,
1578 struct perf_sample *sample,
1579 struct machine *machine)
1580{
1581 struct thread *thread;
1582 struct perf_script *script = container_of(tool, struct perf_script, tool);
1583 struct perf_session *session = script->session;
1584 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1585
1586 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1587 return -1;
1588
1589 thread = machine__findnew_thread(machine, sample->pid,
1590 sample->tid);
1591 if (thread == NULL) {
1592 pr_debug("problem processing SWITCH event, skipping it.\n");
1593 return -1;
1594 }
1595
1596 print_sample_start(sample, thread, evsel);
1597 perf_event__fprintf(event, stdout);
1598 thread__put(thread);
1599 return 0;
1600}
1601
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001602static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001603{
1604 session_done = 1;
1605}
1606
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001607static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001608{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001609 int ret;
1610
Tom Zanussic239da32010-04-01 23:59:18 -05001611 signal(SIGINT, sig_handler);
1612
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001613 /* override event processing functions */
1614 if (script->show_task_events) {
1615 script->tool.comm = process_comm_event;
1616 script->tool.fork = process_fork_event;
1617 script->tool.exit = process_exit_event;
1618 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001619 if (script->show_mmap_events) {
1620 script->tool.mmap = process_mmap_event;
1621 script->tool.mmap2 = process_mmap2_event;
1622 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001623 if (script->show_switch_events)
1624 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301625 if (script->show_namespace_events)
1626 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001627
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001628 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001629
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001630 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001631 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001632
1633 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001634}
1635
Tom Zanussi956ffd02009-11-25 01:15:46 -06001636struct script_spec {
1637 struct list_head node;
1638 struct scripting_ops *ops;
1639 char spec[0];
1640};
1641
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001642static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001643
1644static struct script_spec *script_spec__new(const char *spec,
1645 struct scripting_ops *ops)
1646{
1647 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1648
1649 if (s != NULL) {
1650 strcpy(s->spec, spec);
1651 s->ops = ops;
1652 }
1653
1654 return s;
1655}
1656
Tom Zanussi956ffd02009-11-25 01:15:46 -06001657static void script_spec__add(struct script_spec *s)
1658{
1659 list_add_tail(&s->node, &script_specs);
1660}
1661
1662static struct script_spec *script_spec__find(const char *spec)
1663{
1664 struct script_spec *s;
1665
1666 list_for_each_entry(s, &script_specs, node)
1667 if (strcasecmp(s->spec, spec) == 0)
1668 return s;
1669 return NULL;
1670}
1671
Tom Zanussi956ffd02009-11-25 01:15:46 -06001672int script_spec_register(const char *spec, struct scripting_ops *ops)
1673{
1674 struct script_spec *s;
1675
1676 s = script_spec__find(spec);
1677 if (s)
1678 return -1;
1679
Taeung Song8560bae2016-02-26 00:13:10 +09001680 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001681 if (!s)
1682 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001683 else
1684 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001685
1686 return 0;
1687}
1688
1689static struct scripting_ops *script_spec__lookup(const char *spec)
1690{
1691 struct script_spec *s = script_spec__find(spec);
1692 if (!s)
1693 return NULL;
1694
1695 return s->ops;
1696}
1697
1698static void list_available_languages(void)
1699{
1700 struct script_spec *s;
1701
1702 fprintf(stderr, "\n");
1703 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001704 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001705
1706 list_for_each_entry(s, &script_specs, node)
1707 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1708
1709 fprintf(stderr, "\n");
1710}
1711
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001712static int parse_scriptname(const struct option *opt __maybe_unused,
1713 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001714{
1715 char spec[PATH_MAX];
1716 const char *script, *ext;
1717 int len;
1718
Tom Zanussif526d682010-01-27 02:27:52 -06001719 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001720 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001721 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001722 }
1723
1724 script = strchr(str, ':');
1725 if (script) {
1726 len = script - str;
1727 if (len >= PATH_MAX) {
1728 fprintf(stderr, "invalid language specifier");
1729 return -1;
1730 }
1731 strncpy(spec, str, len);
1732 spec[len] = '\0';
1733 scripting_ops = script_spec__lookup(spec);
1734 if (!scripting_ops) {
1735 fprintf(stderr, "invalid language specifier");
1736 return -1;
1737 }
1738 script++;
1739 } else {
1740 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001741 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001742 if (!ext) {
1743 fprintf(stderr, "invalid script extension");
1744 return -1;
1745 }
1746 scripting_ops = script_spec__lookup(++ext);
1747 if (!scripting_ops) {
1748 fprintf(stderr, "invalid script extension");
1749 return -1;
1750 }
1751 }
1752
1753 script_name = strdup(script);
1754
1755 return 0;
1756}
1757
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001758static int parse_output_fields(const struct option *opt __maybe_unused,
1759 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001760{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001761 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001762 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001763 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001764 int rc = 0;
1765 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001766 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07001767 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07001768
1769 if (!str)
1770 return -ENOMEM;
1771
David Ahern2c9e45f72011-03-17 10:03:21 -06001772 /* first word can state for which event type the user is specifying
1773 * the fields. If no type exists, the specified fields apply to all
1774 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001775 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001776 tok = strchr(str, ':');
1777 if (tok) {
1778 *tok = '\0';
1779 tok++;
1780 if (!strcmp(str, "hw"))
1781 type = PERF_TYPE_HARDWARE;
1782 else if (!strcmp(str, "sw"))
1783 type = PERF_TYPE_SOFTWARE;
1784 else if (!strcmp(str, "trace"))
1785 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001786 else if (!strcmp(str, "raw"))
1787 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001788 else if (!strcmp(str, "break"))
1789 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001790 else {
1791 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001792 rc = -EINVAL;
1793 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001794 }
1795
1796 if (output[type].user_set)
1797 pr_warning("Overriding previous field request for %s events.\n",
1798 event_type(type));
1799
1800 output[type].fields = 0;
1801 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001802 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001803
1804 } else {
1805 tok = str;
1806 if (strlen(str) == 0) {
1807 fprintf(stderr,
1808 "Cannot set fields to 'none' for all event types.\n");
1809 rc = -EINVAL;
1810 goto out;
1811 }
1812
Andi Kleen36ce5652017-06-02 08:48:10 -07001813 /* Don't override defaults for +- */
1814 if (strchr(str, '+') || strchr(str, '-'))
1815 goto parse;
1816
David Ahern2c9e45f72011-03-17 10:03:21 -06001817 if (output_set_by_user())
1818 pr_warning("Overriding previous field request for all events.\n");
1819
1820 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1821 output[j].fields = 0;
1822 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001823 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001824 }
David Ahern1424dc92011-03-09 22:23:28 -07001825 }
1826
Andi Kleen36ce5652017-06-02 08:48:10 -07001827parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001828 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07001829 if (*tok == '+') {
1830 if (change == SET)
1831 goto out_badmix;
1832 change = ADD;
1833 tok++;
1834 } else if (*tok == '-') {
1835 if (change == SET)
1836 goto out_badmix;
1837 change = REMOVE;
1838 tok++;
1839 } else {
1840 if (change != SET && change != DEFAULT)
1841 goto out_badmix;
1842 change = SET;
1843 }
1844
David Ahern745f43e2011-03-09 22:23:26 -07001845 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001846 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001847 break;
David Ahern745f43e2011-03-09 22:23:26 -07001848 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001849 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07001850 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001851 continue;
1852 }
David Ahern745f43e2011-03-09 22:23:26 -07001853 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001854 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001855 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001856 goto out;
1857 }
1858
1859 if (type == -1) {
1860 /* add user option to all events types for
1861 * which it is valid
1862 */
1863 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1864 if (output[j].invalid_fields & all_output_options[i].field) {
1865 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1866 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07001867 } else {
1868 if (change == REMOVE)
1869 output[j].fields &= ~all_output_options[i].field;
1870 else
1871 output[j].fields |= all_output_options[i].field;
1872 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001873 }
1874 } else {
1875 if (output[type].invalid_fields & all_output_options[i].field) {
1876 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1877 all_output_options[i].str, event_type(type));
1878
1879 rc = -EINVAL;
1880 goto out;
1881 }
1882 output[type].fields |= all_output_options[i].field;
1883 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001884 }
1885
1886 if (type >= 0) {
1887 if (output[type].fields == 0) {
1888 pr_debug("No fields requested for %s type. "
1889 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001890 }
David Ahern1424dc92011-03-09 22:23:28 -07001891 }
Andi Kleen36ce5652017-06-02 08:48:10 -07001892 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07001893
Andi Kleen36ce5652017-06-02 08:48:10 -07001894out_badmix:
1895 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
1896 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001897out:
David Ahern745f43e2011-03-09 22:23:26 -07001898 free(str);
1899 return rc;
1900}
1901
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001902/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1903static int is_directory(const char *base_path, const struct dirent *dent)
1904{
1905 char path[PATH_MAX];
1906 struct stat st;
1907
1908 sprintf(path, "%s/%s", base_path, dent->d_name);
1909 if (stat(path, &st))
1910 return 0;
1911
1912 return S_ISDIR(st.st_mode);
1913}
1914
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001915#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1916 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1917 if ((lang_dirent->d_type == DT_DIR || \
1918 (lang_dirent->d_type == DT_UNKNOWN && \
1919 is_directory(scripts_path, lang_dirent))) && \
1920 (strcmp(lang_dirent->d_name, ".")) && \
1921 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001922
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001923#define for_each_script(lang_path, lang_dir, script_dirent) \
1924 while ((script_dirent = readdir(lang_dir)) != NULL) \
1925 if (script_dirent->d_type != DT_DIR && \
1926 (script_dirent->d_type != DT_UNKNOWN || \
1927 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001928
1929
1930#define RECORD_SUFFIX "-record"
1931#define REPORT_SUFFIX "-report"
1932
1933struct script_desc {
1934 struct list_head node;
1935 char *name;
1936 char *half_liner;
1937 char *args;
1938};
1939
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001940static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001941
1942static struct script_desc *script_desc__new(const char *name)
1943{
1944 struct script_desc *s = zalloc(sizeof(*s));
1945
Tom Zanussib5b87312010-11-10 08:16:51 -06001946 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001947 s->name = strdup(name);
1948
1949 return s;
1950}
1951
1952static void script_desc__delete(struct script_desc *s)
1953{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001954 zfree(&s->name);
1955 zfree(&s->half_liner);
1956 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001957 free(s);
1958}
1959
1960static void script_desc__add(struct script_desc *s)
1961{
1962 list_add_tail(&s->node, &script_descs);
1963}
1964
1965static struct script_desc *script_desc__find(const char *name)
1966{
1967 struct script_desc *s;
1968
1969 list_for_each_entry(s, &script_descs, node)
1970 if (strcasecmp(s->name, name) == 0)
1971 return s;
1972 return NULL;
1973}
1974
1975static struct script_desc *script_desc__findnew(const char *name)
1976{
1977 struct script_desc *s = script_desc__find(name);
1978
1979 if (s)
1980 return s;
1981
1982 s = script_desc__new(name);
1983 if (!s)
1984 goto out_delete_desc;
1985
1986 script_desc__add(s);
1987
1988 return s;
1989
1990out_delete_desc:
1991 script_desc__delete(s);
1992
1993 return NULL;
1994}
1995
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001996static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001997{
1998 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001999 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002000
2001 if (strlen(str) > suffix_len) {
2002 p = str + strlen(str) - suffix_len;
2003 if (!strncmp(p, suffix, suffix_len))
2004 return p;
2005 }
2006
2007 return NULL;
2008}
2009
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002010static int read_script_info(struct script_desc *desc, const char *filename)
2011{
2012 char line[BUFSIZ], *p;
2013 FILE *fp;
2014
2015 fp = fopen(filename, "r");
2016 if (!fp)
2017 return -1;
2018
2019 while (fgets(line, sizeof(line), fp)) {
2020 p = ltrim(line);
2021 if (strlen(p) == 0)
2022 continue;
2023 if (*p != '#')
2024 continue;
2025 p++;
2026 if (strlen(p) && *p == '!')
2027 continue;
2028
2029 p = ltrim(p);
2030 if (strlen(p) && p[strlen(p) - 1] == '\n')
2031 p[strlen(p) - 1] = '\0';
2032
2033 if (!strncmp(p, "description:", strlen("description:"))) {
2034 p += strlen("description:");
2035 desc->half_liner = strdup(ltrim(p));
2036 continue;
2037 }
2038
2039 if (!strncmp(p, "args:", strlen("args:"))) {
2040 p += strlen("args:");
2041 desc->args = strdup(ltrim(p));
2042 continue;
2043 }
2044 }
2045
2046 fclose(fp);
2047
2048 return 0;
2049}
2050
Robert Richter38efb532011-11-25 11:38:40 +01002051static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2052{
2053 char *script_root, *str;
2054
2055 script_root = strdup(script_dirent->d_name);
2056 if (!script_root)
2057 return NULL;
2058
2059 str = (char *)ends_with(script_root, suffix);
2060 if (!str) {
2061 free(script_root);
2062 return NULL;
2063 }
2064
2065 *str = '\0';
2066 return script_root;
2067}
2068
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002069static int list_available_scripts(const struct option *opt __maybe_unused,
2070 const char *s __maybe_unused,
2071 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002072{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002073 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002074 char scripts_path[MAXPATHLEN];
2075 DIR *scripts_dir, *lang_dir;
2076 char script_path[MAXPATHLEN];
2077 char lang_path[MAXPATHLEN];
2078 struct script_desc *desc;
2079 char first_half[BUFSIZ];
2080 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002081
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002082 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002083
2084 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002085 if (!scripts_dir) {
2086 fprintf(stdout,
2087 "open(%s) failed.\n"
2088 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2089 scripts_path);
2090 exit(-1);
2091 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002092
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002093 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002094 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002095 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002096 lang_dir = opendir(lang_path);
2097 if (!lang_dir)
2098 continue;
2099
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002100 for_each_script(lang_path, lang_dir, script_dirent) {
2101 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002102 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002103 desc = script_desc__findnew(script_root);
2104 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002105 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002106 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002107 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002108 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002109 }
2110 }
2111
2112 fprintf(stdout, "List of available trace scripts:\n");
2113 list_for_each_entry(desc, &script_descs, node) {
2114 sprintf(first_half, "%s %s", desc->name,
2115 desc->args ? desc->args : "");
2116 fprintf(stdout, " %-36s %s\n", first_half,
2117 desc->half_liner ? desc->half_liner : "");
2118 }
2119
2120 exit(0);
2121}
2122
Feng Tange5f37052012-09-07 16:42:26 +08002123/*
Feng Tang49e639e2012-10-30 11:56:03 +08002124 * Some scripts specify the required events in their "xxx-record" file,
2125 * this function will check if the events in perf.data match those
2126 * mentioned in the "xxx-record".
2127 *
2128 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2129 * which is covered well now. And new parsing code should be added to
2130 * cover the future complexing formats like event groups etc.
2131 */
2132static int check_ev_match(char *dir_name, char *scriptname,
2133 struct perf_session *session)
2134{
2135 char filename[MAXPATHLEN], evname[128];
2136 char line[BUFSIZ], *p;
2137 struct perf_evsel *pos;
2138 int match, len;
2139 FILE *fp;
2140
2141 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2142
2143 fp = fopen(filename, "r");
2144 if (!fp)
2145 return -1;
2146
2147 while (fgets(line, sizeof(line), fp)) {
2148 p = ltrim(line);
2149 if (*p == '#')
2150 continue;
2151
2152 while (strlen(p)) {
2153 p = strstr(p, "-e");
2154 if (!p)
2155 break;
2156
2157 p += 2;
2158 p = ltrim(p);
2159 len = strcspn(p, " \t");
2160 if (!len)
2161 break;
2162
2163 snprintf(evname, len + 1, "%s", p);
2164
2165 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002166 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002167 if (!strcmp(perf_evsel__name(pos), evname)) {
2168 match = 1;
2169 break;
2170 }
2171 }
2172
2173 if (!match) {
2174 fclose(fp);
2175 return -1;
2176 }
2177 }
2178 }
2179
2180 fclose(fp);
2181 return 0;
2182}
2183
2184/*
Feng Tange5f37052012-09-07 16:42:26 +08002185 * Return -1 if none is found, otherwise the actual scripts number.
2186 *
2187 * Currently the only user of this function is the script browser, which
2188 * will list all statically runnable scripts, select one, execute it and
2189 * show the output in a perf browser.
2190 */
2191int find_scripts(char **scripts_array, char **scripts_path_array)
2192{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002193 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002194 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002195 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002196 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002197 struct perf_data_file file = {
2198 .path = input_name,
2199 .mode = PERF_DATA_MODE_READ,
2200 };
Feng Tange5f37052012-09-07 16:42:26 +08002201 char *temp;
2202 int i = 0;
2203
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002204 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002205 if (!session)
2206 return -1;
2207
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002208 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002209
2210 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002211 if (!scripts_dir) {
2212 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002213 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002214 }
Feng Tange5f37052012-09-07 16:42:26 +08002215
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002216 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002217 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002218 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002219#ifdef NO_LIBPERL
2220 if (strstr(lang_path, "perl"))
2221 continue;
2222#endif
2223#ifdef NO_LIBPYTHON
2224 if (strstr(lang_path, "python"))
2225 continue;
2226#endif
2227
2228 lang_dir = opendir(lang_path);
2229 if (!lang_dir)
2230 continue;
2231
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002232 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002233 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002234 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002235 continue;
2236 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002237 script_dirent->d_name);
2238 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002239 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002240 (temp - script_dirent->d_name) + 1,
2241 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002242
2243 if (check_ev_match(lang_path,
2244 scripts_array[i], session))
2245 continue;
2246
Feng Tange5f37052012-09-07 16:42:26 +08002247 i++;
2248 }
Feng Tang49e639e2012-10-30 11:56:03 +08002249 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002250 }
2251
Feng Tang49e639e2012-10-30 11:56:03 +08002252 closedir(scripts_dir);
2253 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002254 return i;
2255}
2256
Tom Zanussi38752942009-12-15 02:53:39 -06002257static char *get_script_path(const char *script_root, const char *suffix)
2258{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002259 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002260 char scripts_path[MAXPATHLEN];
2261 char script_path[MAXPATHLEN];
2262 DIR *scripts_dir, *lang_dir;
2263 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002264 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002265
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002266 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002267
2268 scripts_dir = opendir(scripts_path);
2269 if (!scripts_dir)
2270 return NULL;
2271
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002272 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002273 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002274 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002275 lang_dir = opendir(lang_path);
2276 if (!lang_dir)
2277 continue;
2278
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002279 for_each_script(lang_path, lang_dir, script_dirent) {
2280 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002281 if (__script_root && !strcmp(script_root, __script_root)) {
2282 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002283 closedir(lang_dir);
2284 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002285 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002286 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002287 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002288 }
2289 free(__script_root);
2290 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002291 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002292 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002293 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002294
Robert Richter38efb532011-11-25 11:38:40 +01002295 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002296}
2297
Tom Zanussib5b87312010-11-10 08:16:51 -06002298static bool is_top_script(const char *script_path)
2299{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002300 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002301}
2302
2303static int has_required_arg(char *script_path)
2304{
2305 struct script_desc *desc;
2306 int n_args = 0;
2307 char *p;
2308
2309 desc = script_desc__new(NULL);
2310
2311 if (read_script_info(desc, script_path))
2312 goto out;
2313
2314 if (!desc->args)
2315 goto out;
2316
2317 for (p = desc->args; *p; p++)
2318 if (*p == '<')
2319 n_args++;
2320out:
2321 script_desc__delete(desc);
2322
2323 return n_args;
2324}
2325
David Ahernd54b1a92012-08-26 12:24:46 -06002326static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002327{
2328 char **__argv = malloc(sizeof(const char *) * argc);
2329
David Ahernd54b1a92012-08-26 12:24:46 -06002330 if (!__argv) {
2331 pr_err("malloc failed\n");
2332 return -1;
2333 }
2334
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002335 memcpy(__argv, argv, sizeof(const char *) * argc);
2336 argc = parse_options(argc, (const char **)__argv, record_options,
2337 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2338 free(__argv);
2339
David Ahernd54b1a92012-08-26 12:24:46 -06002340 system_wide = (argc == 0);
2341
2342 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002343}
2344
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002345static void script__setup_sample_type(struct perf_script *script)
2346{
2347 struct perf_session *session = script->session;
2348 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2349
2350 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2351 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2352 (sample_type & PERF_SAMPLE_STACK_USER))
2353 callchain_param.record_mode = CALLCHAIN_DWARF;
2354 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2355 callchain_param.record_mode = CALLCHAIN_LBR;
2356 else
2357 callchain_param.record_mode = CALLCHAIN_FP;
2358 }
2359}
2360
Jiri Olsae099eba2016-01-05 22:09:09 +01002361static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2362 union perf_event *event,
2363 struct perf_session *session)
2364{
2365 struct stat_round_event *round = &event->stat_round;
2366 struct perf_evsel *counter;
2367
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002368 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002369 perf_stat_process_counter(&stat_config, counter);
2370 process_stat(counter, round->time);
2371 }
2372
2373 process_stat_interval(round->time);
2374 return 0;
2375}
2376
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002377static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2378 union perf_event *event,
2379 struct perf_session *session __maybe_unused)
2380{
2381 perf_event__read_stat_config(&stat_config, &event->stat_config);
2382 return 0;
2383}
2384
Jiri Olsacfc88742016-01-05 22:09:06 +01002385static int set_maps(struct perf_script *script)
2386{
2387 struct perf_evlist *evlist = script->session->evlist;
2388
2389 if (!script->cpus || !script->threads)
2390 return 0;
2391
2392 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2393 return -EINVAL;
2394
2395 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2396
2397 if (perf_evlist__alloc_stats(evlist, true))
2398 return -ENOMEM;
2399
2400 script->allocated = true;
2401 return 0;
2402}
2403
2404static
2405int process_thread_map_event(struct perf_tool *tool,
2406 union perf_event *event,
2407 struct perf_session *session __maybe_unused)
2408{
2409 struct perf_script *script = container_of(tool, struct perf_script, tool);
2410
2411 if (script->threads) {
2412 pr_warning("Extra thread map event, ignoring.\n");
2413 return 0;
2414 }
2415
2416 script->threads = thread_map__new_event(&event->thread_map);
2417 if (!script->threads)
2418 return -ENOMEM;
2419
2420 return set_maps(script);
2421}
2422
2423static
2424int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2425 union perf_event *event,
2426 struct perf_session *session __maybe_unused)
2427{
2428 struct perf_script *script = container_of(tool, struct perf_script, tool);
2429
2430 if (script->cpus) {
2431 pr_warning("Extra cpu map event, ignoring.\n");
2432 return 0;
2433 }
2434
2435 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2436 if (!script->cpus)
2437 return -ENOMEM;
2438
2439 return set_maps(script);
2440}
2441
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002442int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002443{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002444 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002445 bool header = false;
2446 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002447 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002448 char *rec_script_path = NULL;
2449 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002450 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002451 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002452 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002453 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002454 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002455 struct perf_script script = {
2456 .tool = {
2457 .sample = process_sample_event,
2458 .mmap = perf_event__process_mmap,
2459 .mmap2 = perf_event__process_mmap2,
2460 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302461 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002462 .exit = perf_event__process_exit,
2463 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002464 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002465 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002466 .tracing_data = perf_event__process_tracing_data,
2467 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002468 .id_index = perf_event__process_id_index,
2469 .auxtrace_info = perf_event__process_auxtrace_info,
2470 .auxtrace = perf_event__process_auxtrace,
2471 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002472 .stat = perf_event__process_stat_event,
2473 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002474 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002475 .thread_map = process_thread_map_event,
2476 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002477 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002478 .ordering_requires_timestamps = true,
2479 },
2480 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002481 struct perf_data_file file = {
2482 .mode = PERF_DATA_MODE_READ,
2483 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002484 const struct option options[] = {
2485 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2486 "dump raw trace in ASCII"),
2487 OPT_INCR('v', "verbose", &verbose,
2488 "be more verbose (show symbol address, etc)"),
2489 OPT_BOOLEAN('L', "Latency", &latency_format,
2490 "show latency attributes (irqs/preemption disabled, etc)"),
2491 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2492 list_available_scripts),
2493 OPT_CALLBACK('s', "script", NULL, "name",
2494 "script file name (lang:script name, script name, or *)",
2495 parse_scriptname),
2496 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2497 "generate perf-script.xx script in specified language"),
2498 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2499 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2500 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002501 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2502 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002503 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2504 "file", "vmlinux pathname"),
2505 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2506 "file", "kallsyms pathname"),
2507 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2508 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002509 OPT_CALLBACK(0, "symfs", NULL, "directory",
2510 "Look for files with symbols relative to this directory",
2511 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002512 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002513 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07002514 "+field to add and -field to remove."
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002515 "Valid types: hw,sw,trace,raw. "
2516 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Huntere2167082016-06-23 16:40:58 +03002517 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
Andi Kleen48d02a12017-02-23 15:46:34 -08002518 "bpf-output,callindent,insn,insnlen,brstackinsn",
2519 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002520 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2521 "system-wide collection from all CPUs"),
2522 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2523 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002524 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2525 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002526 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2527 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2528 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002529 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2530 "only consider symbols in these pids"),
2531 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2532 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002533 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2534 "Set the maximum stack depth when parsing the callchain, "
2535 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002536 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002537 OPT_BOOLEAN('I', "show-info", &show_full_info,
2538 "display extended information from perf.data file"),
2539 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2540 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002541 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2542 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002543 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2544 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002545 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2546 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302547 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2548 "Show namespace events (if recorded)"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002549 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002550 OPT_INTEGER(0, "max-blocks", &max_blocks,
2551 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002552 OPT_BOOLEAN(0, "ns", &nanosecs,
2553 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002554 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2555 "Instruction Tracing options",
2556 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002557 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2558 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002559 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2560 "Enable symbol demangling"),
2561 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2562 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002563 OPT_STRING(0, "time", &script.time_str, "str",
2564 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09002565 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2566 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002567 OPT_END()
2568 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002569 const char * const script_subcommands[] = { "record", "report", NULL };
2570 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002571 "perf script [<options>]",
2572 "perf script [<options>] record <script> [<record-options>] <command>",
2573 "perf script [<options>] report <script> [script-args]",
2574 "perf script [<options>] <script> [<record-options>] <command>",
2575 "perf script [<options>] <top-script> [script-args]",
2576 NULL
2577 };
Tom Zanussi38752942009-12-15 02:53:39 -06002578
Tom Zanussib5b87312010-11-10 08:16:51 -06002579 setup_scripting();
2580
Yunlong Song40cae2b2015-03-18 21:35:54 +08002581 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002582 PARSE_OPT_STOP_AT_NON_OPTION);
2583
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002584 file.path = input_name;
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002585 file.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002586
Tom Zanussib5b87312010-11-10 08:16:51 -06002587 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2588 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2589 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002590 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002591 }
2592
Tom Zanussib5b87312010-11-10 08:16:51 -06002593 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2594 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2595 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002596 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002597 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002598 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002599 return -1;
2600 }
Tom Zanussi38752942009-12-15 02:53:39 -06002601 }
2602
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002603 if (itrace_synth_opts.callchain &&
2604 itrace_synth_opts.callchain_sz > scripting_max_stack)
2605 scripting_max_stack = itrace_synth_opts.callchain_sz;
2606
Ben Hutchings44e668c2010-10-10 16:10:03 +01002607 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002608 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002609
Tom Zanussib5b87312010-11-10 08:16:51 -06002610 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002611 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002612 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002613 pid_t pid;
2614
Tom Zanussib5b87312010-11-10 08:16:51 -06002615 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2616 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2617
2618 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002619 usage_with_options_msg(script_usage, options,
2620 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002621 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002622 }
2623
Tom Zanussib5b87312010-11-10 08:16:51 -06002624 if (is_top_script(argv[0])) {
2625 rep_args = argc - 1;
2626 } else {
2627 int rec_args;
2628
2629 rep_args = has_required_arg(rep_script_path);
2630 rec_args = (argc - 1) - rep_args;
2631 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002632 usage_with_options_msg(script_usage, options,
2633 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002634 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002635 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002636 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002637 }
2638
2639 if (pipe(live_pipe) < 0) {
2640 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002641 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002642 }
2643
2644 pid = fork();
2645 if (pid < 0) {
2646 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002647 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002648 }
2649
2650 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002651 j = 0;
2652
Tom Zanussia0cccc22010-04-01 23:59:25 -05002653 dup2(live_pipe[1], 1);
2654 close(live_pipe[0]);
2655
Robert Richter317df652011-11-25 15:05:25 +01002656 if (is_top_script(argv[0])) {
2657 system_wide = true;
2658 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002659 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2660 err = -1;
2661 goto out;
2662 }
Robert Richter317df652011-11-25 15:05:25 +01002663 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002664
2665 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002666 if (!__argv) {
2667 pr_err("malloc failed\n");
2668 err = -ENOMEM;
2669 goto out;
2670 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002671
Tom Zanussib5b87312010-11-10 08:16:51 -06002672 __argv[j++] = "/bin/sh";
2673 __argv[j++] = rec_script_path;
2674 if (system_wide)
2675 __argv[j++] = "-a";
2676 __argv[j++] = "-q";
2677 __argv[j++] = "-o";
2678 __argv[j++] = "-";
2679 for (i = rep_args + 1; i < argc; i++)
2680 __argv[j++] = argv[i];
2681 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002682
2683 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002684 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002685 exit(-1);
2686 }
2687
2688 dup2(live_pipe[0], 0);
2689 close(live_pipe[1]);
2690
Tom Zanussib5b87312010-11-10 08:16:51 -06002691 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002692 if (!__argv) {
2693 pr_err("malloc failed\n");
2694 err = -ENOMEM;
2695 goto out;
2696 }
2697
Tom Zanussib5b87312010-11-10 08:16:51 -06002698 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002699 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002700 __argv[j++] = rep_script_path;
2701 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002702 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002703 __argv[j++] = "-i";
2704 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002705 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002706
2707 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002708 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002709 exit(-1);
2710 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002711
Tom Zanussib5b87312010-11-10 08:16:51 -06002712 if (rec_script_path)
2713 script_path = rec_script_path;
2714 if (rep_script_path)
2715 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002716
Tom Zanussib5b87312010-11-10 08:16:51 -06002717 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002718 j = 0;
2719
Robert Richter317df652011-11-25 15:05:25 +01002720 if (!rec_script_path)
2721 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002722 else if (!system_wide) {
2723 if (have_cmd(argc - 1, &argv[1]) != 0) {
2724 err = -1;
2725 goto out;
2726 }
2727 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002728
2729 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002730 if (!__argv) {
2731 pr_err("malloc failed\n");
2732 err = -ENOMEM;
2733 goto out;
2734 }
2735
Tom Zanussib5b87312010-11-10 08:16:51 -06002736 __argv[j++] = "/bin/sh";
2737 __argv[j++] = script_path;
2738 if (system_wide)
2739 __argv[j++] = "-a";
2740 for (i = 2; i < argc; i++)
2741 __argv[j++] = argv[i];
2742 __argv[j++] = NULL;
2743
2744 execvp("/bin/sh", (char **)__argv);
2745 free(__argv);
2746 exit(-1);
2747 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002748
Tom Zanussicf4fee52010-03-03 01:04:33 -06002749 if (!script_name)
2750 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002751
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002752 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002753 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002754 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002755
Jiri Olsae90debd2013-12-09 11:02:50 +01002756 if (header || header_only) {
2757 perf_session__fprintf_info(session, stdout, show_full_info);
2758 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002759 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002760 }
2761
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002762 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002763 goto out_delete;
2764
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002765 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002766 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002767
Adrian Huntere2167082016-06-23 16:40:58 +03002768 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2769 itrace_synth_opts.thread_stack = true;
2770
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002771 session->itrace_synth_opts = &itrace_synth_opts;
2772
Anton Blanchard5d67be92011-07-04 21:57:50 +10002773 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002774 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2775 if (err < 0)
2776 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002777 }
2778
David Ahern1424dc92011-03-09 22:23:28 -07002779 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002780 symbol_conf.use_callchain = true;
2781 else
2782 symbol_conf.use_callchain = false;
2783
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002784 if (session->tevent.pevent &&
2785 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002786 machine__resolve_kernel_addr,
2787 &session->machines.host) < 0) {
2788 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2789 return -1;
2790 }
2791
Tom Zanussi956ffd02009-11-25 01:15:46 -06002792 if (generate_script_lang) {
2793 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002794 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002795
David Ahern2c9e45f72011-03-17 10:03:21 -06002796 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002797 fprintf(stderr,
2798 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002799 err = -EINVAL;
2800 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002801 }
2802
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002803 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002804 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002805 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002806 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002807 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002808 }
2809
2810 err = fstat(input, &perf_stat);
2811 if (err < 0) {
2812 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002813 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002814 }
2815
2816 if (!perf_stat.st_size) {
2817 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002818 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002819 }
2820
2821 scripting_ops = script_spec__lookup(generate_script_lang);
2822 if (!scripting_ops) {
2823 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002824 err = -ENOENT;
2825 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002826 }
2827
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002828 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002829 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002830 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002831 }
2832
2833 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002834 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002835 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002836 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002837 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002838 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002839 }
2840
David Ahern9cbdb702011-04-06 21:54:20 -06002841
2842 err = perf_session__check_output_opt(session);
2843 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002844 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002845
David Aherna91f4c42016-11-29 10:15:43 -07002846 /* needs to be parsed after looking up reference time */
2847 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
2848 pr_err("Invalid time string\n");
2849 return -EINVAL;
2850 }
2851
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002852 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002853
Adrian Hunterd445dd22014-08-15 22:08:37 +03002854 flush_scripting();
2855
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002856out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002857 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002858 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002859
2860 if (script_started)
2861 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002862out:
2863 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002864}