blob: db5261c3f719977bd4517c934c8bf0a1e63226e5 [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,
Mark Santaniello106dacd2017-06-19 09:38:25 -070088 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
David Ahern745f43e2011-03-09 22:23:26 -070089};
90
91struct output_option {
92 const char *str;
93 enum perf_output_field field;
94} all_output_options[] = {
95 {.str = "comm", .field = PERF_OUTPUT_COMM},
96 {.str = "tid", .field = PERF_OUTPUT_TID},
97 {.str = "pid", .field = PERF_OUTPUT_PID},
98 {.str = "time", .field = PERF_OUTPUT_TIME},
99 {.str = "cpu", .field = PERF_OUTPUT_CPU},
100 {.str = "event", .field = PERF_OUTPUT_EVNAME},
101 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600102 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700103 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600104 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600105 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900106 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200107 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200108 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200109 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200110 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
111 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100112 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
113 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000114 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300115 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300116 {.str = "insn", .field = PERF_OUTPUT_INSN},
117 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800118 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700119 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
David Ahern745f43e2011-03-09 22:23:26 -0700120};
121
122/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600123static struct {
124 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600125 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400126 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600127 u64 fields;
128 u64 invalid_fields;
129} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700130
David Ahern2c9e45f72011-03-17 10:03:21 -0600131 [PERF_TYPE_HARDWARE] = {
132 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700133
David Ahern2c9e45f72011-03-17 10:03:21 -0600134 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
135 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600136 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200137 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
138 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600139
Wang Nan30372f02016-02-24 11:20:45 +0000140 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600141 },
142
143 [PERF_TYPE_SOFTWARE] = {
144 .user_set = false,
145
146 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
147 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600148 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200149 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000150 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600151
152 .invalid_fields = PERF_OUTPUT_TRACE,
153 },
154
155 [PERF_TYPE_TRACEPOINT] = {
156 .user_set = false,
157
158 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
159 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000160 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600161 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700162
163 [PERF_TYPE_RAW] = {
164 .user_set = false,
165
166 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
167 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600168 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200169 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100170 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
171 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700172
Wang Nan30372f02016-02-24 11:20:45 +0000173 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700174 },
Wang Nan27cfef02015-12-08 02:25:43 +0000175
176 [PERF_TYPE_BREAKPOINT] = {
177 .user_set = false,
178
179 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
180 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
181 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
182 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
183 PERF_OUTPUT_PERIOD,
184
Wang Nan30372f02016-02-24 11:20:45 +0000185 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000186 },
David Ahern1424dc92011-03-09 22:23:28 -0700187};
David Ahern745f43e2011-03-09 22:23:26 -0700188
David Ahern2c9e45f72011-03-17 10:03:21 -0600189static bool output_set_by_user(void)
190{
191 int j;
192 for (j = 0; j < PERF_TYPE_MAX; ++j) {
193 if (output[j].user_set)
194 return true;
195 }
196 return false;
197}
David Ahern745f43e2011-03-09 22:23:26 -0700198
David Ahern9cbdb702011-04-06 21:54:20 -0600199static const char *output_field2str(enum perf_output_field field)
200{
201 int i, imax = ARRAY_SIZE(all_output_options);
202 const char *str = "";
203
204 for (i = 0; i < imax; ++i) {
205 if (all_output_options[i].field == field) {
206 str = all_output_options[i].str;
207 break;
208 }
209 }
210 return str;
211}
212
David Ahern2c9e45f72011-03-17 10:03:21 -0600213#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700214
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300215static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
216 u64 sample_type, const char *sample_msg,
217 enum perf_output_field field,
218 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700219{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300220 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600221 int type = attr->type;
222 const char *evname;
223
224 if (attr->sample_type & sample_type)
225 return 0;
226
227 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300228 if (allow_user_set)
229 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300230 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600231 pr_err("Samples for '%s' event do not have %s attribute set. "
232 "Cannot print '%s' field.\n",
233 evname, sample_msg, output_field2str(field));
234 return -1;
235 }
236
237 /* user did not ask for it explicitly so remove from the default list */
238 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300239 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600240 pr_debug("Samples for '%s' event do not have %s attribute set. "
241 "Skipping '%s' field.\n",
242 evname, sample_msg, output_field2str(field));
243
244 return 0;
245}
246
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300247static int perf_evsel__check_stype(struct perf_evsel *evsel,
248 u64 sample_type, const char *sample_msg,
249 enum perf_output_field field)
250{
251 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
252 false);
253}
254
David Ahern9cbdb702011-04-06 21:54:20 -0600255static int perf_evsel__check_attr(struct perf_evsel *evsel,
256 struct perf_session *session)
257{
258 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300259 bool allow_user_set;
260
Jiri Olsae099eba2016-01-05 22:09:09 +0100261 if (perf_header__has_feat(&session->header, HEADER_STAT))
262 return 0;
263
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300264 allow_user_set = perf_header__has_feat(&session->header,
265 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600266
David Ahern1424dc92011-03-09 22:23:28 -0700267 if (PRINT_FIELD(TRACE) &&
268 !perf_session__has_traces(session, "record -R"))
269 return -EINVAL;
270
David Ahern787bef12011-05-27 14:28:43 -0600271 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300272 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
273 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700274 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700275 }
David Ahern7cec0922011-05-30 13:08:23 -0600276
277 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300278 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
279 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600280 return -EINVAL;
281
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100282 if (PRINT_FIELD(DATA_SRC) &&
283 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
284 PERF_OUTPUT_DATA_SRC))
285 return -EINVAL;
286
287 if (PRINT_FIELD(WEIGHT) &&
288 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
289 PERF_OUTPUT_WEIGHT))
290 return -EINVAL;
291
David Ahern7cec0922011-05-30 13:08:23 -0600292 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
293 pr_err("Display of symbols requested but neither sample IP nor "
294 "sample address\nis selected. Hence, no addresses to convert "
295 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600296 return -EINVAL;
297 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900298 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
299 pr_err("Display of offsets requested but symbol is not"
300 "selected.\n");
301 return -EINVAL;
302 }
Mark Santaniello55b9b502017-06-19 09:38:24 -0700303 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR) &&
Mark Santaniello106dacd2017-06-19 09:38:25 -0700304 !PRINT_FIELD(BRSTACK) && !PRINT_FIELD(BRSTACKSYM) && !PRINT_FIELD(BRSTACKOFF)) {
305 pr_err("Display of DSO requested but no address to convert. Select\n"
306 "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
David Ahern610723f2011-05-27 14:28:44 -0600307 return -EINVAL;
308 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200309 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
310 pr_err("Display of source line number requested but sample IP is not\n"
311 "selected. Hence, no address to lookup the source line number.\n");
312 return -EINVAL;
313 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800314 if (PRINT_FIELD(BRSTACKINSN) &&
315 !(perf_evlist__combined_branch_type(session->evlist) &
316 PERF_SAMPLE_BRANCH_ANY)) {
317 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
318 "Hint: run 'perf record -b ...'\n");
319 return -EINVAL;
320 }
David Ahern1424dc92011-03-09 22:23:28 -0700321 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300322 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
323 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700324 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700325
326 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300327 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
328 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700329 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700330
331 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300332 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
333 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700334 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600335
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200336 if (PRINT_FIELD(PERIOD) &&
337 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
338 PERF_OUTPUT_PERIOD))
339 return -EINVAL;
340
Stephane Eranianfc36f942015-08-31 18:41:10 +0200341 if (PRINT_FIELD(IREGS) &&
342 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
343 PERF_OUTPUT_IREGS))
344 return -EINVAL;
345
David Ahern9cbdb702011-04-06 21:54:20 -0600346 return 0;
347}
348
Adrian Hunter7ea95722013-11-01 15:51:30 +0200349static void set_print_ip_opts(struct perf_event_attr *attr)
350{
351 unsigned int type = attr->type;
352
353 output[type].print_ip_opts = 0;
354 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300355 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200356
357 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300358 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200359
360 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300361 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200362
363 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300364 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200365
366 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300367 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200368}
369
David Ahern9cbdb702011-04-06 21:54:20 -0600370/*
371 * verify all user requested events exist and the samples
372 * have the expected data
373 */
374static int perf_session__check_output_opt(struct perf_session *session)
375{
He Kuang40f20e52016-05-16 04:51:19 +0000376 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600377 struct perf_evsel *evsel;
378
379 for (j = 0; j < PERF_TYPE_MAX; ++j) {
380 evsel = perf_session__find_first_evtype(session, j);
381
382 /*
383 * even if fields is set to 0 (ie., show nothing) event must
384 * exist if user explicitly includes it on the command line
385 */
386 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
387 pr_err("%s events do not exist. "
388 "Remove corresponding -f option to proceed.\n",
389 event_type(j));
390 return -1;
391 }
392
393 if (evsel && output[j].fields &&
394 perf_evsel__check_attr(evsel, session))
395 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400396
397 if (evsel == NULL)
398 continue;
399
Adrian Hunter7ea95722013-11-01 15:51:30 +0200400 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700401 }
402
Adrian Hunter98526ee2014-07-31 09:00:59 +0300403 if (!no_callchain) {
404 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000405 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300406
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300407 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000408 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300409 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
410 use_callchain = true;
411 break;
412 }
413 }
He Kuang71ac8992016-08-04 11:25:43 +0000414 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300415 symbol_conf.use_callchain = false;
416 }
417
David Ahern80b8b492013-11-19 21:07:37 -0700418 /*
419 * set default for tracepoints to print symbols only
420 * if callchains are present
421 */
422 if (symbol_conf.use_callchain &&
423 !output[PERF_TYPE_TRACEPOINT].user_set) {
424 struct perf_event_attr *attr;
425
426 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700427
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300428 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000429 if (evsel->attr.type != j)
430 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700431
He Kuang40f20e52016-05-16 04:51:19 +0000432 attr = &evsel->attr;
433
434 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
435 output[j].fields |= PERF_OUTPUT_IP;
436 output[j].fields |= PERF_OUTPUT_SYM;
437 output[j].fields |= PERF_OUTPUT_DSO;
438 set_print_ip_opts(attr);
439 goto out;
440 }
David Ahern80b8b492013-11-19 21:07:37 -0700441 }
442 }
443
444out:
David Ahern1424dc92011-03-09 22:23:28 -0700445 return 0;
446}
David Ahern745f43e2011-03-09 22:23:26 -0700447
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300448static void print_sample_iregs(struct perf_sample *sample,
Stephane Eranianfc36f942015-08-31 18:41:10 +0200449 struct perf_event_attr *attr)
450{
451 struct regs_dump *regs = &sample->intr_regs;
452 uint64_t mask = attr->sample_regs_intr;
453 unsigned i = 0, r;
454
455 if (!regs)
456 return;
457
458 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
459 u64 val = regs->regs[i++];
460 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
461 }
462}
463
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300464static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700465 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300466 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700467{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300468 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700469 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700470 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700471
David Ahern745f43e2011-03-09 22:23:26 -0700472 if (PRINT_FIELD(COMM)) {
473 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200474 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600475 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200476 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700477 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200478 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700479 }
David Ahernc70c94b2011-03-09 22:23:25 -0700480
David Ahern745f43e2011-03-09 22:23:26 -0700481 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
482 printf("%5d/%-5d ", sample->pid, sample->tid);
483 else if (PRINT_FIELD(PID))
484 printf("%5d ", sample->pid);
485 else if (PRINT_FIELD(TID))
486 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700487
David Ahern745f43e2011-03-09 22:23:26 -0700488 if (PRINT_FIELD(CPU)) {
489 if (latency_format)
490 printf("%3d ", sample->cpu);
491 else
492 printf("[%03d] ", sample->cpu);
493 }
David Ahernc70c94b2011-03-09 22:23:25 -0700494
David Ahern745f43e2011-03-09 22:23:26 -0700495 if (PRINT_FIELD(TIME)) {
496 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300497 secs = nsecs / NSEC_PER_SEC;
498 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900499
Adrian Hunter83e19862015-09-25 16:15:36 +0300500 if (nanosecs)
501 printf("%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900502 else {
503 char sample_time[32];
504 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
505 printf("%12s: ", sample_time);
506 }
David Ahern745f43e2011-03-09 22:23:26 -0700507 }
David Ahernc70c94b2011-03-09 22:23:25 -0700508}
509
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200510static inline char
511mispred_str(struct branch_entry *br)
512{
513 if (!(br->flags.mispred || br->flags.predicted))
514 return '-';
515
516 return br->flags.predicted ? 'P' : 'M';
517}
518
Mark Santaniello55b9b502017-06-19 09:38:24 -0700519static void print_sample_brstack(struct perf_sample *sample,
520 struct thread *thread,
521 struct perf_event_attr *attr)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200522{
523 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700524 struct addr_location alf, alt;
525 u64 i, from, to;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200526
527 if (!(br && br->nr))
528 return;
529
530 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700531 from = br->entries[i].from;
532 to = br->entries[i].to;
533
534 if (PRINT_FIELD(DSO)) {
535 memset(&alf, 0, sizeof(alf));
536 memset(&alt, 0, sizeof(alt));
537 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
538 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
539 }
540
541 printf("0x%"PRIx64, from);
542 if (PRINT_FIELD(DSO)) {
543 printf("(");
544 map__fprintf_dsoname(alf.map, stdout);
545 printf(")");
546 }
547
548 printf("/0x%"PRIx64, to);
549 if (PRINT_FIELD(DSO)) {
550 printf("(");
551 map__fprintf_dsoname(alt.map, stdout);
552 printf(")");
553 }
554
555 printf("/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200556 mispred_str( br->entries + i),
557 br->entries[i].flags.in_tx? 'X' : '-',
558 br->entries[i].flags.abort? 'A' : '-',
559 br->entries[i].flags.cycles);
560 }
561}
562
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300563static void print_sample_brstacksym(struct perf_sample *sample,
Mark Santaniello55b9b502017-06-19 09:38:24 -0700564 struct thread *thread,
565 struct perf_event_attr *attr)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200566{
567 struct branch_stack *br = sample->branch_stack;
568 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200569 u64 i, from, to;
570
571 if (!(br && br->nr))
572 return;
573
574 for (i = 0; i < br->nr; i++) {
575
576 memset(&alf, 0, sizeof(alf));
577 memset(&alt, 0, sizeof(alt));
578 from = br->entries[i].from;
579 to = br->entries[i].to;
580
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300581 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200582 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300583 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200584
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300585 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200586 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300587 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200588
589 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700590 if (PRINT_FIELD(DSO)) {
591 printf("(");
592 map__fprintf_dsoname(alf.map, stdout);
593 printf(")");
594 }
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200595 putchar('/');
596 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700597 if (PRINT_FIELD(DSO)) {
598 printf("(");
599 map__fprintf_dsoname(alt.map, stdout);
600 printf(")");
601 }
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200602 printf("/%c/%c/%c/%d ",
603 mispred_str( br->entries + i),
604 br->entries[i].flags.in_tx? 'X' : '-',
605 br->entries[i].flags.abort? 'A' : '-',
606 br->entries[i].flags.cycles);
607 }
608}
609
Mark Santaniello106dacd2017-06-19 09:38:25 -0700610static void print_sample_brstackoff(struct perf_sample *sample,
611 struct thread *thread,
612 struct perf_event_attr *attr)
613{
614 struct branch_stack *br = sample->branch_stack;
615 struct addr_location alf, alt;
616 u64 i, from, to;
617
618 if (!(br && br->nr))
619 return;
620
621 for (i = 0; i < br->nr; i++) {
622
623 memset(&alf, 0, sizeof(alf));
624 memset(&alt, 0, sizeof(alt));
625 from = br->entries[i].from;
626 to = br->entries[i].to;
627
628 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
629 if (alf.map && !alf.map->dso->adjust_symbols)
630 from = map__map_ip(alf.map, from);
631
632 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
633 if (alt.map && !alt.map->dso->adjust_symbols)
634 to = map__map_ip(alt.map, to);
635
636 printf("0x%"PRIx64, from);
637 if (PRINT_FIELD(DSO)) {
638 printf("(");
639 map__fprintf_dsoname(alf.map, stdout);
640 printf(")");
641 }
642 printf("/0x%"PRIx64, to);
643 if (PRINT_FIELD(DSO)) {
644 printf("(");
645 map__fprintf_dsoname(alt.map, stdout);
646 printf(")");
647 }
648 printf("/%c/%c/%c/%d ",
649 mispred_str(br->entries + i),
650 br->entries[i].flags.in_tx ? 'X' : '-',
651 br->entries[i].flags.abort ? 'A' : '-',
652 br->entries[i].flags.cycles);
653 }
654}
Andi Kleen48d02a12017-02-23 15:46:34 -0800655#define MAXBB 16384UL
656
657static int grab_bb(u8 *buffer, u64 start, u64 end,
658 struct machine *machine, struct thread *thread,
659 bool *is64bit, u8 *cpumode, bool last)
660{
661 long offset, len;
662 struct addr_location al;
663 bool kernel;
664
665 if (!start || !end)
666 return 0;
667
668 kernel = machine__kernel_ip(machine, start);
669 if (kernel)
670 *cpumode = PERF_RECORD_MISC_KERNEL;
671 else
672 *cpumode = PERF_RECORD_MISC_USER;
673
674 /*
675 * Block overlaps between kernel and user.
676 * This can happen due to ring filtering
677 * On Intel CPUs the entry into the kernel is filtered,
678 * but the exit is not. Let the caller patch it up.
679 */
680 if (kernel != machine__kernel_ip(machine, end)) {
681 printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
682 start, end);
683 return -ENXIO;
684 }
685
686 memset(&al, 0, sizeof(al));
687 if (end - start > MAXBB - MAXINSN) {
688 if (last)
689 printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
690 else
691 printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
692 return 0;
693 }
694
695 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
696 if (!al.map || !al.map->dso) {
697 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
698 return 0;
699 }
700 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
701 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
702 return 0;
703 }
704
705 /* Load maps to ensure dso->is_64_bit has been updated */
706 map__load(al.map);
707
708 offset = al.map->map_ip(al.map, start);
709 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
710 end - start + MAXINSN);
711
712 *is64bit = al.map->dso->is_64_bit;
713 if (len <= 0)
714 printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
715 start, end);
716 return len;
717}
718
719static void print_jump(uint64_t ip, struct branch_entry *en,
720 struct perf_insn *x, u8 *inbuf, int len,
721 int insn)
722{
723 printf("\t%016" PRIx64 "\t%-30s\t#%s%s%s%s",
724 ip,
725 dump_insn(x, ip, inbuf, len, NULL),
726 en->flags.predicted ? " PRED" : "",
727 en->flags.mispred ? " MISPRED" : "",
728 en->flags.in_tx ? " INTX" : "",
729 en->flags.abort ? " ABORT" : "");
730 if (en->flags.cycles) {
731 printf(" %d cycles", en->flags.cycles);
732 if (insn)
733 printf(" %.2f IPC", (float)insn / en->flags.cycles);
734 }
735 putchar('\n');
736}
737
738static void print_ip_sym(struct thread *thread, u8 cpumode, int cpu,
739 uint64_t addr, struct symbol **lastsym,
740 struct perf_event_attr *attr)
741{
742 struct addr_location al;
743 int off;
744
745 memset(&al, 0, sizeof(al));
746
747 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
748 if (!al.map)
749 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
750 addr, &al);
751 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
752 return;
753
754 al.cpu = cpu;
755 al.sym = NULL;
756 if (al.map)
757 al.sym = map__find_symbol(al.map, al.addr);
758
759 if (!al.sym)
760 return;
761
762 if (al.addr < al.sym->end)
763 off = al.addr - al.sym->start;
764 else
765 off = al.addr - al.map->start - al.sym->start;
766 printf("\t%s", al.sym->name);
767 if (off)
768 printf("%+d", off);
769 putchar(':');
770 if (PRINT_FIELD(SRCLINE))
771 map__fprintf_srcline(al.map, al.addr, "\t", stdout);
772 putchar('\n');
773 *lastsym = al.sym;
774}
775
776static void print_sample_brstackinsn(struct perf_sample *sample,
777 struct thread *thread,
778 struct perf_event_attr *attr,
779 struct machine *machine)
780{
781 struct branch_stack *br = sample->branch_stack;
782 u64 start, end;
783 int i, insn, len, nr, ilen;
784 struct perf_insn x;
785 u8 buffer[MAXBB];
786 unsigned off;
787 struct symbol *lastsym = NULL;
788
789 if (!(br && br->nr))
790 return;
791 nr = br->nr;
792 if (max_blocks && nr > max_blocks + 1)
793 nr = max_blocks + 1;
794
795 x.thread = thread;
796 x.cpu = sample->cpu;
797
798 putchar('\n');
799
800 /* Handle first from jump, of which we don't know the entry. */
801 len = grab_bb(buffer, br->entries[nr-1].from,
802 br->entries[nr-1].from,
803 machine, thread, &x.is64bit, &x.cpumode, false);
804 if (len > 0) {
805 print_ip_sym(thread, x.cpumode, x.cpu,
806 br->entries[nr - 1].from, &lastsym, attr);
807 print_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
808 &x, buffer, len, 0);
809 }
810
811 /* Print all blocks */
812 for (i = nr - 2; i >= 0; i--) {
813 if (br->entries[i].from || br->entries[i].to)
814 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
815 br->entries[i].from,
816 br->entries[i].to);
817 start = br->entries[i + 1].to;
818 end = br->entries[i].from;
819
820 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
821 /* Patch up missing kernel transfers due to ring filters */
822 if (len == -ENXIO && i > 0) {
823 end = br->entries[--i].from;
824 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
825 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
826 }
827 if (len <= 0)
828 continue;
829
830 insn = 0;
831 for (off = 0;; off += ilen) {
832 uint64_t ip = start + off;
833
834 print_ip_sym(thread, x.cpumode, x.cpu, ip, &lastsym, attr);
835 if (ip == end) {
836 print_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn);
837 break;
838 } else {
839 printf("\t%016" PRIx64 "\t%s\n", ip,
840 dump_insn(&x, ip, buffer + off, len - off, &ilen));
841 if (ilen == 0)
842 break;
843 insn++;
844 }
845 }
846 }
847
848 /*
849 * Hit the branch? In this case we are already done, and the target
850 * has not been executed yet.
851 */
852 if (br->entries[0].from == sample->ip)
853 return;
854 if (br->entries[0].flags.abort)
855 return;
856
857 /*
858 * Print final block upto sample
859 */
860 start = br->entries[0].to;
861 end = sample->ip;
862 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
863 print_ip_sym(thread, x.cpumode, x.cpu, start, &lastsym, attr);
864 if (len <= 0) {
865 /* Print at least last IP if basic block did not work */
866 len = grab_bb(buffer, sample->ip, sample->ip,
867 machine, thread, &x.is64bit, &x.cpumode, false);
868 if (len <= 0)
869 return;
870
871 printf("\t%016" PRIx64 "\t%s\n", sample->ip,
872 dump_insn(&x, sample->ip, buffer, len, NULL));
873 return;
874 }
875 for (off = 0; off <= end - start; off += ilen) {
876 printf("\t%016" PRIx64 "\t%s\n", start + off,
877 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
878 if (ilen == 0)
879 break;
880 }
881}
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200882
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300883static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600884 struct thread *thread,
885 struct perf_event_attr *attr)
886{
887 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600888
889 printf("%16" PRIx64, sample->addr);
890
891 if (!sample_addr_correlates_sym(attr))
892 return;
893
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300894 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600895
896 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900897 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900898 if (PRINT_FIELD(SYMOFFSET))
899 symbol__fprintf_symname_offs(al.sym, &al, stdout);
900 else
901 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600902 }
903
904 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900905 printf(" (");
906 map__fprintf_dsoname(al.map, stdout);
907 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600908 }
909}
910
Adrian Huntere2167082016-06-23 16:40:58 +0300911static void print_sample_callindent(struct perf_sample *sample,
912 struct perf_evsel *evsel,
913 struct thread *thread,
914 struct addr_location *al)
915{
916 struct perf_event_attr *attr = &evsel->attr;
917 size_t depth = thread_stack__depth(thread);
918 struct addr_location addr_al;
919 const char *name = NULL;
920 static int spacing;
921 int len = 0;
922 u64 ip = 0;
923
924 /*
925 * The 'return' has already been popped off the stack so the depth has
926 * to be adjusted to match the 'call'.
927 */
928 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
929 depth += 1;
930
931 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
932 if (sample_addr_correlates_sym(attr)) {
933 thread__resolve(thread, &addr_al, sample);
934 if (addr_al.sym)
935 name = addr_al.sym->name;
936 else
937 ip = sample->addr;
938 } else {
939 ip = sample->addr;
940 }
941 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
942 if (al->sym)
943 name = al->sym->name;
944 else
945 ip = sample->ip;
946 }
947
948 if (name)
949 len = printf("%*s%s", (int)depth * 4, "", name);
950 else if (ip)
951 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
952
953 if (len < 0)
954 return;
955
956 /*
957 * Try to keep the output length from changing frequently so that the
958 * output lines up more nicely.
959 */
960 if (len > spacing || (len && len < spacing - 52))
961 spacing = round_up(len + 4, 32);
962
963 if (len < spacing)
964 printf("%*s", spacing - len, "");
965}
966
Andi Kleen224e2c92016-10-07 16:42:27 +0300967static void print_insn(struct perf_sample *sample,
Andi Kleen48d02a12017-02-23 15:46:34 -0800968 struct perf_event_attr *attr,
969 struct thread *thread,
970 struct machine *machine)
Andi Kleen224e2c92016-10-07 16:42:27 +0300971{
972 if (PRINT_FIELD(INSNLEN))
973 printf(" ilen: %d", sample->insn_len);
974 if (PRINT_FIELD(INSN)) {
975 int i;
976
977 printf(" insn:");
978 for (i = 0; i < sample->insn_len; i++)
979 printf(" %02x", (unsigned char)sample->insn[i]);
980 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800981 if (PRINT_FIELD(BRSTACKINSN))
982 print_sample_brstackinsn(sample, thread, attr, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +0300983}
984
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300985static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +0900986 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200987 struct thread *thread,
Andi Kleen48d02a12017-02-23 15:46:34 -0800988 struct addr_location *al,
989 struct machine *machine)
Akihiro Nagai95582592012-01-30 13:43:09 +0900990{
991 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300992 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900993
Adrian Huntere2167082016-06-23 16:40:58 +0300994 if (PRINT_FIELD(CALLINDENT))
995 print_sample_callindent(sample, evsel, thread, al);
996
Akihiro Nagai95582592012-01-30 13:43:09 +0900997 /* print branch_from information */
998 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300999 unsigned int print_opts = output[attr->type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001000 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001001
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001002 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001003 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001004 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001005 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001006
1007 if (cursor == NULL) {
1008 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001009 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001010 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001011 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001012 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001013 } else
1014 putchar('\n');
1015
1016 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +09001017 }
1018
Akihiro Nagai95582592012-01-30 13:43:09 +09001019 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001020 if (PRINT_FIELD(ADDR) ||
1021 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +03001022 !output[attr->type].user_set)) {
1023 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001024 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +03001025 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001026
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001027 if (print_srcline_last)
1028 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
1029
Andi Kleen48d02a12017-02-23 15:46:34 -08001030 print_insn(sample, attr, thread, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +03001031
Akihiro Nagai95582592012-01-30 13:43:09 +09001032 printf("\n");
1033}
1034
Adrian Hunter055cd332016-06-23 16:40:56 +03001035static struct {
1036 u32 flags;
1037 const char *name;
1038} sample_flags[] = {
1039 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1040 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1041 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1042 {PERF_IP_FLAG_BRANCH, "jmp"},
1043 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1044 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1045 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1046 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1047 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1048 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1049 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1050 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1051 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1052 {0, NULL}
1053};
1054
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001055static void print_sample_flags(u32 flags)
1056{
1057 const char *chars = PERF_IP_FLAG_CHARS;
1058 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001059 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1060 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001061 char str[33];
1062 int i, pos = 0;
1063
Adrian Hunter055cd332016-06-23 16:40:56 +03001064 for (i = 0; sample_flags[i].name ; i++) {
1065 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1066 name = sample_flags[i].name;
1067 break;
1068 }
1069 }
1070
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001071 for (i = 0; i < n; i++, flags >>= 1) {
1072 if (flags & 1)
1073 str[pos++] = chars[i];
1074 }
1075 for (; i < 32; i++, flags >>= 1) {
1076 if (flags & 1)
1077 str[pos++] = '?';
1078 }
1079 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001080
1081 if (name)
1082 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
1083 else
1084 printf(" %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001085}
1086
Wang Nan30372f02016-02-24 11:20:45 +00001087struct printer_data {
1088 int line_no;
1089 bool hit_nul;
1090 bool is_printable;
1091};
1092
1093static void
1094print_sample_bpf_output_printer(enum binary_printer_ops op,
1095 unsigned int val,
1096 void *extra)
1097{
1098 unsigned char ch = (unsigned char)val;
1099 struct printer_data *printer_data = extra;
1100
1101 switch (op) {
1102 case BINARY_PRINT_DATA_BEGIN:
1103 printf("\n");
1104 break;
1105 case BINARY_PRINT_LINE_BEGIN:
1106 printf("%17s", !printer_data->line_no ? "BPF output:" :
1107 " ");
1108 break;
1109 case BINARY_PRINT_ADDR:
1110 printf(" %04x:", val);
1111 break;
1112 case BINARY_PRINT_NUM_DATA:
1113 printf(" %02x", val);
1114 break;
1115 case BINARY_PRINT_NUM_PAD:
1116 printf(" ");
1117 break;
1118 case BINARY_PRINT_SEP:
1119 printf(" ");
1120 break;
1121 case BINARY_PRINT_CHAR_DATA:
1122 if (printer_data->hit_nul && ch)
1123 printer_data->is_printable = false;
1124
1125 if (!isprint(ch)) {
1126 printf("%c", '.');
1127
1128 if (!printer_data->is_printable)
1129 break;
1130
1131 if (ch == '\0')
1132 printer_data->hit_nul = true;
1133 else
1134 printer_data->is_printable = false;
1135 } else {
1136 printf("%c", ch);
1137 }
1138 break;
1139 case BINARY_PRINT_CHAR_PAD:
1140 printf(" ");
1141 break;
1142 case BINARY_PRINT_LINE_END:
1143 printf("\n");
1144 printer_data->line_no++;
1145 break;
1146 case BINARY_PRINT_DATA_END:
1147 default:
1148 break;
1149 }
1150}
1151
1152static void print_sample_bpf_output(struct perf_sample *sample)
1153{
1154 unsigned int nr_bytes = sample->raw_size;
1155 struct printer_data printer_data = {0, false, true};
1156
1157 print_binary(sample->raw_data, nr_bytes, 8,
1158 print_sample_bpf_output_printer, &printer_data);
1159
1160 if (printer_data.is_printable && printer_data.hit_nul)
1161 printf("%17s \"%s\"\n", "BPF string:",
1162 (char *)(sample->raw_data));
1163}
1164
Jiri Olsa809e9422015-11-26 18:55:21 +01001165struct perf_script {
1166 struct perf_tool tool;
1167 struct perf_session *session;
1168 bool show_task_events;
1169 bool show_mmap_events;
1170 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301171 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001172 bool allocated;
1173 struct cpu_map *cpus;
1174 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001175 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001176 const char *time_str;
1177 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001178};
1179
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001180static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1181{
1182 struct perf_evsel *evsel;
1183 int max = 0;
1184
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001185 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001186 int len = strlen(perf_evsel__name(evsel));
1187
1188 max = MAX(len, max);
1189 }
1190
1191 return max;
1192}
1193
Jiri Olsac19ac912016-02-24 09:46:54 +01001194static size_t data_src__printf(u64 data_src)
1195{
1196 struct mem_info mi = { .data_src.val = data_src };
1197 char decode[100];
1198 char out[100];
1199 static int maxlen;
1200 int len;
1201
1202 perf_script__meminfo_scnprintf(decode, 100, &mi);
1203
1204 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1205 if (maxlen < len)
1206 maxlen = len;
1207
1208 return printf("%-*s", maxlen, out);
1209}
1210
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001211static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001212 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001213 struct addr_location *al,
1214 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001215{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001216 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001217 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -07001218
David Ahern2c9e45f72011-03-17 10:03:21 -06001219 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001220 return;
1221
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001222 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -07001223
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001224 if (PRINT_FIELD(PERIOD))
1225 printf("%10" PRIu64 " ", sample->period);
1226
Namhyung Kime944d3d2013-11-18 14:34:52 +09001227 if (PRINT_FIELD(EVNAME)) {
1228 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001229
1230 if (!script->name_width)
1231 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1232
1233 printf("%*s: ", script->name_width,
1234 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001235 }
1236
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001237 if (print_flags)
1238 print_sample_flags(sample->flags);
1239
Akihiro Nagai95582592012-01-30 13:43:09 +09001240 if (is_bts_event(attr)) {
Andi Kleen48d02a12017-02-23 15:46:34 -08001241 print_sample_bts(sample, evsel, thread, al, machine);
Akihiro Nagai95582592012-01-30 13:43:09 +09001242 return;
1243 }
1244
David Ahern745f43e2011-03-09 22:23:26 -07001245 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001246 event_format__print(evsel->tp_format, sample->cpu,
1247 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -06001248 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001249 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -06001250
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001251 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +01001252 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001253
1254 if (PRINT_FIELD(WEIGHT))
1255 printf("%16" PRIu64, sample->weight);
1256
David Ahern787bef12011-05-27 14:28:43 -06001257 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001258 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001259
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001260 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001261 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001262 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001263 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001264
1265 putchar(cursor ? '\n' : ' ');
1266 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001267 }
1268
Stephane Eranianfc36f942015-08-31 18:41:10 +02001269 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001270 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001271
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001272 if (PRINT_FIELD(BRSTACK))
Mark Santaniello55b9b502017-06-19 09:38:24 -07001273 print_sample_brstack(sample, thread, attr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001274 else if (PRINT_FIELD(BRSTACKSYM))
Mark Santaniello55b9b502017-06-19 09:38:24 -07001275 print_sample_brstacksym(sample, thread, attr);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001276 else if (PRINT_FIELD(BRSTACKOFF))
1277 print_sample_brstackoff(sample, thread, attr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001278
Wang Nan30372f02016-02-24 11:20:45 +00001279 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1280 print_sample_bpf_output(sample);
Andi Kleen48d02a12017-02-23 15:46:34 -08001281 print_insn(sample, attr, thread, machine);
David Ahernc70c94b2011-03-09 22:23:25 -07001282 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001283}
1284
Tom Zanussi956ffd02009-11-25 01:15:46 -06001285static struct scripting_ops *scripting_ops;
1286
Jiri Olsa36e33c52016-01-06 11:49:56 +01001287static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1288{
1289 int nthreads = thread_map__nr(counter->threads);
1290 int ncpus = perf_evsel__nr_cpus(counter);
1291 int cpu, thread;
1292 static int header_printed;
1293
1294 if (counter->system_wide)
1295 nthreads = 1;
1296
1297 if (!header_printed) {
1298 printf("%3s %8s %15s %15s %15s %15s %s\n",
1299 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1300 header_printed = 1;
1301 }
1302
1303 for (thread = 0; thread < nthreads; thread++) {
1304 for (cpu = 0; cpu < ncpus; cpu++) {
1305 struct perf_counts_values *counts;
1306
1307 counts = perf_counts(counter->counts, cpu, thread);
1308
1309 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1310 counter->cpus->map[cpu],
1311 thread_map__pid(counter->threads, thread),
1312 counts->val,
1313 counts->ena,
1314 counts->run,
1315 tstamp,
1316 perf_evsel__name(counter));
1317 }
1318 }
1319}
1320
Jiri Olsae099eba2016-01-05 22:09:09 +01001321static void process_stat(struct perf_evsel *counter, u64 tstamp)
1322{
1323 if (scripting_ops && scripting_ops->process_stat)
1324 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001325 else
1326 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001327}
1328
1329static void process_stat_interval(u64 tstamp)
1330{
1331 if (scripting_ops && scripting_ops->process_stat_interval)
1332 scripting_ops->process_stat_interval(tstamp);
1333}
1334
Tom Zanussi956ffd02009-11-25 01:15:46 -06001335static void setup_scripting(void)
1336{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001337 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001338 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001339}
1340
Adrian Hunterd445dd22014-08-15 22:08:37 +03001341static int flush_scripting(void)
1342{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001343 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001344}
1345
Tom Zanussi956ffd02009-11-25 01:15:46 -06001346static int cleanup_scripting(void)
1347{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001348 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001349
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001350 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001351}
1352
Jiri Olsa809e9422015-11-26 18:55:21 +01001353static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001354 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001355 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001356 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001357 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001358{
Jiri Olsa809e9422015-11-26 18:55:21 +01001359 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001360 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001361
David Aherna91f4c42016-11-29 10:15:43 -07001362 if (perf_time__skip_sample(&scr->ptime, sample->time))
1363 return 0;
1364
David Ahern1424dc92011-03-09 22:23:28 -07001365 if (debug_mode) {
1366 if (sample->time < last_timestamp) {
1367 pr_err("Samples misordered, previous: %" PRIu64
1368 " this: %" PRIu64 "\n", last_timestamp,
1369 sample->time);
1370 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001371 }
David Ahern1424dc92011-03-09 22:23:28 -07001372 last_timestamp = sample->time;
1373 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001374 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001375
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001376 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001377 pr_err("problem processing %d event, skipping it.\n",
1378 event->header.type);
1379 return -1;
1380 }
1381
1382 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001383 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001384
Anton Blanchard5d67be92011-07-04 21:57:50 +10001385 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001386 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001387
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001388 if (scripting_ops)
1389 scripting_ops->process_event(event, sample, evsel, &al);
1390 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001391 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001392
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001393out_put:
1394 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001395 return 0;
1396}
1397
Adrian Hunter7ea95722013-11-01 15:51:30 +02001398static int process_attr(struct perf_tool *tool, union perf_event *event,
1399 struct perf_evlist **pevlist)
1400{
1401 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1402 struct perf_evlist *evlist;
1403 struct perf_evsel *evsel, *pos;
1404 int err;
1405
1406 err = perf_event__process_attr(tool, event, pevlist);
1407 if (err)
1408 return err;
1409
1410 evlist = *pevlist;
1411 evsel = perf_evlist__last(*pevlist);
1412
1413 if (evsel->attr.type >= PERF_TYPE_MAX)
1414 return 0;
1415
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001416 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001417 if (pos->attr.type == evsel->attr.type && pos != evsel)
1418 return 0;
1419 }
1420
1421 set_print_ip_opts(&evsel->attr);
1422
Jiri Olsad2b5a312015-10-16 12:41:25 +02001423 if (evsel->attr.sample_type)
1424 err = perf_evsel__check_attr(evsel, scr->session);
1425
1426 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001427}
1428
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001429static int process_comm_event(struct perf_tool *tool,
1430 union perf_event *event,
1431 struct perf_sample *sample,
1432 struct machine *machine)
1433{
1434 struct thread *thread;
1435 struct perf_script *script = container_of(tool, struct perf_script, tool);
1436 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001437 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001438 int ret = -1;
1439
1440 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1441 if (thread == NULL) {
1442 pr_debug("problem processing COMM event, skipping it.\n");
1443 return -1;
1444 }
1445
1446 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1447 goto out;
1448
1449 if (!evsel->attr.sample_id_all) {
1450 sample->cpu = 0;
1451 sample->time = 0;
1452 sample->tid = event->comm.tid;
1453 sample->pid = event->comm.pid;
1454 }
1455 print_sample_start(sample, thread, evsel);
1456 perf_event__fprintf(event, stdout);
1457 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001458out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001459 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001460 return ret;
1461}
1462
Hari Bathini96a44bb2017-03-08 02:12:06 +05301463static int process_namespaces_event(struct perf_tool *tool,
1464 union perf_event *event,
1465 struct perf_sample *sample,
1466 struct machine *machine)
1467{
1468 struct thread *thread;
1469 struct perf_script *script = container_of(tool, struct perf_script, tool);
1470 struct perf_session *session = script->session;
1471 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1472 int ret = -1;
1473
1474 thread = machine__findnew_thread(machine, event->namespaces.pid,
1475 event->namespaces.tid);
1476 if (thread == NULL) {
1477 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1478 return -1;
1479 }
1480
1481 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1482 goto out;
1483
1484 if (!evsel->attr.sample_id_all) {
1485 sample->cpu = 0;
1486 sample->time = 0;
1487 sample->tid = event->namespaces.tid;
1488 sample->pid = event->namespaces.pid;
1489 }
1490 print_sample_start(sample, thread, evsel);
1491 perf_event__fprintf(event, stdout);
1492 ret = 0;
1493out:
1494 thread__put(thread);
1495 return ret;
1496}
1497
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001498static int process_fork_event(struct perf_tool *tool,
1499 union perf_event *event,
1500 struct perf_sample *sample,
1501 struct machine *machine)
1502{
1503 struct thread *thread;
1504 struct perf_script *script = container_of(tool, struct perf_script, tool);
1505 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001506 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001507
1508 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1509 return -1;
1510
1511 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1512 if (thread == NULL) {
1513 pr_debug("problem processing FORK event, skipping it.\n");
1514 return -1;
1515 }
1516
1517 if (!evsel->attr.sample_id_all) {
1518 sample->cpu = 0;
1519 sample->time = event->fork.time;
1520 sample->tid = event->fork.tid;
1521 sample->pid = event->fork.pid;
1522 }
1523 print_sample_start(sample, thread, evsel);
1524 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001525 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001526
1527 return 0;
1528}
1529static int process_exit_event(struct perf_tool *tool,
1530 union perf_event *event,
1531 struct perf_sample *sample,
1532 struct machine *machine)
1533{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001534 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001535 struct thread *thread;
1536 struct perf_script *script = container_of(tool, struct perf_script, tool);
1537 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001538 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001539
1540 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1541 if (thread == NULL) {
1542 pr_debug("problem processing EXIT event, skipping it.\n");
1543 return -1;
1544 }
1545
1546 if (!evsel->attr.sample_id_all) {
1547 sample->cpu = 0;
1548 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001549 sample->tid = event->fork.tid;
1550 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001551 }
1552 print_sample_start(sample, thread, evsel);
1553 perf_event__fprintf(event, stdout);
1554
1555 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001556 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001557
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001558 thread__put(thread);
1559 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001560}
1561
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001562static int process_mmap_event(struct perf_tool *tool,
1563 union perf_event *event,
1564 struct perf_sample *sample,
1565 struct machine *machine)
1566{
1567 struct thread *thread;
1568 struct perf_script *script = container_of(tool, struct perf_script, tool);
1569 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001570 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001571
1572 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1573 return -1;
1574
1575 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1576 if (thread == NULL) {
1577 pr_debug("problem processing MMAP event, skipping it.\n");
1578 return -1;
1579 }
1580
1581 if (!evsel->attr.sample_id_all) {
1582 sample->cpu = 0;
1583 sample->time = 0;
1584 sample->tid = event->mmap.tid;
1585 sample->pid = event->mmap.pid;
1586 }
1587 print_sample_start(sample, thread, evsel);
1588 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001589 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001590 return 0;
1591}
1592
1593static int process_mmap2_event(struct perf_tool *tool,
1594 union perf_event *event,
1595 struct perf_sample *sample,
1596 struct machine *machine)
1597{
1598 struct thread *thread;
1599 struct perf_script *script = container_of(tool, struct perf_script, tool);
1600 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001601 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001602
1603 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1604 return -1;
1605
1606 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1607 if (thread == NULL) {
1608 pr_debug("problem processing MMAP2 event, skipping it.\n");
1609 return -1;
1610 }
1611
1612 if (!evsel->attr.sample_id_all) {
1613 sample->cpu = 0;
1614 sample->time = 0;
1615 sample->tid = event->mmap2.tid;
1616 sample->pid = event->mmap2.pid;
1617 }
1618 print_sample_start(sample, thread, evsel);
1619 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001620 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001621 return 0;
1622}
1623
Adrian Hunter7c148982015-07-21 12:44:06 +03001624static int process_switch_event(struct perf_tool *tool,
1625 union perf_event *event,
1626 struct perf_sample *sample,
1627 struct machine *machine)
1628{
1629 struct thread *thread;
1630 struct perf_script *script = container_of(tool, struct perf_script, tool);
1631 struct perf_session *session = script->session;
1632 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1633
1634 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1635 return -1;
1636
1637 thread = machine__findnew_thread(machine, sample->pid,
1638 sample->tid);
1639 if (thread == NULL) {
1640 pr_debug("problem processing SWITCH event, skipping it.\n");
1641 return -1;
1642 }
1643
1644 print_sample_start(sample, thread, evsel);
1645 perf_event__fprintf(event, stdout);
1646 thread__put(thread);
1647 return 0;
1648}
1649
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001650static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001651{
1652 session_done = 1;
1653}
1654
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001655static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001656{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001657 int ret;
1658
Tom Zanussic239da32010-04-01 23:59:18 -05001659 signal(SIGINT, sig_handler);
1660
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001661 /* override event processing functions */
1662 if (script->show_task_events) {
1663 script->tool.comm = process_comm_event;
1664 script->tool.fork = process_fork_event;
1665 script->tool.exit = process_exit_event;
1666 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001667 if (script->show_mmap_events) {
1668 script->tool.mmap = process_mmap_event;
1669 script->tool.mmap2 = process_mmap2_event;
1670 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001671 if (script->show_switch_events)
1672 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301673 if (script->show_namespace_events)
1674 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001675
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001676 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001677
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001678 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001679 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001680
1681 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001682}
1683
Tom Zanussi956ffd02009-11-25 01:15:46 -06001684struct script_spec {
1685 struct list_head node;
1686 struct scripting_ops *ops;
1687 char spec[0];
1688};
1689
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001690static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001691
1692static struct script_spec *script_spec__new(const char *spec,
1693 struct scripting_ops *ops)
1694{
1695 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1696
1697 if (s != NULL) {
1698 strcpy(s->spec, spec);
1699 s->ops = ops;
1700 }
1701
1702 return s;
1703}
1704
Tom Zanussi956ffd02009-11-25 01:15:46 -06001705static void script_spec__add(struct script_spec *s)
1706{
1707 list_add_tail(&s->node, &script_specs);
1708}
1709
1710static struct script_spec *script_spec__find(const char *spec)
1711{
1712 struct script_spec *s;
1713
1714 list_for_each_entry(s, &script_specs, node)
1715 if (strcasecmp(s->spec, spec) == 0)
1716 return s;
1717 return NULL;
1718}
1719
Tom Zanussi956ffd02009-11-25 01:15:46 -06001720int script_spec_register(const char *spec, struct scripting_ops *ops)
1721{
1722 struct script_spec *s;
1723
1724 s = script_spec__find(spec);
1725 if (s)
1726 return -1;
1727
Taeung Song8560bae2016-02-26 00:13:10 +09001728 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001729 if (!s)
1730 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001731 else
1732 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001733
1734 return 0;
1735}
1736
1737static struct scripting_ops *script_spec__lookup(const char *spec)
1738{
1739 struct script_spec *s = script_spec__find(spec);
1740 if (!s)
1741 return NULL;
1742
1743 return s->ops;
1744}
1745
1746static void list_available_languages(void)
1747{
1748 struct script_spec *s;
1749
1750 fprintf(stderr, "\n");
1751 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001752 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001753
1754 list_for_each_entry(s, &script_specs, node)
1755 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1756
1757 fprintf(stderr, "\n");
1758}
1759
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001760static int parse_scriptname(const struct option *opt __maybe_unused,
1761 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001762{
1763 char spec[PATH_MAX];
1764 const char *script, *ext;
1765 int len;
1766
Tom Zanussif526d682010-01-27 02:27:52 -06001767 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001768 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001769 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001770 }
1771
1772 script = strchr(str, ':');
1773 if (script) {
1774 len = script - str;
1775 if (len >= PATH_MAX) {
1776 fprintf(stderr, "invalid language specifier");
1777 return -1;
1778 }
1779 strncpy(spec, str, len);
1780 spec[len] = '\0';
1781 scripting_ops = script_spec__lookup(spec);
1782 if (!scripting_ops) {
1783 fprintf(stderr, "invalid language specifier");
1784 return -1;
1785 }
1786 script++;
1787 } else {
1788 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001789 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001790 if (!ext) {
1791 fprintf(stderr, "invalid script extension");
1792 return -1;
1793 }
1794 scripting_ops = script_spec__lookup(++ext);
1795 if (!scripting_ops) {
1796 fprintf(stderr, "invalid script extension");
1797 return -1;
1798 }
1799 }
1800
1801 script_name = strdup(script);
1802
1803 return 0;
1804}
1805
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001806static int parse_output_fields(const struct option *opt __maybe_unused,
1807 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001808{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001809 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001810 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001811 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001812 int rc = 0;
1813 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001814 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07001815 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07001816
1817 if (!str)
1818 return -ENOMEM;
1819
David Ahern2c9e45f72011-03-17 10:03:21 -06001820 /* first word can state for which event type the user is specifying
1821 * the fields. If no type exists, the specified fields apply to all
1822 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001823 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001824 tok = strchr(str, ':');
1825 if (tok) {
1826 *tok = '\0';
1827 tok++;
1828 if (!strcmp(str, "hw"))
1829 type = PERF_TYPE_HARDWARE;
1830 else if (!strcmp(str, "sw"))
1831 type = PERF_TYPE_SOFTWARE;
1832 else if (!strcmp(str, "trace"))
1833 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001834 else if (!strcmp(str, "raw"))
1835 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001836 else if (!strcmp(str, "break"))
1837 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001838 else {
1839 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001840 rc = -EINVAL;
1841 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001842 }
1843
1844 if (output[type].user_set)
1845 pr_warning("Overriding previous field request for %s events.\n",
1846 event_type(type));
1847
1848 output[type].fields = 0;
1849 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001850 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001851
1852 } else {
1853 tok = str;
1854 if (strlen(str) == 0) {
1855 fprintf(stderr,
1856 "Cannot set fields to 'none' for all event types.\n");
1857 rc = -EINVAL;
1858 goto out;
1859 }
1860
Andi Kleen36ce5652017-06-02 08:48:10 -07001861 /* Don't override defaults for +- */
1862 if (strchr(str, '+') || strchr(str, '-'))
1863 goto parse;
1864
David Ahern2c9e45f72011-03-17 10:03:21 -06001865 if (output_set_by_user())
1866 pr_warning("Overriding previous field request for all events.\n");
1867
1868 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1869 output[j].fields = 0;
1870 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001871 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001872 }
David Ahern1424dc92011-03-09 22:23:28 -07001873 }
1874
Andi Kleen36ce5652017-06-02 08:48:10 -07001875parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001876 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07001877 if (*tok == '+') {
1878 if (change == SET)
1879 goto out_badmix;
1880 change = ADD;
1881 tok++;
1882 } else if (*tok == '-') {
1883 if (change == SET)
1884 goto out_badmix;
1885 change = REMOVE;
1886 tok++;
1887 } else {
1888 if (change != SET && change != DEFAULT)
1889 goto out_badmix;
1890 change = SET;
1891 }
1892
David Ahern745f43e2011-03-09 22:23:26 -07001893 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001894 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001895 break;
David Ahern745f43e2011-03-09 22:23:26 -07001896 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001897 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07001898 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001899 continue;
1900 }
David Ahern745f43e2011-03-09 22:23:26 -07001901 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001902 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001903 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001904 goto out;
1905 }
1906
1907 if (type == -1) {
1908 /* add user option to all events types for
1909 * which it is valid
1910 */
1911 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1912 if (output[j].invalid_fields & all_output_options[i].field) {
1913 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1914 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07001915 } else {
1916 if (change == REMOVE)
1917 output[j].fields &= ~all_output_options[i].field;
1918 else
1919 output[j].fields |= all_output_options[i].field;
1920 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001921 }
1922 } else {
1923 if (output[type].invalid_fields & all_output_options[i].field) {
1924 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1925 all_output_options[i].str, event_type(type));
1926
1927 rc = -EINVAL;
1928 goto out;
1929 }
1930 output[type].fields |= all_output_options[i].field;
1931 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001932 }
1933
1934 if (type >= 0) {
1935 if (output[type].fields == 0) {
1936 pr_debug("No fields requested for %s type. "
1937 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001938 }
David Ahern1424dc92011-03-09 22:23:28 -07001939 }
Andi Kleen36ce5652017-06-02 08:48:10 -07001940 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07001941
Andi Kleen36ce5652017-06-02 08:48:10 -07001942out_badmix:
1943 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
1944 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001945out:
David Ahern745f43e2011-03-09 22:23:26 -07001946 free(str);
1947 return rc;
1948}
1949
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001950/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1951static int is_directory(const char *base_path, const struct dirent *dent)
1952{
1953 char path[PATH_MAX];
1954 struct stat st;
1955
1956 sprintf(path, "%s/%s", base_path, dent->d_name);
1957 if (stat(path, &st))
1958 return 0;
1959
1960 return S_ISDIR(st.st_mode);
1961}
1962
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001963#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1964 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1965 if ((lang_dirent->d_type == DT_DIR || \
1966 (lang_dirent->d_type == DT_UNKNOWN && \
1967 is_directory(scripts_path, lang_dirent))) && \
1968 (strcmp(lang_dirent->d_name, ".")) && \
1969 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001970
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001971#define for_each_script(lang_path, lang_dir, script_dirent) \
1972 while ((script_dirent = readdir(lang_dir)) != NULL) \
1973 if (script_dirent->d_type != DT_DIR && \
1974 (script_dirent->d_type != DT_UNKNOWN || \
1975 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001976
1977
1978#define RECORD_SUFFIX "-record"
1979#define REPORT_SUFFIX "-report"
1980
1981struct script_desc {
1982 struct list_head node;
1983 char *name;
1984 char *half_liner;
1985 char *args;
1986};
1987
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001988static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001989
1990static struct script_desc *script_desc__new(const char *name)
1991{
1992 struct script_desc *s = zalloc(sizeof(*s));
1993
Tom Zanussib5b87312010-11-10 08:16:51 -06001994 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001995 s->name = strdup(name);
1996
1997 return s;
1998}
1999
2000static void script_desc__delete(struct script_desc *s)
2001{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002002 zfree(&s->name);
2003 zfree(&s->half_liner);
2004 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002005 free(s);
2006}
2007
2008static void script_desc__add(struct script_desc *s)
2009{
2010 list_add_tail(&s->node, &script_descs);
2011}
2012
2013static struct script_desc *script_desc__find(const char *name)
2014{
2015 struct script_desc *s;
2016
2017 list_for_each_entry(s, &script_descs, node)
2018 if (strcasecmp(s->name, name) == 0)
2019 return s;
2020 return NULL;
2021}
2022
2023static struct script_desc *script_desc__findnew(const char *name)
2024{
2025 struct script_desc *s = script_desc__find(name);
2026
2027 if (s)
2028 return s;
2029
2030 s = script_desc__new(name);
2031 if (!s)
2032 goto out_delete_desc;
2033
2034 script_desc__add(s);
2035
2036 return s;
2037
2038out_delete_desc:
2039 script_desc__delete(s);
2040
2041 return NULL;
2042}
2043
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002044static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002045{
2046 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002047 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002048
2049 if (strlen(str) > suffix_len) {
2050 p = str + strlen(str) - suffix_len;
2051 if (!strncmp(p, suffix, suffix_len))
2052 return p;
2053 }
2054
2055 return NULL;
2056}
2057
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002058static int read_script_info(struct script_desc *desc, const char *filename)
2059{
2060 char line[BUFSIZ], *p;
2061 FILE *fp;
2062
2063 fp = fopen(filename, "r");
2064 if (!fp)
2065 return -1;
2066
2067 while (fgets(line, sizeof(line), fp)) {
2068 p = ltrim(line);
2069 if (strlen(p) == 0)
2070 continue;
2071 if (*p != '#')
2072 continue;
2073 p++;
2074 if (strlen(p) && *p == '!')
2075 continue;
2076
2077 p = ltrim(p);
2078 if (strlen(p) && p[strlen(p) - 1] == '\n')
2079 p[strlen(p) - 1] = '\0';
2080
2081 if (!strncmp(p, "description:", strlen("description:"))) {
2082 p += strlen("description:");
2083 desc->half_liner = strdup(ltrim(p));
2084 continue;
2085 }
2086
2087 if (!strncmp(p, "args:", strlen("args:"))) {
2088 p += strlen("args:");
2089 desc->args = strdup(ltrim(p));
2090 continue;
2091 }
2092 }
2093
2094 fclose(fp);
2095
2096 return 0;
2097}
2098
Robert Richter38efb532011-11-25 11:38:40 +01002099static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2100{
2101 char *script_root, *str;
2102
2103 script_root = strdup(script_dirent->d_name);
2104 if (!script_root)
2105 return NULL;
2106
2107 str = (char *)ends_with(script_root, suffix);
2108 if (!str) {
2109 free(script_root);
2110 return NULL;
2111 }
2112
2113 *str = '\0';
2114 return script_root;
2115}
2116
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002117static int list_available_scripts(const struct option *opt __maybe_unused,
2118 const char *s __maybe_unused,
2119 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002120{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002121 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002122 char scripts_path[MAXPATHLEN];
2123 DIR *scripts_dir, *lang_dir;
2124 char script_path[MAXPATHLEN];
2125 char lang_path[MAXPATHLEN];
2126 struct script_desc *desc;
2127 char first_half[BUFSIZ];
2128 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002129
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002130 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002131
2132 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002133 if (!scripts_dir) {
2134 fprintf(stdout,
2135 "open(%s) failed.\n"
2136 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2137 scripts_path);
2138 exit(-1);
2139 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002140
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002141 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002142 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002143 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002144 lang_dir = opendir(lang_path);
2145 if (!lang_dir)
2146 continue;
2147
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002148 for_each_script(lang_path, lang_dir, script_dirent) {
2149 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002150 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002151 desc = script_desc__findnew(script_root);
2152 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002153 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002154 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002155 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002156 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002157 }
2158 }
2159
2160 fprintf(stdout, "List of available trace scripts:\n");
2161 list_for_each_entry(desc, &script_descs, node) {
2162 sprintf(first_half, "%s %s", desc->name,
2163 desc->args ? desc->args : "");
2164 fprintf(stdout, " %-36s %s\n", first_half,
2165 desc->half_liner ? desc->half_liner : "");
2166 }
2167
2168 exit(0);
2169}
2170
Feng Tange5f37052012-09-07 16:42:26 +08002171/*
Feng Tang49e639e2012-10-30 11:56:03 +08002172 * Some scripts specify the required events in their "xxx-record" file,
2173 * this function will check if the events in perf.data match those
2174 * mentioned in the "xxx-record".
2175 *
2176 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2177 * which is covered well now. And new parsing code should be added to
2178 * cover the future complexing formats like event groups etc.
2179 */
2180static int check_ev_match(char *dir_name, char *scriptname,
2181 struct perf_session *session)
2182{
2183 char filename[MAXPATHLEN], evname[128];
2184 char line[BUFSIZ], *p;
2185 struct perf_evsel *pos;
2186 int match, len;
2187 FILE *fp;
2188
2189 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2190
2191 fp = fopen(filename, "r");
2192 if (!fp)
2193 return -1;
2194
2195 while (fgets(line, sizeof(line), fp)) {
2196 p = ltrim(line);
2197 if (*p == '#')
2198 continue;
2199
2200 while (strlen(p)) {
2201 p = strstr(p, "-e");
2202 if (!p)
2203 break;
2204
2205 p += 2;
2206 p = ltrim(p);
2207 len = strcspn(p, " \t");
2208 if (!len)
2209 break;
2210
2211 snprintf(evname, len + 1, "%s", p);
2212
2213 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002214 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002215 if (!strcmp(perf_evsel__name(pos), evname)) {
2216 match = 1;
2217 break;
2218 }
2219 }
2220
2221 if (!match) {
2222 fclose(fp);
2223 return -1;
2224 }
2225 }
2226 }
2227
2228 fclose(fp);
2229 return 0;
2230}
2231
2232/*
Feng Tange5f37052012-09-07 16:42:26 +08002233 * Return -1 if none is found, otherwise the actual scripts number.
2234 *
2235 * Currently the only user of this function is the script browser, which
2236 * will list all statically runnable scripts, select one, execute it and
2237 * show the output in a perf browser.
2238 */
2239int find_scripts(char **scripts_array, char **scripts_path_array)
2240{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002241 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002242 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002243 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002244 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002245 struct perf_data_file file = {
2246 .path = input_name,
2247 .mode = PERF_DATA_MODE_READ,
2248 };
Feng Tange5f37052012-09-07 16:42:26 +08002249 char *temp;
2250 int i = 0;
2251
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002252 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002253 if (!session)
2254 return -1;
2255
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002256 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002257
2258 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002259 if (!scripts_dir) {
2260 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002261 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002262 }
Feng Tange5f37052012-09-07 16:42:26 +08002263
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002264 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002265 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002266 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002267#ifdef NO_LIBPERL
2268 if (strstr(lang_path, "perl"))
2269 continue;
2270#endif
2271#ifdef NO_LIBPYTHON
2272 if (strstr(lang_path, "python"))
2273 continue;
2274#endif
2275
2276 lang_dir = opendir(lang_path);
2277 if (!lang_dir)
2278 continue;
2279
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002280 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002281 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002282 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002283 continue;
2284 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002285 script_dirent->d_name);
2286 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002287 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002288 (temp - script_dirent->d_name) + 1,
2289 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002290
2291 if (check_ev_match(lang_path,
2292 scripts_array[i], session))
2293 continue;
2294
Feng Tange5f37052012-09-07 16:42:26 +08002295 i++;
2296 }
Feng Tang49e639e2012-10-30 11:56:03 +08002297 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002298 }
2299
Feng Tang49e639e2012-10-30 11:56:03 +08002300 closedir(scripts_dir);
2301 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002302 return i;
2303}
2304
Tom Zanussi38752942009-12-15 02:53:39 -06002305static char *get_script_path(const char *script_root, const char *suffix)
2306{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002307 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002308 char scripts_path[MAXPATHLEN];
2309 char script_path[MAXPATHLEN];
2310 DIR *scripts_dir, *lang_dir;
2311 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002312 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002313
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002314 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002315
2316 scripts_dir = opendir(scripts_path);
2317 if (!scripts_dir)
2318 return NULL;
2319
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002320 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002321 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002322 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002323 lang_dir = opendir(lang_path);
2324 if (!lang_dir)
2325 continue;
2326
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002327 for_each_script(lang_path, lang_dir, script_dirent) {
2328 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002329 if (__script_root && !strcmp(script_root, __script_root)) {
2330 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002331 closedir(lang_dir);
2332 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002333 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002334 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002335 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002336 }
2337 free(__script_root);
2338 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002339 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002340 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002341 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002342
Robert Richter38efb532011-11-25 11:38:40 +01002343 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002344}
2345
Tom Zanussib5b87312010-11-10 08:16:51 -06002346static bool is_top_script(const char *script_path)
2347{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002348 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002349}
2350
2351static int has_required_arg(char *script_path)
2352{
2353 struct script_desc *desc;
2354 int n_args = 0;
2355 char *p;
2356
2357 desc = script_desc__new(NULL);
2358
2359 if (read_script_info(desc, script_path))
2360 goto out;
2361
2362 if (!desc->args)
2363 goto out;
2364
2365 for (p = desc->args; *p; p++)
2366 if (*p == '<')
2367 n_args++;
2368out:
2369 script_desc__delete(desc);
2370
2371 return n_args;
2372}
2373
David Ahernd54b1a92012-08-26 12:24:46 -06002374static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002375{
2376 char **__argv = malloc(sizeof(const char *) * argc);
2377
David Ahernd54b1a92012-08-26 12:24:46 -06002378 if (!__argv) {
2379 pr_err("malloc failed\n");
2380 return -1;
2381 }
2382
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002383 memcpy(__argv, argv, sizeof(const char *) * argc);
2384 argc = parse_options(argc, (const char **)__argv, record_options,
2385 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2386 free(__argv);
2387
David Ahernd54b1a92012-08-26 12:24:46 -06002388 system_wide = (argc == 0);
2389
2390 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002391}
2392
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002393static void script__setup_sample_type(struct perf_script *script)
2394{
2395 struct perf_session *session = script->session;
2396 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2397
2398 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2399 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2400 (sample_type & PERF_SAMPLE_STACK_USER))
2401 callchain_param.record_mode = CALLCHAIN_DWARF;
2402 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2403 callchain_param.record_mode = CALLCHAIN_LBR;
2404 else
2405 callchain_param.record_mode = CALLCHAIN_FP;
2406 }
2407}
2408
Jiri Olsae099eba2016-01-05 22:09:09 +01002409static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2410 union perf_event *event,
2411 struct perf_session *session)
2412{
2413 struct stat_round_event *round = &event->stat_round;
2414 struct perf_evsel *counter;
2415
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002416 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002417 perf_stat_process_counter(&stat_config, counter);
2418 process_stat(counter, round->time);
2419 }
2420
2421 process_stat_interval(round->time);
2422 return 0;
2423}
2424
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002425static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2426 union perf_event *event,
2427 struct perf_session *session __maybe_unused)
2428{
2429 perf_event__read_stat_config(&stat_config, &event->stat_config);
2430 return 0;
2431}
2432
Jiri Olsacfc88742016-01-05 22:09:06 +01002433static int set_maps(struct perf_script *script)
2434{
2435 struct perf_evlist *evlist = script->session->evlist;
2436
2437 if (!script->cpus || !script->threads)
2438 return 0;
2439
2440 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2441 return -EINVAL;
2442
2443 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2444
2445 if (perf_evlist__alloc_stats(evlist, true))
2446 return -ENOMEM;
2447
2448 script->allocated = true;
2449 return 0;
2450}
2451
2452static
2453int process_thread_map_event(struct perf_tool *tool,
2454 union perf_event *event,
2455 struct perf_session *session __maybe_unused)
2456{
2457 struct perf_script *script = container_of(tool, struct perf_script, tool);
2458
2459 if (script->threads) {
2460 pr_warning("Extra thread map event, ignoring.\n");
2461 return 0;
2462 }
2463
2464 script->threads = thread_map__new_event(&event->thread_map);
2465 if (!script->threads)
2466 return -ENOMEM;
2467
2468 return set_maps(script);
2469}
2470
2471static
2472int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2473 union perf_event *event,
2474 struct perf_session *session __maybe_unused)
2475{
2476 struct perf_script *script = container_of(tool, struct perf_script, tool);
2477
2478 if (script->cpus) {
2479 pr_warning("Extra cpu map event, ignoring.\n");
2480 return 0;
2481 }
2482
2483 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2484 if (!script->cpus)
2485 return -ENOMEM;
2486
2487 return set_maps(script);
2488}
2489
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002490int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002491{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002492 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002493 bool header = false;
2494 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002495 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002496 char *rec_script_path = NULL;
2497 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002498 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002499 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002500 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002501 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002502 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002503 struct perf_script script = {
2504 .tool = {
2505 .sample = process_sample_event,
2506 .mmap = perf_event__process_mmap,
2507 .mmap2 = perf_event__process_mmap2,
2508 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302509 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002510 .exit = perf_event__process_exit,
2511 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002512 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002513 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002514 .tracing_data = perf_event__process_tracing_data,
2515 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002516 .id_index = perf_event__process_id_index,
2517 .auxtrace_info = perf_event__process_auxtrace_info,
2518 .auxtrace = perf_event__process_auxtrace,
2519 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002520 .stat = perf_event__process_stat_event,
2521 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002522 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002523 .thread_map = process_thread_map_event,
2524 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002525 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002526 .ordering_requires_timestamps = true,
2527 },
2528 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002529 struct perf_data_file file = {
2530 .mode = PERF_DATA_MODE_READ,
2531 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002532 const struct option options[] = {
2533 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2534 "dump raw trace in ASCII"),
2535 OPT_INCR('v', "verbose", &verbose,
2536 "be more verbose (show symbol address, etc)"),
2537 OPT_BOOLEAN('L', "Latency", &latency_format,
2538 "show latency attributes (irqs/preemption disabled, etc)"),
2539 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2540 list_available_scripts),
2541 OPT_CALLBACK('s', "script", NULL, "name",
2542 "script file name (lang:script name, script name, or *)",
2543 parse_scriptname),
2544 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2545 "generate perf-script.xx script in specified language"),
2546 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2547 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2548 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002549 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2550 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002551 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2552 "file", "vmlinux pathname"),
2553 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2554 "file", "kallsyms pathname"),
2555 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2556 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002557 OPT_CALLBACK(0, "symfs", NULL, "directory",
2558 "Look for files with symbols relative to this directory",
2559 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002560 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002561 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07002562 "+field to add and -field to remove."
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002563 "Valid types: hw,sw,trace,raw. "
2564 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Huntere2167082016-06-23 16:40:58 +03002565 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
Andi Kleen48d02a12017-02-23 15:46:34 -08002566 "bpf-output,callindent,insn,insnlen,brstackinsn",
2567 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002568 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2569 "system-wide collection from all CPUs"),
2570 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2571 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002572 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2573 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002574 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2575 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2576 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002577 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2578 "only consider symbols in these pids"),
2579 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2580 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002581 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2582 "Set the maximum stack depth when parsing the callchain, "
2583 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002584 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002585 OPT_BOOLEAN('I', "show-info", &show_full_info,
2586 "display extended information from perf.data file"),
2587 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2588 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002589 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2590 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002591 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2592 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002593 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2594 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302595 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2596 "Show namespace events (if recorded)"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002597 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002598 OPT_INTEGER(0, "max-blocks", &max_blocks,
2599 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002600 OPT_BOOLEAN(0, "ns", &nanosecs,
2601 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002602 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2603 "Instruction Tracing options",
2604 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002605 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2606 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002607 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2608 "Enable symbol demangling"),
2609 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2610 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002611 OPT_STRING(0, "time", &script.time_str, "str",
2612 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09002613 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2614 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002615 OPT_END()
2616 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002617 const char * const script_subcommands[] = { "record", "report", NULL };
2618 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002619 "perf script [<options>]",
2620 "perf script [<options>] record <script> [<record-options>] <command>",
2621 "perf script [<options>] report <script> [script-args]",
2622 "perf script [<options>] <script> [<record-options>] <command>",
2623 "perf script [<options>] <top-script> [script-args]",
2624 NULL
2625 };
Tom Zanussi38752942009-12-15 02:53:39 -06002626
Tom Zanussib5b87312010-11-10 08:16:51 -06002627 setup_scripting();
2628
Yunlong Song40cae2b2015-03-18 21:35:54 +08002629 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002630 PARSE_OPT_STOP_AT_NON_OPTION);
2631
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002632 file.path = input_name;
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002633 file.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002634
Tom Zanussib5b87312010-11-10 08:16:51 -06002635 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2636 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2637 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002638 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002639 }
2640
Tom Zanussib5b87312010-11-10 08:16:51 -06002641 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2642 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2643 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002644 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002645 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002646 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002647 return -1;
2648 }
Tom Zanussi38752942009-12-15 02:53:39 -06002649 }
2650
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002651 if (itrace_synth_opts.callchain &&
2652 itrace_synth_opts.callchain_sz > scripting_max_stack)
2653 scripting_max_stack = itrace_synth_opts.callchain_sz;
2654
Ben Hutchings44e668c2010-10-10 16:10:03 +01002655 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002656 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002657
Tom Zanussib5b87312010-11-10 08:16:51 -06002658 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002659 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002660 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002661 pid_t pid;
2662
Tom Zanussib5b87312010-11-10 08:16:51 -06002663 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2664 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2665
2666 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002667 usage_with_options_msg(script_usage, options,
2668 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002669 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002670 }
2671
Tom Zanussib5b87312010-11-10 08:16:51 -06002672 if (is_top_script(argv[0])) {
2673 rep_args = argc - 1;
2674 } else {
2675 int rec_args;
2676
2677 rep_args = has_required_arg(rep_script_path);
2678 rec_args = (argc - 1) - rep_args;
2679 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002680 usage_with_options_msg(script_usage, options,
2681 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002682 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002683 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002684 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002685 }
2686
2687 if (pipe(live_pipe) < 0) {
2688 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002689 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002690 }
2691
2692 pid = fork();
2693 if (pid < 0) {
2694 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002695 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002696 }
2697
2698 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002699 j = 0;
2700
Tom Zanussia0cccc22010-04-01 23:59:25 -05002701 dup2(live_pipe[1], 1);
2702 close(live_pipe[0]);
2703
Robert Richter317df652011-11-25 15:05:25 +01002704 if (is_top_script(argv[0])) {
2705 system_wide = true;
2706 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002707 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2708 err = -1;
2709 goto out;
2710 }
Robert Richter317df652011-11-25 15:05:25 +01002711 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002712
2713 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002714 if (!__argv) {
2715 pr_err("malloc failed\n");
2716 err = -ENOMEM;
2717 goto out;
2718 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002719
Tom Zanussib5b87312010-11-10 08:16:51 -06002720 __argv[j++] = "/bin/sh";
2721 __argv[j++] = rec_script_path;
2722 if (system_wide)
2723 __argv[j++] = "-a";
2724 __argv[j++] = "-q";
2725 __argv[j++] = "-o";
2726 __argv[j++] = "-";
2727 for (i = rep_args + 1; i < argc; i++)
2728 __argv[j++] = argv[i];
2729 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002730
2731 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002732 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002733 exit(-1);
2734 }
2735
2736 dup2(live_pipe[0], 0);
2737 close(live_pipe[1]);
2738
Tom Zanussib5b87312010-11-10 08:16:51 -06002739 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002740 if (!__argv) {
2741 pr_err("malloc failed\n");
2742 err = -ENOMEM;
2743 goto out;
2744 }
2745
Tom Zanussib5b87312010-11-10 08:16:51 -06002746 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002747 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002748 __argv[j++] = rep_script_path;
2749 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002750 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002751 __argv[j++] = "-i";
2752 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002753 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002754
2755 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002756 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002757 exit(-1);
2758 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002759
Tom Zanussib5b87312010-11-10 08:16:51 -06002760 if (rec_script_path)
2761 script_path = rec_script_path;
2762 if (rep_script_path)
2763 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002764
Tom Zanussib5b87312010-11-10 08:16:51 -06002765 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002766 j = 0;
2767
Robert Richter317df652011-11-25 15:05:25 +01002768 if (!rec_script_path)
2769 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002770 else if (!system_wide) {
2771 if (have_cmd(argc - 1, &argv[1]) != 0) {
2772 err = -1;
2773 goto out;
2774 }
2775 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002776
2777 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002778 if (!__argv) {
2779 pr_err("malloc failed\n");
2780 err = -ENOMEM;
2781 goto out;
2782 }
2783
Tom Zanussib5b87312010-11-10 08:16:51 -06002784 __argv[j++] = "/bin/sh";
2785 __argv[j++] = script_path;
2786 if (system_wide)
2787 __argv[j++] = "-a";
2788 for (i = 2; i < argc; i++)
2789 __argv[j++] = argv[i];
2790 __argv[j++] = NULL;
2791
2792 execvp("/bin/sh", (char **)__argv);
2793 free(__argv);
2794 exit(-1);
2795 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002796
Tom Zanussicf4fee52010-03-03 01:04:33 -06002797 if (!script_name)
2798 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002799
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002800 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002801 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002802 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002803
Jiri Olsae90debd2013-12-09 11:02:50 +01002804 if (header || header_only) {
2805 perf_session__fprintf_info(session, stdout, show_full_info);
2806 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002807 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002808 }
2809
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002810 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002811 goto out_delete;
2812
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002813 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002814 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002815
Adrian Huntere2167082016-06-23 16:40:58 +03002816 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2817 itrace_synth_opts.thread_stack = true;
2818
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002819 session->itrace_synth_opts = &itrace_synth_opts;
2820
Anton Blanchard5d67be92011-07-04 21:57:50 +10002821 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002822 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2823 if (err < 0)
2824 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002825 }
2826
David Ahern1424dc92011-03-09 22:23:28 -07002827 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002828 symbol_conf.use_callchain = true;
2829 else
2830 symbol_conf.use_callchain = false;
2831
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002832 if (session->tevent.pevent &&
2833 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002834 machine__resolve_kernel_addr,
2835 &session->machines.host) < 0) {
2836 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2837 return -1;
2838 }
2839
Tom Zanussi956ffd02009-11-25 01:15:46 -06002840 if (generate_script_lang) {
2841 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002842 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002843
David Ahern2c9e45f72011-03-17 10:03:21 -06002844 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002845 fprintf(stderr,
2846 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002847 err = -EINVAL;
2848 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002849 }
2850
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002851 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002852 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002853 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002854 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002855 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002856 }
2857
2858 err = fstat(input, &perf_stat);
2859 if (err < 0) {
2860 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002861 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002862 }
2863
2864 if (!perf_stat.st_size) {
2865 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002866 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002867 }
2868
2869 scripting_ops = script_spec__lookup(generate_script_lang);
2870 if (!scripting_ops) {
2871 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002872 err = -ENOENT;
2873 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002874 }
2875
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002876 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002877 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002878 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002879 }
2880
2881 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002882 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002883 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002884 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002885 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002886 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002887 }
2888
David Ahern9cbdb702011-04-06 21:54:20 -06002889
2890 err = perf_session__check_output_opt(session);
2891 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002892 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002893
David Aherna91f4c42016-11-29 10:15:43 -07002894 /* needs to be parsed after looking up reference time */
2895 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
2896 pr_err("Invalid time string\n");
2897 return -EINVAL;
2898 }
2899
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002900 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002901
Adrian Hunterd445dd22014-08-15 22:08:37 +03002902 flush_scripting();
2903
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002904out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002905 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002906 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002907
2908 if (script_started)
2909 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002910out:
2911 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002912}