blob: 37297b67905d6f8a995e12a28c33ea306f0d0812 [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
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02004#include "util/cache.h"
Arnaldo Carvalho de Melo0f31c012019-08-21 14:00:17 -03005#include "util/counts.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02006#include "util/debug.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 Melod2360442019-08-15 10:37:24 -030020#include "util/evswitch.h"
Feng Tang36385be2012-09-07 16:42:24 +080021#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020022#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030023#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010024#include "util/cpumap.h"
25#include "util/thread_map.h"
26#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080027#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030028#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030029#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070030#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010031#include "util/path.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030032#include "print_binary.h"
Andi Kleen3ab481a2019-03-05 06:47:45 -080033#include "archinsn.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100034#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030035#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030036#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030037#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030038#include <linux/zalloc.h>
Andi Kleen3ab481a2019-03-05 06:47:45 -080039#include <sys/utsname.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010040#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010041#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080042#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030043#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030044#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030045#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030046#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030047#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030048#include <sys/types.h>
49#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030050#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030051#include <unistd.h>
Andi Kleenb585ebd2018-09-20 11:05:36 -070052#include <subcmd/pager.h>
Jiri Olsa453fa032019-07-21 13:24:43 +020053#include <perf/evlist.h>
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030054#include "util/record.h"
Arnaldo Carvalho de Melo2da39f12019-08-27 11:51:18 -030055#include "util/util.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020056
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030057#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030058
Tom Zanussi956ffd02009-11-25 01:15:46 -060059static char const *script_name;
60static char const *generate_script_lang;
Andi Kleen90b10f42019-03-14 15:50:00 -070061static bool reltime;
62static u64 initial_time;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020063static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020064static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020065static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070066static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090067static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010068static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030069static bool print_flags;
Anton Blanchard5d67be92011-07-04 21:57:50 +100070static const char *cpu_list;
71static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010072static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080073static int max_blocks;
Andi Kleen3ab481a2019-03-05 06:47:45 -080074static bool native_arch;
Tom Zanussi956ffd02009-11-25 01:15:46 -060075
Adrian Hunter44cbe722015-09-25 16:15:50 +030076unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030077
David Ahern745f43e2011-03-09 22:23:26 -070078enum perf_output_field {
79 PERF_OUTPUT_COMM = 1U << 0,
80 PERF_OUTPUT_TID = 1U << 1,
81 PERF_OUTPUT_PID = 1U << 2,
82 PERF_OUTPUT_TIME = 1U << 3,
83 PERF_OUTPUT_CPU = 1U << 4,
84 PERF_OUTPUT_EVNAME = 1U << 5,
85 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060086 PERF_OUTPUT_IP = 1U << 7,
87 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060088 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060089 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090090 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020091 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020092 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020093 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020094 PERF_OUTPUT_BRSTACK = 1U << 15,
95 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010096 PERF_OUTPUT_DATA_SRC = 1U << 17,
97 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000098 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030099 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +0300100 PERF_OUTPUT_INSN = 1U << 21,
101 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -0800102 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -0700103 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +0300104 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -0400105 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -0700106 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800107 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100108 PERF_OUTPUT_MISC = 1U << 29,
Andi Kleendd2e18e2018-12-03 16:18:48 -0800109 PERF_OUTPUT_SRCCODE = 1U << 30,
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300110 PERF_OUTPUT_IPC = 1U << 31,
David Ahern745f43e2011-03-09 22:23:26 -0700111};
112
113struct output_option {
114 const char *str;
115 enum perf_output_field field;
116} all_output_options[] = {
117 {.str = "comm", .field = PERF_OUTPUT_COMM},
118 {.str = "tid", .field = PERF_OUTPUT_TID},
119 {.str = "pid", .field = PERF_OUTPUT_PID},
120 {.str = "time", .field = PERF_OUTPUT_TIME},
121 {.str = "cpu", .field = PERF_OUTPUT_CPU},
122 {.str = "event", .field = PERF_OUTPUT_EVNAME},
123 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600124 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700125 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600126 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600127 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900128 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200129 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200130 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200131 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700132 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200133 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
134 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100135 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
136 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000137 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300138 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300139 {.str = "insn", .field = PERF_OUTPUT_INSN},
140 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800141 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700142 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300143 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400144 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800145 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100146 {.str = "misc", .field = PERF_OUTPUT_MISC},
Andi Kleendd2e18e2018-12-03 16:18:48 -0800147 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300148 {.str = "ipc", .field = PERF_OUTPUT_IPC},
David Ahern745f43e2011-03-09 22:23:26 -0700149};
150
Adrian Hunter14057202017-06-21 13:17:19 +0300151enum {
152 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
153 OUTPUT_TYPE_MAX
154};
155
David Ahern745f43e2011-03-09 22:23:26 -0700156/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600157static struct {
158 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600159 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400160 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600161 u64 fields;
162 u64 invalid_fields;
Andi Kleen4b6ac812019-02-24 07:37:12 -0800163 u64 user_set_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300164} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700165
David Ahern2c9e45f72011-03-17 10:03:21 -0600166 [PERF_TYPE_HARDWARE] = {
167 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700168
David Ahern2c9e45f72011-03-17 10:03:21 -0600169 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
170 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600171 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530172 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
173 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600174
Wang Nan30372f02016-02-24 11:20:45 +0000175 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600176 },
177
178 [PERF_TYPE_SOFTWARE] = {
179 .user_set = false,
180
181 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
182 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600183 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530184 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
185 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
186 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600187
188 .invalid_fields = PERF_OUTPUT_TRACE,
189 },
190
191 [PERF_TYPE_TRACEPOINT] = {
192 .user_set = false,
193
194 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
195 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000196 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600197 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700198
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530199 [PERF_TYPE_HW_CACHE] = {
200 .user_set = false,
201
202 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
203 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
204 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
205 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
206 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
207
208 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
209 },
210
Arun Sharma0817a6a2011-04-14 10:38:18 -0700211 [PERF_TYPE_RAW] = {
212 .user_set = false,
213
214 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
215 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600216 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530217 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
218 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
219 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
220 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700221
Wang Nan30372f02016-02-24 11:20:45 +0000222 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700223 },
Wang Nan27cfef02015-12-08 02:25:43 +0000224
225 [PERF_TYPE_BREAKPOINT] = {
226 .user_set = false,
227
228 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
229 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
230 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530231 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
232 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000233
Wang Nan30372f02016-02-24 11:20:45 +0000234 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000235 },
Adrian Hunter14057202017-06-21 13:17:19 +0300236
237 [OUTPUT_TYPE_SYNTH] = {
238 .user_set = false,
239
240 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
241 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
242 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530243 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
244 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300245
246 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
247 },
David Ahern1424dc92011-03-09 22:23:28 -0700248};
David Ahern745f43e2011-03-09 22:23:26 -0700249
Jiri Olsa32dcd022019-07-21 13:23:51 +0200250struct evsel_script {
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300251 char *filename;
252 FILE *fp;
253 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800254 /* For metric output */
255 u64 val;
256 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300257};
258
Jiri Olsa32dcd022019-07-21 13:23:51 +0200259static inline struct evsel_script *evsel_script(struct evsel *evsel)
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800260{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200261 return (struct evsel_script *)evsel->priv;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800262}
263
Jiri Olsa32dcd022019-07-21 13:23:51 +0200264static struct evsel_script *perf_evsel_script__new(struct evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100265 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300266{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200267 struct evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300268
269 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100270 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300271 goto out_free;
272 es->fp = fopen(es->filename, "w");
273 if (es->fp == NULL)
274 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300275 }
276
277 return es;
278out_free_filename:
279 zfree(&es->filename);
280out_free:
281 free(es);
282 return NULL;
283}
284
Jiri Olsa32dcd022019-07-21 13:23:51 +0200285static void perf_evsel_script__delete(struct evsel_script *es)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300286{
287 zfree(&es->filename);
288 fclose(es->fp);
289 es->fp = NULL;
290 free(es);
291}
292
Jiri Olsa32dcd022019-07-21 13:23:51 +0200293static int perf_evsel_script__fprintf(struct evsel_script *es, FILE *fp)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300294{
295 struct stat st;
296
297 fstat(fileno(es->fp), &st);
298 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
299 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
300}
301
Adrian Hunter14057202017-06-21 13:17:19 +0300302static inline int output_type(unsigned int type)
303{
304 switch (type) {
305 case PERF_TYPE_SYNTH:
306 return OUTPUT_TYPE_SYNTH;
307 default:
308 return type;
309 }
310}
311
312static inline unsigned int attr_type(unsigned int type)
313{
314 switch (type) {
315 case OUTPUT_TYPE_SYNTH:
316 return PERF_TYPE_SYNTH;
317 default:
318 return type;
319 }
320}
321
David Ahern2c9e45f72011-03-17 10:03:21 -0600322static bool output_set_by_user(void)
323{
324 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300325 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600326 if (output[j].user_set)
327 return true;
328 }
329 return false;
330}
David Ahern745f43e2011-03-09 22:23:26 -0700331
David Ahern9cbdb702011-04-06 21:54:20 -0600332static const char *output_field2str(enum perf_output_field field)
333{
334 int i, imax = ARRAY_SIZE(all_output_options);
335 const char *str = "";
336
337 for (i = 0; i < imax; ++i) {
338 if (all_output_options[i].field == field) {
339 str = all_output_options[i].str;
340 break;
341 }
342 }
343 return str;
344}
345
Adrian Hunter14057202017-06-21 13:17:19 +0300346#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700347
Jiri Olsa32dcd022019-07-21 13:23:51 +0200348static int perf_evsel__do_check_stype(struct evsel *evsel,
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300349 u64 sample_type, const char *sample_msg,
350 enum perf_output_field field,
351 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700352{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200353 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300354 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600355 const char *evname;
356
357 if (attr->sample_type & sample_type)
358 return 0;
359
Andi Kleen4b6ac812019-02-24 07:37:12 -0800360 if (output[type].user_set_fields & field) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300361 if (allow_user_set)
362 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300363 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600364 pr_err("Samples for '%s' event do not have %s attribute set. "
365 "Cannot print '%s' field.\n",
366 evname, sample_msg, output_field2str(field));
367 return -1;
368 }
369
370 /* user did not ask for it explicitly so remove from the default list */
371 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300372 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600373 pr_debug("Samples for '%s' event do not have %s attribute set. "
374 "Skipping '%s' field.\n",
375 evname, sample_msg, output_field2str(field));
376
377 return 0;
378}
379
Jiri Olsa32dcd022019-07-21 13:23:51 +0200380static int perf_evsel__check_stype(struct evsel *evsel,
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300381 u64 sample_type, const char *sample_msg,
382 enum perf_output_field field)
383{
384 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
385 false);
386}
387
Jiri Olsa32dcd022019-07-21 13:23:51 +0200388static int perf_evsel__check_attr(struct evsel *evsel,
David Ahern9cbdb702011-04-06 21:54:20 -0600389 struct perf_session *session)
390{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200391 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300392 bool allow_user_set;
393
Jiri Olsae099eba2016-01-05 22:09:09 +0100394 if (perf_header__has_feat(&session->header, HEADER_STAT))
395 return 0;
396
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300397 allow_user_set = perf_header__has_feat(&session->header,
398 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600399
David Ahern1424dc92011-03-09 22:23:28 -0700400 if (PRINT_FIELD(TRACE) &&
401 !perf_session__has_traces(session, "record -R"))
402 return -EINVAL;
403
David Ahern787bef12011-05-27 14:28:43 -0600404 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300405 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
406 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700407 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700408 }
David Ahern7cec0922011-05-30 13:08:23 -0600409
410 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300411 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
412 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600413 return -EINVAL;
414
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100415 if (PRINT_FIELD(DATA_SRC) &&
416 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
417 PERF_OUTPUT_DATA_SRC))
418 return -EINVAL;
419
420 if (PRINT_FIELD(WEIGHT) &&
421 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
422 PERF_OUTPUT_WEIGHT))
423 return -EINVAL;
424
Andi Kleen37fed3d2018-09-18 05:32:09 -0700425 if (PRINT_FIELD(SYM) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200426 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600427 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700428 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600429 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600430 return -EINVAL;
431 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900432 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
433 pr_err("Display of offsets requested but symbol is not"
434 "selected.\n");
435 return -EINVAL;
436 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700437 if (PRINT_FIELD(DSO) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200438 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
Andi Kleen37fed3d2018-09-18 05:32:09 -0700439 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600440 return -EINVAL;
441 }
Andi Kleendd2e18e2018-12-03 16:18:48 -0800442 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
Adrian Huntercc8fae12013-12-06 09:42:57 +0200443 pr_err("Display of source line number requested but sample IP is not\n"
444 "selected. Hence, no address to lookup the source line number.\n");
445 return -EINVAL;
446 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800447 if (PRINT_FIELD(BRSTACKINSN) &&
448 !(perf_evlist__combined_branch_type(session->evlist) &
449 PERF_SAMPLE_BRANCH_ANY)) {
450 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
451 "Hint: run 'perf record -b ...'\n");
452 return -EINVAL;
453 }
David Ahern1424dc92011-03-09 22:23:28 -0700454 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300455 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
456 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700457 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700458
459 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300460 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
461 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700462 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700463
464 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300465 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
466 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700467 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600468
Stephane Eranianfc36f942015-08-31 18:41:10 +0200469 if (PRINT_FIELD(IREGS) &&
470 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
471 PERF_OUTPUT_IREGS))
472 return -EINVAL;
473
Andi Kleenb1491ac2017-09-05 11:40:57 -0700474 if (PRINT_FIELD(UREGS) &&
475 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
476 PERF_OUTPUT_UREGS))
477 return -EINVAL;
478
Kan Liang49d58f02017-08-29 13:11:11 -0400479 if (PRINT_FIELD(PHYS_ADDR) &&
480 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
481 PERF_OUTPUT_PHYS_ADDR))
482 return -EINVAL;
483
David Ahern9cbdb702011-04-06 21:54:20 -0600484 return 0;
485}
486
Adrian Hunter7ea95722013-11-01 15:51:30 +0200487static void set_print_ip_opts(struct perf_event_attr *attr)
488{
Adrian Hunter14057202017-06-21 13:17:19 +0300489 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200490
491 output[type].print_ip_opts = 0;
492 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300493 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200494
495 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300496 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200497
498 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300499 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200500
501 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300502 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200503
504 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300505 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200506}
507
David Ahern9cbdb702011-04-06 21:54:20 -0600508/*
509 * verify all user requested events exist and the samples
510 * have the expected data
511 */
512static int perf_session__check_output_opt(struct perf_session *session)
513{
He Kuang40f20e52016-05-16 04:51:19 +0000514 unsigned int j;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200515 struct evsel *evsel;
David Ahern9cbdb702011-04-06 21:54:20 -0600516
Adrian Hunter14057202017-06-21 13:17:19 +0300517 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
518 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600519
520 /*
521 * even if fields is set to 0 (ie., show nothing) event must
522 * exist if user explicitly includes it on the command line
523 */
Adrian Hunter14057202017-06-21 13:17:19 +0300524 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
525 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600526 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300527 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600528 event_type(j));
529 return -1;
530 }
531
532 if (evsel && output[j].fields &&
533 perf_evsel__check_attr(evsel, session))
534 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400535
536 if (evsel == NULL)
537 continue;
538
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200539 set_print_ip_opts(&evsel->core.attr);
David Ahern1424dc92011-03-09 22:23:28 -0700540 }
541
Adrian Hunter98526ee2014-07-31 09:00:59 +0300542 if (!no_callchain) {
543 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000544 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300545
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300546 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000547 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300548 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300549 use_callchain = true;
550 break;
551 }
552 }
He Kuang71ac8992016-08-04 11:25:43 +0000553 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300554 symbol_conf.use_callchain = false;
555 }
556
David Ahern80b8b492013-11-19 21:07:37 -0700557 /*
558 * set default for tracepoints to print symbols only
559 * if callchains are present
560 */
561 if (symbol_conf.use_callchain &&
562 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700563 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700564
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300565 evlist__for_each_entry(session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200566 if (evsel->core.attr.type != j)
He Kuang40f20e52016-05-16 04:51:19 +0000567 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700568
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300569 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000570 output[j].fields |= PERF_OUTPUT_IP;
571 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530572 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000573 output[j].fields |= PERF_OUTPUT_DSO;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200574 set_print_ip_opts(&evsel->core.attr);
He Kuang40f20e52016-05-16 04:51:19 +0000575 goto out;
576 }
David Ahern80b8b492013-11-19 21:07:37 -0700577 }
578 }
579
580out:
David Ahern1424dc92011-03-09 22:23:28 -0700581 return 0;
582}
David Ahern745f43e2011-03-09 22:23:26 -0700583
Milian Wolff9add8fe2018-11-07 23:34:37 +0100584static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
585 FILE *fp
586)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200587{
Andi Kleenb1491ac2017-09-05 11:40:57 -0700588 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300589 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700590
591 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300592 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700593
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300594 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700595
596 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
597 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300598 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700599 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300600
Milian Wolffb07d16f2018-11-07 10:37:05 +0100601 fprintf(fp, "\n");
602
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300603 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700604}
605
Milian Wolff9add8fe2018-11-07 23:34:37 +0100606static int perf_sample__fprintf_iregs(struct perf_sample *sample,
607 struct perf_event_attr *attr, FILE *fp)
608{
609 return perf_sample__fprintf_regs(&sample->intr_regs,
610 attr->sample_regs_intr, fp);
611}
612
613static int perf_sample__fprintf_uregs(struct perf_sample *sample,
614 struct perf_event_attr *attr, FILE *fp)
615{
616 return perf_sample__fprintf_regs(&sample->user_regs,
617 attr->sample_regs_user, fp);
618}
619
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300620static int perf_sample__fprintf_start(struct perf_sample *sample,
621 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200622 struct evsel *evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100623 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700624{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200625 struct perf_event_attr *attr = &evsel->core.attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700626 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700627 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300628 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700629
David Ahern745f43e2011-03-09 22:23:26 -0700630 if (PRINT_FIELD(COMM)) {
631 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300632 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300633 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300634 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700635 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300636 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700637 }
David Ahernc70c94b2011-03-09 22:23:25 -0700638
David Ahern745f43e2011-03-09 22:23:26 -0700639 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300640 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700641 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300642 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700643 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300644 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700645
David Ahern745f43e2011-03-09 22:23:26 -0700646 if (PRINT_FIELD(CPU)) {
647 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300648 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700649 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300650 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700651 }
David Ahernc70c94b2011-03-09 22:23:25 -0700652
Jiri Olsa28a0b392018-01-07 17:03:52 +0100653 if (PRINT_FIELD(MISC)) {
654 int ret = 0;
655
656 #define has(m) \
657 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
658
659 if (has(KERNEL))
660 ret += fprintf(fp, "K");
661 if (has(USER))
662 ret += fprintf(fp, "U");
663 if (has(HYPERVISOR))
664 ret += fprintf(fp, "H");
665 if (has(GUEST_KERNEL))
666 ret += fprintf(fp, "G");
667 if (has(GUEST_USER))
668 ret += fprintf(fp, "g");
669
670 switch (type) {
671 case PERF_RECORD_MMAP:
672 case PERF_RECORD_MMAP2:
673 if (has(MMAP_DATA))
674 ret += fprintf(fp, "M");
675 break;
676 case PERF_RECORD_COMM:
677 if (has(COMM_EXEC))
678 ret += fprintf(fp, "E");
679 break;
680 case PERF_RECORD_SWITCH:
681 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300682 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100683 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300684 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
685 ret += fprintf(fp, "p");
686 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100687 default:
688 break;
689 }
690
691 #undef has
692
693 ret += fprintf(fp, "%*s", 6 - ret, " ");
694 printed += ret;
695 }
696
David Ahern745f43e2011-03-09 22:23:26 -0700697 if (PRINT_FIELD(TIME)) {
Andi Kleen90b10f42019-03-14 15:50:00 -0700698 u64 t = sample->time;
699 if (reltime) {
700 if (!initial_time)
701 initial_time = sample->time;
702 t = sample->time - initial_time;
703 }
704 nsecs = t;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300705 secs = nsecs / NSEC_PER_SEC;
706 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900707
Andi Kleen52bab882019-03-05 06:47:47 -0800708 if (symbol_conf.nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300709 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900710 else {
711 char sample_time[32];
Andi Kleen90b10f42019-03-14 15:50:00 -0700712 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300713 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900714 }
David Ahern745f43e2011-03-09 22:23:26 -0700715 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300716
717 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700718}
719
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200720static inline char
721mispred_str(struct branch_entry *br)
722{
723 if (!(br->flags.mispred || br->flags.predicted))
724 return '-';
725
726 return br->flags.predicted ? 'P' : 'M';
727}
728
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300729static int perf_sample__fprintf_brstack(struct perf_sample *sample,
730 struct thread *thread,
731 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200732{
733 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700734 struct addr_location alf, alt;
735 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300736 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200737
738 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300739 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200740
741 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700742 from = br->entries[i].from;
743 to = br->entries[i].to;
744
745 if (PRINT_FIELD(DSO)) {
746 memset(&alf, 0, sizeof(alf));
747 memset(&alt, 0, sizeof(alt));
Adrian Hunter692d0e62018-11-06 23:07:12 +0200748 thread__find_map_fb(thread, sample->cpumode, from, &alf);
749 thread__find_map_fb(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700750 }
751
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300752 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700753 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300754 printed += fprintf(fp, "(");
755 printed += map__fprintf_dsoname(alf.map, fp);
756 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700757 }
758
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300759 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700760 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300761 printed += fprintf(fp, "(");
762 printed += map__fprintf_dsoname(alt.map, fp);
763 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700764 }
765
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300766 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200767 mispred_str( br->entries + i),
768 br->entries[i].flags.in_tx? 'X' : '-',
769 br->entries[i].flags.abort? 'A' : '-',
770 br->entries[i].flags.cycles);
771 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300772
773 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200774}
775
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300776static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
777 struct thread *thread,
778 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200779{
780 struct branch_stack *br = sample->branch_stack;
781 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200782 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300783 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200784
785 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300786 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200787
788 for (i = 0; i < br->nr; i++) {
789
790 memset(&alf, 0, sizeof(alf));
791 memset(&alt, 0, sizeof(alt));
792 from = br->entries[i].from;
793 to = br->entries[i].to;
794
Adrian Hunter692d0e62018-11-06 23:07:12 +0200795 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
796 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200797
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300798 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700799 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300800 printed += fprintf(fp, "(");
801 printed += map__fprintf_dsoname(alf.map, fp);
802 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700803 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300804 printed += fprintf(fp, "%c", '/');
805 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700806 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300807 printed += fprintf(fp, "(");
808 printed += map__fprintf_dsoname(alt.map, fp);
809 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700810 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300811 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200812 mispred_str( br->entries + i),
813 br->entries[i].flags.in_tx? 'X' : '-',
814 br->entries[i].flags.abort? 'A' : '-',
815 br->entries[i].flags.cycles);
816 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300817
818 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200819}
820
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300821static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
822 struct thread *thread,
823 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700824{
825 struct branch_stack *br = sample->branch_stack;
826 struct addr_location alf, alt;
827 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300828 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700829
830 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300831 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700832
833 for (i = 0; i < br->nr; i++) {
834
835 memset(&alf, 0, sizeof(alf));
836 memset(&alt, 0, sizeof(alt));
837 from = br->entries[i].from;
838 to = br->entries[i].to;
839
Adrian Hunter692d0e62018-11-06 23:07:12 +0200840 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300841 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700842 from = map__map_ip(alf.map, from);
843
Adrian Hunter692d0e62018-11-06 23:07:12 +0200844 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300845 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700846 to = map__map_ip(alt.map, to);
847
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300848 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700849 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300850 printed += fprintf(fp, "(");
851 printed += map__fprintf_dsoname(alf.map, fp);
852 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700853 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300854 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700855 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300856 printed += fprintf(fp, "(");
857 printed += map__fprintf_dsoname(alt.map, fp);
858 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700859 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300860 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700861 mispred_str(br->entries + i),
862 br->entries[i].flags.in_tx ? 'X' : '-',
863 br->entries[i].flags.abort ? 'A' : '-',
864 br->entries[i].flags.cycles);
865 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300866
867 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700868}
Andi Kleen48d02a12017-02-23 15:46:34 -0800869#define MAXBB 16384UL
870
871static int grab_bb(u8 *buffer, u64 start, u64 end,
872 struct machine *machine, struct thread *thread,
873 bool *is64bit, u8 *cpumode, bool last)
874{
875 long offset, len;
876 struct addr_location al;
877 bool kernel;
878
879 if (!start || !end)
880 return 0;
881
882 kernel = machine__kernel_ip(machine, start);
883 if (kernel)
884 *cpumode = PERF_RECORD_MISC_KERNEL;
885 else
886 *cpumode = PERF_RECORD_MISC_USER;
887
888 /*
889 * Block overlaps between kernel and user.
890 * This can happen due to ring filtering
891 * On Intel CPUs the entry into the kernel is filtered,
892 * but the exit is not. Let the caller patch it up.
893 */
894 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300895 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800896 return -ENXIO;
897 }
898
899 memset(&al, 0, sizeof(al));
900 if (end - start > MAXBB - MAXINSN) {
901 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300902 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800903 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300904 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800905 return 0;
906 }
907
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300908 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300909 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800910 return 0;
911 }
912 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
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
917 /* Load maps to ensure dso->is_64_bit has been updated */
918 map__load(al.map);
919
920 offset = al.map->map_ip(al.map, start);
921 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
922 end - start + MAXINSN);
923
924 *is64bit = al.map->dso->is_64_bit;
925 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300926 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800927 start, end);
928 return len;
929}
930
Andi Kleendd2e18e2018-12-03 16:18:48 -0800931static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
932{
933 struct addr_location al;
934 int ret = 0;
935
936 memset(&al, 0, sizeof(al));
937 thread__find_map(thread, cpumode, addr, &al);
938 if (!al.map)
939 return 0;
940 ret = map__fprintf_srccode(al.map, al.addr, stdout,
941 &thread->srccode_state);
942 if (ret)
943 ret += printf("\n");
944 return ret;
945}
946
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300947static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
948 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -0700949 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -0800950{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300951 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
952 dump_insn(x, ip, inbuf, len, NULL),
953 en->flags.predicted ? " PRED" : "",
954 en->flags.mispred ? " MISPRED" : "",
955 en->flags.in_tx ? " INTX" : "",
956 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800957 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -0700958 *total_cycles += en->flags.cycles;
959 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800960 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300961 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800962 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300963 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800964}
965
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300966static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
967 u8 cpumode, int cpu, struct symbol **lastsym,
968 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800969{
970 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300971 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800972
973 memset(&al, 0, sizeof(al));
974
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300975 thread__find_map(thread, cpumode, addr, &al);
976
Andi Kleen48d02a12017-02-23 15:46:34 -0800977 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300978 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800979
980 al.cpu = cpu;
981 al.sym = NULL;
982 if (al.map)
983 al.sym = map__find_symbol(al.map, al.addr);
984
985 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300986 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800987
988 if (al.addr < al.sym->end)
989 off = al.addr - al.sym->start;
990 else
991 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300992 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800993 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300994 printed += fprintf(fp, "%+d", off);
995 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800996 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300997 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
998 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800999 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001000
1001 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001002}
1003
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001004static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1005 struct thread *thread,
1006 struct perf_event_attr *attr,
1007 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -08001008{
1009 struct branch_stack *br = sample->branch_stack;
1010 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001011 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001012 struct perf_insn x;
1013 u8 buffer[MAXBB];
1014 unsigned off;
1015 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -07001016 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001017
1018 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001019 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001020 nr = br->nr;
1021 if (max_blocks && nr > max_blocks + 1)
1022 nr = max_blocks + 1;
1023
1024 x.thread = thread;
1025 x.cpu = sample->cpu;
1026
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001027 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -08001028
1029 /* Handle first from jump, of which we don't know the entry. */
1030 len = grab_bb(buffer, br->entries[nr-1].from,
1031 br->entries[nr-1].from,
1032 machine, thread, &x.is64bit, &x.cpumode, false);
1033 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001034 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1035 x.cpumode, x.cpu, &lastsym, attr, fp);
1036 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001037 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001038 if (PRINT_FIELD(SRCCODE))
1039 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
Andi Kleen48d02a12017-02-23 15:46:34 -08001040 }
1041
1042 /* Print all blocks */
1043 for (i = nr - 2; i >= 0; i--) {
1044 if (br->entries[i].from || br->entries[i].to)
1045 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1046 br->entries[i].from,
1047 br->entries[i].to);
1048 start = br->entries[i + 1].to;
1049 end = br->entries[i].from;
1050
1051 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1052 /* Patch up missing kernel transfers due to ring filters */
1053 if (len == -ENXIO && i > 0) {
1054 end = br->entries[--i].from;
1055 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1056 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1057 }
1058 if (len <= 0)
1059 continue;
1060
1061 insn = 0;
1062 for (off = 0;; off += ilen) {
1063 uint64_t ip = start + off;
1064
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001065 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001066 if (ip == end) {
Andi Kleendde4e732019-07-11 11:19:21 -07001067 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, ++insn, fp,
Andi Kleenfe571202018-09-24 10:07:32 -07001068 &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001069 if (PRINT_FIELD(SRCCODE))
1070 printed += print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001071 break;
1072 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001073 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1074 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001075 if (ilen == 0)
1076 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001077 if (PRINT_FIELD(SRCCODE))
1078 print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001079 insn++;
1080 }
1081 }
1082 }
1083
1084 /*
1085 * Hit the branch? In this case we are already done, and the target
1086 * has not been executed yet.
1087 */
1088 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001089 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001090 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001091 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001092
1093 /*
1094 * Print final block upto sample
Andi Kleen61f61152018-11-19 21:06:17 -08001095 *
1096 * Due to pipeline delays the LBRs might be missing a branch
1097 * or two, which can result in very large or negative blocks
1098 * between final branch and sample. When this happens just
1099 * continue walking after the last TO until we hit a branch.
Andi Kleen48d02a12017-02-23 15:46:34 -08001100 */
1101 start = br->entries[0].to;
1102 end = sample->ip;
Andi Kleen61f61152018-11-19 21:06:17 -08001103 if (end < start) {
1104 /* Missing jump. Scan 128 bytes for the next branch */
1105 end = start + 128;
1106 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001107 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001108 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001109 if (len <= 0) {
1110 /* Print at least last IP if basic block did not work */
1111 len = grab_bb(buffer, sample->ip, sample->ip,
1112 machine, thread, &x.is64bit, &x.cpumode, false);
1113 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001114 goto out;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001115 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001116 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001117 if (PRINT_FIELD(SRCCODE))
1118 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001119 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001120 }
1121 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001122 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1123 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001124 if (ilen == 0)
1125 break;
Andi Kleen61f61152018-11-19 21:06:17 -08001126 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1127 /*
1128 * Hit a missing branch. Just stop.
1129 */
1130 printed += fprintf(fp, "\t... not reaching sample ...\n");
1131 break;
1132 }
Andi Kleendd2e18e2018-12-03 16:18:48 -08001133 if (PRINT_FIELD(SRCCODE))
1134 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001135 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001136out:
1137 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001138}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001139
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001140static int perf_sample__fprintf_addr(struct perf_sample *sample,
1141 struct thread *thread,
1142 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001143{
1144 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001145 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001146
1147 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001148 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001149
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001150 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001151
1152 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001153 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001154 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001155 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001156 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001157 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001158 }
1159
1160 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001161 printed += fprintf(fp, " (");
1162 printed += map__fprintf_dsoname(al.map, fp);
1163 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001164 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001165out:
1166 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001167}
1168
Andi Kleen99f753f2018-09-20 11:05:39 -07001169static const char *resolve_branch_sym(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001170 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001171 struct thread *thread,
1172 struct addr_location *al,
1173 u64 *ip)
1174{
1175 struct addr_location addr_al;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001176 struct perf_event_attr *attr = &evsel->core.attr;
Andi Kleen99f753f2018-09-20 11:05:39 -07001177 const char *name = NULL;
1178
1179 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1180 if (sample_addr_correlates_sym(attr)) {
1181 thread__resolve(thread, &addr_al, sample);
1182 if (addr_al.sym)
1183 name = addr_al.sym->name;
1184 else
1185 *ip = sample->addr;
1186 } else {
1187 *ip = sample->addr;
1188 }
1189 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1190 if (al->sym)
1191 name = al->sym->name;
1192 else
1193 *ip = sample->ip;
1194 }
1195 return name;
1196}
1197
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001198static int perf_sample__fprintf_callindent(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001199 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001200 struct thread *thread,
1201 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001202{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001203 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter256d92b2018-12-21 14:06:19 +02001204 size_t depth = thread_stack__depth(thread, sample->cpu);
Adrian Huntere2167082016-06-23 16:40:58 +03001205 const char *name = NULL;
1206 static int spacing;
1207 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001208 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001209 u64 ip = 0;
1210
1211 /*
1212 * The 'return' has already been popped off the stack so the depth has
1213 * to be adjusted to match the 'call'.
1214 */
1215 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1216 depth += 1;
1217
Andi Kleen99f753f2018-09-20 11:05:39 -07001218 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001219
Andi Kleena78cdee2018-09-18 05:32:10 -07001220 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1221 dlen += fprintf(fp, "(");
1222 dlen += map__fprintf_dsoname(al->map, fp);
1223 dlen += fprintf(fp, ")\t");
1224 }
1225
Adrian Huntere2167082016-06-23 16:40:58 +03001226 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001227 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001228 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001229 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001230
1231 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001232 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001233
1234 /*
1235 * Try to keep the output length from changing frequently so that the
1236 * output lines up more nicely.
1237 */
1238 if (len > spacing || (len && len < spacing - 52))
1239 spacing = round_up(len + 4, 32);
1240
1241 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001242 len += fprintf(fp, "%*s", spacing - len, "");
1243
Andi Kleena78cdee2018-09-18 05:32:10 -07001244 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001245}
1246
Andi Kleen3ab481a2019-03-05 06:47:45 -08001247__weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1248 struct thread *thread __maybe_unused,
1249 struct machine *machine __maybe_unused)
1250{
1251}
1252
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001253static int perf_sample__fprintf_insn(struct perf_sample *sample,
1254 struct perf_event_attr *attr,
1255 struct thread *thread,
1256 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001257{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001258 int printed = 0;
1259
Andi Kleen3ab481a2019-03-05 06:47:45 -08001260 if (sample->insn_len == 0 && native_arch)
1261 arch_fetch_insn(sample, thread, machine);
1262
Andi Kleen224e2c92016-10-07 16:42:27 +03001263 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001264 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen3ab481a2019-03-05 06:47:45 -08001265 if (PRINT_FIELD(INSN) && sample->insn_len) {
Andi Kleen224e2c92016-10-07 16:42:27 +03001266 int i;
1267
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001268 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001269 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001270 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001271 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001272 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001273 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1274
1275 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001276}
1277
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001278static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1279 struct perf_event_attr *attr, FILE *fp)
1280{
1281 unsigned int ipc;
1282
1283 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1284 return 0;
1285
1286 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1287
1288 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1289 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1290}
1291
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001292static int perf_sample__fprintf_bts(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001293 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001294 struct thread *thread,
1295 struct addr_location *al,
1296 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001297{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001298 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001299 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001300 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001301 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001302
Adrian Huntere2167082016-06-23 16:40:58 +03001303 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001304 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001305
Akihiro Nagai95582592012-01-30 13:43:09 +09001306 /* print branch_from information */
1307 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001308 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001309 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001310
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001311 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001312 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001313 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001314 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001315
1316 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001317 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001318 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001319 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001320 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001321 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001322 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001323 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001324
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001325 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001326 }
1327
Akihiro Nagai95582592012-01-30 13:43:09 +09001328 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001329 if (PRINT_FIELD(ADDR) ||
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001330 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001331 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001332 printed += fprintf(fp, " => ");
1333 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001334 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001335
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001336 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1337
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001338 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001339 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001340
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001341 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001342 printed += fprintf(fp, "\n");
1343 if (PRINT_FIELD(SRCCODE)) {
1344 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1345 &thread->srccode_state);
1346 if (ret) {
1347 printed += ret;
1348 printed += printf("\n");
1349 }
1350 }
1351 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001352}
1353
Adrian Hunter055cd332016-06-23 16:40:56 +03001354static struct {
1355 u32 flags;
1356 const char *name;
1357} sample_flags[] = {
1358 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1359 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1360 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1361 {PERF_IP_FLAG_BRANCH, "jmp"},
1362 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1363 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1364 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1365 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1366 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1367 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1368 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1369 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1370 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1371 {0, NULL}
1372};
1373
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001374static const char *sample_flags_to_name(u32 flags)
1375{
1376 int i;
1377
1378 for (i = 0; sample_flags[i].name ; i++) {
1379 if (sample_flags[i].flags == flags)
1380 return sample_flags[i].name;
1381 }
1382
1383 return NULL;
1384}
1385
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001386static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001387{
1388 const char *chars = PERF_IP_FLAG_CHARS;
1389 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001390 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1391 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001392 char str[33];
1393 int i, pos = 0;
1394
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001395 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1396 if (name)
1397 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1398
1399 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1400 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1401 if (name)
1402 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1403 }
1404
1405 if (flags & PERF_IP_FLAG_TRACE_END) {
1406 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1407 if (name)
1408 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001409 }
1410
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001411 for (i = 0; i < n; i++, flags >>= 1) {
1412 if (flags & 1)
1413 str[pos++] = chars[i];
1414 }
1415 for (; i < 32; i++, flags >>= 1) {
1416 if (flags & 1)
1417 str[pos++] = '?';
1418 }
1419 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001420
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001421 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001422}
1423
Wang Nan30372f02016-02-24 11:20:45 +00001424struct printer_data {
1425 int line_no;
1426 bool hit_nul;
1427 bool is_printable;
1428};
1429
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001430static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1431 unsigned int val,
1432 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001433{
1434 unsigned char ch = (unsigned char)val;
1435 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001436 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001437
1438 switch (op) {
1439 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001440 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001441 break;
1442 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001443 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001444 " ");
1445 break;
1446 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001447 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001448 break;
1449 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001450 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001451 break;
1452 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001453 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001454 break;
1455 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001456 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001457 break;
1458 case BINARY_PRINT_CHAR_DATA:
1459 if (printer_data->hit_nul && ch)
1460 printer_data->is_printable = false;
1461
1462 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001463 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001464
1465 if (!printer_data->is_printable)
1466 break;
1467
1468 if (ch == '\0')
1469 printer_data->hit_nul = true;
1470 else
1471 printer_data->is_printable = false;
1472 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001473 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001474 }
1475 break;
1476 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001477 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001478 break;
1479 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001480 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001481 printer_data->line_no++;
1482 break;
1483 case BINARY_PRINT_DATA_END:
1484 default:
1485 break;
1486 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001487
1488 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001489}
1490
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001491static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001492{
1493 unsigned int nr_bytes = sample->raw_size;
1494 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001495 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1496 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001497
1498 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001499 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1500
1501 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001502}
1503
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001504static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001505{
1506 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001507 return fprintf(fp, "%*s", spacing - len, "");
1508
1509 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001510}
1511
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001512static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001513{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001514 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001515}
1516
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001517static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001518{
1519 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1520 int len;
1521
1522 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001523 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001524
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001525 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001526 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001527 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001528}
1529
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001530static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001531{
1532 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1533 int len;
1534
1535 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001536 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001537
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001538 len = fprintf(fp, " hints: %#x extensions: %#x ",
1539 data->hints, data->extensions);
1540 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001541}
1542
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001543static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001544{
1545 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1546 int len;
1547
1548 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001549 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001550
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001551 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1552 data->hw, data->cstate, data->subcstate);
1553 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001554}
1555
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001556static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001557{
1558 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1559 int len;
1560
1561 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001562 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001563
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001564 len = fprintf(fp, " IP: %u ", data->ip);
1565 return len + perf_sample__fprintf_pt_spacing(len, 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_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001569{
1570 struct perf_synth_intel_pwrx *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, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001577 data->deepest_cstate, data->last_cstate,
1578 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001579 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001580}
1581
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001582static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001583{
1584 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1585 unsigned int percent, freq;
1586 int len;
1587
1588 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001589 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001590
1591 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001592 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001593 if (data->max_nonturbo) {
1594 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001595 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001596 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001597 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001598}
1599
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001600static int perf_sample__fprintf_synth(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001601 struct evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001602{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001603 switch (evsel->core.attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001604 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001605 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001606 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001607 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001608 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001609 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001610 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001611 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001612 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001613 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001614 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001615 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001616 default:
1617 break;
1618 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001619
1620 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001621}
1622
Jiri Olsa809e9422015-11-26 18:55:21 +01001623struct perf_script {
1624 struct perf_tool tool;
1625 struct perf_session *session;
1626 bool show_task_events;
1627 bool show_mmap_events;
1628 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301629 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001630 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001631 bool show_round_events;
Jiri Olsa490c8cc2019-05-08 15:20:08 +02001632 bool show_bpf_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001633 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001634 bool per_event_dump;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001635 struct evswitch evswitch;
Jiri Olsaf8548392019-07-21 13:23:49 +02001636 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +02001637 struct perf_thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001638 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001639 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001640 struct perf_time_interval *ptime_range;
1641 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001642 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001643};
1644
Jiri Olsa63503db2019-07-21 13:23:52 +02001645static int perf_evlist__max_name_len(struct evlist *evlist)
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001646{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001647 struct evsel *evsel;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001648 int max = 0;
1649
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001650 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001651 int len = strlen(perf_evsel__name(evsel));
1652
1653 max = MAX(len, max);
1654 }
1655
1656 return max;
1657}
1658
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001659static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001660{
1661 struct mem_info mi = { .data_src.val = data_src };
1662 char decode[100];
1663 char out[100];
1664 static int maxlen;
1665 int len;
1666
1667 perf_script__meminfo_scnprintf(decode, 100, &mi);
1668
1669 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1670 if (maxlen < len)
1671 maxlen = len;
1672
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001673 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001674}
1675
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001676struct metric_ctx {
1677 struct perf_sample *sample;
1678 struct thread *thread;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001679 struct evsel *evsel;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001680 FILE *fp;
1681};
1682
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001683static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1684 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001685 const char *fmt,
1686 const char *unit, double val)
1687{
1688 struct metric_ctx *mctx = ctx;
1689
1690 if (!fmt)
1691 return;
1692 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001693 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001694 fputs("\tmetric: ", mctx->fp);
1695 if (color)
1696 color_fprintf(mctx->fp, color, fmt, val);
1697 else
1698 printf(fmt, val);
1699 fprintf(mctx->fp, " %s\n", unit);
1700}
1701
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001702static void script_new_line(struct perf_stat_config *config __maybe_unused,
1703 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001704{
1705 struct metric_ctx *mctx = ctx;
1706
1707 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001708 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001709 fputs("\tmetric: ", mctx->fp);
1710}
1711
1712static void perf_sample__fprint_metric(struct perf_script *script,
1713 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001714 struct evsel *evsel,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001715 struct perf_sample *sample,
1716 FILE *fp)
1717{
1718 struct perf_stat_output_ctx ctx = {
1719 .print_metric = script_print_metric,
1720 .new_line = script_new_line,
1721 .ctx = &(struct metric_ctx) {
1722 .sample = sample,
1723 .thread = thread,
1724 .evsel = evsel,
1725 .fp = fp,
1726 },
1727 .force_header = false,
1728 };
Jiri Olsa32dcd022019-07-21 13:23:51 +02001729 struct evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001730 u64 val;
1731
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001732 if (!evsel->stats)
1733 perf_evlist__alloc_stats(script->session->evlist, false);
1734 if (evsel_script(evsel->leader)->gnum++ == 0)
1735 perf_stat__reset_shadow_stats();
1736 val = sample->period * evsel->scale;
1737 perf_stat__update_shadow_stats(evsel,
1738 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001739 sample->cpu,
1740 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001741 evsel_script(evsel)->val = val;
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001742 if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) {
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001743 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001744 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001745 evsel_script(ev2)->val,
1746 sample->cpu,
1747 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001748 NULL,
1749 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001750 }
1751 evsel_script(evsel->leader)->gnum = 0;
1752 }
1753}
1754
Andi Kleen99f753f2018-09-20 11:05:39 -07001755static bool show_event(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001756 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001757 struct thread *thread,
1758 struct addr_location *al)
1759{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001760 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001761
1762 if (!symbol_conf.graph_function)
1763 return true;
1764
1765 if (thread->filter) {
1766 if (depth <= thread->filter_entry_depth) {
1767 thread->filter = false;
1768 return false;
1769 }
1770 return true;
1771 } else {
1772 const char *s = symbol_conf.graph_function;
1773 u64 ip;
1774 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1775 &ip);
1776 unsigned nlen;
1777
1778 if (!name)
1779 return false;
1780 nlen = strlen(name);
1781 while (*s) {
1782 unsigned len = strcspn(s, ",");
1783 if (nlen == len && !strncmp(name, s, len)) {
1784 thread->filter = true;
1785 thread->filter_entry_depth = depth;
1786 return true;
1787 }
1788 s += len;
1789 if (*s == ',')
1790 s++;
1791 }
1792 return false;
1793 }
1794}
1795
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001796static void process_event(struct perf_script *script,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001797 struct perf_sample *sample, struct evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001798 struct addr_location *al,
1799 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001800{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001801 struct thread *thread = al->thread;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001802 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001803 unsigned int type = output_type(attr->type);
Jiri Olsa32dcd022019-07-21 13:23:51 +02001804 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001805 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001806
Adrian Hunter14057202017-06-21 13:17:19 +03001807 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001808 return;
1809
Andi Kleen99f753f2018-09-20 11:05:39 -07001810 if (!show_event(sample, evsel, thread, al))
1811 return;
1812
Arnaldo Carvalho de Melo8829e562019-08-15 11:00:11 -03001813 if (evswitch__discard(&script->evswitch, evsel))
1814 return;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001815
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001816 ++es->samples;
1817
Jiri Olsa28a0b392018-01-07 17:03:52 +01001818 perf_sample__fprintf_start(sample, thread, evsel,
1819 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001820
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001821 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001822 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001823
Namhyung Kime944d3d2013-11-18 14:34:52 +09001824 if (PRINT_FIELD(EVNAME)) {
1825 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001826
1827 if (!script->name_width)
1828 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1829
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001830 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001831 }
1832
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001833 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001834 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001835
Akihiro Nagai95582592012-01-30 13:43:09 +09001836 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001837 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001838 return;
1839 }
1840
Andi Kleen96167162019-01-17 11:48:34 -08001841 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001842 event_format__fprintf(evsel->tp_format, sample->cpu,
1843 sample->raw_data, sample->raw_size, fp);
1844 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001845
1846 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001847 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001848
David Ahern7cec0922011-05-30 13:08:23 -06001849 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001850 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001851
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001852 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001853 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001854
1855 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001856 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001857
David Ahern787bef12011-05-27 14:28:43 -06001858 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001859 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001860
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001861 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001862 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001863 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001864 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001865
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001866 fputc(cursor ? '\n' : ' ', fp);
1867 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001868 }
1869
Stephane Eranianfc36f942015-08-31 18:41:10 +02001870 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001871 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001872
Andi Kleenb1491ac2017-09-05 11:40:57 -07001873 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001874 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001875
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001876 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001877 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001878 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001879 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001880 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001881 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001882
Wang Nan30372f02016-02-24 11:20:45 +00001883 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001884 perf_sample__fprintf_bpf_output(sample, fp);
1885 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001886
1887 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001888 fprintf(fp, "%16" PRIx64, sample->phys_addr);
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001889
1890 perf_sample__fprintf_ipc(sample, attr, fp);
1891
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001892 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001893
Andi Kleendd2e18e2018-12-03 16:18:48 -08001894 if (PRINT_FIELD(SRCCODE)) {
1895 if (map__fprintf_srccode(al->map, al->addr, stdout,
1896 &thread->srccode_state))
1897 printf("\n");
1898 }
1899
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001900 if (PRINT_FIELD(METRIC))
1901 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001902
1903 if (verbose)
1904 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001905}
1906
Tom Zanussi956ffd02009-11-25 01:15:46 -06001907static struct scripting_ops *scripting_ops;
1908
Jiri Olsa32dcd022019-07-21 13:23:51 +02001909static void __process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsa36e33c52016-01-06 11:49:56 +01001910{
Jiri Olsaa2f354e2019-08-22 13:11:41 +02001911 int nthreads = perf_thread_map__nr(counter->core.threads);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001912 int ncpus = perf_evsel__nr_cpus(counter);
1913 int cpu, thread;
1914 static int header_printed;
1915
1916 if (counter->system_wide)
1917 nthreads = 1;
1918
1919 if (!header_printed) {
1920 printf("%3s %8s %15s %15s %15s %15s %s\n",
1921 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1922 header_printed = 1;
1923 }
1924
1925 for (thread = 0; thread < nthreads; thread++) {
1926 for (cpu = 0; cpu < ncpus; cpu++) {
1927 struct perf_counts_values *counts;
1928
1929 counts = perf_counts(counter->counts, cpu, thread);
1930
1931 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
Jiri Olsad400bd32019-07-21 13:24:37 +02001932 counter->core.cpus->map[cpu],
Jiri Olsaa2f354e2019-08-22 13:11:41 +02001933 perf_thread_map__pid(counter->core.threads, thread),
Jiri Olsa36e33c52016-01-06 11:49:56 +01001934 counts->val,
1935 counts->ena,
1936 counts->run,
1937 tstamp,
1938 perf_evsel__name(counter));
1939 }
1940 }
1941}
1942
Jiri Olsa32dcd022019-07-21 13:23:51 +02001943static void process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsae099eba2016-01-05 22:09:09 +01001944{
1945 if (scripting_ops && scripting_ops->process_stat)
1946 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001947 else
1948 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001949}
1950
1951static void process_stat_interval(u64 tstamp)
1952{
1953 if (scripting_ops && scripting_ops->process_stat_interval)
1954 scripting_ops->process_stat_interval(tstamp);
1955}
1956
Tom Zanussi956ffd02009-11-25 01:15:46 -06001957static void setup_scripting(void)
1958{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001959 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001960 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001961}
1962
Adrian Hunterd445dd22014-08-15 22:08:37 +03001963static int flush_scripting(void)
1964{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001965 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001966}
1967
Tom Zanussi956ffd02009-11-25 01:15:46 -06001968static int cleanup_scripting(void)
1969{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001970 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001971
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001972 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001973}
1974
Andi Kleene87e5482019-03-11 07:44:52 -07001975static bool filter_cpu(struct perf_sample *sample)
1976{
1977 if (cpu_list)
1978 return !test_bit(sample->cpu, cpu_bitmap);
1979 return false;
1980}
1981
Jiri Olsa809e9422015-11-26 18:55:21 +01001982static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001983 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001984 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001985 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001986 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001987{
Jiri Olsa809e9422015-11-26 18:55:21 +01001988 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001989 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001990
Jin Yao2ab046c2017-12-08 21:13:46 +08001991 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1992 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001993 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001994 }
David Aherna91f4c42016-11-29 10:15:43 -07001995
David Ahern1424dc92011-03-09 22:23:28 -07001996 if (debug_mode) {
1997 if (sample->time < last_timestamp) {
1998 pr_err("Samples misordered, previous: %" PRIu64
1999 " this: %" PRIu64 "\n", last_timestamp,
2000 sample->time);
2001 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02002002 }
David Ahern1424dc92011-03-09 22:23:28 -07002003 last_timestamp = sample->time;
2004 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002005 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002006
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03002007 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07002008 pr_err("problem processing %d event, skipping it.\n",
2009 event->header.type);
2010 return -1;
2011 }
2012
2013 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002014 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07002015
Andi Kleene87e5482019-03-11 07:44:52 -07002016 if (filter_cpu(sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002017 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002018
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002019 if (scripting_ops)
2020 scripting_ops->process_event(event, sample, evsel, &al);
2021 else
Andi Kleen48d02a12017-02-23 15:46:34 -08002022 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002023
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002024out_put:
2025 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002026 return 0;
2027}
2028
Adrian Hunter7ea95722013-11-01 15:51:30 +02002029static int process_attr(struct perf_tool *tool, union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02002030 struct evlist **pevlist)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002031{
2032 struct perf_script *scr = container_of(tool, struct perf_script, tool);
Jiri Olsa63503db2019-07-21 13:23:52 +02002033 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002034 struct evsel *evsel, *pos;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002035 int err;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002036 static struct evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002037
2038 err = perf_event__process_attr(tool, event, pevlist);
2039 if (err)
2040 return err;
2041
2042 evlist = *pevlist;
2043 evsel = perf_evlist__last(*pevlist);
2044
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302045 if (!evsel->priv) {
2046 if (scr->per_event_dump) {
2047 evsel->priv = perf_evsel_script__new(evsel,
2048 scr->session->data);
2049 } else {
2050 es = zalloc(sizeof(*es));
2051 if (!es)
2052 return -ENOMEM;
2053 es->fp = stdout;
2054 evsel->priv = es;
2055 }
2056 }
2057
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002058 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2059 evsel->core.attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002060 return 0;
2061
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002062 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002063 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002064 return 0;
2065 }
2066
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002067 set_print_ip_opts(&evsel->core.attr);
Adrian Hunter7ea95722013-11-01 15:51:30 +02002068
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002069 if (evsel->core.attr.sample_type)
Jiri Olsad2b5a312015-10-16 12:41:25 +02002070 err = perf_evsel__check_attr(evsel, scr->session);
2071
2072 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002073}
2074
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002075static int process_comm_event(struct perf_tool *tool,
2076 union perf_event *event,
2077 struct perf_sample *sample,
2078 struct machine *machine)
2079{
2080 struct thread *thread;
2081 struct perf_script *script = container_of(tool, struct perf_script, tool);
2082 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002083 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002084 int ret = -1;
2085
2086 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2087 if (thread == NULL) {
2088 pr_debug("problem processing COMM event, skipping it.\n");
2089 return -1;
2090 }
2091
2092 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2093 goto out;
2094
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002095 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002096 sample->cpu = 0;
2097 sample->time = 0;
2098 sample->tid = event->comm.tid;
2099 sample->pid = event->comm.pid;
2100 }
Andi Kleene87e5482019-03-11 07:44:52 -07002101 if (!filter_cpu(sample)) {
2102 perf_sample__fprintf_start(sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01002103 PERF_RECORD_COMM, stdout);
Andi Kleene87e5482019-03-11 07:44:52 -07002104 perf_event__fprintf(event, stdout);
2105 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002106 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002107out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002108 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002109 return ret;
2110}
2111
Hari Bathini96a44bb2017-03-08 02:12:06 +05302112static int process_namespaces_event(struct perf_tool *tool,
2113 union perf_event *event,
2114 struct perf_sample *sample,
2115 struct machine *machine)
2116{
2117 struct thread *thread;
2118 struct perf_script *script = container_of(tool, struct perf_script, tool);
2119 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002120 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302121 int ret = -1;
2122
2123 thread = machine__findnew_thread(machine, event->namespaces.pid,
2124 event->namespaces.tid);
2125 if (thread == NULL) {
2126 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2127 return -1;
2128 }
2129
2130 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2131 goto out;
2132
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002133 if (!evsel->core.attr.sample_id_all) {
Hari Bathini96a44bb2017-03-08 02:12:06 +05302134 sample->cpu = 0;
2135 sample->time = 0;
2136 sample->tid = event->namespaces.tid;
2137 sample->pid = event->namespaces.pid;
2138 }
Andi Kleene87e5482019-03-11 07:44:52 -07002139 if (!filter_cpu(sample)) {
2140 perf_sample__fprintf_start(sample, thread, evsel,
2141 PERF_RECORD_NAMESPACES, stdout);
2142 perf_event__fprintf(event, stdout);
2143 }
Hari Bathini96a44bb2017-03-08 02:12:06 +05302144 ret = 0;
2145out:
2146 thread__put(thread);
2147 return ret;
2148}
2149
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002150static int process_fork_event(struct perf_tool *tool,
2151 union perf_event *event,
2152 struct perf_sample *sample,
2153 struct machine *machine)
2154{
2155 struct thread *thread;
2156 struct perf_script *script = container_of(tool, struct perf_script, tool);
2157 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002158 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002159
2160 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2161 return -1;
2162
2163 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2164 if (thread == NULL) {
2165 pr_debug("problem processing FORK event, skipping it.\n");
2166 return -1;
2167 }
2168
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002169 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002170 sample->cpu = 0;
2171 sample->time = event->fork.time;
2172 sample->tid = event->fork.tid;
2173 sample->pid = event->fork.pid;
2174 }
Andi Kleene87e5482019-03-11 07:44:52 -07002175 if (!filter_cpu(sample)) {
2176 perf_sample__fprintf_start(sample, thread, evsel,
2177 PERF_RECORD_FORK, stdout);
2178 perf_event__fprintf(event, stdout);
2179 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002180 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002181
2182 return 0;
2183}
2184static int process_exit_event(struct perf_tool *tool,
2185 union perf_event *event,
2186 struct perf_sample *sample,
2187 struct machine *machine)
2188{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002189 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002190 struct thread *thread;
2191 struct perf_script *script = container_of(tool, struct perf_script, tool);
2192 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002193 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002194
2195 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2196 if (thread == NULL) {
2197 pr_debug("problem processing EXIT event, skipping it.\n");
2198 return -1;
2199 }
2200
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002201 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002202 sample->cpu = 0;
2203 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002204 sample->tid = event->fork.tid;
2205 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002206 }
Andi Kleene87e5482019-03-11 07:44:52 -07002207 if (!filter_cpu(sample)) {
2208 perf_sample__fprintf_start(sample, thread, evsel,
2209 PERF_RECORD_EXIT, stdout);
2210 perf_event__fprintf(event, stdout);
2211 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002212
2213 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002214 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002215
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002216 thread__put(thread);
2217 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002218}
2219
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002220static int process_mmap_event(struct perf_tool *tool,
2221 union perf_event *event,
2222 struct perf_sample *sample,
2223 struct machine *machine)
2224{
2225 struct thread *thread;
2226 struct perf_script *script = container_of(tool, struct perf_script, tool);
2227 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002228 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002229
2230 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2231 return -1;
2232
2233 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2234 if (thread == NULL) {
2235 pr_debug("problem processing MMAP event, skipping it.\n");
2236 return -1;
2237 }
2238
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002239 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002240 sample->cpu = 0;
2241 sample->time = 0;
2242 sample->tid = event->mmap.tid;
2243 sample->pid = event->mmap.pid;
2244 }
Andi Kleene87e5482019-03-11 07:44:52 -07002245 if (!filter_cpu(sample)) {
2246 perf_sample__fprintf_start(sample, thread, evsel,
2247 PERF_RECORD_MMAP, stdout);
2248 perf_event__fprintf(event, stdout);
2249 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002250 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002251 return 0;
2252}
2253
2254static int process_mmap2_event(struct perf_tool *tool,
2255 union perf_event *event,
2256 struct perf_sample *sample,
2257 struct machine *machine)
2258{
2259 struct thread *thread;
2260 struct perf_script *script = container_of(tool, struct perf_script, tool);
2261 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002262 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002263
2264 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2265 return -1;
2266
2267 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2268 if (thread == NULL) {
2269 pr_debug("problem processing MMAP2 event, skipping it.\n");
2270 return -1;
2271 }
2272
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002273 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002274 sample->cpu = 0;
2275 sample->time = 0;
2276 sample->tid = event->mmap2.tid;
2277 sample->pid = event->mmap2.pid;
2278 }
Andi Kleene87e5482019-03-11 07:44:52 -07002279 if (!filter_cpu(sample)) {
2280 perf_sample__fprintf_start(sample, thread, evsel,
2281 PERF_RECORD_MMAP2, stdout);
2282 perf_event__fprintf(event, stdout);
2283 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002284 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002285 return 0;
2286}
2287
Adrian Hunter7c148982015-07-21 12:44:06 +03002288static int process_switch_event(struct perf_tool *tool,
2289 union perf_event *event,
2290 struct perf_sample *sample,
2291 struct machine *machine)
2292{
2293 struct thread *thread;
2294 struct perf_script *script = container_of(tool, struct perf_script, tool);
2295 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002296 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Adrian Hunter7c148982015-07-21 12:44:06 +03002297
2298 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2299 return -1;
2300
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002301 if (scripting_ops && scripting_ops->process_switch)
2302 scripting_ops->process_switch(event, sample, machine);
2303
2304 if (!script->show_switch_events)
2305 return 0;
2306
Adrian Hunter7c148982015-07-21 12:44:06 +03002307 thread = machine__findnew_thread(machine, sample->pid,
2308 sample->tid);
2309 if (thread == NULL) {
2310 pr_debug("problem processing SWITCH event, skipping it.\n");
2311 return -1;
2312 }
2313
Andi Kleene87e5482019-03-11 07:44:52 -07002314 if (!filter_cpu(sample)) {
2315 perf_sample__fprintf_start(sample, thread, evsel,
2316 PERF_RECORD_SWITCH, stdout);
2317 perf_event__fprintf(event, stdout);
2318 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002319 thread__put(thread);
2320 return 0;
2321}
2322
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002323static int
2324process_lost_event(struct perf_tool *tool,
2325 union perf_event *event,
2326 struct perf_sample *sample,
2327 struct machine *machine)
2328{
2329 struct perf_script *script = container_of(tool, struct perf_script, tool);
2330 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002331 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002332 struct thread *thread;
2333
2334 thread = machine__findnew_thread(machine, sample->pid,
2335 sample->tid);
2336 if (thread == NULL)
2337 return -1;
2338
Andi Kleene87e5482019-03-11 07:44:52 -07002339 if (!filter_cpu(sample)) {
2340 perf_sample__fprintf_start(sample, thread, evsel,
2341 PERF_RECORD_LOST, stdout);
2342 perf_event__fprintf(event, stdout);
2343 }
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002344 thread__put(thread);
2345 return 0;
2346}
2347
Jiri Olsa3233b372018-02-06 19:17:59 +01002348static int
2349process_finished_round_event(struct perf_tool *tool __maybe_unused,
2350 union perf_event *event,
2351 struct ordered_events *oe __maybe_unused)
2352
2353{
2354 perf_event__fprintf(event, stdout);
2355 return 0;
2356}
2357
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002358static int
2359process_bpf_events(struct perf_tool *tool __maybe_unused,
2360 union perf_event *event,
2361 struct perf_sample *sample,
2362 struct machine *machine)
2363{
2364 struct thread *thread;
2365 struct perf_script *script = container_of(tool, struct perf_script, tool);
2366 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002367 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002368
2369 if (machine__process_ksymbol(machine, event, sample) < 0)
2370 return -1;
2371
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002372 if (!evsel->core.attr.sample_id_all) {
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002373 perf_event__fprintf(event, stdout);
2374 return 0;
2375 }
2376
2377 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2378 if (thread == NULL) {
2379 pr_debug("problem processing MMAP event, skipping it.\n");
2380 return -1;
2381 }
2382
2383 if (!filter_cpu(sample)) {
2384 perf_sample__fprintf_start(sample, thread, evsel,
2385 event->header.type, stdout);
2386 perf_event__fprintf(event, stdout);
2387 }
2388
2389 thread__put(thread);
2390 return 0;
2391}
2392
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002393static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002394{
2395 session_done = 1;
2396}
2397
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002398static void perf_script__fclose_per_event_dump(struct perf_script *script)
2399{
Jiri Olsa63503db2019-07-21 13:23:52 +02002400 struct evlist *evlist = script->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002401 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002402
2403 evlist__for_each_entry(evlist, evsel) {
2404 if (!evsel->priv)
2405 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002406 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002407 evsel->priv = NULL;
2408 }
2409}
2410
2411static int perf_script__fopen_per_event_dump(struct perf_script *script)
2412{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002413 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002414
2415 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002416 /*
2417 * Already setup? I.e. we may be called twice in cases like
2418 * Intel PT, one for the intel_pt// and dummy events, then
2419 * for the evsels syntheized from the auxtrace info.
2420 *
2421 * Ses perf_script__process_auxtrace_info.
2422 */
2423 if (evsel->priv != NULL)
2424 continue;
2425
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002426 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002427 if (evsel->priv == NULL)
2428 goto out_err_fclose;
2429 }
2430
2431 return 0;
2432
2433out_err_fclose:
2434 perf_script__fclose_per_event_dump(script);
2435 return -1;
2436}
2437
2438static int perf_script__setup_per_event_dump(struct perf_script *script)
2439{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002440 struct evsel *evsel;
2441 static struct evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002442
2443 if (script->per_event_dump)
2444 return perf_script__fopen_per_event_dump(script);
2445
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002446 es_stdout.fp = stdout;
2447
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002448 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002449 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002450
2451 return 0;
2452}
2453
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002454static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2455{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002456 struct evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002457
2458 evlist__for_each_entry(script->session->evlist, evsel) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002459 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002460
2461 perf_evsel_script__fprintf(es, stdout);
2462 perf_evsel_script__delete(es);
2463 evsel->priv = NULL;
2464 }
2465}
2466
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002467static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002468{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002469 int ret;
2470
Tom Zanussic239da32010-04-01 23:59:18 -05002471 signal(SIGINT, sig_handler);
2472
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002473 perf_stat__init_shadow_stats();
2474
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002475 /* override event processing functions */
2476 if (script->show_task_events) {
2477 script->tool.comm = process_comm_event;
2478 script->tool.fork = process_fork_event;
2479 script->tool.exit = process_exit_event;
2480 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002481 if (script->show_mmap_events) {
2482 script->tool.mmap = process_mmap_event;
2483 script->tool.mmap2 = process_mmap2_event;
2484 }
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002485 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
Adrian Hunter7c148982015-07-21 12:44:06 +03002486 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302487 if (script->show_namespace_events)
2488 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002489 if (script->show_lost_events)
2490 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002491 if (script->show_round_events) {
2492 script->tool.ordered_events = false;
2493 script->tool.finished_round = process_finished_round_event;
2494 }
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002495 if (script->show_bpf_events) {
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -03002496 script->tool.ksymbol = process_bpf_events;
2497 script->tool.bpf = process_bpf_events;
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002498 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002499
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002500 if (perf_script__setup_per_event_dump(script)) {
2501 pr_err("Couldn't create the per event dump files\n");
2502 return -1;
2503 }
2504
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002505 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002506
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002507 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002508 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002509
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002510 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002511 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002512
2513 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002514}
2515
Tom Zanussi956ffd02009-11-25 01:15:46 -06002516struct script_spec {
2517 struct list_head node;
2518 struct scripting_ops *ops;
2519 char spec[0];
2520};
2521
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002522static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002523
2524static struct script_spec *script_spec__new(const char *spec,
2525 struct scripting_ops *ops)
2526{
2527 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2528
2529 if (s != NULL) {
2530 strcpy(s->spec, spec);
2531 s->ops = ops;
2532 }
2533
2534 return s;
2535}
2536
Tom Zanussi956ffd02009-11-25 01:15:46 -06002537static void script_spec__add(struct script_spec *s)
2538{
2539 list_add_tail(&s->node, &script_specs);
2540}
2541
2542static struct script_spec *script_spec__find(const char *spec)
2543{
2544 struct script_spec *s;
2545
2546 list_for_each_entry(s, &script_specs, node)
2547 if (strcasecmp(s->spec, spec) == 0)
2548 return s;
2549 return NULL;
2550}
2551
Tom Zanussi956ffd02009-11-25 01:15:46 -06002552int script_spec_register(const char *spec, struct scripting_ops *ops)
2553{
2554 struct script_spec *s;
2555
2556 s = script_spec__find(spec);
2557 if (s)
2558 return -1;
2559
Taeung Song8560bae2016-02-26 00:13:10 +09002560 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002561 if (!s)
2562 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002563 else
2564 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002565
2566 return 0;
2567}
2568
2569static struct scripting_ops *script_spec__lookup(const char *spec)
2570{
2571 struct script_spec *s = script_spec__find(spec);
2572 if (!s)
2573 return NULL;
2574
2575 return s->ops;
2576}
2577
2578static void list_available_languages(void)
2579{
2580 struct script_spec *s;
2581
2582 fprintf(stderr, "\n");
2583 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002584 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002585
2586 list_for_each_entry(s, &script_specs, node)
2587 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2588
2589 fprintf(stderr, "\n");
2590}
2591
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002592static int parse_scriptname(const struct option *opt __maybe_unused,
2593 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002594{
2595 char spec[PATH_MAX];
2596 const char *script, *ext;
2597 int len;
2598
Tom Zanussif526d682010-01-27 02:27:52 -06002599 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002600 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002601 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002602 }
2603
2604 script = strchr(str, ':');
2605 if (script) {
2606 len = script - str;
2607 if (len >= PATH_MAX) {
2608 fprintf(stderr, "invalid language specifier");
2609 return -1;
2610 }
2611 strncpy(spec, str, len);
2612 spec[len] = '\0';
2613 scripting_ops = script_spec__lookup(spec);
2614 if (!scripting_ops) {
2615 fprintf(stderr, "invalid language specifier");
2616 return -1;
2617 }
2618 script++;
2619 } else {
2620 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002621 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002622 if (!ext) {
2623 fprintf(stderr, "invalid script extension");
2624 return -1;
2625 }
2626 scripting_ops = script_spec__lookup(++ext);
2627 if (!scripting_ops) {
2628 fprintf(stderr, "invalid script extension");
2629 return -1;
2630 }
2631 }
2632
2633 script_name = strdup(script);
2634
2635 return 0;
2636}
2637
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002638static int parse_output_fields(const struct option *opt __maybe_unused,
2639 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002640{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002641 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002642 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002643 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002644 int rc = 0;
2645 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002646 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002647 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002648
2649 if (!str)
2650 return -ENOMEM;
2651
David Ahern2c9e45f72011-03-17 10:03:21 -06002652 /* first word can state for which event type the user is specifying
2653 * the fields. If no type exists, the specified fields apply to all
2654 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002655 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002656 tok = strchr(str, ':');
2657 if (tok) {
2658 *tok = '\0';
2659 tok++;
2660 if (!strcmp(str, "hw"))
2661 type = PERF_TYPE_HARDWARE;
2662 else if (!strcmp(str, "sw"))
2663 type = PERF_TYPE_SOFTWARE;
2664 else if (!strcmp(str, "trace"))
2665 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002666 else if (!strcmp(str, "raw"))
2667 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002668 else if (!strcmp(str, "break"))
2669 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002670 else if (!strcmp(str, "synth"))
2671 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002672 else {
2673 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002674 rc = -EINVAL;
2675 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002676 }
2677
2678 if (output[type].user_set)
2679 pr_warning("Overriding previous field request for %s events.\n",
2680 event_type(type));
2681
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002682 /* Don't override defaults for +- */
2683 if (strchr(tok, '+') || strchr(tok, '-'))
2684 goto parse;
2685
David Ahern2c9e45f72011-03-17 10:03:21 -06002686 output[type].fields = 0;
2687 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002688 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002689
2690 } else {
2691 tok = str;
2692 if (strlen(str) == 0) {
2693 fprintf(stderr,
2694 "Cannot set fields to 'none' for all event types.\n");
2695 rc = -EINVAL;
2696 goto out;
2697 }
2698
Andi Kleen36ce5652017-06-02 08:48:10 -07002699 /* Don't override defaults for +- */
2700 if (strchr(str, '+') || strchr(str, '-'))
2701 goto parse;
2702
David Ahern2c9e45f72011-03-17 10:03:21 -06002703 if (output_set_by_user())
2704 pr_warning("Overriding previous field request for all events.\n");
2705
Adrian Hunter14057202017-06-21 13:17:19 +03002706 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002707 output[j].fields = 0;
2708 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002709 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002710 }
David Ahern1424dc92011-03-09 22:23:28 -07002711 }
2712
Andi Kleen36ce5652017-06-02 08:48:10 -07002713parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002714 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002715 if (*tok == '+') {
2716 if (change == SET)
2717 goto out_badmix;
2718 change = ADD;
2719 tok++;
2720 } else if (*tok == '-') {
2721 if (change == SET)
2722 goto out_badmix;
2723 change = REMOVE;
2724 tok++;
2725 } else {
2726 if (change != SET && change != DEFAULT)
2727 goto out_badmix;
2728 change = SET;
2729 }
2730
David Ahern745f43e2011-03-09 22:23:26 -07002731 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002732 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002733 break;
David Ahern745f43e2011-03-09 22:23:26 -07002734 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002735 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002736 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002737 continue;
2738 }
David Ahern745f43e2011-03-09 22:23:26 -07002739 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002740 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002741 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002742 goto out;
2743 }
2744
2745 if (type == -1) {
2746 /* add user option to all events types for
2747 * which it is valid
2748 */
Adrian Hunter14057202017-06-21 13:17:19 +03002749 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002750 if (output[j].invalid_fields & all_output_options[i].field) {
2751 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2752 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002753 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002754 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002755 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002756 output[j].user_set_fields &= ~all_output_options[i].field;
2757 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002758 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002759 output[j].user_set_fields |= all_output_options[i].field;
2760 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002761 output[j].user_set = true;
2762 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002763 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002764 }
2765 } else {
2766 if (output[type].invalid_fields & all_output_options[i].field) {
2767 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2768 all_output_options[i].str, event_type(type));
2769
2770 rc = -EINVAL;
2771 goto out;
2772 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002773 if (change == REMOVE)
2774 output[type].fields &= ~all_output_options[i].field;
2775 else
2776 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002777 output[type].user_set = true;
2778 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002779 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002780 }
2781
2782 if (type >= 0) {
2783 if (output[type].fields == 0) {
2784 pr_debug("No fields requested for %s type. "
2785 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002786 }
David Ahern1424dc92011-03-09 22:23:28 -07002787 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002788 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002789
Andi Kleen36ce5652017-06-02 08:48:10 -07002790out_badmix:
2791 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2792 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002793out:
David Ahern745f43e2011-03-09 22:23:26 -07002794 free(str);
2795 return rc;
2796}
2797
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002798#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2799 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2800 if ((lang_dirent->d_type == DT_DIR || \
2801 (lang_dirent->d_type == DT_UNKNOWN && \
2802 is_directory(scripts_path, lang_dirent))) && \
2803 (strcmp(lang_dirent->d_name, ".")) && \
2804 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002805
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002806#define for_each_script(lang_path, lang_dir, script_dirent) \
2807 while ((script_dirent = readdir(lang_dir)) != NULL) \
2808 if (script_dirent->d_type != DT_DIR && \
2809 (script_dirent->d_type != DT_UNKNOWN || \
2810 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002811
2812
2813#define RECORD_SUFFIX "-record"
2814#define REPORT_SUFFIX "-report"
2815
2816struct script_desc {
2817 struct list_head node;
2818 char *name;
2819 char *half_liner;
2820 char *args;
2821};
2822
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002823static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002824
2825static struct script_desc *script_desc__new(const char *name)
2826{
2827 struct script_desc *s = zalloc(sizeof(*s));
2828
Tom Zanussib5b87312010-11-10 08:16:51 -06002829 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002830 s->name = strdup(name);
2831
2832 return s;
2833}
2834
2835static void script_desc__delete(struct script_desc *s)
2836{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002837 zfree(&s->name);
2838 zfree(&s->half_liner);
2839 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002840 free(s);
2841}
2842
2843static void script_desc__add(struct script_desc *s)
2844{
2845 list_add_tail(&s->node, &script_descs);
2846}
2847
2848static struct script_desc *script_desc__find(const char *name)
2849{
2850 struct script_desc *s;
2851
2852 list_for_each_entry(s, &script_descs, node)
2853 if (strcasecmp(s->name, name) == 0)
2854 return s;
2855 return NULL;
2856}
2857
2858static struct script_desc *script_desc__findnew(const char *name)
2859{
2860 struct script_desc *s = script_desc__find(name);
2861
2862 if (s)
2863 return s;
2864
2865 s = script_desc__new(name);
2866 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002867 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002868
2869 script_desc__add(s);
2870
2871 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002872}
2873
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002874static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002875{
2876 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002877 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002878
2879 if (strlen(str) > suffix_len) {
2880 p = str + strlen(str) - suffix_len;
2881 if (!strncmp(p, suffix, suffix_len))
2882 return p;
2883 }
2884
2885 return NULL;
2886}
2887
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002888static int read_script_info(struct script_desc *desc, const char *filename)
2889{
2890 char line[BUFSIZ], *p;
2891 FILE *fp;
2892
2893 fp = fopen(filename, "r");
2894 if (!fp)
2895 return -1;
2896
2897 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002898 p = skip_spaces(line);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002899 if (strlen(p) == 0)
2900 continue;
2901 if (*p != '#')
2902 continue;
2903 p++;
2904 if (strlen(p) && *p == '!')
2905 continue;
2906
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002907 p = skip_spaces(p);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002908 if (strlen(p) && p[strlen(p) - 1] == '\n')
2909 p[strlen(p) - 1] = '\0';
2910
2911 if (!strncmp(p, "description:", strlen("description:"))) {
2912 p += strlen("description:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002913 desc->half_liner = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002914 continue;
2915 }
2916
2917 if (!strncmp(p, "args:", strlen("args:"))) {
2918 p += strlen("args:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002919 desc->args = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002920 continue;
2921 }
2922 }
2923
2924 fclose(fp);
2925
2926 return 0;
2927}
2928
Robert Richter38efb532011-11-25 11:38:40 +01002929static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2930{
2931 char *script_root, *str;
2932
2933 script_root = strdup(script_dirent->d_name);
2934 if (!script_root)
2935 return NULL;
2936
2937 str = (char *)ends_with(script_root, suffix);
2938 if (!str) {
2939 free(script_root);
2940 return NULL;
2941 }
2942
2943 *str = '\0';
2944 return script_root;
2945}
2946
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002947static int list_available_scripts(const struct option *opt __maybe_unused,
2948 const char *s __maybe_unused,
2949 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002950{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002951 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002952 char scripts_path[MAXPATHLEN];
2953 DIR *scripts_dir, *lang_dir;
2954 char script_path[MAXPATHLEN];
2955 char lang_path[MAXPATHLEN];
2956 struct script_desc *desc;
2957 char first_half[BUFSIZ];
2958 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002959
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002960 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002961
2962 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002963 if (!scripts_dir) {
2964 fprintf(stdout,
2965 "open(%s) failed.\n"
2966 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2967 scripts_path);
2968 exit(-1);
2969 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002970
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002971 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002972 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2973 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002974 lang_dir = opendir(lang_path);
2975 if (!lang_dir)
2976 continue;
2977
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002978 for_each_script(lang_path, lang_dir, script_dirent) {
2979 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002980 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002981 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002982 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2983 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002984 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002985 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002986 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002987 }
2988 }
2989
2990 fprintf(stdout, "List of available trace scripts:\n");
2991 list_for_each_entry(desc, &script_descs, node) {
2992 sprintf(first_half, "%s %s", desc->name,
2993 desc->args ? desc->args : "");
2994 fprintf(stdout, " %-36s %s\n", first_half,
2995 desc->half_liner ? desc->half_liner : "");
2996 }
2997
2998 exit(0);
2999}
3000
Feng Tange5f37052012-09-07 16:42:26 +08003001/*
Feng Tang49e639e2012-10-30 11:56:03 +08003002 * Some scripts specify the required events in their "xxx-record" file,
3003 * this function will check if the events in perf.data match those
3004 * mentioned in the "xxx-record".
3005 *
3006 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3007 * which is covered well now. And new parsing code should be added to
3008 * cover the future complexing formats like event groups etc.
3009 */
3010static int check_ev_match(char *dir_name, char *scriptname,
3011 struct perf_session *session)
3012{
3013 char filename[MAXPATHLEN], evname[128];
3014 char line[BUFSIZ], *p;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003015 struct evsel *pos;
Feng Tang49e639e2012-10-30 11:56:03 +08003016 int match, len;
3017 FILE *fp;
3018
Jiri Olsa77f18152018-03-19 09:29:01 +01003019 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08003020
3021 fp = fopen(filename, "r");
3022 if (!fp)
3023 return -1;
3024
3025 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003026 p = skip_spaces(line);
Feng Tang49e639e2012-10-30 11:56:03 +08003027 if (*p == '#')
3028 continue;
3029
3030 while (strlen(p)) {
3031 p = strstr(p, "-e");
3032 if (!p)
3033 break;
3034
3035 p += 2;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003036 p = skip_spaces(p);
Feng Tang49e639e2012-10-30 11:56:03 +08003037 len = strcspn(p, " \t");
3038 if (!len)
3039 break;
3040
3041 snprintf(evname, len + 1, "%s", p);
3042
3043 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003044 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08003045 if (!strcmp(perf_evsel__name(pos), evname)) {
3046 match = 1;
3047 break;
3048 }
3049 }
3050
3051 if (!match) {
3052 fclose(fp);
3053 return -1;
3054 }
3055 }
3056 }
3057
3058 fclose(fp);
3059 return 0;
3060}
3061
3062/*
Feng Tange5f37052012-09-07 16:42:26 +08003063 * Return -1 if none is found, otherwise the actual scripts number.
3064 *
3065 * Currently the only user of this function is the script browser, which
3066 * will list all statically runnable scripts, select one, execute it and
3067 * show the output in a perf browser.
3068 */
Andi Kleen905e4af2019-03-11 07:45:01 -07003069int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3070 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08003071{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003072 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08003073 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08003074 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08003075 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003076 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003077 .path = input_name,
3078 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003079 };
Feng Tange5f37052012-09-07 16:42:26 +08003080 char *temp;
3081 int i = 0;
3082
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003083 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08003084 if (!session)
3085 return -1;
3086
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003087 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003088
3089 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003090 if (!scripts_dir) {
3091 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003092 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003093 }
Feng Tange5f37052012-09-07 16:42:26 +08003094
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003095 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003096 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3097 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003098#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003099 if (strstr(lang_path, "perl"))
3100 continue;
3101#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003102#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003103 if (strstr(lang_path, "python"))
3104 continue;
3105#endif
3106
3107 lang_dir = opendir(lang_path);
3108 if (!lang_dir)
3109 continue;
3110
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003111 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003112 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003113 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003114 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003115 if (i >= num)
3116 break;
3117 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3118 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003119 script_dirent->d_name);
3120 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003121 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003122 (temp - script_dirent->d_name) + 1,
3123 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003124
3125 if (check_ev_match(lang_path,
3126 scripts_array[i], session))
3127 continue;
3128
Feng Tange5f37052012-09-07 16:42:26 +08003129 i++;
3130 }
Feng Tang49e639e2012-10-30 11:56:03 +08003131 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003132 }
3133
Feng Tang49e639e2012-10-30 11:56:03 +08003134 closedir(scripts_dir);
3135 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003136 return i;
3137}
3138
Tom Zanussi38752942009-12-15 02:53:39 -06003139static char *get_script_path(const char *script_root, const char *suffix)
3140{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003141 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003142 char scripts_path[MAXPATHLEN];
3143 char script_path[MAXPATHLEN];
3144 DIR *scripts_dir, *lang_dir;
3145 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003146 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003147
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003148 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003149
3150 scripts_dir = opendir(scripts_path);
3151 if (!scripts_dir)
3152 return NULL;
3153
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003154 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003155 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3156 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003157 lang_dir = opendir(lang_path);
3158 if (!lang_dir)
3159 continue;
3160
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003161 for_each_script(lang_path, lang_dir, script_dirent) {
3162 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003163 if (__script_root && !strcmp(script_root, __script_root)) {
3164 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003165 closedir(lang_dir);
3166 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003167 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3168 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003169 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003170 }
3171 free(__script_root);
3172 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003173 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003174 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003175 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003176
Robert Richter38efb532011-11-25 11:38:40 +01003177 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003178}
3179
Tom Zanussib5b87312010-11-10 08:16:51 -06003180static bool is_top_script(const char *script_path)
3181{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003182 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003183}
3184
3185static int has_required_arg(char *script_path)
3186{
3187 struct script_desc *desc;
3188 int n_args = 0;
3189 char *p;
3190
3191 desc = script_desc__new(NULL);
3192
3193 if (read_script_info(desc, script_path))
3194 goto out;
3195
3196 if (!desc->args)
3197 goto out;
3198
3199 for (p = desc->args; *p; p++)
3200 if (*p == '<')
3201 n_args++;
3202out:
3203 script_desc__delete(desc);
3204
3205 return n_args;
3206}
3207
David Ahernd54b1a92012-08-26 12:24:46 -06003208static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003209{
3210 char **__argv = malloc(sizeof(const char *) * argc);
3211
David Ahernd54b1a92012-08-26 12:24:46 -06003212 if (!__argv) {
3213 pr_err("malloc failed\n");
3214 return -1;
3215 }
3216
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003217 memcpy(__argv, argv, sizeof(const char *) * argc);
3218 argc = parse_options(argc, (const char **)__argv, record_options,
3219 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3220 free(__argv);
3221
David Ahernd54b1a92012-08-26 12:24:46 -06003222 system_wide = (argc == 0);
3223
3224 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003225}
3226
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003227static void script__setup_sample_type(struct perf_script *script)
3228{
3229 struct perf_session *session = script->session;
3230 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3231
3232 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3233 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003234 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003235 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003236 dwarf_callchain_users = true;
3237 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003238 callchain_param.record_mode = CALLCHAIN_LBR;
3239 else
3240 callchain_param.record_mode = CALLCHAIN_FP;
3241 }
3242}
3243
Jiri Olsa89f16882018-09-13 14:54:03 +02003244static int process_stat_round_event(struct perf_session *session,
3245 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003246{
Jiri Olsa72932372019-08-28 15:57:16 +02003247 struct perf_record_stat_round *round = &event->stat_round;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003248 struct evsel *counter;
Jiri Olsae099eba2016-01-05 22:09:09 +01003249
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003250 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003251 perf_stat_process_counter(&stat_config, counter);
3252 process_stat(counter, round->time);
3253 }
3254
3255 process_stat_interval(round->time);
3256 return 0;
3257}
3258
Jiri Olsa89f16882018-09-13 14:54:03 +02003259static int process_stat_config_event(struct perf_session *session __maybe_unused,
3260 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003261{
3262 perf_event__read_stat_config(&stat_config, &event->stat_config);
3263 return 0;
3264}
3265
Jiri Olsacfc88742016-01-05 22:09:06 +01003266static int set_maps(struct perf_script *script)
3267{
Jiri Olsa63503db2019-07-21 13:23:52 +02003268 struct evlist *evlist = script->session->evlist;
Jiri Olsacfc88742016-01-05 22:09:06 +01003269
3270 if (!script->cpus || !script->threads)
3271 return 0;
3272
3273 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3274 return -EINVAL;
3275
Jiri Olsa453fa032019-07-21 13:24:43 +02003276 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
Jiri Olsacfc88742016-01-05 22:09:06 +01003277
3278 if (perf_evlist__alloc_stats(evlist, true))
3279 return -ENOMEM;
3280
3281 script->allocated = true;
3282 return 0;
3283}
3284
3285static
Jiri Olsa89f16882018-09-13 14:54:03 +02003286int process_thread_map_event(struct perf_session *session,
3287 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003288{
Jiri Olsa89f16882018-09-13 14:54:03 +02003289 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003290 struct perf_script *script = container_of(tool, struct perf_script, tool);
3291
3292 if (script->threads) {
3293 pr_warning("Extra thread map event, ignoring.\n");
3294 return 0;
3295 }
3296
3297 script->threads = thread_map__new_event(&event->thread_map);
3298 if (!script->threads)
3299 return -ENOMEM;
3300
3301 return set_maps(script);
3302}
3303
3304static
Jiri Olsa89f16882018-09-13 14:54:03 +02003305int process_cpu_map_event(struct perf_session *session,
3306 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003307{
Jiri Olsa89f16882018-09-13 14:54:03 +02003308 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003309 struct perf_script *script = container_of(tool, struct perf_script, tool);
3310
3311 if (script->cpus) {
3312 pr_warning("Extra cpu map event, ignoring.\n");
3313 return 0;
3314 }
3315
3316 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3317 if (!script->cpus)
3318 return -ENOMEM;
3319
3320 return set_maps(script);
3321}
3322
Jiri Olsa89f16882018-09-13 14:54:03 +02003323static int process_feature_event(struct perf_session *session,
3324 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303325{
3326 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003327 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303328 return 0;
3329}
3330
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003331#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003332static int perf_script__process_auxtrace_info(struct perf_session *session,
3333 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003334{
Jiri Olsa89f16882018-09-13 14:54:03 +02003335 struct perf_tool *tool = session->tool;
3336
3337 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003338
3339 if (ret == 0) {
3340 struct perf_script *script = container_of(tool, struct perf_script, tool);
3341
3342 ret = perf_script__setup_per_event_dump(script);
3343 }
3344
3345 return ret;
3346}
3347#else
3348#define perf_script__process_auxtrace_info 0
3349#endif
3350
Andi Kleenb585ebd2018-09-20 11:05:36 -07003351static int parse_insn_trace(const struct option *opt __maybe_unused,
3352 const char *str __maybe_unused,
3353 int unset __maybe_unused)
3354{
3355 parse_output_fields(NULL, "+insn,-event,-period", 0);
3356 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003357 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003358 return 0;
3359}
3360
3361static int parse_xed(const struct option *opt __maybe_unused,
3362 const char *str __maybe_unused,
3363 int unset __maybe_unused)
3364{
3365 force_pager("xed -F insn: -A -64 | less");
3366 return 0;
3367}
3368
Andi Kleend1b15522018-09-20 11:05:38 -07003369static int parse_call_trace(const struct option *opt __maybe_unused,
3370 const char *str __maybe_unused,
3371 int unset __maybe_unused)
3372{
3373 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3374 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003375 symbol_conf.nanosecs = true;
Jiri Olsa1c492422019-05-08 15:20:05 +02003376 symbol_conf.pad_output_len_dso = 50;
Andi Kleend1b15522018-09-20 11:05:38 -07003377 return 0;
3378}
3379
3380static int parse_callret_trace(const struct option *opt __maybe_unused,
3381 const char *str __maybe_unused,
3382 int unset __maybe_unused)
3383{
3384 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3385 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003386 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003387 return 0;
3388}
3389
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003390int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003391{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003392 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003393 bool header = false;
3394 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003395 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003396 char *rec_script_path = NULL;
3397 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003398 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003399 struct itrace_synth_opts itrace_synth_opts = {
3400 .set = false,
3401 .default_no_sample = true,
3402 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003403 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003404 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003405 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003406 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003407 struct perf_script script = {
3408 .tool = {
3409 .sample = process_sample_event,
3410 .mmap = perf_event__process_mmap,
3411 .mmap2 = perf_event__process_mmap2,
3412 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303413 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003414 .exit = perf_event__process_exit,
3415 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003416 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003417 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003418 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303419 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003420 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003421 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003422 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003423 .auxtrace = perf_event__process_auxtrace,
3424 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003425 .stat = perf_event__process_stat_event,
3426 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003427 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003428 .thread_map = process_thread_map_event,
3429 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003430 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003431 .ordering_requires_timestamps = true,
3432 },
3433 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003434 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003435 .mode = PERF_DATA_MODE_READ,
3436 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003437 const struct option options[] = {
3438 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3439 "dump raw trace in ASCII"),
3440 OPT_INCR('v', "verbose", &verbose,
3441 "be more verbose (show symbol address, etc)"),
3442 OPT_BOOLEAN('L', "Latency", &latency_format,
3443 "show latency attributes (irqs/preemption disabled, etc)"),
3444 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3445 list_available_scripts),
3446 OPT_CALLBACK('s', "script", NULL, "name",
3447 "script file name (lang:script name, script name, or *)",
3448 parse_scriptname),
3449 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3450 "generate perf-script.xx script in specified language"),
3451 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3452 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3453 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003454 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3455 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003456 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3457 "file", "vmlinux pathname"),
3458 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3459 "file", "kallsyms pathname"),
3460 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3461 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003462 OPT_CALLBACK(0, "symfs", NULL, "directory",
3463 "Look for files with symbols relative to this directory",
3464 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003465 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003466 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003467 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003468 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003469 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303470 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3471 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
Adrian Hunter68fb45b2019-05-20 14:37:14 +03003472 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003473 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003474 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3475 "system-wide collection from all CPUs"),
3476 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3477 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003478 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3479 "Decode instructions from itrace", parse_insn_trace),
3480 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3481 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003482 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3483 "Decode calls from from itrace", parse_call_trace),
3484 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3485 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003486 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3487 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003488 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3489 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003490 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3491 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3492 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003493 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3494 "only consider symbols in these pids"),
3495 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3496 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003497 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3498 "Set the maximum stack depth when parsing the callchain, "
3499 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003500 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Andi Kleen90b10f42019-03-14 15:50:00 -07003501 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003502 OPT_BOOLEAN('I', "show-info", &show_full_info,
3503 "display extended information from perf.data file"),
3504 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3505 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003506 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3507 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003508 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3509 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003510 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3511 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303512 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3513 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003514 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3515 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003516 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3517 "Show round events (if recorded)"),
Jiri Olsa490c8cc2019-05-08 15:20:08 +02003518 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3519 "Show bpf related events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003520 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3521 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003522 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003523 OPT_INTEGER(0, "max-blocks", &max_blocks,
3524 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003525 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003526 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003527 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003528 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003529 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003530 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3531 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003532 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3533 "Enable symbol demangling"),
3534 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3535 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003536 OPT_STRING(0, "time", &script.time_str, "str",
3537 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003538 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3539 "Show inline function"),
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003540 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3541 "guest mount directory under which every guest os"
3542 " instance has a subdir"),
3543 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3544 "file", "file saving guest os vmlinux"),
3545 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3546 "file", "file saving guest os /proc/kallsyms"),
3547 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3548 "file", "file saving guest os /proc/modules"),
Arnaldo Carvalho de Meloadd3a712019-08-15 11:21:21 -03003549 OPTS_EVSWITCH(&script.evswitch),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003550 OPT_END()
3551 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003552 const char * const script_subcommands[] = { "record", "report", NULL };
3553 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003554 "perf script [<options>]",
3555 "perf script [<options>] record <script> [<record-options>] <command>",
3556 "perf script [<options>] report <script> [script-args]",
3557 "perf script [<options>] <script> [<record-options>] <command>",
3558 "perf script [<options>] <top-script> [script-args]",
3559 NULL
3560 };
Tom Zanussi38752942009-12-15 02:53:39 -06003561
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003562 perf_set_singlethreaded();
3563
Tom Zanussib5b87312010-11-10 08:16:51 -06003564 setup_scripting();
3565
Yunlong Song40cae2b2015-03-18 21:35:54 +08003566 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003567 PARSE_OPT_STOP_AT_NON_OPTION);
3568
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003569 if (symbol_conf.guestmount ||
3570 symbol_conf.default_guest_vmlinux_name ||
3571 symbol_conf.default_guest_kallsyms ||
3572 symbol_conf.default_guest_modules) {
3573 /*
3574 * Enable guest sample processing.
3575 */
3576 perf_guest = true;
3577 }
3578
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003579 data.path = input_name;
3580 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003581
Tom Zanussib5b87312010-11-10 08:16:51 -06003582 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3583 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3584 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003585 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003586 }
3587
Tom Zanussib5b87312010-11-10 08:16:51 -06003588 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3589 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3590 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003591 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003592 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003593 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003594 return -1;
3595 }
Tom Zanussi38752942009-12-15 02:53:39 -06003596 }
3597
Andi Kleen90b10f42019-03-14 15:50:00 -07003598 if (script.time_str && reltime) {
3599 fprintf(stderr, "Don't combine --reltime with --time\n");
3600 return -1;
3601 }
3602
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003603 if (itrace_synth_opts.callchain &&
3604 itrace_synth_opts.callchain_sz > scripting_max_stack)
3605 scripting_max_stack = itrace_synth_opts.callchain_sz;
3606
Ben Hutchings44e668c2010-10-10 16:10:03 +01003607 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003608 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003609
Tom Zanussib5b87312010-11-10 08:16:51 -06003610 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003611 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003612 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003613 pid_t pid;
3614
Tom Zanussib5b87312010-11-10 08:16:51 -06003615 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3616 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3617
3618 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003619 usage_with_options_msg(script_usage, options,
3620 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003621 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003622 }
3623
Tom Zanussib5b87312010-11-10 08:16:51 -06003624 if (is_top_script(argv[0])) {
3625 rep_args = argc - 1;
3626 } else {
3627 int rec_args;
3628
3629 rep_args = has_required_arg(rep_script_path);
3630 rec_args = (argc - 1) - rep_args;
3631 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003632 usage_with_options_msg(script_usage, options,
3633 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003634 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003635 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003636 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003637 }
3638
3639 if (pipe(live_pipe) < 0) {
3640 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003641 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003642 }
3643
3644 pid = fork();
3645 if (pid < 0) {
3646 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003647 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003648 }
3649
3650 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003651 j = 0;
3652
Tom Zanussia0cccc22010-04-01 23:59:25 -05003653 dup2(live_pipe[1], 1);
3654 close(live_pipe[0]);
3655
Robert Richter317df652011-11-25 15:05:25 +01003656 if (is_top_script(argv[0])) {
3657 system_wide = true;
3658 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003659 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3660 err = -1;
3661 goto out;
3662 }
Robert Richter317df652011-11-25 15:05:25 +01003663 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003664
3665 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003666 if (!__argv) {
3667 pr_err("malloc failed\n");
3668 err = -ENOMEM;
3669 goto out;
3670 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003671
Tom Zanussib5b87312010-11-10 08:16:51 -06003672 __argv[j++] = "/bin/sh";
3673 __argv[j++] = rec_script_path;
3674 if (system_wide)
3675 __argv[j++] = "-a";
3676 __argv[j++] = "-q";
3677 __argv[j++] = "-o";
3678 __argv[j++] = "-";
3679 for (i = rep_args + 1; i < argc; i++)
3680 __argv[j++] = argv[i];
3681 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003682
3683 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003684 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003685 exit(-1);
3686 }
3687
3688 dup2(live_pipe[0], 0);
3689 close(live_pipe[1]);
3690
Tom Zanussib5b87312010-11-10 08:16:51 -06003691 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003692 if (!__argv) {
3693 pr_err("malloc failed\n");
3694 err = -ENOMEM;
3695 goto out;
3696 }
3697
Tom Zanussib5b87312010-11-10 08:16:51 -06003698 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003699 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003700 __argv[j++] = rep_script_path;
3701 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003702 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003703 __argv[j++] = "-i";
3704 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003705 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003706
3707 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003708 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003709 exit(-1);
3710 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003711
Tom Zanussib5b87312010-11-10 08:16:51 -06003712 if (rec_script_path)
3713 script_path = rec_script_path;
3714 if (rep_script_path)
3715 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003716
Tom Zanussib5b87312010-11-10 08:16:51 -06003717 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003718 j = 0;
3719
Robert Richter317df652011-11-25 15:05:25 +01003720 if (!rec_script_path)
3721 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003722 else if (!system_wide) {
3723 if (have_cmd(argc - 1, &argv[1]) != 0) {
3724 err = -1;
3725 goto out;
3726 }
3727 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003728
3729 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003730 if (!__argv) {
3731 pr_err("malloc failed\n");
3732 err = -ENOMEM;
3733 goto out;
3734 }
3735
Tom Zanussib5b87312010-11-10 08:16:51 -06003736 __argv[j++] = "/bin/sh";
3737 __argv[j++] = script_path;
3738 if (system_wide)
3739 __argv[j++] = "-a";
3740 for (i = 2; i < argc; i++)
3741 __argv[j++] = argv[i];
3742 __argv[j++] = NULL;
3743
3744 execvp("/bin/sh", (char **)__argv);
3745 free(__argv);
3746 exit(-1);
3747 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003748
Milian Wolffc1c9b962018-10-21 21:14:23 +02003749 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003750 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003751 use_browser = 0;
3752 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003753
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003754 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003755 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003756 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003757
Jiri Olsae90debd2013-12-09 11:02:50 +01003758 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003759 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003760 perf_session__fprintf_info(session, stdout, show_full_info);
3761 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003762 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003763 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003764 if (show_full_info)
3765 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003766
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003767 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003768 goto out_delete;
3769
Andi Kleen3ab481a2019-03-05 06:47:45 -08003770 uname(&uts);
Song Liu9d491692019-06-20 18:44:38 -07003771 if (data.is_pipe || /* assume pipe_mode indicates native_arch */
3772 !strcmp(uts.machine, session->header.env.arch) ||
Andi Kleen3ab481a2019-03-05 06:47:45 -08003773 (!strcmp(uts.machine, "x86_64") &&
3774 !strcmp(session->header.env.arch, "i386")))
3775 native_arch = true;
3776
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003777 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003778 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003779
Andi Kleen99f753f2018-09-20 11:05:39 -07003780 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3781 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003782 itrace_synth_opts.thread_stack = true;
3783
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003784 session->itrace_synth_opts = &itrace_synth_opts;
3785
Anton Blanchard5d67be92011-07-04 21:57:50 +10003786 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003787 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3788 if (err < 0)
3789 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003790 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003791 }
3792
David Ahern1424dc92011-03-09 22:23:28 -07003793 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003794 symbol_conf.use_callchain = true;
3795 else
3796 symbol_conf.use_callchain = false;
3797
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003798 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003799 tep_set_function_resolver(session->tevent.pevent,
3800 machine__resolve_kernel_addr,
3801 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003802 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003803 err = -1;
3804 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003805 }
3806
Tom Zanussi956ffd02009-11-25 01:15:46 -06003807 if (generate_script_lang) {
3808 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003809 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003810
David Ahern2c9e45f72011-03-17 10:03:21 -06003811 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003812 fprintf(stderr,
3813 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003814 err = -EINVAL;
3815 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003816 }
3817
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003818 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003819 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003820 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003821 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003822 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003823 }
3824
3825 err = fstat(input, &perf_stat);
3826 if (err < 0) {
3827 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003828 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003829 }
3830
3831 if (!perf_stat.st_size) {
3832 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003833 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003834 }
3835
3836 scripting_ops = script_spec__lookup(generate_script_lang);
3837 if (!scripting_ops) {
3838 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003839 err = -ENOENT;
3840 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003841 }
3842
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003843 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003844 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003845 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003846 }
3847
3848 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003849 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003850 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003851 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003852 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003853 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003854 }
3855
David Ahern9cbdb702011-04-06 21:54:20 -06003856
3857 err = perf_session__check_output_opt(session);
3858 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003859 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003860
Jin Yao284c4e12019-03-01 18:13:06 +08003861 if (script.time_str) {
3862 err = perf_time__parse_for_ranges(script.time_str, session,
3863 &script.ptime_range,
3864 &script.range_size,
3865 &script.range_num);
3866 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08003867 goto out_delete;
Adrian Hunter400ae982019-06-04 16:00:00 +03003868
3869 itrace_synth_opts__set_time_range(&itrace_synth_opts,
3870 script.ptime_range,
3871 script.range_num);
David Aherna91f4c42016-11-29 10:15:43 -07003872 }
3873
Arnaldo Carvalho de Melo124e02b2019-08-15 11:31:29 -03003874 err = evswitch__init(&script.evswitch, session->evlist, stderr);
3875 if (err)
3876 goto out_delete;
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003877
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003878 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003879
Adrian Hunterd445dd22014-08-15 22:08:37 +03003880 flush_scripting();
3881
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003882out_delete:
Adrian Hunter400ae982019-06-04 16:00:00 +03003883 if (script.ptime_range) {
3884 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
Jin Yao284c4e12019-03-01 18:13:06 +08003885 zfree(&script.ptime_range);
Adrian Hunter400ae982019-06-04 16:00:00 +03003886 }
Jin Yaocc2ef582018-01-10 23:00:33 +08003887
Jiri Olsacfc88742016-01-05 22:09:06 +01003888 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003889 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003890
3891 if (script_started)
3892 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003893out:
3894 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003895}