blob: 7b2f0922050cc73ede7e40878845cc2656c10dcf [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002#include "builtin.h"
3
Arnaldo Carvalho de Melo0f31c012019-08-21 14:00:17 -03004#include "util/counts.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02005#include "util/debug.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -03006#include "util/dso.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06007#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02008#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06009#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +020010#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010013#include "util/map.h"
Arnaldo Carvalho de Melo97b9d862019-08-22 17:10:08 -030014#include "util/srcline.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020015#include "util/symbol.h"
16#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010017#include "util/trace-event.h"
David Ahern1424dc92011-03-09 22:23:28 -070018#include "util/evlist.h"
19#include "util/evsel.h"
Arnaldo Carvalho de Meloca125272019-09-24 15:41:51 -030020#include "util/evsel_fprintf.h"
Arnaldo Carvalho de Melod2360442019-08-15 10:37:24 -030021#include "util/evswitch.h"
Feng Tang36385be2012-09-07 16:42:24 +080022#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020023#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030024#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010025#include "util/cpumap.h"
26#include "util/thread_map.h"
27#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080028#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030029#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030030#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070031#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010032#include "util/path.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030033#include "ui/ui.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030034#include "print_binary.h"
Andi Kleen3ab481a2019-03-05 06:47:45 -080035#include "archinsn.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100036#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030037#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030038#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030039#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030040#include <linux/zalloc.h>
Andi Kleen3ab481a2019-03-05 06:47:45 -080041#include <sys/utsname.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010042#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010043#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080044#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030045#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030046#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030047#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030048#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030049#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030050#include <sys/types.h>
51#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030052#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030053#include <unistd.h>
Andi Kleenb585ebd2018-09-20 11:05:36 -070054#include <subcmd/pager.h>
Jiri Olsa453fa032019-07-21 13:24:43 +020055#include <perf/evlist.h>
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053056#include <linux/err.h>
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030057#include "util/record.h"
Arnaldo Carvalho de Melo2da39f12019-08-27 11:51:18 -030058#include "util/util.h"
Arnaldo Carvalho de Meloc1a604d2019-08-29 15:20:59 -030059#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020060
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030061#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030062
Tom Zanussi956ffd02009-11-25 01:15:46 -060063static char const *script_name;
64static char const *generate_script_lang;
Andi Kleen90b10f42019-03-14 15:50:00 -070065static bool reltime;
66static u64 initial_time;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020067static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020068static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020069static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070070static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090071static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010072static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030073static bool print_flags;
Anton Blanchard5d67be92011-07-04 21:57:50 +100074static const char *cpu_list;
75static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010076static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080077static int max_blocks;
Andi Kleen3ab481a2019-03-05 06:47:45 -080078static bool native_arch;
Tom Zanussi956ffd02009-11-25 01:15:46 -060079
Adrian Hunter44cbe722015-09-25 16:15:50 +030080unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030081
David Ahern745f43e2011-03-09 22:23:26 -070082enum perf_output_field {
83 PERF_OUTPUT_COMM = 1U << 0,
84 PERF_OUTPUT_TID = 1U << 1,
85 PERF_OUTPUT_PID = 1U << 2,
86 PERF_OUTPUT_TIME = 1U << 3,
87 PERF_OUTPUT_CPU = 1U << 4,
88 PERF_OUTPUT_EVNAME = 1U << 5,
89 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060090 PERF_OUTPUT_IP = 1U << 7,
91 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060092 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060093 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090094 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020095 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020096 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020097 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020098 PERF_OUTPUT_BRSTACK = 1U << 15,
99 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100100 PERF_OUTPUT_DATA_SRC = 1U << 17,
101 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +0000102 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +0300103 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +0300104 PERF_OUTPUT_INSN = 1U << 21,
105 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -0800106 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -0700107 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +0300108 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -0400109 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -0700110 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800111 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100112 PERF_OUTPUT_MISC = 1U << 29,
Andi Kleendd2e18e2018-12-03 16:18:48 -0800113 PERF_OUTPUT_SRCCODE = 1U << 30,
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300114 PERF_OUTPUT_IPC = 1U << 31,
David Ahern745f43e2011-03-09 22:23:26 -0700115};
116
117struct output_option {
118 const char *str;
119 enum perf_output_field field;
120} all_output_options[] = {
121 {.str = "comm", .field = PERF_OUTPUT_COMM},
122 {.str = "tid", .field = PERF_OUTPUT_TID},
123 {.str = "pid", .field = PERF_OUTPUT_PID},
124 {.str = "time", .field = PERF_OUTPUT_TIME},
125 {.str = "cpu", .field = PERF_OUTPUT_CPU},
126 {.str = "event", .field = PERF_OUTPUT_EVNAME},
127 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600128 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700129 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600130 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600131 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900132 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200133 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200134 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200135 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700136 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200137 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
138 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100139 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
140 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000141 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300142 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300143 {.str = "insn", .field = PERF_OUTPUT_INSN},
144 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800145 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700146 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300147 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400148 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800149 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100150 {.str = "misc", .field = PERF_OUTPUT_MISC},
Andi Kleendd2e18e2018-12-03 16:18:48 -0800151 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300152 {.str = "ipc", .field = PERF_OUTPUT_IPC},
David Ahern745f43e2011-03-09 22:23:26 -0700153};
154
Adrian Hunter14057202017-06-21 13:17:19 +0300155enum {
156 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
157 OUTPUT_TYPE_MAX
158};
159
David Ahern745f43e2011-03-09 22:23:26 -0700160/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600161static struct {
162 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600163 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400164 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600165 u64 fields;
166 u64 invalid_fields;
Andi Kleen4b6ac812019-02-24 07:37:12 -0800167 u64 user_set_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300168} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700169
David Ahern2c9e45f72011-03-17 10:03:21 -0600170 [PERF_TYPE_HARDWARE] = {
171 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700172
David Ahern2c9e45f72011-03-17 10:03:21 -0600173 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
174 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600175 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530176 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
177 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600178
Wang Nan30372f02016-02-24 11:20:45 +0000179 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600180 },
181
182 [PERF_TYPE_SOFTWARE] = {
183 .user_set = false,
184
185 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
186 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600187 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530188 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
189 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
190 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600191
192 .invalid_fields = PERF_OUTPUT_TRACE,
193 },
194
195 [PERF_TYPE_TRACEPOINT] = {
196 .user_set = false,
197
198 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
199 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000200 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600201 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700202
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530203 [PERF_TYPE_HW_CACHE] = {
204 .user_set = false,
205
206 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
207 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
208 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
209 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
210 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
211
212 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
213 },
214
Arun Sharma0817a6a2011-04-14 10:38:18 -0700215 [PERF_TYPE_RAW] = {
216 .user_set = false,
217
218 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
219 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600220 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530221 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
222 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
223 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
224 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700225
Wang Nan30372f02016-02-24 11:20:45 +0000226 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700227 },
Wang Nan27cfef02015-12-08 02:25:43 +0000228
229 [PERF_TYPE_BREAKPOINT] = {
230 .user_set = false,
231
232 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
233 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
234 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530235 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
236 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000237
Wang Nan30372f02016-02-24 11:20:45 +0000238 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000239 },
Adrian Hunter14057202017-06-21 13:17:19 +0300240
241 [OUTPUT_TYPE_SYNTH] = {
242 .user_set = false,
243
244 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
245 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
246 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530247 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
248 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300249
250 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
251 },
David Ahern1424dc92011-03-09 22:23:28 -0700252};
David Ahern745f43e2011-03-09 22:23:26 -0700253
Jiri Olsa32dcd022019-07-21 13:23:51 +0200254struct evsel_script {
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300255 char *filename;
256 FILE *fp;
257 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800258 /* For metric output */
259 u64 val;
260 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300261};
262
Jiri Olsa32dcd022019-07-21 13:23:51 +0200263static inline struct evsel_script *evsel_script(struct evsel *evsel)
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800264{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200265 return (struct evsel_script *)evsel->priv;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800266}
267
Jiri Olsa32dcd022019-07-21 13:23:51 +0200268static struct evsel_script *perf_evsel_script__new(struct evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100269 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300270{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200271 struct evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300272
273 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100274 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300275 goto out_free;
276 es->fp = fopen(es->filename, "w");
277 if (es->fp == NULL)
278 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300279 }
280
281 return es;
282out_free_filename:
283 zfree(&es->filename);
284out_free:
285 free(es);
286 return NULL;
287}
288
Jiri Olsa32dcd022019-07-21 13:23:51 +0200289static void perf_evsel_script__delete(struct evsel_script *es)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300290{
291 zfree(&es->filename);
292 fclose(es->fp);
293 es->fp = NULL;
294 free(es);
295}
296
Jiri Olsa32dcd022019-07-21 13:23:51 +0200297static int perf_evsel_script__fprintf(struct evsel_script *es, FILE *fp)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300298{
299 struct stat st;
300
301 fstat(fileno(es->fp), &st);
302 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
303 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
304}
305
Adrian Hunter14057202017-06-21 13:17:19 +0300306static inline int output_type(unsigned int type)
307{
308 switch (type) {
309 case PERF_TYPE_SYNTH:
310 return OUTPUT_TYPE_SYNTH;
311 default:
312 return type;
313 }
314}
315
316static inline unsigned int attr_type(unsigned int type)
317{
318 switch (type) {
319 case OUTPUT_TYPE_SYNTH:
320 return PERF_TYPE_SYNTH;
321 default:
322 return type;
323 }
324}
325
David Ahern2c9e45f72011-03-17 10:03:21 -0600326static bool output_set_by_user(void)
327{
328 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300329 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600330 if (output[j].user_set)
331 return true;
332 }
333 return false;
334}
David Ahern745f43e2011-03-09 22:23:26 -0700335
David Ahern9cbdb702011-04-06 21:54:20 -0600336static const char *output_field2str(enum perf_output_field field)
337{
338 int i, imax = ARRAY_SIZE(all_output_options);
339 const char *str = "";
340
341 for (i = 0; i < imax; ++i) {
342 if (all_output_options[i].field == field) {
343 str = all_output_options[i].str;
344 break;
345 }
346 }
347 return str;
348}
349
Adrian Hunter14057202017-06-21 13:17:19 +0300350#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700351
Jiri Olsa32dcd022019-07-21 13:23:51 +0200352static int perf_evsel__do_check_stype(struct evsel *evsel,
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300353 u64 sample_type, const char *sample_msg,
354 enum perf_output_field field,
355 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700356{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200357 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300358 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600359 const char *evname;
360
361 if (attr->sample_type & sample_type)
362 return 0;
363
Andi Kleen4b6ac812019-02-24 07:37:12 -0800364 if (output[type].user_set_fields & field) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300365 if (allow_user_set)
366 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300367 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600368 pr_err("Samples for '%s' event do not have %s attribute set. "
369 "Cannot print '%s' field.\n",
370 evname, sample_msg, output_field2str(field));
371 return -1;
372 }
373
374 /* user did not ask for it explicitly so remove from the default list */
375 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300376 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600377 pr_debug("Samples for '%s' event do not have %s attribute set. "
378 "Skipping '%s' field.\n",
379 evname, sample_msg, output_field2str(field));
380
381 return 0;
382}
383
Jiri Olsa32dcd022019-07-21 13:23:51 +0200384static int perf_evsel__check_stype(struct evsel *evsel,
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300385 u64 sample_type, const char *sample_msg,
386 enum perf_output_field field)
387{
388 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
389 false);
390}
391
Jiri Olsa32dcd022019-07-21 13:23:51 +0200392static int perf_evsel__check_attr(struct evsel *evsel,
David Ahern9cbdb702011-04-06 21:54:20 -0600393 struct perf_session *session)
394{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200395 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300396 bool allow_user_set;
397
Jiri Olsae099eba2016-01-05 22:09:09 +0100398 if (perf_header__has_feat(&session->header, HEADER_STAT))
399 return 0;
400
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300401 allow_user_set = perf_header__has_feat(&session->header,
402 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600403
David Ahern1424dc92011-03-09 22:23:28 -0700404 if (PRINT_FIELD(TRACE) &&
405 !perf_session__has_traces(session, "record -R"))
406 return -EINVAL;
407
David Ahern787bef12011-05-27 14:28:43 -0600408 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300409 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
410 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700411 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700412 }
David Ahern7cec0922011-05-30 13:08:23 -0600413
414 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300415 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
416 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600417 return -EINVAL;
418
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100419 if (PRINT_FIELD(DATA_SRC) &&
420 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
421 PERF_OUTPUT_DATA_SRC))
422 return -EINVAL;
423
424 if (PRINT_FIELD(WEIGHT) &&
425 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
426 PERF_OUTPUT_WEIGHT))
427 return -EINVAL;
428
Andi Kleen37fed3d2018-09-18 05:32:09 -0700429 if (PRINT_FIELD(SYM) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200430 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600431 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700432 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600433 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600434 return -EINVAL;
435 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900436 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
437 pr_err("Display of offsets requested but symbol is not"
438 "selected.\n");
439 return -EINVAL;
440 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700441 if (PRINT_FIELD(DSO) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200442 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
Andi Kleen37fed3d2018-09-18 05:32:09 -0700443 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600444 return -EINVAL;
445 }
Andi Kleendd2e18e2018-12-03 16:18:48 -0800446 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
Adrian Huntercc8fae12013-12-06 09:42:57 +0200447 pr_err("Display of source line number requested but sample IP is not\n"
448 "selected. Hence, no address to lookup the source line number.\n");
449 return -EINVAL;
450 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800451 if (PRINT_FIELD(BRSTACKINSN) &&
452 !(perf_evlist__combined_branch_type(session->evlist) &
453 PERF_SAMPLE_BRANCH_ANY)) {
454 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
455 "Hint: run 'perf record -b ...'\n");
456 return -EINVAL;
457 }
David Ahern1424dc92011-03-09 22:23:28 -0700458 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300459 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
460 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700461 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700462
463 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300464 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
465 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700466 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700467
468 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300469 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
470 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700471 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600472
Stephane Eranianfc36f942015-08-31 18:41:10 +0200473 if (PRINT_FIELD(IREGS) &&
474 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
475 PERF_OUTPUT_IREGS))
476 return -EINVAL;
477
Andi Kleenb1491ac2017-09-05 11:40:57 -0700478 if (PRINT_FIELD(UREGS) &&
479 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
480 PERF_OUTPUT_UREGS))
481 return -EINVAL;
482
Kan Liang49d58f02017-08-29 13:11:11 -0400483 if (PRINT_FIELD(PHYS_ADDR) &&
484 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
485 PERF_OUTPUT_PHYS_ADDR))
486 return -EINVAL;
487
David Ahern9cbdb702011-04-06 21:54:20 -0600488 return 0;
489}
490
Adrian Hunter7ea95722013-11-01 15:51:30 +0200491static void set_print_ip_opts(struct perf_event_attr *attr)
492{
Adrian Hunter14057202017-06-21 13:17:19 +0300493 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200494
495 output[type].print_ip_opts = 0;
496 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300497 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200498
499 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300500 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200501
502 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300503 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200504
505 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300506 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200507
508 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300509 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200510}
511
David Ahern9cbdb702011-04-06 21:54:20 -0600512/*
513 * verify all user requested events exist and the samples
514 * have the expected data
515 */
516static int perf_session__check_output_opt(struct perf_session *session)
517{
He Kuang40f20e52016-05-16 04:51:19 +0000518 unsigned int j;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200519 struct evsel *evsel;
David Ahern9cbdb702011-04-06 21:54:20 -0600520
Adrian Hunter14057202017-06-21 13:17:19 +0300521 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
522 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600523
524 /*
525 * even if fields is set to 0 (ie., show nothing) event must
526 * exist if user explicitly includes it on the command line
527 */
Adrian Hunter14057202017-06-21 13:17:19 +0300528 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
529 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600530 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300531 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600532 event_type(j));
533 return -1;
534 }
535
536 if (evsel && output[j].fields &&
537 perf_evsel__check_attr(evsel, session))
538 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400539
540 if (evsel == NULL)
541 continue;
542
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200543 set_print_ip_opts(&evsel->core.attr);
David Ahern1424dc92011-03-09 22:23:28 -0700544 }
545
Adrian Hunter98526ee2014-07-31 09:00:59 +0300546 if (!no_callchain) {
547 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000548 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300549
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300550 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000551 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300552 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300553 use_callchain = true;
554 break;
555 }
556 }
He Kuang71ac8992016-08-04 11:25:43 +0000557 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300558 symbol_conf.use_callchain = false;
559 }
560
David Ahern80b8b492013-11-19 21:07:37 -0700561 /*
562 * set default for tracepoints to print symbols only
563 * if callchains are present
564 */
565 if (symbol_conf.use_callchain &&
566 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700567 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700568
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300569 evlist__for_each_entry(session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200570 if (evsel->core.attr.type != j)
He Kuang40f20e52016-05-16 04:51:19 +0000571 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700572
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300573 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000574 output[j].fields |= PERF_OUTPUT_IP;
575 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530576 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000577 output[j].fields |= PERF_OUTPUT_DSO;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200578 set_print_ip_opts(&evsel->core.attr);
He Kuang40f20e52016-05-16 04:51:19 +0000579 goto out;
580 }
David Ahern80b8b492013-11-19 21:07:37 -0700581 }
582 }
583
584out:
David Ahern1424dc92011-03-09 22:23:28 -0700585 return 0;
586}
David Ahern745f43e2011-03-09 22:23:26 -0700587
Milian Wolff9add8fe2018-11-07 23:34:37 +0100588static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
589 FILE *fp
590)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200591{
Andi Kleenb1491ac2017-09-05 11:40:57 -0700592 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300593 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700594
595 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300596 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700597
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300598 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700599
600 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
601 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300602 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700603 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300604
Milian Wolffb07d16f2018-11-07 10:37:05 +0100605 fprintf(fp, "\n");
606
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300607 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700608}
609
Milian Wolff9add8fe2018-11-07 23:34:37 +0100610static int perf_sample__fprintf_iregs(struct perf_sample *sample,
611 struct perf_event_attr *attr, FILE *fp)
612{
613 return perf_sample__fprintf_regs(&sample->intr_regs,
614 attr->sample_regs_intr, fp);
615}
616
617static int perf_sample__fprintf_uregs(struct perf_sample *sample,
618 struct perf_event_attr *attr, FILE *fp)
619{
620 return perf_sample__fprintf_regs(&sample->user_regs,
621 attr->sample_regs_user, fp);
622}
623
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300624static int perf_sample__fprintf_start(struct perf_sample *sample,
625 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200626 struct evsel *evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100627 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700628{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200629 struct perf_event_attr *attr = &evsel->core.attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700630 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700631 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300632 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700633
David Ahern745f43e2011-03-09 22:23:26 -0700634 if (PRINT_FIELD(COMM)) {
635 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300636 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300637 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300638 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700639 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300640 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700641 }
David Ahernc70c94b2011-03-09 22:23:25 -0700642
David Ahern745f43e2011-03-09 22:23:26 -0700643 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300644 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700645 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300646 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700647 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300648 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700649
David Ahern745f43e2011-03-09 22:23:26 -0700650 if (PRINT_FIELD(CPU)) {
651 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300652 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700653 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300654 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700655 }
David Ahernc70c94b2011-03-09 22:23:25 -0700656
Jiri Olsa28a0b392018-01-07 17:03:52 +0100657 if (PRINT_FIELD(MISC)) {
658 int ret = 0;
659
660 #define has(m) \
661 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
662
663 if (has(KERNEL))
664 ret += fprintf(fp, "K");
665 if (has(USER))
666 ret += fprintf(fp, "U");
667 if (has(HYPERVISOR))
668 ret += fprintf(fp, "H");
669 if (has(GUEST_KERNEL))
670 ret += fprintf(fp, "G");
671 if (has(GUEST_USER))
672 ret += fprintf(fp, "g");
673
674 switch (type) {
675 case PERF_RECORD_MMAP:
676 case PERF_RECORD_MMAP2:
677 if (has(MMAP_DATA))
678 ret += fprintf(fp, "M");
679 break;
680 case PERF_RECORD_COMM:
681 if (has(COMM_EXEC))
682 ret += fprintf(fp, "E");
683 break;
684 case PERF_RECORD_SWITCH:
685 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300686 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100687 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300688 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
689 ret += fprintf(fp, "p");
690 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100691 default:
692 break;
693 }
694
695 #undef has
696
697 ret += fprintf(fp, "%*s", 6 - ret, " ");
698 printed += ret;
699 }
700
David Ahern745f43e2011-03-09 22:23:26 -0700701 if (PRINT_FIELD(TIME)) {
Andi Kleen90b10f42019-03-14 15:50:00 -0700702 u64 t = sample->time;
703 if (reltime) {
704 if (!initial_time)
705 initial_time = sample->time;
706 t = sample->time - initial_time;
707 }
708 nsecs = t;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300709 secs = nsecs / NSEC_PER_SEC;
710 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900711
Andi Kleen52bab882019-03-05 06:47:47 -0800712 if (symbol_conf.nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300713 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900714 else {
715 char sample_time[32];
Andi Kleen90b10f42019-03-14 15:50:00 -0700716 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300717 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900718 }
David Ahern745f43e2011-03-09 22:23:26 -0700719 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300720
721 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700722}
723
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200724static inline char
725mispred_str(struct branch_entry *br)
726{
727 if (!(br->flags.mispred || br->flags.predicted))
728 return '-';
729
730 return br->flags.predicted ? 'P' : 'M';
731}
732
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300733static int perf_sample__fprintf_brstack(struct perf_sample *sample,
734 struct thread *thread,
735 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200736{
737 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700738 struct addr_location alf, alt;
739 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300740 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200741
742 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300743 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200744
745 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700746 from = br->entries[i].from;
747 to = br->entries[i].to;
748
749 if (PRINT_FIELD(DSO)) {
750 memset(&alf, 0, sizeof(alf));
751 memset(&alt, 0, sizeof(alt));
Adrian Hunter692d0e62018-11-06 23:07:12 +0200752 thread__find_map_fb(thread, sample->cpumode, from, &alf);
753 thread__find_map_fb(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700754 }
755
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300756 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700757 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300758 printed += fprintf(fp, "(");
759 printed += map__fprintf_dsoname(alf.map, fp);
760 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700761 }
762
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300763 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700764 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300765 printed += fprintf(fp, "(");
766 printed += map__fprintf_dsoname(alt.map, fp);
767 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700768 }
769
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300770 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200771 mispred_str( br->entries + i),
772 br->entries[i].flags.in_tx? 'X' : '-',
773 br->entries[i].flags.abort? 'A' : '-',
774 br->entries[i].flags.cycles);
775 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300776
777 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200778}
779
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300780static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
781 struct thread *thread,
782 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200783{
784 struct branch_stack *br = sample->branch_stack;
785 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200786 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300787 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200788
789 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300790 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200791
792 for (i = 0; i < br->nr; i++) {
793
794 memset(&alf, 0, sizeof(alf));
795 memset(&alt, 0, sizeof(alt));
796 from = br->entries[i].from;
797 to = br->entries[i].to;
798
Adrian Hunter692d0e62018-11-06 23:07:12 +0200799 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
800 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200801
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300802 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700803 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300804 printed += fprintf(fp, "(");
805 printed += map__fprintf_dsoname(alf.map, fp);
806 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700807 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300808 printed += fprintf(fp, "%c", '/');
809 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700810 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300811 printed += fprintf(fp, "(");
812 printed += map__fprintf_dsoname(alt.map, fp);
813 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700814 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300815 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200816 mispred_str( br->entries + i),
817 br->entries[i].flags.in_tx? 'X' : '-',
818 br->entries[i].flags.abort? 'A' : '-',
819 br->entries[i].flags.cycles);
820 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300821
822 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200823}
824
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300825static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
826 struct thread *thread,
827 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700828{
829 struct branch_stack *br = sample->branch_stack;
830 struct addr_location alf, alt;
831 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300832 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700833
834 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300835 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700836
837 for (i = 0; i < br->nr; i++) {
838
839 memset(&alf, 0, sizeof(alf));
840 memset(&alt, 0, sizeof(alt));
841 from = br->entries[i].from;
842 to = br->entries[i].to;
843
Adrian Hunter692d0e62018-11-06 23:07:12 +0200844 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300845 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700846 from = map__map_ip(alf.map, from);
847
Adrian Hunter692d0e62018-11-06 23:07:12 +0200848 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300849 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700850 to = map__map_ip(alt.map, to);
851
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300852 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700853 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300854 printed += fprintf(fp, "(");
855 printed += map__fprintf_dsoname(alf.map, fp);
856 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700857 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300858 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700859 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300860 printed += fprintf(fp, "(");
861 printed += map__fprintf_dsoname(alt.map, fp);
862 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700863 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300864 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700865 mispred_str(br->entries + i),
866 br->entries[i].flags.in_tx ? 'X' : '-',
867 br->entries[i].flags.abort ? 'A' : '-',
868 br->entries[i].flags.cycles);
869 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300870
871 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700872}
Andi Kleen48d02a12017-02-23 15:46:34 -0800873#define MAXBB 16384UL
874
875static int grab_bb(u8 *buffer, u64 start, u64 end,
876 struct machine *machine, struct thread *thread,
877 bool *is64bit, u8 *cpumode, bool last)
878{
879 long offset, len;
880 struct addr_location al;
881 bool kernel;
882
883 if (!start || !end)
884 return 0;
885
886 kernel = machine__kernel_ip(machine, start);
887 if (kernel)
888 *cpumode = PERF_RECORD_MISC_KERNEL;
889 else
890 *cpumode = PERF_RECORD_MISC_USER;
891
892 /*
893 * Block overlaps between kernel and user.
894 * This can happen due to ring filtering
895 * On Intel CPUs the entry into the kernel is filtered,
896 * but the exit is not. Let the caller patch it up.
897 */
898 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300899 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800900 return -ENXIO;
901 }
902
903 memset(&al, 0, sizeof(al));
904 if (end - start > MAXBB - MAXINSN) {
905 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300906 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800907 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300908 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800909 return 0;
910 }
911
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300912 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300913 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800914 return 0;
915 }
916 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300917 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800918 return 0;
919 }
920
921 /* Load maps to ensure dso->is_64_bit has been updated */
922 map__load(al.map);
923
924 offset = al.map->map_ip(al.map, start);
925 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
926 end - start + MAXINSN);
927
928 *is64bit = al.map->dso->is_64_bit;
929 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300930 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800931 start, end);
932 return len;
933}
934
Arnaldo Carvalho de Melo540a63e2019-11-22 12:39:06 -0300935static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struct srccode_state *state)
936{
937 char *srcfile;
938 int ret = 0;
939 unsigned line;
940 int len;
941 char *srccode;
942
943 if (!map || !map->dso)
944 return 0;
945 srcfile = get_srcline_split(map->dso,
946 map__rip_2objdump(map, addr),
947 &line);
948 if (!srcfile)
949 return 0;
950
951 /* Avoid redundant printing */
952 if (state &&
953 state->srcfile &&
954 !strcmp(state->srcfile, srcfile) &&
955 state->line == line) {
956 free(srcfile);
957 return 0;
958 }
959
960 srccode = find_sourceline(srcfile, line, &len);
961 if (!srccode)
962 goto out_free_line;
963
964 ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
965
966 if (state) {
967 state->srcfile = srcfile;
968 state->line = line;
969 }
970 return ret;
971
972out_free_line:
973 free(srcfile);
974 return ret;
975}
976
Andi Kleendd2e18e2018-12-03 16:18:48 -0800977static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
978{
979 struct addr_location al;
980 int ret = 0;
981
982 memset(&al, 0, sizeof(al));
983 thread__find_map(thread, cpumode, addr, &al);
984 if (!al.map)
985 return 0;
986 ret = map__fprintf_srccode(al.map, al.addr, stdout,
987 &thread->srccode_state);
988 if (ret)
989 ret += printf("\n");
990 return ret;
991}
992
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300993static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
994 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -0700995 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -0800996{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300997 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
998 dump_insn(x, ip, inbuf, len, NULL),
999 en->flags.predicted ? " PRED" : "",
1000 en->flags.mispred ? " MISPRED" : "",
1001 en->flags.in_tx ? " INTX" : "",
1002 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -08001003 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -07001004 *total_cycles += en->flags.cycles;
1005 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -08001006 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001007 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -08001008 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001009 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -08001010}
1011
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001012static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
1013 u8 cpumode, int cpu, struct symbol **lastsym,
1014 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -08001015{
1016 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001017 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001018
1019 memset(&al, 0, sizeof(al));
1020
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -03001021 thread__find_map(thread, cpumode, addr, &al);
1022
Andi Kleen48d02a12017-02-23 15:46:34 -08001023 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001024 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001025
1026 al.cpu = cpu;
1027 al.sym = NULL;
1028 if (al.map)
1029 al.sym = map__find_symbol(al.map, al.addr);
1030
1031 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001032 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001033
1034 if (al.addr < al.sym->end)
1035 off = al.addr - al.sym->start;
1036 else
1037 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001038 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -08001039 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001040 printed += fprintf(fp, "%+d", off);
1041 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -08001042 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001043 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
1044 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -08001045 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001046
1047 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001048}
1049
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001050static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1051 struct thread *thread,
1052 struct perf_event_attr *attr,
1053 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -08001054{
1055 struct branch_stack *br = sample->branch_stack;
1056 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001057 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001058 struct perf_insn x;
1059 u8 buffer[MAXBB];
1060 unsigned off;
1061 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -07001062 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001063
1064 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001065 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001066 nr = br->nr;
1067 if (max_blocks && nr > max_blocks + 1)
1068 nr = max_blocks + 1;
1069
1070 x.thread = thread;
1071 x.cpu = sample->cpu;
1072
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001073 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -08001074
1075 /* Handle first from jump, of which we don't know the entry. */
1076 len = grab_bb(buffer, br->entries[nr-1].from,
1077 br->entries[nr-1].from,
1078 machine, thread, &x.is64bit, &x.cpumode, false);
1079 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001080 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1081 x.cpumode, x.cpu, &lastsym, attr, fp);
1082 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001083 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001084 if (PRINT_FIELD(SRCCODE))
1085 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
Andi Kleen48d02a12017-02-23 15:46:34 -08001086 }
1087
1088 /* Print all blocks */
1089 for (i = nr - 2; i >= 0; i--) {
1090 if (br->entries[i].from || br->entries[i].to)
1091 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1092 br->entries[i].from,
1093 br->entries[i].to);
1094 start = br->entries[i + 1].to;
1095 end = br->entries[i].from;
1096
1097 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1098 /* Patch up missing kernel transfers due to ring filters */
1099 if (len == -ENXIO && i > 0) {
1100 end = br->entries[--i].from;
1101 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1102 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1103 }
1104 if (len <= 0)
1105 continue;
1106
1107 insn = 0;
Andi Kleene98df282019-09-27 16:35:44 -07001108 for (off = 0; off < (unsigned)len; off += ilen) {
Andi Kleen48d02a12017-02-23 15:46:34 -08001109 uint64_t ip = start + off;
1110
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001111 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001112 if (ip == end) {
Andi Kleendde4e732019-07-11 11:19:21 -07001113 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, ++insn, fp,
Andi Kleenfe571202018-09-24 10:07:32 -07001114 &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001115 if (PRINT_FIELD(SRCCODE))
1116 printed += print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001117 break;
1118 } else {
Andi Kleene98df282019-09-27 16:35:44 -07001119 ilen = 0;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001120 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1121 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001122 if (ilen == 0)
1123 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001124 if (PRINT_FIELD(SRCCODE))
1125 print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001126 insn++;
1127 }
1128 }
Andi Kleene98df282019-09-27 16:35:44 -07001129 if (off != (unsigned)len)
1130 printed += fprintf(fp, "\tmismatch of LBR data and executable\n");
Andi Kleen48d02a12017-02-23 15:46:34 -08001131 }
1132
1133 /*
1134 * Hit the branch? In this case we are already done, and the target
1135 * has not been executed yet.
1136 */
1137 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001138 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001139 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001140 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001141
1142 /*
1143 * Print final block upto sample
Andi Kleen61f61152018-11-19 21:06:17 -08001144 *
1145 * Due to pipeline delays the LBRs might be missing a branch
1146 * or two, which can result in very large or negative blocks
1147 * between final branch and sample. When this happens just
1148 * continue walking after the last TO until we hit a branch.
Andi Kleen48d02a12017-02-23 15:46:34 -08001149 */
1150 start = br->entries[0].to;
1151 end = sample->ip;
Andi Kleen61f61152018-11-19 21:06:17 -08001152 if (end < start) {
1153 /* Missing jump. Scan 128 bytes for the next branch */
1154 end = start + 128;
1155 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001156 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001157 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001158 if (len <= 0) {
1159 /* Print at least last IP if basic block did not work */
1160 len = grab_bb(buffer, sample->ip, sample->ip,
1161 machine, thread, &x.is64bit, &x.cpumode, false);
1162 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001163 goto out;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001164 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001165 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001166 if (PRINT_FIELD(SRCCODE))
1167 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001168 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001169 }
1170 for (off = 0; off <= end - start; off += ilen) {
Andi Kleene98df282019-09-27 16:35:44 -07001171 ilen = 0;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001172 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1173 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001174 if (ilen == 0)
1175 break;
Andi Kleen61f61152018-11-19 21:06:17 -08001176 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1177 /*
1178 * Hit a missing branch. Just stop.
1179 */
1180 printed += fprintf(fp, "\t... not reaching sample ...\n");
1181 break;
1182 }
Andi Kleendd2e18e2018-12-03 16:18:48 -08001183 if (PRINT_FIELD(SRCCODE))
1184 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001185 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001186out:
1187 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001188}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001189
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001190static int perf_sample__fprintf_addr(struct perf_sample *sample,
1191 struct thread *thread,
1192 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001193{
1194 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001195 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001196
1197 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001198 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001199
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001200 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001201
1202 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001203 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001204 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001205 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001206 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001207 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001208 }
1209
1210 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001211 printed += fprintf(fp, " (");
1212 printed += map__fprintf_dsoname(al.map, fp);
1213 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001214 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001215out:
1216 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001217}
1218
Andi Kleen99f753f2018-09-20 11:05:39 -07001219static const char *resolve_branch_sym(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001220 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001221 struct thread *thread,
1222 struct addr_location *al,
1223 u64 *ip)
1224{
1225 struct addr_location addr_al;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001226 struct perf_event_attr *attr = &evsel->core.attr;
Andi Kleen99f753f2018-09-20 11:05:39 -07001227 const char *name = NULL;
1228
1229 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1230 if (sample_addr_correlates_sym(attr)) {
1231 thread__resolve(thread, &addr_al, sample);
1232 if (addr_al.sym)
1233 name = addr_al.sym->name;
1234 else
1235 *ip = sample->addr;
1236 } else {
1237 *ip = sample->addr;
1238 }
1239 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1240 if (al->sym)
1241 name = al->sym->name;
1242 else
1243 *ip = sample->ip;
1244 }
1245 return name;
1246}
1247
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001248static int perf_sample__fprintf_callindent(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001249 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001250 struct thread *thread,
1251 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001252{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001253 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter256d92b2018-12-21 14:06:19 +02001254 size_t depth = thread_stack__depth(thread, sample->cpu);
Adrian Huntere2167082016-06-23 16:40:58 +03001255 const char *name = NULL;
1256 static int spacing;
1257 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001258 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001259 u64 ip = 0;
1260
1261 /*
1262 * The 'return' has already been popped off the stack so the depth has
1263 * to be adjusted to match the 'call'.
1264 */
1265 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1266 depth += 1;
1267
Andi Kleen99f753f2018-09-20 11:05:39 -07001268 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001269
Andi Kleena78cdee2018-09-18 05:32:10 -07001270 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1271 dlen += fprintf(fp, "(");
1272 dlen += map__fprintf_dsoname(al->map, fp);
1273 dlen += fprintf(fp, ")\t");
1274 }
1275
Adrian Huntere2167082016-06-23 16:40:58 +03001276 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001277 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001278 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001279 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001280
1281 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001282 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001283
1284 /*
1285 * Try to keep the output length from changing frequently so that the
1286 * output lines up more nicely.
1287 */
1288 if (len > spacing || (len && len < spacing - 52))
1289 spacing = round_up(len + 4, 32);
1290
1291 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001292 len += fprintf(fp, "%*s", spacing - len, "");
1293
Andi Kleena78cdee2018-09-18 05:32:10 -07001294 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001295}
1296
Andi Kleen3ab481a2019-03-05 06:47:45 -08001297__weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1298 struct thread *thread __maybe_unused,
1299 struct machine *machine __maybe_unused)
1300{
1301}
1302
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001303static int perf_sample__fprintf_insn(struct perf_sample *sample,
1304 struct perf_event_attr *attr,
1305 struct thread *thread,
1306 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001307{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001308 int printed = 0;
1309
Andi Kleen3ab481a2019-03-05 06:47:45 -08001310 if (sample->insn_len == 0 && native_arch)
1311 arch_fetch_insn(sample, thread, machine);
1312
Andi Kleen224e2c92016-10-07 16:42:27 +03001313 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001314 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen3ab481a2019-03-05 06:47:45 -08001315 if (PRINT_FIELD(INSN) && sample->insn_len) {
Andi Kleen224e2c92016-10-07 16:42:27 +03001316 int i;
1317
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001318 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001319 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001320 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001321 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001322 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001323 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1324
1325 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001326}
1327
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001328static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1329 struct perf_event_attr *attr, FILE *fp)
1330{
1331 unsigned int ipc;
1332
1333 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1334 return 0;
1335
1336 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1337
1338 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1339 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1340}
1341
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001342static int perf_sample__fprintf_bts(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001343 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001344 struct thread *thread,
1345 struct addr_location *al,
1346 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001347{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001348 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001349 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001350 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001351 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001352
Adrian Huntere2167082016-06-23 16:40:58 +03001353 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001354 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001355
Akihiro Nagai95582592012-01-30 13:43:09 +09001356 /* print branch_from information */
1357 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001358 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001359 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001360
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001361 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001362 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001363 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001364 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001365
1366 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001367 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001368 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001369 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001370 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001371 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001372 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001373 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001374
Arnaldo Carvalho de Melo9620bc362019-09-25 15:06:59 -03001375 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor,
1376 symbol_conf.bt_stop_list, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001377 }
1378
Akihiro Nagai95582592012-01-30 13:43:09 +09001379 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001380 if (PRINT_FIELD(ADDR) ||
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001381 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001382 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001383 printed += fprintf(fp, " => ");
1384 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001385 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001386
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001387 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1388
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001389 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001390 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001391
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001392 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001393 printed += fprintf(fp, "\n");
1394 if (PRINT_FIELD(SRCCODE)) {
1395 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1396 &thread->srccode_state);
1397 if (ret) {
1398 printed += ret;
1399 printed += printf("\n");
1400 }
1401 }
1402 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001403}
1404
Adrian Hunter055cd332016-06-23 16:40:56 +03001405static struct {
1406 u32 flags;
1407 const char *name;
1408} sample_flags[] = {
1409 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1410 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1411 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1412 {PERF_IP_FLAG_BRANCH, "jmp"},
1413 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1414 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1415 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1416 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1417 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1418 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1419 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1420 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1421 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1422 {0, NULL}
1423};
1424
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001425static const char *sample_flags_to_name(u32 flags)
1426{
1427 int i;
1428
1429 for (i = 0; sample_flags[i].name ; i++) {
1430 if (sample_flags[i].flags == flags)
1431 return sample_flags[i].name;
1432 }
1433
1434 return NULL;
1435}
1436
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001437static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001438{
1439 const char *chars = PERF_IP_FLAG_CHARS;
1440 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001441 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1442 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001443 char str[33];
1444 int i, pos = 0;
1445
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001446 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1447 if (name)
1448 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1449
1450 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1451 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1452 if (name)
1453 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1454 }
1455
1456 if (flags & PERF_IP_FLAG_TRACE_END) {
1457 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1458 if (name)
1459 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001460 }
1461
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001462 for (i = 0; i < n; i++, flags >>= 1) {
1463 if (flags & 1)
1464 str[pos++] = chars[i];
1465 }
1466 for (; i < 32; i++, flags >>= 1) {
1467 if (flags & 1)
1468 str[pos++] = '?';
1469 }
1470 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001471
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001472 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001473}
1474
Wang Nan30372f02016-02-24 11:20:45 +00001475struct printer_data {
1476 int line_no;
1477 bool hit_nul;
1478 bool is_printable;
1479};
1480
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001481static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1482 unsigned int val,
1483 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001484{
1485 unsigned char ch = (unsigned char)val;
1486 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001487 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001488
1489 switch (op) {
1490 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001491 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001492 break;
1493 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001494 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001495 " ");
1496 break;
1497 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001498 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001499 break;
1500 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001501 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001502 break;
1503 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001504 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001505 break;
1506 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001507 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001508 break;
1509 case BINARY_PRINT_CHAR_DATA:
1510 if (printer_data->hit_nul && ch)
1511 printer_data->is_printable = false;
1512
1513 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001514 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001515
1516 if (!printer_data->is_printable)
1517 break;
1518
1519 if (ch == '\0')
1520 printer_data->hit_nul = true;
1521 else
1522 printer_data->is_printable = false;
1523 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001524 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001525 }
1526 break;
1527 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001528 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001529 break;
1530 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001531 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001532 printer_data->line_no++;
1533 break;
1534 case BINARY_PRINT_DATA_END:
1535 default:
1536 break;
1537 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001538
1539 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001540}
1541
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001542static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001543{
1544 unsigned int nr_bytes = sample->raw_size;
1545 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001546 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1547 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001548
1549 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001550 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1551
1552 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001553}
1554
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001555static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001556{
1557 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001558 return fprintf(fp, "%*s", spacing - len, "");
1559
1560 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001561}
1562
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001563static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001564{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001565 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001566}
1567
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001568static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001569{
1570 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1571 int len;
1572
1573 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001574 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001575
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001576 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001577 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001578 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001579}
1580
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001581static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001582{
1583 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1584 int len;
1585
1586 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001587 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001588
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001589 len = fprintf(fp, " hints: %#x extensions: %#x ",
1590 data->hints, data->extensions);
1591 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001592}
1593
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001594static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001595{
1596 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1597 int len;
1598
1599 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001600 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001601
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001602 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1603 data->hw, data->cstate, data->subcstate);
1604 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001605}
1606
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001607static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001608{
1609 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1610 int len;
1611
1612 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001613 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001614
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001615 len = fprintf(fp, " IP: %u ", data->ip);
1616 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001617}
1618
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001619static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001620{
1621 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1622 int len;
1623
1624 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001625 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001626
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001627 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001628 data->deepest_cstate, data->last_cstate,
1629 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001630 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001631}
1632
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001633static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001634{
1635 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1636 unsigned int percent, freq;
1637 int len;
1638
1639 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001640 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001641
1642 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001643 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001644 if (data->max_nonturbo) {
1645 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001646 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001647 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001648 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001649}
1650
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001651static int perf_sample__fprintf_synth(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001652 struct evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001653{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001654 switch (evsel->core.attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001655 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001656 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001657 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001658 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001659 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001660 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001661 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001662 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001663 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001664 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001665 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001666 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001667 default:
1668 break;
1669 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001670
1671 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001672}
1673
Jiri Olsa809e9422015-11-26 18:55:21 +01001674struct perf_script {
1675 struct perf_tool tool;
1676 struct perf_session *session;
1677 bool show_task_events;
1678 bool show_mmap_events;
1679 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301680 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001681 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001682 bool show_round_events;
Jiri Olsa490c8cc2019-05-08 15:20:08 +02001683 bool show_bpf_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001684 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001685 bool per_event_dump;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001686 struct evswitch evswitch;
Jiri Olsaf8548392019-07-21 13:23:49 +02001687 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +02001688 struct perf_thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001689 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001690 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001691 struct perf_time_interval *ptime_range;
1692 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001693 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001694};
1695
Jiri Olsa63503db2019-07-21 13:23:52 +02001696static int perf_evlist__max_name_len(struct evlist *evlist)
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001697{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001698 struct evsel *evsel;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001699 int max = 0;
1700
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001701 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001702 int len = strlen(perf_evsel__name(evsel));
1703
1704 max = MAX(len, max);
1705 }
1706
1707 return max;
1708}
1709
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001710static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001711{
1712 struct mem_info mi = { .data_src.val = data_src };
1713 char decode[100];
1714 char out[100];
1715 static int maxlen;
1716 int len;
1717
1718 perf_script__meminfo_scnprintf(decode, 100, &mi);
1719
1720 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1721 if (maxlen < len)
1722 maxlen = len;
1723
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001724 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001725}
1726
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001727struct metric_ctx {
1728 struct perf_sample *sample;
1729 struct thread *thread;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001730 struct evsel *evsel;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001731 FILE *fp;
1732};
1733
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001734static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1735 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001736 const char *fmt,
1737 const char *unit, double val)
1738{
1739 struct metric_ctx *mctx = ctx;
1740
1741 if (!fmt)
1742 return;
1743 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001744 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001745 fputs("\tmetric: ", mctx->fp);
1746 if (color)
1747 color_fprintf(mctx->fp, color, fmt, val);
1748 else
1749 printf(fmt, val);
1750 fprintf(mctx->fp, " %s\n", unit);
1751}
1752
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001753static void script_new_line(struct perf_stat_config *config __maybe_unused,
1754 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001755{
1756 struct metric_ctx *mctx = ctx;
1757
1758 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001759 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001760 fputs("\tmetric: ", mctx->fp);
1761}
1762
1763static void perf_sample__fprint_metric(struct perf_script *script,
1764 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001765 struct evsel *evsel,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001766 struct perf_sample *sample,
1767 FILE *fp)
1768{
1769 struct perf_stat_output_ctx ctx = {
1770 .print_metric = script_print_metric,
1771 .new_line = script_new_line,
1772 .ctx = &(struct metric_ctx) {
1773 .sample = sample,
1774 .thread = thread,
1775 .evsel = evsel,
1776 .fp = fp,
1777 },
1778 .force_header = false,
1779 };
Jiri Olsa32dcd022019-07-21 13:23:51 +02001780 struct evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001781 u64 val;
1782
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001783 if (!evsel->stats)
1784 perf_evlist__alloc_stats(script->session->evlist, false);
1785 if (evsel_script(evsel->leader)->gnum++ == 0)
1786 perf_stat__reset_shadow_stats();
1787 val = sample->period * evsel->scale;
1788 perf_stat__update_shadow_stats(evsel,
1789 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001790 sample->cpu,
1791 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001792 evsel_script(evsel)->val = val;
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001793 if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) {
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001794 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001795 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001796 evsel_script(ev2)->val,
1797 sample->cpu,
1798 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001799 NULL,
1800 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001801 }
1802 evsel_script(evsel->leader)->gnum = 0;
1803 }
1804}
1805
Andi Kleen99f753f2018-09-20 11:05:39 -07001806static bool show_event(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001807 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001808 struct thread *thread,
1809 struct addr_location *al)
1810{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001811 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001812
1813 if (!symbol_conf.graph_function)
1814 return true;
1815
1816 if (thread->filter) {
1817 if (depth <= thread->filter_entry_depth) {
1818 thread->filter = false;
1819 return false;
1820 }
1821 return true;
1822 } else {
1823 const char *s = symbol_conf.graph_function;
1824 u64 ip;
1825 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1826 &ip);
1827 unsigned nlen;
1828
1829 if (!name)
1830 return false;
1831 nlen = strlen(name);
1832 while (*s) {
1833 unsigned len = strcspn(s, ",");
1834 if (nlen == len && !strncmp(name, s, len)) {
1835 thread->filter = true;
1836 thread->filter_entry_depth = depth;
1837 return true;
1838 }
1839 s += len;
1840 if (*s == ',')
1841 s++;
1842 }
1843 return false;
1844 }
1845}
1846
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001847static void process_event(struct perf_script *script,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001848 struct perf_sample *sample, struct evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001849 struct addr_location *al,
1850 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001851{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001852 struct thread *thread = al->thread;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001853 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001854 unsigned int type = output_type(attr->type);
Jiri Olsa32dcd022019-07-21 13:23:51 +02001855 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001856 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001857
Adrian Hunter14057202017-06-21 13:17:19 +03001858 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001859 return;
1860
Andi Kleen99f753f2018-09-20 11:05:39 -07001861 if (!show_event(sample, evsel, thread, al))
1862 return;
1863
Arnaldo Carvalho de Melo8829e562019-08-15 11:00:11 -03001864 if (evswitch__discard(&script->evswitch, evsel))
1865 return;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001866
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001867 ++es->samples;
1868
Jiri Olsa28a0b392018-01-07 17:03:52 +01001869 perf_sample__fprintf_start(sample, thread, evsel,
1870 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001871
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001872 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001873 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001874
Namhyung Kime944d3d2013-11-18 14:34:52 +09001875 if (PRINT_FIELD(EVNAME)) {
1876 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001877
1878 if (!script->name_width)
1879 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1880
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001881 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001882 }
1883
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001884 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001885 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001886
Akihiro Nagai95582592012-01-30 13:43:09 +09001887 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001888 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001889 return;
1890 }
1891
Andi Kleen96167162019-01-17 11:48:34 -08001892 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001893 event_format__fprintf(evsel->tp_format, sample->cpu,
1894 sample->raw_data, sample->raw_size, fp);
1895 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001896
1897 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001898 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001899
David Ahern7cec0922011-05-30 13:08:23 -06001900 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001901 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001902
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001903 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001904 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001905
1906 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001907 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001908
David Ahern787bef12011-05-27 14:28:43 -06001909 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001910 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001911
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001912 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001913 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001914 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001915 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001916
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001917 fputc(cursor ? '\n' : ' ', fp);
Arnaldo Carvalho de Melo9620bc362019-09-25 15:06:59 -03001918 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor,
1919 symbol_conf.bt_stop_list, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001920 }
1921
Stephane Eranianfc36f942015-08-31 18:41:10 +02001922 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001923 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001924
Andi Kleenb1491ac2017-09-05 11:40:57 -07001925 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001926 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001927
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001928 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001929 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001930 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001931 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001932 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001933 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001934
Wang Nan30372f02016-02-24 11:20:45 +00001935 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001936 perf_sample__fprintf_bpf_output(sample, fp);
1937 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001938
1939 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001940 fprintf(fp, "%16" PRIx64, sample->phys_addr);
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001941
1942 perf_sample__fprintf_ipc(sample, attr, fp);
1943
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001944 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001945
Andi Kleendd2e18e2018-12-03 16:18:48 -08001946 if (PRINT_FIELD(SRCCODE)) {
1947 if (map__fprintf_srccode(al->map, al->addr, stdout,
1948 &thread->srccode_state))
1949 printf("\n");
1950 }
1951
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001952 if (PRINT_FIELD(METRIC))
1953 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001954
1955 if (verbose)
1956 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001957}
1958
Tom Zanussi956ffd02009-11-25 01:15:46 -06001959static struct scripting_ops *scripting_ops;
1960
Jiri Olsa32dcd022019-07-21 13:23:51 +02001961static void __process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsa36e33c52016-01-06 11:49:56 +01001962{
Jiri Olsaa2f354e2019-08-22 13:11:41 +02001963 int nthreads = perf_thread_map__nr(counter->core.threads);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001964 int ncpus = perf_evsel__nr_cpus(counter);
1965 int cpu, thread;
1966 static int header_printed;
1967
Jiri Olsa648b5af2019-08-06 11:35:19 +02001968 if (counter->core.system_wide)
Jiri Olsa36e33c52016-01-06 11:49:56 +01001969 nthreads = 1;
1970
1971 if (!header_printed) {
1972 printf("%3s %8s %15s %15s %15s %15s %s\n",
1973 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1974 header_printed = 1;
1975 }
1976
1977 for (thread = 0; thread < nthreads; thread++) {
1978 for (cpu = 0; cpu < ncpus; cpu++) {
1979 struct perf_counts_values *counts;
1980
1981 counts = perf_counts(counter->counts, cpu, thread);
1982
1983 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
Jiri Olsad400bd32019-07-21 13:24:37 +02001984 counter->core.cpus->map[cpu],
Jiri Olsaa2f354e2019-08-22 13:11:41 +02001985 perf_thread_map__pid(counter->core.threads, thread),
Jiri Olsa36e33c52016-01-06 11:49:56 +01001986 counts->val,
1987 counts->ena,
1988 counts->run,
1989 tstamp,
1990 perf_evsel__name(counter));
1991 }
1992 }
1993}
1994
Jiri Olsa32dcd022019-07-21 13:23:51 +02001995static void process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsae099eba2016-01-05 22:09:09 +01001996{
1997 if (scripting_ops && scripting_ops->process_stat)
1998 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001999 else
2000 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01002001}
2002
2003static void process_stat_interval(u64 tstamp)
2004{
2005 if (scripting_ops && scripting_ops->process_stat_interval)
2006 scripting_ops->process_stat_interval(tstamp);
2007}
2008
Tom Zanussi956ffd02009-11-25 01:15:46 -06002009static void setup_scripting(void)
2010{
Tom Zanussi16c632d2009-11-25 01:15:48 -06002011 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06002012 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002013}
2014
Adrian Hunterd445dd22014-08-15 22:08:37 +03002015static int flush_scripting(void)
2016{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002017 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03002018}
2019
Tom Zanussi956ffd02009-11-25 01:15:46 -06002020static int cleanup_scripting(void)
2021{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002022 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05002023
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002024 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002025}
2026
Andi Kleene87e5482019-03-11 07:44:52 -07002027static bool filter_cpu(struct perf_sample *sample)
2028{
2029 if (cpu_list)
2030 return !test_bit(sample->cpu, cpu_bitmap);
2031 return false;
2032}
2033
Jiri Olsa809e9422015-11-26 18:55:21 +01002034static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002035 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002036 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002037 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002038 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002039{
Jiri Olsa809e9422015-11-26 18:55:21 +01002040 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07002041 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002042
Jin Yao2ab046c2017-12-08 21:13:46 +08002043 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
2044 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07002045 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08002046 }
David Aherna91f4c42016-11-29 10:15:43 -07002047
David Ahern1424dc92011-03-09 22:23:28 -07002048 if (debug_mode) {
2049 if (sample->time < last_timestamp) {
2050 pr_err("Samples misordered, previous: %" PRIu64
2051 " this: %" PRIu64 "\n", last_timestamp,
2052 sample->time);
2053 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02002054 }
David Ahern1424dc92011-03-09 22:23:28 -07002055 last_timestamp = sample->time;
2056 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002057 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002058
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03002059 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07002060 pr_err("problem processing %d event, skipping it.\n",
2061 event->header.type);
2062 return -1;
2063 }
2064
2065 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002066 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07002067
Andi Kleene87e5482019-03-11 07:44:52 -07002068 if (filter_cpu(sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002069 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002070
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002071 if (scripting_ops)
2072 scripting_ops->process_event(event, sample, evsel, &al);
2073 else
Andi Kleen48d02a12017-02-23 15:46:34 -08002074 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002075
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002076out_put:
2077 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002078 return 0;
2079}
2080
Adrian Hunter7ea95722013-11-01 15:51:30 +02002081static int process_attr(struct perf_tool *tool, union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02002082 struct evlist **pevlist)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002083{
2084 struct perf_script *scr = container_of(tool, struct perf_script, tool);
Jiri Olsa63503db2019-07-21 13:23:52 +02002085 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002086 struct evsel *evsel, *pos;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002087 int err;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002088 static struct evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002089
2090 err = perf_event__process_attr(tool, event, pevlist);
2091 if (err)
2092 return err;
2093
2094 evlist = *pevlist;
Jiri Olsa515dbe42019-09-03 10:39:52 +02002095 evsel = evlist__last(*pevlist);
Adrian Hunter7ea95722013-11-01 15:51:30 +02002096
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302097 if (!evsel->priv) {
2098 if (scr->per_event_dump) {
2099 evsel->priv = perf_evsel_script__new(evsel,
2100 scr->session->data);
2101 } else {
2102 es = zalloc(sizeof(*es));
2103 if (!es)
2104 return -ENOMEM;
2105 es->fp = stdout;
2106 evsel->priv = es;
2107 }
2108 }
2109
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002110 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2111 evsel->core.attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002112 return 0;
2113
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002114 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002115 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002116 return 0;
2117 }
2118
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002119 set_print_ip_opts(&evsel->core.attr);
Adrian Hunter7ea95722013-11-01 15:51:30 +02002120
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002121 if (evsel->core.attr.sample_type)
Jiri Olsad2b5a312015-10-16 12:41:25 +02002122 err = perf_evsel__check_attr(evsel, scr->session);
2123
2124 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002125}
2126
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002127static int process_comm_event(struct perf_tool *tool,
2128 union perf_event *event,
2129 struct perf_sample *sample,
2130 struct machine *machine)
2131{
2132 struct thread *thread;
2133 struct perf_script *script = container_of(tool, struct perf_script, tool);
2134 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002135 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002136 int ret = -1;
2137
2138 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2139 if (thread == NULL) {
2140 pr_debug("problem processing COMM event, skipping it.\n");
2141 return -1;
2142 }
2143
2144 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2145 goto out;
2146
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002147 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002148 sample->cpu = 0;
2149 sample->time = 0;
2150 sample->tid = event->comm.tid;
2151 sample->pid = event->comm.pid;
2152 }
Andi Kleene87e5482019-03-11 07:44:52 -07002153 if (!filter_cpu(sample)) {
2154 perf_sample__fprintf_start(sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01002155 PERF_RECORD_COMM, stdout);
Andi Kleene87e5482019-03-11 07:44:52 -07002156 perf_event__fprintf(event, stdout);
2157 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002158 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002159out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002160 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002161 return ret;
2162}
2163
Hari Bathini96a44bb2017-03-08 02:12:06 +05302164static int process_namespaces_event(struct perf_tool *tool,
2165 union perf_event *event,
2166 struct perf_sample *sample,
2167 struct machine *machine)
2168{
2169 struct thread *thread;
2170 struct perf_script *script = container_of(tool, struct perf_script, tool);
2171 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002172 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302173 int ret = -1;
2174
2175 thread = machine__findnew_thread(machine, event->namespaces.pid,
2176 event->namespaces.tid);
2177 if (thread == NULL) {
2178 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2179 return -1;
2180 }
2181
2182 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2183 goto out;
2184
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002185 if (!evsel->core.attr.sample_id_all) {
Hari Bathini96a44bb2017-03-08 02:12:06 +05302186 sample->cpu = 0;
2187 sample->time = 0;
2188 sample->tid = event->namespaces.tid;
2189 sample->pid = event->namespaces.pid;
2190 }
Andi Kleene87e5482019-03-11 07:44:52 -07002191 if (!filter_cpu(sample)) {
2192 perf_sample__fprintf_start(sample, thread, evsel,
2193 PERF_RECORD_NAMESPACES, stdout);
2194 perf_event__fprintf(event, stdout);
2195 }
Hari Bathini96a44bb2017-03-08 02:12:06 +05302196 ret = 0;
2197out:
2198 thread__put(thread);
2199 return ret;
2200}
2201
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002202static int process_fork_event(struct perf_tool *tool,
2203 union perf_event *event,
2204 struct perf_sample *sample,
2205 struct machine *machine)
2206{
2207 struct thread *thread;
2208 struct perf_script *script = container_of(tool, struct perf_script, tool);
2209 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002210 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002211
2212 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2213 return -1;
2214
2215 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2216 if (thread == NULL) {
2217 pr_debug("problem processing FORK event, skipping it.\n");
2218 return -1;
2219 }
2220
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002221 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002222 sample->cpu = 0;
2223 sample->time = event->fork.time;
2224 sample->tid = event->fork.tid;
2225 sample->pid = event->fork.pid;
2226 }
Andi Kleene87e5482019-03-11 07:44:52 -07002227 if (!filter_cpu(sample)) {
2228 perf_sample__fprintf_start(sample, thread, evsel,
2229 PERF_RECORD_FORK, stdout);
2230 perf_event__fprintf(event, stdout);
2231 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002232 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002233
2234 return 0;
2235}
2236static int process_exit_event(struct perf_tool *tool,
2237 union perf_event *event,
2238 struct perf_sample *sample,
2239 struct machine *machine)
2240{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002241 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002242 struct thread *thread;
2243 struct perf_script *script = container_of(tool, struct perf_script, tool);
2244 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002245 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002246
2247 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2248 if (thread == NULL) {
2249 pr_debug("problem processing EXIT event, skipping it.\n");
2250 return -1;
2251 }
2252
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002253 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002254 sample->cpu = 0;
2255 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002256 sample->tid = event->fork.tid;
2257 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002258 }
Andi Kleene87e5482019-03-11 07:44:52 -07002259 if (!filter_cpu(sample)) {
2260 perf_sample__fprintf_start(sample, thread, evsel,
2261 PERF_RECORD_EXIT, stdout);
2262 perf_event__fprintf(event, stdout);
2263 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002264
2265 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002266 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002267
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002268 thread__put(thread);
2269 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002270}
2271
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002272static int process_mmap_event(struct perf_tool *tool,
2273 union perf_event *event,
2274 struct perf_sample *sample,
2275 struct machine *machine)
2276{
2277 struct thread *thread;
2278 struct perf_script *script = container_of(tool, struct perf_script, tool);
2279 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002280 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002281
2282 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2283 return -1;
2284
2285 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2286 if (thread == NULL) {
2287 pr_debug("problem processing MMAP event, skipping it.\n");
2288 return -1;
2289 }
2290
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002291 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002292 sample->cpu = 0;
2293 sample->time = 0;
2294 sample->tid = event->mmap.tid;
2295 sample->pid = event->mmap.pid;
2296 }
Andi Kleene87e5482019-03-11 07:44:52 -07002297 if (!filter_cpu(sample)) {
2298 perf_sample__fprintf_start(sample, thread, evsel,
2299 PERF_RECORD_MMAP, stdout);
2300 perf_event__fprintf(event, stdout);
2301 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002302 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002303 return 0;
2304}
2305
2306static int process_mmap2_event(struct perf_tool *tool,
2307 union perf_event *event,
2308 struct perf_sample *sample,
2309 struct machine *machine)
2310{
2311 struct thread *thread;
2312 struct perf_script *script = container_of(tool, struct perf_script, tool);
2313 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002314 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002315
2316 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2317 return -1;
2318
2319 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2320 if (thread == NULL) {
2321 pr_debug("problem processing MMAP2 event, skipping it.\n");
2322 return -1;
2323 }
2324
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002325 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002326 sample->cpu = 0;
2327 sample->time = 0;
2328 sample->tid = event->mmap2.tid;
2329 sample->pid = event->mmap2.pid;
2330 }
Andi Kleene87e5482019-03-11 07:44:52 -07002331 if (!filter_cpu(sample)) {
2332 perf_sample__fprintf_start(sample, thread, evsel,
2333 PERF_RECORD_MMAP2, stdout);
2334 perf_event__fprintf(event, stdout);
2335 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002336 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002337 return 0;
2338}
2339
Adrian Hunter7c148982015-07-21 12:44:06 +03002340static int process_switch_event(struct perf_tool *tool,
2341 union perf_event *event,
2342 struct perf_sample *sample,
2343 struct machine *machine)
2344{
2345 struct thread *thread;
2346 struct perf_script *script = container_of(tool, struct perf_script, tool);
2347 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002348 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Adrian Hunter7c148982015-07-21 12:44:06 +03002349
2350 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2351 return -1;
2352
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002353 if (scripting_ops && scripting_ops->process_switch)
2354 scripting_ops->process_switch(event, sample, machine);
2355
2356 if (!script->show_switch_events)
2357 return 0;
2358
Adrian Hunter7c148982015-07-21 12:44:06 +03002359 thread = machine__findnew_thread(machine, sample->pid,
2360 sample->tid);
2361 if (thread == NULL) {
2362 pr_debug("problem processing SWITCH event, skipping it.\n");
2363 return -1;
2364 }
2365
Andi Kleene87e5482019-03-11 07:44:52 -07002366 if (!filter_cpu(sample)) {
2367 perf_sample__fprintf_start(sample, thread, evsel,
2368 PERF_RECORD_SWITCH, stdout);
2369 perf_event__fprintf(event, stdout);
2370 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002371 thread__put(thread);
2372 return 0;
2373}
2374
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002375static int
2376process_lost_event(struct perf_tool *tool,
2377 union perf_event *event,
2378 struct perf_sample *sample,
2379 struct machine *machine)
2380{
2381 struct perf_script *script = container_of(tool, struct perf_script, tool);
2382 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002383 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002384 struct thread *thread;
2385
2386 thread = machine__findnew_thread(machine, sample->pid,
2387 sample->tid);
2388 if (thread == NULL)
2389 return -1;
2390
Andi Kleene87e5482019-03-11 07:44:52 -07002391 if (!filter_cpu(sample)) {
2392 perf_sample__fprintf_start(sample, thread, evsel,
2393 PERF_RECORD_LOST, stdout);
2394 perf_event__fprintf(event, stdout);
2395 }
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002396 thread__put(thread);
2397 return 0;
2398}
2399
Jiri Olsa3233b372018-02-06 19:17:59 +01002400static int
2401process_finished_round_event(struct perf_tool *tool __maybe_unused,
2402 union perf_event *event,
2403 struct ordered_events *oe __maybe_unused)
2404
2405{
2406 perf_event__fprintf(event, stdout);
2407 return 0;
2408}
2409
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002410static int
2411process_bpf_events(struct perf_tool *tool __maybe_unused,
2412 union perf_event *event,
2413 struct perf_sample *sample,
2414 struct machine *machine)
2415{
2416 struct thread *thread;
2417 struct perf_script *script = container_of(tool, struct perf_script, tool);
2418 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002419 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002420
2421 if (machine__process_ksymbol(machine, event, sample) < 0)
2422 return -1;
2423
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002424 if (!evsel->core.attr.sample_id_all) {
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002425 perf_event__fprintf(event, stdout);
2426 return 0;
2427 }
2428
2429 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2430 if (thread == NULL) {
2431 pr_debug("problem processing MMAP event, skipping it.\n");
2432 return -1;
2433 }
2434
2435 if (!filter_cpu(sample)) {
2436 perf_sample__fprintf_start(sample, thread, evsel,
2437 event->header.type, stdout);
2438 perf_event__fprintf(event, stdout);
2439 }
2440
2441 thread__put(thread);
2442 return 0;
2443}
2444
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002445static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002446{
2447 session_done = 1;
2448}
2449
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002450static void perf_script__fclose_per_event_dump(struct perf_script *script)
2451{
Jiri Olsa63503db2019-07-21 13:23:52 +02002452 struct evlist *evlist = script->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002453 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002454
2455 evlist__for_each_entry(evlist, evsel) {
2456 if (!evsel->priv)
2457 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002458 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002459 evsel->priv = NULL;
2460 }
2461}
2462
2463static int perf_script__fopen_per_event_dump(struct perf_script *script)
2464{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002465 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002466
2467 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002468 /*
2469 * Already setup? I.e. we may be called twice in cases like
2470 * Intel PT, one for the intel_pt// and dummy events, then
2471 * for the evsels syntheized from the auxtrace info.
2472 *
2473 * Ses perf_script__process_auxtrace_info.
2474 */
2475 if (evsel->priv != NULL)
2476 continue;
2477
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002478 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002479 if (evsel->priv == NULL)
2480 goto out_err_fclose;
2481 }
2482
2483 return 0;
2484
2485out_err_fclose:
2486 perf_script__fclose_per_event_dump(script);
2487 return -1;
2488}
2489
2490static int perf_script__setup_per_event_dump(struct perf_script *script)
2491{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002492 struct evsel *evsel;
2493 static struct evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002494
2495 if (script->per_event_dump)
2496 return perf_script__fopen_per_event_dump(script);
2497
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002498 es_stdout.fp = stdout;
2499
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002500 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002501 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002502
2503 return 0;
2504}
2505
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002506static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2507{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002508 struct evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002509
2510 evlist__for_each_entry(script->session->evlist, evsel) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002511 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002512
2513 perf_evsel_script__fprintf(es, stdout);
2514 perf_evsel_script__delete(es);
2515 evsel->priv = NULL;
2516 }
2517}
2518
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002519static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002520{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002521 int ret;
2522
Tom Zanussic239da32010-04-01 23:59:18 -05002523 signal(SIGINT, sig_handler);
2524
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002525 perf_stat__init_shadow_stats();
2526
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002527 /* override event processing functions */
2528 if (script->show_task_events) {
2529 script->tool.comm = process_comm_event;
2530 script->tool.fork = process_fork_event;
2531 script->tool.exit = process_exit_event;
2532 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002533 if (script->show_mmap_events) {
2534 script->tool.mmap = process_mmap_event;
2535 script->tool.mmap2 = process_mmap2_event;
2536 }
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002537 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
Adrian Hunter7c148982015-07-21 12:44:06 +03002538 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302539 if (script->show_namespace_events)
2540 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002541 if (script->show_lost_events)
2542 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002543 if (script->show_round_events) {
2544 script->tool.ordered_events = false;
2545 script->tool.finished_round = process_finished_round_event;
2546 }
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002547 if (script->show_bpf_events) {
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -03002548 script->tool.ksymbol = process_bpf_events;
2549 script->tool.bpf = process_bpf_events;
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002550 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002551
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002552 if (perf_script__setup_per_event_dump(script)) {
2553 pr_err("Couldn't create the per event dump files\n");
2554 return -1;
2555 }
2556
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002557 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002558
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002559 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002560 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002561
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002562 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002563 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002564
2565 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002566}
2567
Tom Zanussi956ffd02009-11-25 01:15:46 -06002568struct script_spec {
2569 struct list_head node;
2570 struct scripting_ops *ops;
2571 char spec[0];
2572};
2573
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002574static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002575
2576static struct script_spec *script_spec__new(const char *spec,
2577 struct scripting_ops *ops)
2578{
2579 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2580
2581 if (s != NULL) {
2582 strcpy(s->spec, spec);
2583 s->ops = ops;
2584 }
2585
2586 return s;
2587}
2588
Tom Zanussi956ffd02009-11-25 01:15:46 -06002589static void script_spec__add(struct script_spec *s)
2590{
2591 list_add_tail(&s->node, &script_specs);
2592}
2593
2594static struct script_spec *script_spec__find(const char *spec)
2595{
2596 struct script_spec *s;
2597
2598 list_for_each_entry(s, &script_specs, node)
2599 if (strcasecmp(s->spec, spec) == 0)
2600 return s;
2601 return NULL;
2602}
2603
Tom Zanussi956ffd02009-11-25 01:15:46 -06002604int script_spec_register(const char *spec, struct scripting_ops *ops)
2605{
2606 struct script_spec *s;
2607
2608 s = script_spec__find(spec);
2609 if (s)
2610 return -1;
2611
Taeung Song8560bae2016-02-26 00:13:10 +09002612 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002613 if (!s)
2614 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002615 else
2616 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002617
2618 return 0;
2619}
2620
2621static struct scripting_ops *script_spec__lookup(const char *spec)
2622{
2623 struct script_spec *s = script_spec__find(spec);
2624 if (!s)
2625 return NULL;
2626
2627 return s->ops;
2628}
2629
2630static void list_available_languages(void)
2631{
2632 struct script_spec *s;
2633
2634 fprintf(stderr, "\n");
2635 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002636 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002637
2638 list_for_each_entry(s, &script_specs, node)
2639 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2640
2641 fprintf(stderr, "\n");
2642}
2643
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002644static int parse_scriptname(const struct option *opt __maybe_unused,
2645 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002646{
2647 char spec[PATH_MAX];
2648 const char *script, *ext;
2649 int len;
2650
Tom Zanussif526d682010-01-27 02:27:52 -06002651 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002652 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002653 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002654 }
2655
2656 script = strchr(str, ':');
2657 if (script) {
2658 len = script - str;
2659 if (len >= PATH_MAX) {
2660 fprintf(stderr, "invalid language specifier");
2661 return -1;
2662 }
2663 strncpy(spec, str, len);
2664 spec[len] = '\0';
2665 scripting_ops = script_spec__lookup(spec);
2666 if (!scripting_ops) {
2667 fprintf(stderr, "invalid language specifier");
2668 return -1;
2669 }
2670 script++;
2671 } else {
2672 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002673 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002674 if (!ext) {
2675 fprintf(stderr, "invalid script extension");
2676 return -1;
2677 }
2678 scripting_ops = script_spec__lookup(++ext);
2679 if (!scripting_ops) {
2680 fprintf(stderr, "invalid script extension");
2681 return -1;
2682 }
2683 }
2684
2685 script_name = strdup(script);
2686
2687 return 0;
2688}
2689
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002690static int parse_output_fields(const struct option *opt __maybe_unused,
2691 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002692{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002693 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002694 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002695 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002696 int rc = 0;
2697 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002698 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002699 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002700
2701 if (!str)
2702 return -ENOMEM;
2703
David Ahern2c9e45f72011-03-17 10:03:21 -06002704 /* first word can state for which event type the user is specifying
2705 * the fields. If no type exists, the specified fields apply to all
2706 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002707 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002708 tok = strchr(str, ':');
2709 if (tok) {
2710 *tok = '\0';
2711 tok++;
2712 if (!strcmp(str, "hw"))
2713 type = PERF_TYPE_HARDWARE;
2714 else if (!strcmp(str, "sw"))
2715 type = PERF_TYPE_SOFTWARE;
2716 else if (!strcmp(str, "trace"))
2717 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002718 else if (!strcmp(str, "raw"))
2719 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002720 else if (!strcmp(str, "break"))
2721 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002722 else if (!strcmp(str, "synth"))
2723 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002724 else {
2725 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002726 rc = -EINVAL;
2727 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002728 }
2729
2730 if (output[type].user_set)
2731 pr_warning("Overriding previous field request for %s events.\n",
2732 event_type(type));
2733
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002734 /* Don't override defaults for +- */
2735 if (strchr(tok, '+') || strchr(tok, '-'))
2736 goto parse;
2737
David Ahern2c9e45f72011-03-17 10:03:21 -06002738 output[type].fields = 0;
2739 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002740 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002741
2742 } else {
2743 tok = str;
2744 if (strlen(str) == 0) {
2745 fprintf(stderr,
2746 "Cannot set fields to 'none' for all event types.\n");
2747 rc = -EINVAL;
2748 goto out;
2749 }
2750
Andi Kleen36ce5652017-06-02 08:48:10 -07002751 /* Don't override defaults for +- */
2752 if (strchr(str, '+') || strchr(str, '-'))
2753 goto parse;
2754
David Ahern2c9e45f72011-03-17 10:03:21 -06002755 if (output_set_by_user())
2756 pr_warning("Overriding previous field request for all events.\n");
2757
Adrian Hunter14057202017-06-21 13:17:19 +03002758 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002759 output[j].fields = 0;
2760 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002761 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002762 }
David Ahern1424dc92011-03-09 22:23:28 -07002763 }
2764
Andi Kleen36ce5652017-06-02 08:48:10 -07002765parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002766 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002767 if (*tok == '+') {
2768 if (change == SET)
2769 goto out_badmix;
2770 change = ADD;
2771 tok++;
2772 } else if (*tok == '-') {
2773 if (change == SET)
2774 goto out_badmix;
2775 change = REMOVE;
2776 tok++;
2777 } else {
2778 if (change != SET && change != DEFAULT)
2779 goto out_badmix;
2780 change = SET;
2781 }
2782
David Ahern745f43e2011-03-09 22:23:26 -07002783 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002784 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002785 break;
David Ahern745f43e2011-03-09 22:23:26 -07002786 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002787 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002788 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002789 continue;
2790 }
David Ahern745f43e2011-03-09 22:23:26 -07002791 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002792 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002793 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002794 goto out;
2795 }
2796
2797 if (type == -1) {
2798 /* add user option to all events types for
2799 * which it is valid
2800 */
Adrian Hunter14057202017-06-21 13:17:19 +03002801 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002802 if (output[j].invalid_fields & all_output_options[i].field) {
2803 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2804 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002805 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002806 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002807 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002808 output[j].user_set_fields &= ~all_output_options[i].field;
2809 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002810 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002811 output[j].user_set_fields |= all_output_options[i].field;
2812 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002813 output[j].user_set = true;
2814 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002815 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002816 }
2817 } else {
2818 if (output[type].invalid_fields & all_output_options[i].field) {
2819 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2820 all_output_options[i].str, event_type(type));
2821
2822 rc = -EINVAL;
2823 goto out;
2824 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002825 if (change == REMOVE)
2826 output[type].fields &= ~all_output_options[i].field;
2827 else
2828 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002829 output[type].user_set = true;
2830 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002831 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002832 }
2833
2834 if (type >= 0) {
2835 if (output[type].fields == 0) {
2836 pr_debug("No fields requested for %s type. "
2837 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002838 }
David Ahern1424dc92011-03-09 22:23:28 -07002839 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002840 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002841
Andi Kleen36ce5652017-06-02 08:48:10 -07002842out_badmix:
2843 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2844 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002845out:
David Ahern745f43e2011-03-09 22:23:26 -07002846 free(str);
2847 return rc;
2848}
2849
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002850#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2851 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2852 if ((lang_dirent->d_type == DT_DIR || \
2853 (lang_dirent->d_type == DT_UNKNOWN && \
2854 is_directory(scripts_path, lang_dirent))) && \
2855 (strcmp(lang_dirent->d_name, ".")) && \
2856 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002857
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002858#define for_each_script(lang_path, lang_dir, script_dirent) \
2859 while ((script_dirent = readdir(lang_dir)) != NULL) \
2860 if (script_dirent->d_type != DT_DIR && \
2861 (script_dirent->d_type != DT_UNKNOWN || \
2862 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002863
2864
2865#define RECORD_SUFFIX "-record"
2866#define REPORT_SUFFIX "-report"
2867
2868struct script_desc {
2869 struct list_head node;
2870 char *name;
2871 char *half_liner;
2872 char *args;
2873};
2874
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002875static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002876
2877static struct script_desc *script_desc__new(const char *name)
2878{
2879 struct script_desc *s = zalloc(sizeof(*s));
2880
Tom Zanussib5b87312010-11-10 08:16:51 -06002881 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002882 s->name = strdup(name);
2883
2884 return s;
2885}
2886
2887static void script_desc__delete(struct script_desc *s)
2888{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002889 zfree(&s->name);
2890 zfree(&s->half_liner);
2891 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002892 free(s);
2893}
2894
2895static void script_desc__add(struct script_desc *s)
2896{
2897 list_add_tail(&s->node, &script_descs);
2898}
2899
2900static struct script_desc *script_desc__find(const char *name)
2901{
2902 struct script_desc *s;
2903
2904 list_for_each_entry(s, &script_descs, node)
2905 if (strcasecmp(s->name, name) == 0)
2906 return s;
2907 return NULL;
2908}
2909
2910static struct script_desc *script_desc__findnew(const char *name)
2911{
2912 struct script_desc *s = script_desc__find(name);
2913
2914 if (s)
2915 return s;
2916
2917 s = script_desc__new(name);
2918 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002919 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002920
2921 script_desc__add(s);
2922
2923 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002924}
2925
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002926static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002927{
2928 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002929 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002930
2931 if (strlen(str) > suffix_len) {
2932 p = str + strlen(str) - suffix_len;
2933 if (!strncmp(p, suffix, suffix_len))
2934 return p;
2935 }
2936
2937 return NULL;
2938}
2939
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002940static int read_script_info(struct script_desc *desc, const char *filename)
2941{
2942 char line[BUFSIZ], *p;
2943 FILE *fp;
2944
2945 fp = fopen(filename, "r");
2946 if (!fp)
2947 return -1;
2948
2949 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002950 p = skip_spaces(line);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002951 if (strlen(p) == 0)
2952 continue;
2953 if (*p != '#')
2954 continue;
2955 p++;
2956 if (strlen(p) && *p == '!')
2957 continue;
2958
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002959 p = skip_spaces(p);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002960 if (strlen(p) && p[strlen(p) - 1] == '\n')
2961 p[strlen(p) - 1] = '\0';
2962
2963 if (!strncmp(p, "description:", strlen("description:"))) {
2964 p += strlen("description:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002965 desc->half_liner = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002966 continue;
2967 }
2968
2969 if (!strncmp(p, "args:", strlen("args:"))) {
2970 p += strlen("args:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002971 desc->args = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002972 continue;
2973 }
2974 }
2975
2976 fclose(fp);
2977
2978 return 0;
2979}
2980
Robert Richter38efb532011-11-25 11:38:40 +01002981static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2982{
2983 char *script_root, *str;
2984
2985 script_root = strdup(script_dirent->d_name);
2986 if (!script_root)
2987 return NULL;
2988
2989 str = (char *)ends_with(script_root, suffix);
2990 if (!str) {
2991 free(script_root);
2992 return NULL;
2993 }
2994
2995 *str = '\0';
2996 return script_root;
2997}
2998
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002999static int list_available_scripts(const struct option *opt __maybe_unused,
3000 const char *s __maybe_unused,
3001 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003002{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003003 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003004 char scripts_path[MAXPATHLEN];
3005 DIR *scripts_dir, *lang_dir;
3006 char script_path[MAXPATHLEN];
3007 char lang_path[MAXPATHLEN];
3008 struct script_desc *desc;
3009 char first_half[BUFSIZ];
3010 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003011
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003012 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003013
3014 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00003015 if (!scripts_dir) {
3016 fprintf(stdout,
3017 "open(%s) failed.\n"
3018 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
3019 scripts_path);
3020 exit(-1);
3021 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003022
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003023 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003024 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3025 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003026 lang_dir = opendir(lang_path);
3027 if (!lang_dir)
3028 continue;
3029
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003030 for_each_script(lang_path, lang_dir, script_dirent) {
3031 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01003032 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003033 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01003034 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3035 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003036 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01003037 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003038 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003039 }
3040 }
3041
3042 fprintf(stdout, "List of available trace scripts:\n");
3043 list_for_each_entry(desc, &script_descs, node) {
3044 sprintf(first_half, "%s %s", desc->name,
3045 desc->args ? desc->args : "");
3046 fprintf(stdout, " %-36s %s\n", first_half,
3047 desc->half_liner ? desc->half_liner : "");
3048 }
3049
3050 exit(0);
3051}
3052
Feng Tange5f37052012-09-07 16:42:26 +08003053/*
Feng Tang49e639e2012-10-30 11:56:03 +08003054 * Some scripts specify the required events in their "xxx-record" file,
3055 * this function will check if the events in perf.data match those
3056 * mentioned in the "xxx-record".
3057 *
3058 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3059 * which is covered well now. And new parsing code should be added to
3060 * cover the future complexing formats like event groups etc.
3061 */
3062static int check_ev_match(char *dir_name, char *scriptname,
3063 struct perf_session *session)
3064{
3065 char filename[MAXPATHLEN], evname[128];
3066 char line[BUFSIZ], *p;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003067 struct evsel *pos;
Feng Tang49e639e2012-10-30 11:56:03 +08003068 int match, len;
3069 FILE *fp;
3070
Jiri Olsa77f18152018-03-19 09:29:01 +01003071 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08003072
3073 fp = fopen(filename, "r");
3074 if (!fp)
3075 return -1;
3076
3077 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003078 p = skip_spaces(line);
Feng Tang49e639e2012-10-30 11:56:03 +08003079 if (*p == '#')
3080 continue;
3081
3082 while (strlen(p)) {
3083 p = strstr(p, "-e");
3084 if (!p)
3085 break;
3086
3087 p += 2;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003088 p = skip_spaces(p);
Feng Tang49e639e2012-10-30 11:56:03 +08003089 len = strcspn(p, " \t");
3090 if (!len)
3091 break;
3092
3093 snprintf(evname, len + 1, "%s", p);
3094
3095 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003096 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08003097 if (!strcmp(perf_evsel__name(pos), evname)) {
3098 match = 1;
3099 break;
3100 }
3101 }
3102
3103 if (!match) {
3104 fclose(fp);
3105 return -1;
3106 }
3107 }
3108 }
3109
3110 fclose(fp);
3111 return 0;
3112}
3113
3114/*
Feng Tange5f37052012-09-07 16:42:26 +08003115 * Return -1 if none is found, otherwise the actual scripts number.
3116 *
3117 * Currently the only user of this function is the script browser, which
3118 * will list all statically runnable scripts, select one, execute it and
3119 * show the output in a perf browser.
3120 */
Andi Kleen905e4af2019-03-11 07:45:01 -07003121int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3122 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08003123{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003124 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08003125 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08003126 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08003127 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003128 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003129 .path = input_name,
3130 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003131 };
Feng Tange5f37052012-09-07 16:42:26 +08003132 char *temp;
3133 int i = 0;
3134
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003135 session = perf_session__new(&data, false, NULL);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05303136 if (IS_ERR(session))
3137 return PTR_ERR(session);
Feng Tang49e639e2012-10-30 11:56:03 +08003138
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003139 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003140
3141 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003142 if (!scripts_dir) {
3143 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003144 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003145 }
Feng Tange5f37052012-09-07 16:42:26 +08003146
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003147 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003148 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3149 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003150#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003151 if (strstr(lang_path, "perl"))
3152 continue;
3153#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003154#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003155 if (strstr(lang_path, "python"))
3156 continue;
3157#endif
3158
3159 lang_dir = opendir(lang_path);
3160 if (!lang_dir)
3161 continue;
3162
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003163 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003164 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003165 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003166 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003167 if (i >= num)
3168 break;
3169 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3170 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003171 script_dirent->d_name);
3172 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003173 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003174 (temp - script_dirent->d_name) + 1,
3175 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003176
3177 if (check_ev_match(lang_path,
3178 scripts_array[i], session))
3179 continue;
3180
Feng Tange5f37052012-09-07 16:42:26 +08003181 i++;
3182 }
Feng Tang49e639e2012-10-30 11:56:03 +08003183 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003184 }
3185
Feng Tang49e639e2012-10-30 11:56:03 +08003186 closedir(scripts_dir);
3187 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003188 return i;
3189}
3190
Tom Zanussi38752942009-12-15 02:53:39 -06003191static char *get_script_path(const char *script_root, const char *suffix)
3192{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003193 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003194 char scripts_path[MAXPATHLEN];
3195 char script_path[MAXPATHLEN];
3196 DIR *scripts_dir, *lang_dir;
3197 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003198 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003199
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003200 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003201
3202 scripts_dir = opendir(scripts_path);
3203 if (!scripts_dir)
3204 return NULL;
3205
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003206 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003207 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3208 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003209 lang_dir = opendir(lang_path);
3210 if (!lang_dir)
3211 continue;
3212
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003213 for_each_script(lang_path, lang_dir, script_dirent) {
3214 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003215 if (__script_root && !strcmp(script_root, __script_root)) {
3216 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003217 closedir(lang_dir);
3218 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003219 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3220 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003221 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003222 }
3223 free(__script_root);
3224 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003225 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003226 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003227 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003228
Robert Richter38efb532011-11-25 11:38:40 +01003229 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003230}
3231
Tom Zanussib5b87312010-11-10 08:16:51 -06003232static bool is_top_script(const char *script_path)
3233{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003234 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003235}
3236
3237static int has_required_arg(char *script_path)
3238{
3239 struct script_desc *desc;
3240 int n_args = 0;
3241 char *p;
3242
3243 desc = script_desc__new(NULL);
3244
3245 if (read_script_info(desc, script_path))
3246 goto out;
3247
3248 if (!desc->args)
3249 goto out;
3250
3251 for (p = desc->args; *p; p++)
3252 if (*p == '<')
3253 n_args++;
3254out:
3255 script_desc__delete(desc);
3256
3257 return n_args;
3258}
3259
David Ahernd54b1a92012-08-26 12:24:46 -06003260static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003261{
3262 char **__argv = malloc(sizeof(const char *) * argc);
3263
David Ahernd54b1a92012-08-26 12:24:46 -06003264 if (!__argv) {
3265 pr_err("malloc failed\n");
3266 return -1;
3267 }
3268
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003269 memcpy(__argv, argv, sizeof(const char *) * argc);
3270 argc = parse_options(argc, (const char **)__argv, record_options,
3271 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3272 free(__argv);
3273
David Ahernd54b1a92012-08-26 12:24:46 -06003274 system_wide = (argc == 0);
3275
3276 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003277}
3278
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003279static void script__setup_sample_type(struct perf_script *script)
3280{
3281 struct perf_session *session = script->session;
3282 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3283
3284 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3285 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003286 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003287 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003288 dwarf_callchain_users = true;
3289 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003290 callchain_param.record_mode = CALLCHAIN_LBR;
3291 else
3292 callchain_param.record_mode = CALLCHAIN_FP;
3293 }
3294}
3295
Jiri Olsa89f16882018-09-13 14:54:03 +02003296static int process_stat_round_event(struct perf_session *session,
3297 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003298{
Jiri Olsa72932372019-08-28 15:57:16 +02003299 struct perf_record_stat_round *round = &event->stat_round;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003300 struct evsel *counter;
Jiri Olsae099eba2016-01-05 22:09:09 +01003301
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003302 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003303 perf_stat_process_counter(&stat_config, counter);
3304 process_stat(counter, round->time);
3305 }
3306
3307 process_stat_interval(round->time);
3308 return 0;
3309}
3310
Jiri Olsa89f16882018-09-13 14:54:03 +02003311static int process_stat_config_event(struct perf_session *session __maybe_unused,
3312 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003313{
3314 perf_event__read_stat_config(&stat_config, &event->stat_config);
3315 return 0;
3316}
3317
Jiri Olsacfc88742016-01-05 22:09:06 +01003318static int set_maps(struct perf_script *script)
3319{
Jiri Olsa63503db2019-07-21 13:23:52 +02003320 struct evlist *evlist = script->session->evlist;
Jiri Olsacfc88742016-01-05 22:09:06 +01003321
3322 if (!script->cpus || !script->threads)
3323 return 0;
3324
3325 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3326 return -EINVAL;
3327
Jiri Olsa453fa032019-07-21 13:24:43 +02003328 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
Jiri Olsacfc88742016-01-05 22:09:06 +01003329
3330 if (perf_evlist__alloc_stats(evlist, true))
3331 return -ENOMEM;
3332
3333 script->allocated = true;
3334 return 0;
3335}
3336
3337static
Jiri Olsa89f16882018-09-13 14:54:03 +02003338int process_thread_map_event(struct perf_session *session,
3339 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003340{
Jiri Olsa89f16882018-09-13 14:54:03 +02003341 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003342 struct perf_script *script = container_of(tool, struct perf_script, tool);
3343
3344 if (script->threads) {
3345 pr_warning("Extra thread map event, ignoring.\n");
3346 return 0;
3347 }
3348
3349 script->threads = thread_map__new_event(&event->thread_map);
3350 if (!script->threads)
3351 return -ENOMEM;
3352
3353 return set_maps(script);
3354}
3355
3356static
Jiri Olsa89f16882018-09-13 14:54:03 +02003357int process_cpu_map_event(struct perf_session *session,
3358 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003359{
Jiri Olsa89f16882018-09-13 14:54:03 +02003360 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003361 struct perf_script *script = container_of(tool, struct perf_script, tool);
3362
3363 if (script->cpus) {
3364 pr_warning("Extra cpu map event, ignoring.\n");
3365 return 0;
3366 }
3367
3368 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3369 if (!script->cpus)
3370 return -ENOMEM;
3371
3372 return set_maps(script);
3373}
3374
Jiri Olsa89f16882018-09-13 14:54:03 +02003375static int process_feature_event(struct perf_session *session,
3376 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303377{
3378 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003379 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303380 return 0;
3381}
3382
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003383#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003384static int perf_script__process_auxtrace_info(struct perf_session *session,
3385 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003386{
Jiri Olsa89f16882018-09-13 14:54:03 +02003387 struct perf_tool *tool = session->tool;
3388
3389 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003390
3391 if (ret == 0) {
3392 struct perf_script *script = container_of(tool, struct perf_script, tool);
3393
3394 ret = perf_script__setup_per_event_dump(script);
3395 }
3396
3397 return ret;
3398}
3399#else
3400#define perf_script__process_auxtrace_info 0
3401#endif
3402
Andi Kleenb585ebd2018-09-20 11:05:36 -07003403static int parse_insn_trace(const struct option *opt __maybe_unused,
3404 const char *str __maybe_unused,
3405 int unset __maybe_unused)
3406{
3407 parse_output_fields(NULL, "+insn,-event,-period", 0);
3408 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003409 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003410 return 0;
3411}
3412
3413static int parse_xed(const struct option *opt __maybe_unused,
3414 const char *str __maybe_unused,
3415 int unset __maybe_unused)
3416{
3417 force_pager("xed -F insn: -A -64 | less");
3418 return 0;
3419}
3420
Andi Kleend1b15522018-09-20 11:05:38 -07003421static int parse_call_trace(const struct option *opt __maybe_unused,
3422 const char *str __maybe_unused,
3423 int unset __maybe_unused)
3424{
3425 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3426 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003427 symbol_conf.nanosecs = true;
Jiri Olsa1c492422019-05-08 15:20:05 +02003428 symbol_conf.pad_output_len_dso = 50;
Andi Kleend1b15522018-09-20 11:05:38 -07003429 return 0;
3430}
3431
3432static int parse_callret_trace(const struct option *opt __maybe_unused,
3433 const char *str __maybe_unused,
3434 int unset __maybe_unused)
3435{
3436 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3437 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003438 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003439 return 0;
3440}
3441
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003442int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003443{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003444 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003445 bool header = false;
3446 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003447 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003448 char *rec_script_path = NULL;
3449 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003450 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003451 struct itrace_synth_opts itrace_synth_opts = {
3452 .set = false,
3453 .default_no_sample = true,
3454 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003455 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003456 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003457 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003458 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003459 struct perf_script script = {
3460 .tool = {
3461 .sample = process_sample_event,
3462 .mmap = perf_event__process_mmap,
3463 .mmap2 = perf_event__process_mmap2,
3464 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303465 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003466 .exit = perf_event__process_exit,
3467 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003468 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003469 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003470 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303471 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003472 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003473 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003474 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003475 .auxtrace = perf_event__process_auxtrace,
3476 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003477 .stat = perf_event__process_stat_event,
3478 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003479 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003480 .thread_map = process_thread_map_event,
3481 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003482 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003483 .ordering_requires_timestamps = true,
3484 },
3485 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003486 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003487 .mode = PERF_DATA_MODE_READ,
3488 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003489 const struct option options[] = {
3490 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3491 "dump raw trace in ASCII"),
3492 OPT_INCR('v', "verbose", &verbose,
3493 "be more verbose (show symbol address, etc)"),
3494 OPT_BOOLEAN('L', "Latency", &latency_format,
3495 "show latency attributes (irqs/preemption disabled, etc)"),
3496 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3497 list_available_scripts),
3498 OPT_CALLBACK('s', "script", NULL, "name",
3499 "script file name (lang:script name, script name, or *)",
3500 parse_scriptname),
3501 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3502 "generate perf-script.xx script in specified language"),
3503 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3504 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3505 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003506 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3507 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003508 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3509 "file", "vmlinux pathname"),
3510 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3511 "file", "kallsyms pathname"),
3512 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3513 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003514 OPT_CALLBACK(0, "symfs", NULL, "directory",
3515 "Look for files with symbols relative to this directory",
3516 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003517 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003518 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003519 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003520 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003521 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303522 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3523 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
Adrian Hunter68fb45b2019-05-20 14:37:14 +03003524 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003525 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003526 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3527 "system-wide collection from all CPUs"),
3528 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3529 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003530 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3531 "Decode instructions from itrace", parse_insn_trace),
3532 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3533 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003534 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3535 "Decode calls from from itrace", parse_call_trace),
3536 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3537 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003538 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3539 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003540 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3541 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003542 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3543 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3544 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003545 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3546 "only consider symbols in these pids"),
3547 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3548 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003549 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3550 "Set the maximum stack depth when parsing the callchain, "
3551 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003552 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Andi Kleen90b10f42019-03-14 15:50:00 -07003553 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003554 OPT_BOOLEAN('I', "show-info", &show_full_info,
3555 "display extended information from perf.data file"),
3556 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3557 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003558 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3559 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003560 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3561 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003562 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3563 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303564 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3565 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003566 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3567 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003568 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3569 "Show round events (if recorded)"),
Jiri Olsa490c8cc2019-05-08 15:20:08 +02003570 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3571 "Show bpf related events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003572 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3573 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003574 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003575 OPT_INTEGER(0, "max-blocks", &max_blocks,
3576 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003577 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003578 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003579 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003580 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003581 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003582 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3583 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003584 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3585 "Enable symbol demangling"),
3586 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3587 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003588 OPT_STRING(0, "time", &script.time_str, "str",
3589 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003590 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3591 "Show inline function"),
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003592 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3593 "guest mount directory under which every guest os"
3594 " instance has a subdir"),
3595 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3596 "file", "file saving guest os vmlinux"),
3597 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3598 "file", "file saving guest os /proc/kallsyms"),
3599 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3600 "file", "file saving guest os /proc/modules"),
Arnaldo Carvalho de Meloadd3a712019-08-15 11:21:21 -03003601 OPTS_EVSWITCH(&script.evswitch),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003602 OPT_END()
3603 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003604 const char * const script_subcommands[] = { "record", "report", NULL };
3605 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003606 "perf script [<options>]",
3607 "perf script [<options>] record <script> [<record-options>] <command>",
3608 "perf script [<options>] report <script> [script-args]",
3609 "perf script [<options>] <script> [<record-options>] <command>",
3610 "perf script [<options>] <top-script> [script-args]",
3611 NULL
3612 };
Tom Zanussi38752942009-12-15 02:53:39 -06003613
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003614 perf_set_singlethreaded();
3615
Tom Zanussib5b87312010-11-10 08:16:51 -06003616 setup_scripting();
3617
Yunlong Song40cae2b2015-03-18 21:35:54 +08003618 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003619 PARSE_OPT_STOP_AT_NON_OPTION);
3620
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003621 if (symbol_conf.guestmount ||
3622 symbol_conf.default_guest_vmlinux_name ||
3623 symbol_conf.default_guest_kallsyms ||
3624 symbol_conf.default_guest_modules) {
3625 /*
3626 * Enable guest sample processing.
3627 */
3628 perf_guest = true;
3629 }
3630
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003631 data.path = input_name;
3632 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003633
Tom Zanussib5b87312010-11-10 08:16:51 -06003634 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3635 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3636 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003637 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003638 }
3639
Tom Zanussib5b87312010-11-10 08:16:51 -06003640 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3641 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3642 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003643 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003644 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003645 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003646 return -1;
3647 }
Tom Zanussi38752942009-12-15 02:53:39 -06003648 }
3649
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003650 if (itrace_synth_opts.callchain &&
3651 itrace_synth_opts.callchain_sz > scripting_max_stack)
3652 scripting_max_stack = itrace_synth_opts.callchain_sz;
3653
Ben Hutchings44e668c2010-10-10 16:10:03 +01003654 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003655 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003656
Tom Zanussib5b87312010-11-10 08:16:51 -06003657 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003658 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003659 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003660 pid_t pid;
3661
Tom Zanussib5b87312010-11-10 08:16:51 -06003662 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3663 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3664
3665 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003666 usage_with_options_msg(script_usage, options,
3667 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003668 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003669 }
3670
Tom Zanussib5b87312010-11-10 08:16:51 -06003671 if (is_top_script(argv[0])) {
3672 rep_args = argc - 1;
3673 } else {
3674 int rec_args;
3675
3676 rep_args = has_required_arg(rep_script_path);
3677 rec_args = (argc - 1) - rep_args;
3678 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003679 usage_with_options_msg(script_usage, options,
3680 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003681 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003682 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003683 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003684 }
3685
3686 if (pipe(live_pipe) < 0) {
3687 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003688 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003689 }
3690
3691 pid = fork();
3692 if (pid < 0) {
3693 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003694 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003695 }
3696
3697 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003698 j = 0;
3699
Tom Zanussia0cccc22010-04-01 23:59:25 -05003700 dup2(live_pipe[1], 1);
3701 close(live_pipe[0]);
3702
Robert Richter317df652011-11-25 15:05:25 +01003703 if (is_top_script(argv[0])) {
3704 system_wide = true;
3705 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003706 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3707 err = -1;
3708 goto out;
3709 }
Robert Richter317df652011-11-25 15:05:25 +01003710 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003711
3712 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003713 if (!__argv) {
3714 pr_err("malloc failed\n");
3715 err = -ENOMEM;
3716 goto out;
3717 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003718
Tom Zanussib5b87312010-11-10 08:16:51 -06003719 __argv[j++] = "/bin/sh";
3720 __argv[j++] = rec_script_path;
3721 if (system_wide)
3722 __argv[j++] = "-a";
3723 __argv[j++] = "-q";
3724 __argv[j++] = "-o";
3725 __argv[j++] = "-";
3726 for (i = rep_args + 1; i < argc; i++)
3727 __argv[j++] = argv[i];
3728 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003729
3730 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003731 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003732 exit(-1);
3733 }
3734
3735 dup2(live_pipe[0], 0);
3736 close(live_pipe[1]);
3737
Tom Zanussib5b87312010-11-10 08:16:51 -06003738 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003739 if (!__argv) {
3740 pr_err("malloc failed\n");
3741 err = -ENOMEM;
3742 goto out;
3743 }
3744
Tom Zanussib5b87312010-11-10 08:16:51 -06003745 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003746 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003747 __argv[j++] = rep_script_path;
3748 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003749 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003750 __argv[j++] = "-i";
3751 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003752 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003753
3754 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003755 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003756 exit(-1);
3757 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003758
Tom Zanussib5b87312010-11-10 08:16:51 -06003759 if (rec_script_path)
3760 script_path = rec_script_path;
3761 if (rep_script_path)
3762 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003763
Tom Zanussib5b87312010-11-10 08:16:51 -06003764 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003765 j = 0;
3766
Robert Richter317df652011-11-25 15:05:25 +01003767 if (!rec_script_path)
3768 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003769 else if (!system_wide) {
3770 if (have_cmd(argc - 1, &argv[1]) != 0) {
3771 err = -1;
3772 goto out;
3773 }
3774 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003775
3776 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003777 if (!__argv) {
3778 pr_err("malloc failed\n");
3779 err = -ENOMEM;
3780 goto out;
3781 }
3782
Tom Zanussib5b87312010-11-10 08:16:51 -06003783 __argv[j++] = "/bin/sh";
3784 __argv[j++] = script_path;
3785 if (system_wide)
3786 __argv[j++] = "-a";
3787 for (i = 2; i < argc; i++)
3788 __argv[j++] = argv[i];
3789 __argv[j++] = NULL;
3790
3791 execvp("/bin/sh", (char **)__argv);
3792 free(__argv);
3793 exit(-1);
3794 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003795
Milian Wolffc1c9b962018-10-21 21:14:23 +02003796 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003797 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003798 use_browser = 0;
3799 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003800
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003801 session = perf_session__new(&data, false, &script.tool);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05303802 if (IS_ERR(session))
3803 return PTR_ERR(session);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003804
Jiri Olsae90debd2013-12-09 11:02:50 +01003805 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003806 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003807 perf_session__fprintf_info(session, stdout, show_full_info);
3808 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003809 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003810 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003811 if (show_full_info)
3812 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003813
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003814 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003815 goto out_delete;
3816
Andi Kleen3ab481a2019-03-05 06:47:45 -08003817 uname(&uts);
Song Liu9d491692019-06-20 18:44:38 -07003818 if (data.is_pipe || /* assume pipe_mode indicates native_arch */
3819 !strcmp(uts.machine, session->header.env.arch) ||
Andi Kleen3ab481a2019-03-05 06:47:45 -08003820 (!strcmp(uts.machine, "x86_64") &&
3821 !strcmp(session->header.env.arch, "i386")))
3822 native_arch = true;
3823
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003824 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003825 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003826
Andi Kleen99f753f2018-09-20 11:05:39 -07003827 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3828 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003829 itrace_synth_opts.thread_stack = true;
3830
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003831 session->itrace_synth_opts = &itrace_synth_opts;
3832
Anton Blanchard5d67be92011-07-04 21:57:50 +10003833 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003834 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3835 if (err < 0)
3836 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003837 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003838 }
3839
David Ahern1424dc92011-03-09 22:23:28 -07003840 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003841 symbol_conf.use_callchain = true;
3842 else
3843 symbol_conf.use_callchain = false;
3844
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003845 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003846 tep_set_function_resolver(session->tevent.pevent,
3847 machine__resolve_kernel_addr,
3848 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003849 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003850 err = -1;
3851 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003852 }
3853
Tom Zanussi956ffd02009-11-25 01:15:46 -06003854 if (generate_script_lang) {
3855 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003856 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003857
David Ahern2c9e45f72011-03-17 10:03:21 -06003858 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003859 fprintf(stderr,
3860 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003861 err = -EINVAL;
3862 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003863 }
3864
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003865 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003866 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003867 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003868 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003869 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003870 }
3871
3872 err = fstat(input, &perf_stat);
3873 if (err < 0) {
3874 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003875 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003876 }
3877
3878 if (!perf_stat.st_size) {
3879 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003880 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003881 }
3882
3883 scripting_ops = script_spec__lookup(generate_script_lang);
3884 if (!scripting_ops) {
3885 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003886 err = -ENOENT;
3887 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003888 }
3889
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003890 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003891 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003892 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003893 }
3894
3895 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003896 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003897 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003898 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003899 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003900 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003901 }
3902
David Ahern9cbdb702011-04-06 21:54:20 -06003903
3904 err = perf_session__check_output_opt(session);
3905 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003906 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003907
Jin Yao284c4e12019-03-01 18:13:06 +08003908 if (script.time_str) {
Andi Kleenb3509b62019-10-11 11:21:39 -07003909 err = perf_time__parse_for_ranges_reltime(script.time_str, session,
Jin Yao284c4e12019-03-01 18:13:06 +08003910 &script.ptime_range,
3911 &script.range_size,
Andi Kleenb3509b62019-10-11 11:21:39 -07003912 &script.range_num,
3913 reltime);
Jin Yao284c4e12019-03-01 18:13:06 +08003914 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08003915 goto out_delete;
Adrian Hunter400ae982019-06-04 16:00:00 +03003916
3917 itrace_synth_opts__set_time_range(&itrace_synth_opts,
3918 script.ptime_range,
3919 script.range_num);
David Aherna91f4c42016-11-29 10:15:43 -07003920 }
3921
Arnaldo Carvalho de Melo124e02b2019-08-15 11:31:29 -03003922 err = evswitch__init(&script.evswitch, session->evlist, stderr);
3923 if (err)
3924 goto out_delete;
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003925
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003926 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003927
Adrian Hunterd445dd22014-08-15 22:08:37 +03003928 flush_scripting();
3929
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003930out_delete:
Adrian Hunter400ae982019-06-04 16:00:00 +03003931 if (script.ptime_range) {
3932 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
Jin Yao284c4e12019-03-01 18:13:06 +08003933 zfree(&script.ptime_range);
Adrian Hunter400ae982019-06-04 16:00:00 +03003934 }
Jin Yaocc2ef582018-01-10 23:00:33 +08003935
Jiri Olsacfc88742016-01-05 22:09:06 +01003936 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003937 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003938
3939 if (script_started)
3940 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003941out:
3942 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003943}