blob: fff02e0d70c40b1289605b127ab2396320404ee8 [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
Andrea Gelminib7eead82010-08-05 15:51:38 +02004#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02005#include "util/cache.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"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020014#include "util/symbol.h"
15#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010016#include "util/trace-event.h"
David Ahern1424dc92011-03-09 22:23:28 -070017#include "util/evlist.h"
18#include "util/evsel.h"
Arnaldo Carvalho de Melod2360442019-08-15 10:37:24 -030019#include "util/evswitch.h"
Feng Tang36385be2012-09-07 16:42:24 +080020#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020021#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030022#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010023#include "util/cpumap.h"
24#include "util/thread_map.h"
25#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080026#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030027#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030028#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070029#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010030#include "util/path.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030031#include "print_binary.h"
Andi Kleen3ab481a2019-03-05 06:47:45 -080032#include "archinsn.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100033#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030034#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030035#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030036#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030037#include <linux/zalloc.h>
Andi Kleen3ab481a2019-03-05 06:47:45 -080038#include <sys/utsname.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010039#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010040#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080041#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030042#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030043#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030044#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030045#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030046#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030047#include <sys/types.h>
48#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030049#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030050#include <unistd.h>
Andi Kleenb585ebd2018-09-20 11:05:36 -070051#include <subcmd/pager.h>
Jiri Olsa453fa032019-07-21 13:24:43 +020052#include <perf/evlist.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020053
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030054#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030055
Tom Zanussi956ffd02009-11-25 01:15:46 -060056static char const *script_name;
57static char const *generate_script_lang;
Andi Kleen90b10f42019-03-14 15:50:00 -070058static bool reltime;
59static u64 initial_time;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020060static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020061static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020062static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070063static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090064static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010065static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030066static bool print_flags;
Anton Blanchard5d67be92011-07-04 21:57:50 +100067static const char *cpu_list;
68static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010069static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080070static int max_blocks;
Andi Kleen3ab481a2019-03-05 06:47:45 -080071static bool native_arch;
Tom Zanussi956ffd02009-11-25 01:15:46 -060072
Adrian Hunter44cbe722015-09-25 16:15:50 +030073unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030074
David Ahern745f43e2011-03-09 22:23:26 -070075enum perf_output_field {
76 PERF_OUTPUT_COMM = 1U << 0,
77 PERF_OUTPUT_TID = 1U << 1,
78 PERF_OUTPUT_PID = 1U << 2,
79 PERF_OUTPUT_TIME = 1U << 3,
80 PERF_OUTPUT_CPU = 1U << 4,
81 PERF_OUTPUT_EVNAME = 1U << 5,
82 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060083 PERF_OUTPUT_IP = 1U << 7,
84 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060085 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060086 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090087 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020088 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020089 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020090 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020091 PERF_OUTPUT_BRSTACK = 1U << 15,
92 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010093 PERF_OUTPUT_DATA_SRC = 1U << 17,
94 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000095 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030096 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030097 PERF_OUTPUT_INSN = 1U << 21,
98 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080099 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -0700100 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +0300101 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -0400102 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -0700103 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800104 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100105 PERF_OUTPUT_MISC = 1U << 29,
Andi Kleendd2e18e2018-12-03 16:18:48 -0800106 PERF_OUTPUT_SRCCODE = 1U << 30,
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300107 PERF_OUTPUT_IPC = 1U << 31,
David Ahern745f43e2011-03-09 22:23:26 -0700108};
109
110struct output_option {
111 const char *str;
112 enum perf_output_field field;
113} all_output_options[] = {
114 {.str = "comm", .field = PERF_OUTPUT_COMM},
115 {.str = "tid", .field = PERF_OUTPUT_TID},
116 {.str = "pid", .field = PERF_OUTPUT_PID},
117 {.str = "time", .field = PERF_OUTPUT_TIME},
118 {.str = "cpu", .field = PERF_OUTPUT_CPU},
119 {.str = "event", .field = PERF_OUTPUT_EVNAME},
120 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600121 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700122 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600123 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600124 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900125 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200126 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200127 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200128 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700129 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200130 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
131 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100132 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
133 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000134 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300135 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300136 {.str = "insn", .field = PERF_OUTPUT_INSN},
137 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800138 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700139 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300140 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400141 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800142 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100143 {.str = "misc", .field = PERF_OUTPUT_MISC},
Andi Kleendd2e18e2018-12-03 16:18:48 -0800144 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300145 {.str = "ipc", .field = PERF_OUTPUT_IPC},
David Ahern745f43e2011-03-09 22:23:26 -0700146};
147
Adrian Hunter14057202017-06-21 13:17:19 +0300148enum {
149 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
150 OUTPUT_TYPE_MAX
151};
152
David Ahern745f43e2011-03-09 22:23:26 -0700153/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600154static struct {
155 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600156 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400157 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600158 u64 fields;
159 u64 invalid_fields;
Andi Kleen4b6ac812019-02-24 07:37:12 -0800160 u64 user_set_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300161} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700162
David Ahern2c9e45f72011-03-17 10:03:21 -0600163 [PERF_TYPE_HARDWARE] = {
164 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700165
David Ahern2c9e45f72011-03-17 10:03:21 -0600166 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
167 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600168 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530169 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
170 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600171
Wang Nan30372f02016-02-24 11:20:45 +0000172 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600173 },
174
175 [PERF_TYPE_SOFTWARE] = {
176 .user_set = false,
177
178 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
179 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600180 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530181 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
182 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
183 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600184
185 .invalid_fields = PERF_OUTPUT_TRACE,
186 },
187
188 [PERF_TYPE_TRACEPOINT] = {
189 .user_set = false,
190
191 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
192 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000193 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600194 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700195
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530196 [PERF_TYPE_HW_CACHE] = {
197 .user_set = false,
198
199 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
200 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
201 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
202 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
203 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
204
205 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
206 },
207
Arun Sharma0817a6a2011-04-14 10:38:18 -0700208 [PERF_TYPE_RAW] = {
209 .user_set = false,
210
211 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
212 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600213 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530214 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
215 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
216 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
217 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700218
Wang Nan30372f02016-02-24 11:20:45 +0000219 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700220 },
Wang Nan27cfef02015-12-08 02:25:43 +0000221
222 [PERF_TYPE_BREAKPOINT] = {
223 .user_set = false,
224
225 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
226 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
227 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530228 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
229 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000230
Wang Nan30372f02016-02-24 11:20:45 +0000231 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000232 },
Adrian Hunter14057202017-06-21 13:17:19 +0300233
234 [OUTPUT_TYPE_SYNTH] = {
235 .user_set = false,
236
237 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
238 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
239 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530240 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
241 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300242
243 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
244 },
David Ahern1424dc92011-03-09 22:23:28 -0700245};
David Ahern745f43e2011-03-09 22:23:26 -0700246
Jiri Olsa32dcd022019-07-21 13:23:51 +0200247struct evsel_script {
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300248 char *filename;
249 FILE *fp;
250 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800251 /* For metric output */
252 u64 val;
253 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300254};
255
Jiri Olsa32dcd022019-07-21 13:23:51 +0200256static inline struct evsel_script *evsel_script(struct evsel *evsel)
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800257{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200258 return (struct evsel_script *)evsel->priv;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800259}
260
Jiri Olsa32dcd022019-07-21 13:23:51 +0200261static struct evsel_script *perf_evsel_script__new(struct evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100262 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300263{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200264 struct evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300265
266 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100267 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300268 goto out_free;
269 es->fp = fopen(es->filename, "w");
270 if (es->fp == NULL)
271 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300272 }
273
274 return es;
275out_free_filename:
276 zfree(&es->filename);
277out_free:
278 free(es);
279 return NULL;
280}
281
Jiri Olsa32dcd022019-07-21 13:23:51 +0200282static void perf_evsel_script__delete(struct evsel_script *es)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300283{
284 zfree(&es->filename);
285 fclose(es->fp);
286 es->fp = NULL;
287 free(es);
288}
289
Jiri Olsa32dcd022019-07-21 13:23:51 +0200290static int perf_evsel_script__fprintf(struct evsel_script *es, FILE *fp)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300291{
292 struct stat st;
293
294 fstat(fileno(es->fp), &st);
295 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
296 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
297}
298
Adrian Hunter14057202017-06-21 13:17:19 +0300299static inline int output_type(unsigned int type)
300{
301 switch (type) {
302 case PERF_TYPE_SYNTH:
303 return OUTPUT_TYPE_SYNTH;
304 default:
305 return type;
306 }
307}
308
309static inline unsigned int attr_type(unsigned int type)
310{
311 switch (type) {
312 case OUTPUT_TYPE_SYNTH:
313 return PERF_TYPE_SYNTH;
314 default:
315 return type;
316 }
317}
318
David Ahern2c9e45f72011-03-17 10:03:21 -0600319static bool output_set_by_user(void)
320{
321 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300322 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600323 if (output[j].user_set)
324 return true;
325 }
326 return false;
327}
David Ahern745f43e2011-03-09 22:23:26 -0700328
David Ahern9cbdb702011-04-06 21:54:20 -0600329static const char *output_field2str(enum perf_output_field field)
330{
331 int i, imax = ARRAY_SIZE(all_output_options);
332 const char *str = "";
333
334 for (i = 0; i < imax; ++i) {
335 if (all_output_options[i].field == field) {
336 str = all_output_options[i].str;
337 break;
338 }
339 }
340 return str;
341}
342
Adrian Hunter14057202017-06-21 13:17:19 +0300343#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700344
Jiri Olsa32dcd022019-07-21 13:23:51 +0200345static int perf_evsel__do_check_stype(struct evsel *evsel,
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300346 u64 sample_type, const char *sample_msg,
347 enum perf_output_field field,
348 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700349{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200350 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300351 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600352 const char *evname;
353
354 if (attr->sample_type & sample_type)
355 return 0;
356
Andi Kleen4b6ac812019-02-24 07:37:12 -0800357 if (output[type].user_set_fields & field) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300358 if (allow_user_set)
359 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300360 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600361 pr_err("Samples for '%s' event do not have %s attribute set. "
362 "Cannot print '%s' field.\n",
363 evname, sample_msg, output_field2str(field));
364 return -1;
365 }
366
367 /* user did not ask for it explicitly so remove from the default list */
368 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300369 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600370 pr_debug("Samples for '%s' event do not have %s attribute set. "
371 "Skipping '%s' field.\n",
372 evname, sample_msg, output_field2str(field));
373
374 return 0;
375}
376
Jiri Olsa32dcd022019-07-21 13:23:51 +0200377static int perf_evsel__check_stype(struct evsel *evsel,
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300378 u64 sample_type, const char *sample_msg,
379 enum perf_output_field field)
380{
381 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
382 false);
383}
384
Jiri Olsa32dcd022019-07-21 13:23:51 +0200385static int perf_evsel__check_attr(struct evsel *evsel,
David Ahern9cbdb702011-04-06 21:54:20 -0600386 struct perf_session *session)
387{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200388 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300389 bool allow_user_set;
390
Jiri Olsae099eba2016-01-05 22:09:09 +0100391 if (perf_header__has_feat(&session->header, HEADER_STAT))
392 return 0;
393
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300394 allow_user_set = perf_header__has_feat(&session->header,
395 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600396
David Ahern1424dc92011-03-09 22:23:28 -0700397 if (PRINT_FIELD(TRACE) &&
398 !perf_session__has_traces(session, "record -R"))
399 return -EINVAL;
400
David Ahern787bef12011-05-27 14:28:43 -0600401 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300402 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
403 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700404 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700405 }
David Ahern7cec0922011-05-30 13:08:23 -0600406
407 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300408 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
409 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600410 return -EINVAL;
411
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100412 if (PRINT_FIELD(DATA_SRC) &&
413 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
414 PERF_OUTPUT_DATA_SRC))
415 return -EINVAL;
416
417 if (PRINT_FIELD(WEIGHT) &&
418 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
419 PERF_OUTPUT_WEIGHT))
420 return -EINVAL;
421
Andi Kleen37fed3d2018-09-18 05:32:09 -0700422 if (PRINT_FIELD(SYM) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200423 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600424 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700425 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600426 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600427 return -EINVAL;
428 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900429 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
430 pr_err("Display of offsets requested but symbol is not"
431 "selected.\n");
432 return -EINVAL;
433 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700434 if (PRINT_FIELD(DSO) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200435 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
Andi Kleen37fed3d2018-09-18 05:32:09 -0700436 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600437 return -EINVAL;
438 }
Andi Kleendd2e18e2018-12-03 16:18:48 -0800439 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
Adrian Huntercc8fae12013-12-06 09:42:57 +0200440 pr_err("Display of source line number requested but sample IP is not\n"
441 "selected. Hence, no address to lookup the source line number.\n");
442 return -EINVAL;
443 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800444 if (PRINT_FIELD(BRSTACKINSN) &&
445 !(perf_evlist__combined_branch_type(session->evlist) &
446 PERF_SAMPLE_BRANCH_ANY)) {
447 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
448 "Hint: run 'perf record -b ...'\n");
449 return -EINVAL;
450 }
David Ahern1424dc92011-03-09 22:23:28 -0700451 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300452 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
453 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700454 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700455
456 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300457 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
458 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700459 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700460
461 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300462 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
463 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700464 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600465
Stephane Eranianfc36f942015-08-31 18:41:10 +0200466 if (PRINT_FIELD(IREGS) &&
467 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
468 PERF_OUTPUT_IREGS))
469 return -EINVAL;
470
Andi Kleenb1491ac2017-09-05 11:40:57 -0700471 if (PRINT_FIELD(UREGS) &&
472 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
473 PERF_OUTPUT_UREGS))
474 return -EINVAL;
475
Kan Liang49d58f02017-08-29 13:11:11 -0400476 if (PRINT_FIELD(PHYS_ADDR) &&
477 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
478 PERF_OUTPUT_PHYS_ADDR))
479 return -EINVAL;
480
David Ahern9cbdb702011-04-06 21:54:20 -0600481 return 0;
482}
483
Adrian Hunter7ea95722013-11-01 15:51:30 +0200484static void set_print_ip_opts(struct perf_event_attr *attr)
485{
Adrian Hunter14057202017-06-21 13:17:19 +0300486 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200487
488 output[type].print_ip_opts = 0;
489 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300490 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200491
492 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300493 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200494
495 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300496 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200497
498 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300499 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200500
501 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300502 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200503}
504
David Ahern9cbdb702011-04-06 21:54:20 -0600505/*
506 * verify all user requested events exist and the samples
507 * have the expected data
508 */
509static int perf_session__check_output_opt(struct perf_session *session)
510{
He Kuang40f20e52016-05-16 04:51:19 +0000511 unsigned int j;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200512 struct evsel *evsel;
David Ahern9cbdb702011-04-06 21:54:20 -0600513
Adrian Hunter14057202017-06-21 13:17:19 +0300514 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
515 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600516
517 /*
518 * even if fields is set to 0 (ie., show nothing) event must
519 * exist if user explicitly includes it on the command line
520 */
Adrian Hunter14057202017-06-21 13:17:19 +0300521 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
522 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600523 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300524 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600525 event_type(j));
526 return -1;
527 }
528
529 if (evsel && output[j].fields &&
530 perf_evsel__check_attr(evsel, session))
531 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400532
533 if (evsel == NULL)
534 continue;
535
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200536 set_print_ip_opts(&evsel->core.attr);
David Ahern1424dc92011-03-09 22:23:28 -0700537 }
538
Adrian Hunter98526ee2014-07-31 09:00:59 +0300539 if (!no_callchain) {
540 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000541 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300542
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300543 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000544 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300545 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300546 use_callchain = true;
547 break;
548 }
549 }
He Kuang71ac8992016-08-04 11:25:43 +0000550 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300551 symbol_conf.use_callchain = false;
552 }
553
David Ahern80b8b492013-11-19 21:07:37 -0700554 /*
555 * set default for tracepoints to print symbols only
556 * if callchains are present
557 */
558 if (symbol_conf.use_callchain &&
559 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700560 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700561
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300562 evlist__for_each_entry(session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200563 if (evsel->core.attr.type != j)
He Kuang40f20e52016-05-16 04:51:19 +0000564 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700565
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300566 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000567 output[j].fields |= PERF_OUTPUT_IP;
568 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530569 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000570 output[j].fields |= PERF_OUTPUT_DSO;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200571 set_print_ip_opts(&evsel->core.attr);
He Kuang40f20e52016-05-16 04:51:19 +0000572 goto out;
573 }
David Ahern80b8b492013-11-19 21:07:37 -0700574 }
575 }
576
577out:
David Ahern1424dc92011-03-09 22:23:28 -0700578 return 0;
579}
David Ahern745f43e2011-03-09 22:23:26 -0700580
Milian Wolff9add8fe2018-11-07 23:34:37 +0100581static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
582 FILE *fp
583)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200584{
Andi Kleenb1491ac2017-09-05 11:40:57 -0700585 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300586 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700587
588 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300589 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700590
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300591 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700592
593 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
594 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300595 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700596 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300597
Milian Wolffb07d16f2018-11-07 10:37:05 +0100598 fprintf(fp, "\n");
599
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300600 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700601}
602
Milian Wolff9add8fe2018-11-07 23:34:37 +0100603static int perf_sample__fprintf_iregs(struct perf_sample *sample,
604 struct perf_event_attr *attr, FILE *fp)
605{
606 return perf_sample__fprintf_regs(&sample->intr_regs,
607 attr->sample_regs_intr, fp);
608}
609
610static int perf_sample__fprintf_uregs(struct perf_sample *sample,
611 struct perf_event_attr *attr, FILE *fp)
612{
613 return perf_sample__fprintf_regs(&sample->user_regs,
614 attr->sample_regs_user, fp);
615}
616
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300617static int perf_sample__fprintf_start(struct perf_sample *sample,
618 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200619 struct evsel *evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100620 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700621{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200622 struct perf_event_attr *attr = &evsel->core.attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700623 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700624 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300625 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700626
David Ahern745f43e2011-03-09 22:23:26 -0700627 if (PRINT_FIELD(COMM)) {
628 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300629 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300630 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300631 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700632 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300633 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700634 }
David Ahernc70c94b2011-03-09 22:23:25 -0700635
David Ahern745f43e2011-03-09 22:23:26 -0700636 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300637 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700638 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300639 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700640 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300641 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700642
David Ahern745f43e2011-03-09 22:23:26 -0700643 if (PRINT_FIELD(CPU)) {
644 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300645 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700646 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300647 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700648 }
David Ahernc70c94b2011-03-09 22:23:25 -0700649
Jiri Olsa28a0b392018-01-07 17:03:52 +0100650 if (PRINT_FIELD(MISC)) {
651 int ret = 0;
652
653 #define has(m) \
654 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
655
656 if (has(KERNEL))
657 ret += fprintf(fp, "K");
658 if (has(USER))
659 ret += fprintf(fp, "U");
660 if (has(HYPERVISOR))
661 ret += fprintf(fp, "H");
662 if (has(GUEST_KERNEL))
663 ret += fprintf(fp, "G");
664 if (has(GUEST_USER))
665 ret += fprintf(fp, "g");
666
667 switch (type) {
668 case PERF_RECORD_MMAP:
669 case PERF_RECORD_MMAP2:
670 if (has(MMAP_DATA))
671 ret += fprintf(fp, "M");
672 break;
673 case PERF_RECORD_COMM:
674 if (has(COMM_EXEC))
675 ret += fprintf(fp, "E");
676 break;
677 case PERF_RECORD_SWITCH:
678 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300679 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100680 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300681 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
682 ret += fprintf(fp, "p");
683 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100684 default:
685 break;
686 }
687
688 #undef has
689
690 ret += fprintf(fp, "%*s", 6 - ret, " ");
691 printed += ret;
692 }
693
David Ahern745f43e2011-03-09 22:23:26 -0700694 if (PRINT_FIELD(TIME)) {
Andi Kleen90b10f42019-03-14 15:50:00 -0700695 u64 t = sample->time;
696 if (reltime) {
697 if (!initial_time)
698 initial_time = sample->time;
699 t = sample->time - initial_time;
700 }
701 nsecs = t;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300702 secs = nsecs / NSEC_PER_SEC;
703 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900704
Andi Kleen52bab882019-03-05 06:47:47 -0800705 if (symbol_conf.nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300706 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900707 else {
708 char sample_time[32];
Andi Kleen90b10f42019-03-14 15:50:00 -0700709 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300710 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900711 }
David Ahern745f43e2011-03-09 22:23:26 -0700712 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300713
714 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700715}
716
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200717static inline char
718mispred_str(struct branch_entry *br)
719{
720 if (!(br->flags.mispred || br->flags.predicted))
721 return '-';
722
723 return br->flags.predicted ? 'P' : 'M';
724}
725
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300726static int perf_sample__fprintf_brstack(struct perf_sample *sample,
727 struct thread *thread,
728 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200729{
730 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700731 struct addr_location alf, alt;
732 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300733 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200734
735 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300736 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200737
738 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700739 from = br->entries[i].from;
740 to = br->entries[i].to;
741
742 if (PRINT_FIELD(DSO)) {
743 memset(&alf, 0, sizeof(alf));
744 memset(&alt, 0, sizeof(alt));
Adrian Hunter692d0e62018-11-06 23:07:12 +0200745 thread__find_map_fb(thread, sample->cpumode, from, &alf);
746 thread__find_map_fb(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700747 }
748
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300749 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700750 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300751 printed += fprintf(fp, "(");
752 printed += map__fprintf_dsoname(alf.map, fp);
753 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700754 }
755
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300756 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700757 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300758 printed += fprintf(fp, "(");
759 printed += map__fprintf_dsoname(alt.map, fp);
760 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700761 }
762
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300763 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200764 mispred_str( br->entries + i),
765 br->entries[i].flags.in_tx? 'X' : '-',
766 br->entries[i].flags.abort? 'A' : '-',
767 br->entries[i].flags.cycles);
768 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300769
770 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200771}
772
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300773static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
774 struct thread *thread,
775 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200776{
777 struct branch_stack *br = sample->branch_stack;
778 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200779 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300780 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200781
782 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300783 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200784
785 for (i = 0; i < br->nr; i++) {
786
787 memset(&alf, 0, sizeof(alf));
788 memset(&alt, 0, sizeof(alt));
789 from = br->entries[i].from;
790 to = br->entries[i].to;
791
Adrian Hunter692d0e62018-11-06 23:07:12 +0200792 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
793 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200794
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300795 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700796 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300797 printed += fprintf(fp, "(");
798 printed += map__fprintf_dsoname(alf.map, fp);
799 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700800 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300801 printed += fprintf(fp, "%c", '/');
802 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700803 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300804 printed += fprintf(fp, "(");
805 printed += map__fprintf_dsoname(alt.map, fp);
806 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700807 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300808 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200809 mispred_str( br->entries + i),
810 br->entries[i].flags.in_tx? 'X' : '-',
811 br->entries[i].flags.abort? 'A' : '-',
812 br->entries[i].flags.cycles);
813 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300814
815 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200816}
817
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300818static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
819 struct thread *thread,
820 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700821{
822 struct branch_stack *br = sample->branch_stack;
823 struct addr_location alf, alt;
824 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300825 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700826
827 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300828 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700829
830 for (i = 0; i < br->nr; i++) {
831
832 memset(&alf, 0, sizeof(alf));
833 memset(&alt, 0, sizeof(alt));
834 from = br->entries[i].from;
835 to = br->entries[i].to;
836
Adrian Hunter692d0e62018-11-06 23:07:12 +0200837 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300838 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700839 from = map__map_ip(alf.map, from);
840
Adrian Hunter692d0e62018-11-06 23:07:12 +0200841 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300842 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700843 to = map__map_ip(alt.map, to);
844
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300845 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700846 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300847 printed += fprintf(fp, "(");
848 printed += map__fprintf_dsoname(alf.map, fp);
849 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700850 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300851 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700852 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300853 printed += fprintf(fp, "(");
854 printed += map__fprintf_dsoname(alt.map, fp);
855 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700856 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300857 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700858 mispred_str(br->entries + i),
859 br->entries[i].flags.in_tx ? 'X' : '-',
860 br->entries[i].flags.abort ? 'A' : '-',
861 br->entries[i].flags.cycles);
862 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300863
864 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700865}
Andi Kleen48d02a12017-02-23 15:46:34 -0800866#define MAXBB 16384UL
867
868static int grab_bb(u8 *buffer, u64 start, u64 end,
869 struct machine *machine, struct thread *thread,
870 bool *is64bit, u8 *cpumode, bool last)
871{
872 long offset, len;
873 struct addr_location al;
874 bool kernel;
875
876 if (!start || !end)
877 return 0;
878
879 kernel = machine__kernel_ip(machine, start);
880 if (kernel)
881 *cpumode = PERF_RECORD_MISC_KERNEL;
882 else
883 *cpumode = PERF_RECORD_MISC_USER;
884
885 /*
886 * Block overlaps between kernel and user.
887 * This can happen due to ring filtering
888 * On Intel CPUs the entry into the kernel is filtered,
889 * but the exit is not. Let the caller patch it up.
890 */
891 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300892 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800893 return -ENXIO;
894 }
895
896 memset(&al, 0, sizeof(al));
897 if (end - start > MAXBB - MAXINSN) {
898 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300899 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800900 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300901 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800902 return 0;
903 }
904
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300905 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300906 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800907 return 0;
908 }
909 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300910 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800911 return 0;
912 }
913
914 /* Load maps to ensure dso->is_64_bit has been updated */
915 map__load(al.map);
916
917 offset = al.map->map_ip(al.map, start);
918 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
919 end - start + MAXINSN);
920
921 *is64bit = al.map->dso->is_64_bit;
922 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300923 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800924 start, end);
925 return len;
926}
927
Andi Kleendd2e18e2018-12-03 16:18:48 -0800928static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
929{
930 struct addr_location al;
931 int ret = 0;
932
933 memset(&al, 0, sizeof(al));
934 thread__find_map(thread, cpumode, addr, &al);
935 if (!al.map)
936 return 0;
937 ret = map__fprintf_srccode(al.map, al.addr, stdout,
938 &thread->srccode_state);
939 if (ret)
940 ret += printf("\n");
941 return ret;
942}
943
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300944static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
945 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -0700946 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -0800947{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300948 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
949 dump_insn(x, ip, inbuf, len, NULL),
950 en->flags.predicted ? " PRED" : "",
951 en->flags.mispred ? " MISPRED" : "",
952 en->flags.in_tx ? " INTX" : "",
953 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800954 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -0700955 *total_cycles += en->flags.cycles;
956 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800957 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300958 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800959 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300960 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800961}
962
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300963static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
964 u8 cpumode, int cpu, struct symbol **lastsym,
965 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800966{
967 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300968 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800969
970 memset(&al, 0, sizeof(al));
971
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300972 thread__find_map(thread, cpumode, addr, &al);
973
Andi Kleen48d02a12017-02-23 15:46:34 -0800974 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300975 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800976
977 al.cpu = cpu;
978 al.sym = NULL;
979 if (al.map)
980 al.sym = map__find_symbol(al.map, al.addr);
981
982 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300983 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800984
985 if (al.addr < al.sym->end)
986 off = al.addr - al.sym->start;
987 else
988 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300989 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800990 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300991 printed += fprintf(fp, "%+d", off);
992 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800993 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300994 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
995 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800996 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300997
998 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800999}
1000
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001001static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1002 struct thread *thread,
1003 struct perf_event_attr *attr,
1004 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -08001005{
1006 struct branch_stack *br = sample->branch_stack;
1007 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001008 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001009 struct perf_insn x;
1010 u8 buffer[MAXBB];
1011 unsigned off;
1012 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -07001013 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001014
1015 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001016 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001017 nr = br->nr;
1018 if (max_blocks && nr > max_blocks + 1)
1019 nr = max_blocks + 1;
1020
1021 x.thread = thread;
1022 x.cpu = sample->cpu;
1023
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001024 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -08001025
1026 /* Handle first from jump, of which we don't know the entry. */
1027 len = grab_bb(buffer, br->entries[nr-1].from,
1028 br->entries[nr-1].from,
1029 machine, thread, &x.is64bit, &x.cpumode, false);
1030 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001031 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1032 x.cpumode, x.cpu, &lastsym, attr, fp);
1033 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001034 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001035 if (PRINT_FIELD(SRCCODE))
1036 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
Andi Kleen48d02a12017-02-23 15:46:34 -08001037 }
1038
1039 /* Print all blocks */
1040 for (i = nr - 2; i >= 0; i--) {
1041 if (br->entries[i].from || br->entries[i].to)
1042 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1043 br->entries[i].from,
1044 br->entries[i].to);
1045 start = br->entries[i + 1].to;
1046 end = br->entries[i].from;
1047
1048 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1049 /* Patch up missing kernel transfers due to ring filters */
1050 if (len == -ENXIO && i > 0) {
1051 end = br->entries[--i].from;
1052 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1053 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1054 }
1055 if (len <= 0)
1056 continue;
1057
1058 insn = 0;
1059 for (off = 0;; off += ilen) {
1060 uint64_t ip = start + off;
1061
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001062 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001063 if (ip == end) {
Andi Kleendde4e732019-07-11 11:19:21 -07001064 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, ++insn, fp,
Andi Kleenfe571202018-09-24 10:07:32 -07001065 &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001066 if (PRINT_FIELD(SRCCODE))
1067 printed += print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001068 break;
1069 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001070 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1071 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001072 if (ilen == 0)
1073 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001074 if (PRINT_FIELD(SRCCODE))
1075 print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001076 insn++;
1077 }
1078 }
1079 }
1080
1081 /*
1082 * Hit the branch? In this case we are already done, and the target
1083 * has not been executed yet.
1084 */
1085 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001086 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001087 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001088 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001089
1090 /*
1091 * Print final block upto sample
Andi Kleen61f61152018-11-19 21:06:17 -08001092 *
1093 * Due to pipeline delays the LBRs might be missing a branch
1094 * or two, which can result in very large or negative blocks
1095 * between final branch and sample. When this happens just
1096 * continue walking after the last TO until we hit a branch.
Andi Kleen48d02a12017-02-23 15:46:34 -08001097 */
1098 start = br->entries[0].to;
1099 end = sample->ip;
Andi Kleen61f61152018-11-19 21:06:17 -08001100 if (end < start) {
1101 /* Missing jump. Scan 128 bytes for the next branch */
1102 end = start + 128;
1103 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001104 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001105 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001106 if (len <= 0) {
1107 /* Print at least last IP if basic block did not work */
1108 len = grab_bb(buffer, sample->ip, sample->ip,
1109 machine, thread, &x.is64bit, &x.cpumode, false);
1110 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001111 goto out;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001112 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001113 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001114 if (PRINT_FIELD(SRCCODE))
1115 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001116 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001117 }
1118 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001119 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1120 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001121 if (ilen == 0)
1122 break;
Andi Kleen61f61152018-11-19 21:06:17 -08001123 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1124 /*
1125 * Hit a missing branch. Just stop.
1126 */
1127 printed += fprintf(fp, "\t... not reaching sample ...\n");
1128 break;
1129 }
Andi Kleendd2e18e2018-12-03 16:18:48 -08001130 if (PRINT_FIELD(SRCCODE))
1131 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001132 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001133out:
1134 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001135}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001136
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001137static int perf_sample__fprintf_addr(struct perf_sample *sample,
1138 struct thread *thread,
1139 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001140{
1141 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001142 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001143
1144 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001145 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001146
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001147 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001148
1149 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001150 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001151 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001152 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001153 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001154 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001155 }
1156
1157 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001158 printed += fprintf(fp, " (");
1159 printed += map__fprintf_dsoname(al.map, fp);
1160 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001161 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001162out:
1163 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001164}
1165
Andi Kleen99f753f2018-09-20 11:05:39 -07001166static const char *resolve_branch_sym(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001167 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001168 struct thread *thread,
1169 struct addr_location *al,
1170 u64 *ip)
1171{
1172 struct addr_location addr_al;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001173 struct perf_event_attr *attr = &evsel->core.attr;
Andi Kleen99f753f2018-09-20 11:05:39 -07001174 const char *name = NULL;
1175
1176 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1177 if (sample_addr_correlates_sym(attr)) {
1178 thread__resolve(thread, &addr_al, sample);
1179 if (addr_al.sym)
1180 name = addr_al.sym->name;
1181 else
1182 *ip = sample->addr;
1183 } else {
1184 *ip = sample->addr;
1185 }
1186 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1187 if (al->sym)
1188 name = al->sym->name;
1189 else
1190 *ip = sample->ip;
1191 }
1192 return name;
1193}
1194
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001195static int perf_sample__fprintf_callindent(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001196 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001197 struct thread *thread,
1198 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001199{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001200 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter256d92b2018-12-21 14:06:19 +02001201 size_t depth = thread_stack__depth(thread, sample->cpu);
Adrian Huntere2167082016-06-23 16:40:58 +03001202 const char *name = NULL;
1203 static int spacing;
1204 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001205 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001206 u64 ip = 0;
1207
1208 /*
1209 * The 'return' has already been popped off the stack so the depth has
1210 * to be adjusted to match the 'call'.
1211 */
1212 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1213 depth += 1;
1214
Andi Kleen99f753f2018-09-20 11:05:39 -07001215 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001216
Andi Kleena78cdee2018-09-18 05:32:10 -07001217 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1218 dlen += fprintf(fp, "(");
1219 dlen += map__fprintf_dsoname(al->map, fp);
1220 dlen += fprintf(fp, ")\t");
1221 }
1222
Adrian Huntere2167082016-06-23 16:40:58 +03001223 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001224 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001225 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001226 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001227
1228 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001229 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001230
1231 /*
1232 * Try to keep the output length from changing frequently so that the
1233 * output lines up more nicely.
1234 */
1235 if (len > spacing || (len && len < spacing - 52))
1236 spacing = round_up(len + 4, 32);
1237
1238 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001239 len += fprintf(fp, "%*s", spacing - len, "");
1240
Andi Kleena78cdee2018-09-18 05:32:10 -07001241 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001242}
1243
Andi Kleen3ab481a2019-03-05 06:47:45 -08001244__weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1245 struct thread *thread __maybe_unused,
1246 struct machine *machine __maybe_unused)
1247{
1248}
1249
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001250static int perf_sample__fprintf_insn(struct perf_sample *sample,
1251 struct perf_event_attr *attr,
1252 struct thread *thread,
1253 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001254{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001255 int printed = 0;
1256
Andi Kleen3ab481a2019-03-05 06:47:45 -08001257 if (sample->insn_len == 0 && native_arch)
1258 arch_fetch_insn(sample, thread, machine);
1259
Andi Kleen224e2c92016-10-07 16:42:27 +03001260 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001261 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen3ab481a2019-03-05 06:47:45 -08001262 if (PRINT_FIELD(INSN) && sample->insn_len) {
Andi Kleen224e2c92016-10-07 16:42:27 +03001263 int i;
1264
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001265 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001266 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001267 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001268 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001269 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001270 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1271
1272 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001273}
1274
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001275static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1276 struct perf_event_attr *attr, FILE *fp)
1277{
1278 unsigned int ipc;
1279
1280 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1281 return 0;
1282
1283 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1284
1285 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1286 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1287}
1288
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001289static int perf_sample__fprintf_bts(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001290 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001291 struct thread *thread,
1292 struct addr_location *al,
1293 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001294{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001295 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001296 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001297 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001298 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001299
Adrian Huntere2167082016-06-23 16:40:58 +03001300 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001301 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001302
Akihiro Nagai95582592012-01-30 13:43:09 +09001303 /* print branch_from information */
1304 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001305 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001306 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001307
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001308 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001309 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001310 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001311 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001312
1313 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001314 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001315 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001316 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001317 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001318 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001319 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001320 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001321
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001322 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001323 }
1324
Akihiro Nagai95582592012-01-30 13:43:09 +09001325 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001326 if (PRINT_FIELD(ADDR) ||
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001327 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001328 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001329 printed += fprintf(fp, " => ");
1330 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001331 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001332
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001333 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1334
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001335 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001336 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001337
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001338 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001339 printed += fprintf(fp, "\n");
1340 if (PRINT_FIELD(SRCCODE)) {
1341 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1342 &thread->srccode_state);
1343 if (ret) {
1344 printed += ret;
1345 printed += printf("\n");
1346 }
1347 }
1348 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001349}
1350
Adrian Hunter055cd332016-06-23 16:40:56 +03001351static struct {
1352 u32 flags;
1353 const char *name;
1354} sample_flags[] = {
1355 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1356 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1357 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1358 {PERF_IP_FLAG_BRANCH, "jmp"},
1359 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1360 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1361 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1362 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1363 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1364 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1365 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1366 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1367 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1368 {0, NULL}
1369};
1370
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001371static const char *sample_flags_to_name(u32 flags)
1372{
1373 int i;
1374
1375 for (i = 0; sample_flags[i].name ; i++) {
1376 if (sample_flags[i].flags == flags)
1377 return sample_flags[i].name;
1378 }
1379
1380 return NULL;
1381}
1382
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001383static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001384{
1385 const char *chars = PERF_IP_FLAG_CHARS;
1386 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001387 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1388 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001389 char str[33];
1390 int i, pos = 0;
1391
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001392 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1393 if (name)
1394 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1395
1396 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1397 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1398 if (name)
1399 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1400 }
1401
1402 if (flags & PERF_IP_FLAG_TRACE_END) {
1403 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1404 if (name)
1405 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001406 }
1407
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001408 for (i = 0; i < n; i++, flags >>= 1) {
1409 if (flags & 1)
1410 str[pos++] = chars[i];
1411 }
1412 for (; i < 32; i++, flags >>= 1) {
1413 if (flags & 1)
1414 str[pos++] = '?';
1415 }
1416 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001417
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001418 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001419}
1420
Wang Nan30372f02016-02-24 11:20:45 +00001421struct printer_data {
1422 int line_no;
1423 bool hit_nul;
1424 bool is_printable;
1425};
1426
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001427static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1428 unsigned int val,
1429 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001430{
1431 unsigned char ch = (unsigned char)val;
1432 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001433 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001434
1435 switch (op) {
1436 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001437 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001438 break;
1439 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001440 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001441 " ");
1442 break;
1443 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001444 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001445 break;
1446 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001447 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001448 break;
1449 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001450 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001451 break;
1452 case BINARY_PRINT_SEP:
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_CHAR_DATA:
1456 if (printer_data->hit_nul && ch)
1457 printer_data->is_printable = false;
1458
1459 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001460 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001461
1462 if (!printer_data->is_printable)
1463 break;
1464
1465 if (ch == '\0')
1466 printer_data->hit_nul = true;
1467 else
1468 printer_data->is_printable = false;
1469 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001470 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001471 }
1472 break;
1473 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001474 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001475 break;
1476 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001477 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001478 printer_data->line_no++;
1479 break;
1480 case BINARY_PRINT_DATA_END:
1481 default:
1482 break;
1483 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001484
1485 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001486}
1487
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001488static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001489{
1490 unsigned int nr_bytes = sample->raw_size;
1491 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001492 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1493 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001494
1495 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001496 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1497
1498 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001499}
1500
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001501static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001502{
1503 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001504 return fprintf(fp, "%*s", spacing - len, "");
1505
1506 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001507}
1508
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001509static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001510{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001511 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001512}
1513
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001514static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001515{
1516 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1517 int len;
1518
1519 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001520 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001521
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001522 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001523 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001524 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001525}
1526
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001527static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001528{
1529 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1530 int len;
1531
1532 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001533 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001534
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001535 len = fprintf(fp, " hints: %#x extensions: %#x ",
1536 data->hints, data->extensions);
1537 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001538}
1539
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001540static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001541{
1542 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1543 int len;
1544
1545 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001546 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001547
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001548 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1549 data->hw, data->cstate, data->subcstate);
1550 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001551}
1552
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001553static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001554{
1555 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1556 int len;
1557
1558 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001559 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001560
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001561 len = fprintf(fp, " IP: %u ", data->ip);
1562 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001563}
1564
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001565static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001566{
1567 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1568 int len;
1569
1570 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001571 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001572
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001573 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001574 data->deepest_cstate, data->last_cstate,
1575 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001576 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001577}
1578
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001579static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001580{
1581 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1582 unsigned int percent, freq;
1583 int len;
1584
1585 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001586 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001587
1588 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001589 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001590 if (data->max_nonturbo) {
1591 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001592 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001593 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001594 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001595}
1596
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001597static int perf_sample__fprintf_synth(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001598 struct evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001599{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001600 switch (evsel->core.attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001601 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001602 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001603 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001604 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001605 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001606 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001607 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001608 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001609 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001610 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001611 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001612 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001613 default:
1614 break;
1615 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001616
1617 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001618}
1619
Jiri Olsa809e9422015-11-26 18:55:21 +01001620struct perf_script {
1621 struct perf_tool tool;
1622 struct perf_session *session;
1623 bool show_task_events;
1624 bool show_mmap_events;
1625 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301626 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001627 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001628 bool show_round_events;
Jiri Olsa490c8cc2019-05-08 15:20:08 +02001629 bool show_bpf_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001630 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001631 bool per_event_dump;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001632 struct evswitch evswitch;
Jiri Olsaf8548392019-07-21 13:23:49 +02001633 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +02001634 struct perf_thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001635 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001636 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001637 struct perf_time_interval *ptime_range;
1638 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001639 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001640};
1641
Jiri Olsa63503db2019-07-21 13:23:52 +02001642static int perf_evlist__max_name_len(struct evlist *evlist)
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001643{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001644 struct evsel *evsel;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001645 int max = 0;
1646
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001647 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001648 int len = strlen(perf_evsel__name(evsel));
1649
1650 max = MAX(len, max);
1651 }
1652
1653 return max;
1654}
1655
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001656static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001657{
1658 struct mem_info mi = { .data_src.val = data_src };
1659 char decode[100];
1660 char out[100];
1661 static int maxlen;
1662 int len;
1663
1664 perf_script__meminfo_scnprintf(decode, 100, &mi);
1665
1666 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1667 if (maxlen < len)
1668 maxlen = len;
1669
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001670 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001671}
1672
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001673struct metric_ctx {
1674 struct perf_sample *sample;
1675 struct thread *thread;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001676 struct evsel *evsel;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001677 FILE *fp;
1678};
1679
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001680static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1681 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001682 const char *fmt,
1683 const char *unit, double val)
1684{
1685 struct metric_ctx *mctx = ctx;
1686
1687 if (!fmt)
1688 return;
1689 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001690 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001691 fputs("\tmetric: ", mctx->fp);
1692 if (color)
1693 color_fprintf(mctx->fp, color, fmt, val);
1694 else
1695 printf(fmt, val);
1696 fprintf(mctx->fp, " %s\n", unit);
1697}
1698
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001699static void script_new_line(struct perf_stat_config *config __maybe_unused,
1700 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001701{
1702 struct metric_ctx *mctx = ctx;
1703
1704 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001705 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001706 fputs("\tmetric: ", mctx->fp);
1707}
1708
1709static void perf_sample__fprint_metric(struct perf_script *script,
1710 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001711 struct evsel *evsel,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001712 struct perf_sample *sample,
1713 FILE *fp)
1714{
1715 struct perf_stat_output_ctx ctx = {
1716 .print_metric = script_print_metric,
1717 .new_line = script_new_line,
1718 .ctx = &(struct metric_ctx) {
1719 .sample = sample,
1720 .thread = thread,
1721 .evsel = evsel,
1722 .fp = fp,
1723 },
1724 .force_header = false,
1725 };
Jiri Olsa32dcd022019-07-21 13:23:51 +02001726 struct evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001727 u64 val;
1728
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001729 if (!evsel->stats)
1730 perf_evlist__alloc_stats(script->session->evlist, false);
1731 if (evsel_script(evsel->leader)->gnum++ == 0)
1732 perf_stat__reset_shadow_stats();
1733 val = sample->period * evsel->scale;
1734 perf_stat__update_shadow_stats(evsel,
1735 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001736 sample->cpu,
1737 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001738 evsel_script(evsel)->val = val;
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001739 if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) {
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001740 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001741 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001742 evsel_script(ev2)->val,
1743 sample->cpu,
1744 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001745 NULL,
1746 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001747 }
1748 evsel_script(evsel->leader)->gnum = 0;
1749 }
1750}
1751
Andi Kleen99f753f2018-09-20 11:05:39 -07001752static bool show_event(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001753 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001754 struct thread *thread,
1755 struct addr_location *al)
1756{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001757 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001758
1759 if (!symbol_conf.graph_function)
1760 return true;
1761
1762 if (thread->filter) {
1763 if (depth <= thread->filter_entry_depth) {
1764 thread->filter = false;
1765 return false;
1766 }
1767 return true;
1768 } else {
1769 const char *s = symbol_conf.graph_function;
1770 u64 ip;
1771 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1772 &ip);
1773 unsigned nlen;
1774
1775 if (!name)
1776 return false;
1777 nlen = strlen(name);
1778 while (*s) {
1779 unsigned len = strcspn(s, ",");
1780 if (nlen == len && !strncmp(name, s, len)) {
1781 thread->filter = true;
1782 thread->filter_entry_depth = depth;
1783 return true;
1784 }
1785 s += len;
1786 if (*s == ',')
1787 s++;
1788 }
1789 return false;
1790 }
1791}
1792
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001793static void process_event(struct perf_script *script,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001794 struct perf_sample *sample, struct evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001795 struct addr_location *al,
1796 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001797{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001798 struct thread *thread = al->thread;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001799 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001800 unsigned int type = output_type(attr->type);
Jiri Olsa32dcd022019-07-21 13:23:51 +02001801 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001802 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001803
Adrian Hunter14057202017-06-21 13:17:19 +03001804 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001805 return;
1806
Andi Kleen99f753f2018-09-20 11:05:39 -07001807 if (!show_event(sample, evsel, thread, al))
1808 return;
1809
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001810 if (script->evswitch.on && script->evswitch.discarding) {
1811 if (script->evswitch.on != evsel)
1812 return;
1813
1814 script->evswitch.discarding = false;
Arnaldo Carvalho de Melo6469eb62019-08-14 16:40:58 -03001815
1816 if (!script->evswitch.show_on_off_events)
1817 return;
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03001818
1819 goto print_it;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001820 }
1821
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03001822 if (script->evswitch.off && !script->evswitch.discarding) {
1823 if (script->evswitch.off != evsel)
1824 goto print_it;
1825
1826 script->evswitch.discarding = true;
1827
1828 if (!script->evswitch.show_on_off_events)
1829 return;
1830 }
1831print_it:
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001832 ++es->samples;
1833
Jiri Olsa28a0b392018-01-07 17:03:52 +01001834 perf_sample__fprintf_start(sample, thread, evsel,
1835 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001836
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001837 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001838 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001839
Namhyung Kime944d3d2013-11-18 14:34:52 +09001840 if (PRINT_FIELD(EVNAME)) {
1841 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001842
1843 if (!script->name_width)
1844 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1845
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001846 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001847 }
1848
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001849 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001850 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001851
Akihiro Nagai95582592012-01-30 13:43:09 +09001852 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001853 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001854 return;
1855 }
1856
Andi Kleen96167162019-01-17 11:48:34 -08001857 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001858 event_format__fprintf(evsel->tp_format, sample->cpu,
1859 sample->raw_data, sample->raw_size, fp);
1860 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001861
1862 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001863 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001864
David Ahern7cec0922011-05-30 13:08:23 -06001865 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001866 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001867
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001868 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001869 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001870
1871 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001872 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001873
David Ahern787bef12011-05-27 14:28:43 -06001874 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001875 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001876
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001877 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001878 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001879 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001880 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001881
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001882 fputc(cursor ? '\n' : ' ', fp);
1883 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001884 }
1885
Stephane Eranianfc36f942015-08-31 18:41:10 +02001886 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001887 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001888
Andi Kleenb1491ac2017-09-05 11:40:57 -07001889 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001890 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001891
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001892 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001893 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001894 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001895 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001896 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001897 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001898
Wang Nan30372f02016-02-24 11:20:45 +00001899 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001900 perf_sample__fprintf_bpf_output(sample, fp);
1901 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001902
1903 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001904 fprintf(fp, "%16" PRIx64, sample->phys_addr);
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001905
1906 perf_sample__fprintf_ipc(sample, attr, fp);
1907
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001908 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001909
Andi Kleendd2e18e2018-12-03 16:18:48 -08001910 if (PRINT_FIELD(SRCCODE)) {
1911 if (map__fprintf_srccode(al->map, al->addr, stdout,
1912 &thread->srccode_state))
1913 printf("\n");
1914 }
1915
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001916 if (PRINT_FIELD(METRIC))
1917 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001918
1919 if (verbose)
1920 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001921}
1922
Tom Zanussi956ffd02009-11-25 01:15:46 -06001923static struct scripting_ops *scripting_ops;
1924
Jiri Olsa32dcd022019-07-21 13:23:51 +02001925static void __process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsa36e33c52016-01-06 11:49:56 +01001926{
Jiri Olsaaf663bd2019-07-21 13:24:39 +02001927 int nthreads = thread_map__nr(counter->core.threads);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001928 int ncpus = perf_evsel__nr_cpus(counter);
1929 int cpu, thread;
1930 static int header_printed;
1931
1932 if (counter->system_wide)
1933 nthreads = 1;
1934
1935 if (!header_printed) {
1936 printf("%3s %8s %15s %15s %15s %15s %s\n",
1937 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1938 header_printed = 1;
1939 }
1940
1941 for (thread = 0; thread < nthreads; thread++) {
1942 for (cpu = 0; cpu < ncpus; cpu++) {
1943 struct perf_counts_values *counts;
1944
1945 counts = perf_counts(counter->counts, cpu, thread);
1946
1947 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
Jiri Olsad400bd32019-07-21 13:24:37 +02001948 counter->core.cpus->map[cpu],
Jiri Olsaaf663bd2019-07-21 13:24:39 +02001949 thread_map__pid(counter->core.threads, thread),
Jiri Olsa36e33c52016-01-06 11:49:56 +01001950 counts->val,
1951 counts->ena,
1952 counts->run,
1953 tstamp,
1954 perf_evsel__name(counter));
1955 }
1956 }
1957}
1958
Jiri Olsa32dcd022019-07-21 13:23:51 +02001959static void process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsae099eba2016-01-05 22:09:09 +01001960{
1961 if (scripting_ops && scripting_ops->process_stat)
1962 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001963 else
1964 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001965}
1966
1967static void process_stat_interval(u64 tstamp)
1968{
1969 if (scripting_ops && scripting_ops->process_stat_interval)
1970 scripting_ops->process_stat_interval(tstamp);
1971}
1972
Tom Zanussi956ffd02009-11-25 01:15:46 -06001973static void setup_scripting(void)
1974{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001975 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001976 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001977}
1978
Adrian Hunterd445dd22014-08-15 22:08:37 +03001979static int flush_scripting(void)
1980{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001981 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001982}
1983
Tom Zanussi956ffd02009-11-25 01:15:46 -06001984static int cleanup_scripting(void)
1985{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001986 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001987
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001988 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001989}
1990
Andi Kleene87e5482019-03-11 07:44:52 -07001991static bool filter_cpu(struct perf_sample *sample)
1992{
1993 if (cpu_list)
1994 return !test_bit(sample->cpu, cpu_bitmap);
1995 return false;
1996}
1997
Jiri Olsa809e9422015-11-26 18:55:21 +01001998static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001999 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002000 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002001 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002002 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002003{
Jiri Olsa809e9422015-11-26 18:55:21 +01002004 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07002005 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002006
Jin Yao2ab046c2017-12-08 21:13:46 +08002007 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
2008 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07002009 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08002010 }
David Aherna91f4c42016-11-29 10:15:43 -07002011
David Ahern1424dc92011-03-09 22:23:28 -07002012 if (debug_mode) {
2013 if (sample->time < last_timestamp) {
2014 pr_err("Samples misordered, previous: %" PRIu64
2015 " this: %" PRIu64 "\n", last_timestamp,
2016 sample->time);
2017 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02002018 }
David Ahern1424dc92011-03-09 22:23:28 -07002019 last_timestamp = sample->time;
2020 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002021 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002022
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03002023 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07002024 pr_err("problem processing %d event, skipping it.\n",
2025 event->header.type);
2026 return -1;
2027 }
2028
2029 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002030 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07002031
Andi Kleene87e5482019-03-11 07:44:52 -07002032 if (filter_cpu(sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002033 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002034
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002035 if (scripting_ops)
2036 scripting_ops->process_event(event, sample, evsel, &al);
2037 else
Andi Kleen48d02a12017-02-23 15:46:34 -08002038 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002039
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002040out_put:
2041 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002042 return 0;
2043}
2044
Adrian Hunter7ea95722013-11-01 15:51:30 +02002045static int process_attr(struct perf_tool *tool, union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02002046 struct evlist **pevlist)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002047{
2048 struct perf_script *scr = container_of(tool, struct perf_script, tool);
Jiri Olsa63503db2019-07-21 13:23:52 +02002049 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002050 struct evsel *evsel, *pos;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002051 int err;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002052 static struct evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002053
2054 err = perf_event__process_attr(tool, event, pevlist);
2055 if (err)
2056 return err;
2057
2058 evlist = *pevlist;
2059 evsel = perf_evlist__last(*pevlist);
2060
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302061 if (!evsel->priv) {
2062 if (scr->per_event_dump) {
2063 evsel->priv = perf_evsel_script__new(evsel,
2064 scr->session->data);
2065 } else {
2066 es = zalloc(sizeof(*es));
2067 if (!es)
2068 return -ENOMEM;
2069 es->fp = stdout;
2070 evsel->priv = es;
2071 }
2072 }
2073
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002074 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2075 evsel->core.attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002076 return 0;
2077
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002078 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002079 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002080 return 0;
2081 }
2082
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002083 set_print_ip_opts(&evsel->core.attr);
Adrian Hunter7ea95722013-11-01 15:51:30 +02002084
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002085 if (evsel->core.attr.sample_type)
Jiri Olsad2b5a312015-10-16 12:41:25 +02002086 err = perf_evsel__check_attr(evsel, scr->session);
2087
2088 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002089}
2090
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002091static int process_comm_event(struct perf_tool *tool,
2092 union perf_event *event,
2093 struct perf_sample *sample,
2094 struct machine *machine)
2095{
2096 struct thread *thread;
2097 struct perf_script *script = container_of(tool, struct perf_script, tool);
2098 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002099 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002100 int ret = -1;
2101
2102 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2103 if (thread == NULL) {
2104 pr_debug("problem processing COMM event, skipping it.\n");
2105 return -1;
2106 }
2107
2108 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2109 goto out;
2110
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002111 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002112 sample->cpu = 0;
2113 sample->time = 0;
2114 sample->tid = event->comm.tid;
2115 sample->pid = event->comm.pid;
2116 }
Andi Kleene87e5482019-03-11 07:44:52 -07002117 if (!filter_cpu(sample)) {
2118 perf_sample__fprintf_start(sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01002119 PERF_RECORD_COMM, stdout);
Andi Kleene87e5482019-03-11 07:44:52 -07002120 perf_event__fprintf(event, stdout);
2121 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002122 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002123out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002124 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002125 return ret;
2126}
2127
Hari Bathini96a44bb2017-03-08 02:12:06 +05302128static int process_namespaces_event(struct perf_tool *tool,
2129 union perf_event *event,
2130 struct perf_sample *sample,
2131 struct machine *machine)
2132{
2133 struct thread *thread;
2134 struct perf_script *script = container_of(tool, struct perf_script, tool);
2135 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002136 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302137 int ret = -1;
2138
2139 thread = machine__findnew_thread(machine, event->namespaces.pid,
2140 event->namespaces.tid);
2141 if (thread == NULL) {
2142 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2143 return -1;
2144 }
2145
2146 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2147 goto out;
2148
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002149 if (!evsel->core.attr.sample_id_all) {
Hari Bathini96a44bb2017-03-08 02:12:06 +05302150 sample->cpu = 0;
2151 sample->time = 0;
2152 sample->tid = event->namespaces.tid;
2153 sample->pid = event->namespaces.pid;
2154 }
Andi Kleene87e5482019-03-11 07:44:52 -07002155 if (!filter_cpu(sample)) {
2156 perf_sample__fprintf_start(sample, thread, evsel,
2157 PERF_RECORD_NAMESPACES, stdout);
2158 perf_event__fprintf(event, stdout);
2159 }
Hari Bathini96a44bb2017-03-08 02:12:06 +05302160 ret = 0;
2161out:
2162 thread__put(thread);
2163 return ret;
2164}
2165
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002166static int process_fork_event(struct perf_tool *tool,
2167 union perf_event *event,
2168 struct perf_sample *sample,
2169 struct machine *machine)
2170{
2171 struct thread *thread;
2172 struct perf_script *script = container_of(tool, struct perf_script, tool);
2173 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002174 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002175
2176 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2177 return -1;
2178
2179 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2180 if (thread == NULL) {
2181 pr_debug("problem processing FORK event, skipping it.\n");
2182 return -1;
2183 }
2184
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002185 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002186 sample->cpu = 0;
2187 sample->time = event->fork.time;
2188 sample->tid = event->fork.tid;
2189 sample->pid = event->fork.pid;
2190 }
Andi Kleene87e5482019-03-11 07:44:52 -07002191 if (!filter_cpu(sample)) {
2192 perf_sample__fprintf_start(sample, thread, evsel,
2193 PERF_RECORD_FORK, stdout);
2194 perf_event__fprintf(event, stdout);
2195 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002196 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002197
2198 return 0;
2199}
2200static int process_exit_event(struct perf_tool *tool,
2201 union perf_event *event,
2202 struct perf_sample *sample,
2203 struct machine *machine)
2204{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002205 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002206 struct thread *thread;
2207 struct perf_script *script = container_of(tool, struct perf_script, tool);
2208 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002209 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002210
2211 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2212 if (thread == NULL) {
2213 pr_debug("problem processing EXIT event, skipping it.\n");
2214 return -1;
2215 }
2216
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002217 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002218 sample->cpu = 0;
2219 sample->time = 0;
Adrian Hunter53ff6bc32015-08-18 12:07:05 +03002220 sample->tid = event->fork.tid;
2221 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002222 }
Andi Kleene87e5482019-03-11 07:44:52 -07002223 if (!filter_cpu(sample)) {
2224 perf_sample__fprintf_start(sample, thread, evsel,
2225 PERF_RECORD_EXIT, stdout);
2226 perf_event__fprintf(event, stdout);
2227 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002228
2229 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002230 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002231
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002232 thread__put(thread);
2233 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002234}
2235
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002236static int process_mmap_event(struct perf_tool *tool,
2237 union perf_event *event,
2238 struct perf_sample *sample,
2239 struct machine *machine)
2240{
2241 struct thread *thread;
2242 struct perf_script *script = container_of(tool, struct perf_script, tool);
2243 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002244 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002245
2246 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2247 return -1;
2248
2249 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2250 if (thread == NULL) {
2251 pr_debug("problem processing MMAP event, skipping it.\n");
2252 return -1;
2253 }
2254
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002255 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002256 sample->cpu = 0;
2257 sample->time = 0;
2258 sample->tid = event->mmap.tid;
2259 sample->pid = event->mmap.pid;
2260 }
Andi Kleene87e5482019-03-11 07:44:52 -07002261 if (!filter_cpu(sample)) {
2262 perf_sample__fprintf_start(sample, thread, evsel,
2263 PERF_RECORD_MMAP, stdout);
2264 perf_event__fprintf(event, stdout);
2265 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002266 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002267 return 0;
2268}
2269
2270static int process_mmap2_event(struct perf_tool *tool,
2271 union perf_event *event,
2272 struct perf_sample *sample,
2273 struct machine *machine)
2274{
2275 struct thread *thread;
2276 struct perf_script *script = container_of(tool, struct perf_script, tool);
2277 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002278 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002279
2280 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2281 return -1;
2282
2283 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2284 if (thread == NULL) {
2285 pr_debug("problem processing MMAP2 event, skipping it.\n");
2286 return -1;
2287 }
2288
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002289 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002290 sample->cpu = 0;
2291 sample->time = 0;
2292 sample->tid = event->mmap2.tid;
2293 sample->pid = event->mmap2.pid;
2294 }
Andi Kleene87e5482019-03-11 07:44:52 -07002295 if (!filter_cpu(sample)) {
2296 perf_sample__fprintf_start(sample, thread, evsel,
2297 PERF_RECORD_MMAP2, stdout);
2298 perf_event__fprintf(event, stdout);
2299 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002300 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002301 return 0;
2302}
2303
Adrian Hunter7c148982015-07-21 12:44:06 +03002304static int process_switch_event(struct perf_tool *tool,
2305 union perf_event *event,
2306 struct perf_sample *sample,
2307 struct machine *machine)
2308{
2309 struct thread *thread;
2310 struct perf_script *script = container_of(tool, struct perf_script, tool);
2311 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002312 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Adrian Hunter7c148982015-07-21 12:44:06 +03002313
2314 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2315 return -1;
2316
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002317 if (scripting_ops && scripting_ops->process_switch)
2318 scripting_ops->process_switch(event, sample, machine);
2319
2320 if (!script->show_switch_events)
2321 return 0;
2322
Adrian Hunter7c148982015-07-21 12:44:06 +03002323 thread = machine__findnew_thread(machine, sample->pid,
2324 sample->tid);
2325 if (thread == NULL) {
2326 pr_debug("problem processing SWITCH event, skipping it.\n");
2327 return -1;
2328 }
2329
Andi Kleene87e5482019-03-11 07:44:52 -07002330 if (!filter_cpu(sample)) {
2331 perf_sample__fprintf_start(sample, thread, evsel,
2332 PERF_RECORD_SWITCH, stdout);
2333 perf_event__fprintf(event, stdout);
2334 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002335 thread__put(thread);
2336 return 0;
2337}
2338
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002339static int
2340process_lost_event(struct perf_tool *tool,
2341 union perf_event *event,
2342 struct perf_sample *sample,
2343 struct machine *machine)
2344{
2345 struct perf_script *script = container_of(tool, struct perf_script, tool);
2346 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002347 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002348 struct thread *thread;
2349
2350 thread = machine__findnew_thread(machine, sample->pid,
2351 sample->tid);
2352 if (thread == NULL)
2353 return -1;
2354
Andi Kleene87e5482019-03-11 07:44:52 -07002355 if (!filter_cpu(sample)) {
2356 perf_sample__fprintf_start(sample, thread, evsel,
2357 PERF_RECORD_LOST, stdout);
2358 perf_event__fprintf(event, stdout);
2359 }
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002360 thread__put(thread);
2361 return 0;
2362}
2363
Jiri Olsa3233b372018-02-06 19:17:59 +01002364static int
2365process_finished_round_event(struct perf_tool *tool __maybe_unused,
2366 union perf_event *event,
2367 struct ordered_events *oe __maybe_unused)
2368
2369{
2370 perf_event__fprintf(event, stdout);
2371 return 0;
2372}
2373
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002374static int
2375process_bpf_events(struct perf_tool *tool __maybe_unused,
2376 union perf_event *event,
2377 struct perf_sample *sample,
2378 struct machine *machine)
2379{
2380 struct thread *thread;
2381 struct perf_script *script = container_of(tool, struct perf_script, tool);
2382 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002383 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002384
2385 if (machine__process_ksymbol(machine, event, sample) < 0)
2386 return -1;
2387
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002388 if (!evsel->core.attr.sample_id_all) {
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002389 perf_event__fprintf(event, stdout);
2390 return 0;
2391 }
2392
2393 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2394 if (thread == NULL) {
2395 pr_debug("problem processing MMAP event, skipping it.\n");
2396 return -1;
2397 }
2398
2399 if (!filter_cpu(sample)) {
2400 perf_sample__fprintf_start(sample, thread, evsel,
2401 event->header.type, stdout);
2402 perf_event__fprintf(event, stdout);
2403 }
2404
2405 thread__put(thread);
2406 return 0;
2407}
2408
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002409static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002410{
2411 session_done = 1;
2412}
2413
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002414static void perf_script__fclose_per_event_dump(struct perf_script *script)
2415{
Jiri Olsa63503db2019-07-21 13:23:52 +02002416 struct evlist *evlist = script->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002417 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002418
2419 evlist__for_each_entry(evlist, evsel) {
2420 if (!evsel->priv)
2421 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002422 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002423 evsel->priv = NULL;
2424 }
2425}
2426
2427static int perf_script__fopen_per_event_dump(struct perf_script *script)
2428{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002429 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002430
2431 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002432 /*
2433 * Already setup? I.e. we may be called twice in cases like
2434 * Intel PT, one for the intel_pt// and dummy events, then
2435 * for the evsels syntheized from the auxtrace info.
2436 *
2437 * Ses perf_script__process_auxtrace_info.
2438 */
2439 if (evsel->priv != NULL)
2440 continue;
2441
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002442 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002443 if (evsel->priv == NULL)
2444 goto out_err_fclose;
2445 }
2446
2447 return 0;
2448
2449out_err_fclose:
2450 perf_script__fclose_per_event_dump(script);
2451 return -1;
2452}
2453
2454static int perf_script__setup_per_event_dump(struct perf_script *script)
2455{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002456 struct evsel *evsel;
2457 static struct evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002458
2459 if (script->per_event_dump)
2460 return perf_script__fopen_per_event_dump(script);
2461
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002462 es_stdout.fp = stdout;
2463
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002464 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002465 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002466
2467 return 0;
2468}
2469
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002470static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2471{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002472 struct evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002473
2474 evlist__for_each_entry(script->session->evlist, evsel) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002475 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002476
2477 perf_evsel_script__fprintf(es, stdout);
2478 perf_evsel_script__delete(es);
2479 evsel->priv = NULL;
2480 }
2481}
2482
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002483static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002484{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002485 int ret;
2486
Tom Zanussic239da32010-04-01 23:59:18 -05002487 signal(SIGINT, sig_handler);
2488
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002489 perf_stat__init_shadow_stats();
2490
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002491 /* override event processing functions */
2492 if (script->show_task_events) {
2493 script->tool.comm = process_comm_event;
2494 script->tool.fork = process_fork_event;
2495 script->tool.exit = process_exit_event;
2496 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002497 if (script->show_mmap_events) {
2498 script->tool.mmap = process_mmap_event;
2499 script->tool.mmap2 = process_mmap2_event;
2500 }
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002501 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
Adrian Hunter7c148982015-07-21 12:44:06 +03002502 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302503 if (script->show_namespace_events)
2504 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002505 if (script->show_lost_events)
2506 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002507 if (script->show_round_events) {
2508 script->tool.ordered_events = false;
2509 script->tool.finished_round = process_finished_round_event;
2510 }
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002511 if (script->show_bpf_events) {
2512 script->tool.ksymbol = process_bpf_events;
2513 script->tool.bpf_event = process_bpf_events;
2514 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002515
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002516 if (perf_script__setup_per_event_dump(script)) {
2517 pr_err("Couldn't create the per event dump files\n");
2518 return -1;
2519 }
2520
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002521 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002522
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002523 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002524 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002525
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002526 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002527 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002528
2529 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002530}
2531
Tom Zanussi956ffd02009-11-25 01:15:46 -06002532struct script_spec {
2533 struct list_head node;
2534 struct scripting_ops *ops;
2535 char spec[0];
2536};
2537
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002538static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002539
2540static struct script_spec *script_spec__new(const char *spec,
2541 struct scripting_ops *ops)
2542{
2543 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2544
2545 if (s != NULL) {
2546 strcpy(s->spec, spec);
2547 s->ops = ops;
2548 }
2549
2550 return s;
2551}
2552
Tom Zanussi956ffd02009-11-25 01:15:46 -06002553static void script_spec__add(struct script_spec *s)
2554{
2555 list_add_tail(&s->node, &script_specs);
2556}
2557
2558static struct script_spec *script_spec__find(const char *spec)
2559{
2560 struct script_spec *s;
2561
2562 list_for_each_entry(s, &script_specs, node)
2563 if (strcasecmp(s->spec, spec) == 0)
2564 return s;
2565 return NULL;
2566}
2567
Tom Zanussi956ffd02009-11-25 01:15:46 -06002568int script_spec_register(const char *spec, struct scripting_ops *ops)
2569{
2570 struct script_spec *s;
2571
2572 s = script_spec__find(spec);
2573 if (s)
2574 return -1;
2575
Taeung Song8560bae2016-02-26 00:13:10 +09002576 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002577 if (!s)
2578 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002579 else
2580 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002581
2582 return 0;
2583}
2584
2585static struct scripting_ops *script_spec__lookup(const char *spec)
2586{
2587 struct script_spec *s = script_spec__find(spec);
2588 if (!s)
2589 return NULL;
2590
2591 return s->ops;
2592}
2593
2594static void list_available_languages(void)
2595{
2596 struct script_spec *s;
2597
2598 fprintf(stderr, "\n");
2599 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002600 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002601
2602 list_for_each_entry(s, &script_specs, node)
2603 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2604
2605 fprintf(stderr, "\n");
2606}
2607
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002608static int parse_scriptname(const struct option *opt __maybe_unused,
2609 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002610{
2611 char spec[PATH_MAX];
2612 const char *script, *ext;
2613 int len;
2614
Tom Zanussif526d682010-01-27 02:27:52 -06002615 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002616 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002617 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002618 }
2619
2620 script = strchr(str, ':');
2621 if (script) {
2622 len = script - str;
2623 if (len >= PATH_MAX) {
2624 fprintf(stderr, "invalid language specifier");
2625 return -1;
2626 }
2627 strncpy(spec, str, len);
2628 spec[len] = '\0';
2629 scripting_ops = script_spec__lookup(spec);
2630 if (!scripting_ops) {
2631 fprintf(stderr, "invalid language specifier");
2632 return -1;
2633 }
2634 script++;
2635 } else {
2636 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002637 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002638 if (!ext) {
2639 fprintf(stderr, "invalid script extension");
2640 return -1;
2641 }
2642 scripting_ops = script_spec__lookup(++ext);
2643 if (!scripting_ops) {
2644 fprintf(stderr, "invalid script extension");
2645 return -1;
2646 }
2647 }
2648
2649 script_name = strdup(script);
2650
2651 return 0;
2652}
2653
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002654static int parse_output_fields(const struct option *opt __maybe_unused,
2655 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002656{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002657 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002658 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002659 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002660 int rc = 0;
2661 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002662 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002663 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002664
2665 if (!str)
2666 return -ENOMEM;
2667
David Ahern2c9e45f72011-03-17 10:03:21 -06002668 /* first word can state for which event type the user is specifying
2669 * the fields. If no type exists, the specified fields apply to all
2670 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002671 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002672 tok = strchr(str, ':');
2673 if (tok) {
2674 *tok = '\0';
2675 tok++;
2676 if (!strcmp(str, "hw"))
2677 type = PERF_TYPE_HARDWARE;
2678 else if (!strcmp(str, "sw"))
2679 type = PERF_TYPE_SOFTWARE;
2680 else if (!strcmp(str, "trace"))
2681 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002682 else if (!strcmp(str, "raw"))
2683 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002684 else if (!strcmp(str, "break"))
2685 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002686 else if (!strcmp(str, "synth"))
2687 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002688 else {
2689 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002690 rc = -EINVAL;
2691 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002692 }
2693
2694 if (output[type].user_set)
2695 pr_warning("Overriding previous field request for %s events.\n",
2696 event_type(type));
2697
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002698 /* Don't override defaults for +- */
2699 if (strchr(tok, '+') || strchr(tok, '-'))
2700 goto parse;
2701
David Ahern2c9e45f72011-03-17 10:03:21 -06002702 output[type].fields = 0;
2703 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002704 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002705
2706 } else {
2707 tok = str;
2708 if (strlen(str) == 0) {
2709 fprintf(stderr,
2710 "Cannot set fields to 'none' for all event types.\n");
2711 rc = -EINVAL;
2712 goto out;
2713 }
2714
Andi Kleen36ce5652017-06-02 08:48:10 -07002715 /* Don't override defaults for +- */
2716 if (strchr(str, '+') || strchr(str, '-'))
2717 goto parse;
2718
David Ahern2c9e45f72011-03-17 10:03:21 -06002719 if (output_set_by_user())
2720 pr_warning("Overriding previous field request for all events.\n");
2721
Adrian Hunter14057202017-06-21 13:17:19 +03002722 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002723 output[j].fields = 0;
2724 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002725 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002726 }
David Ahern1424dc92011-03-09 22:23:28 -07002727 }
2728
Andi Kleen36ce5652017-06-02 08:48:10 -07002729parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002730 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002731 if (*tok == '+') {
2732 if (change == SET)
2733 goto out_badmix;
2734 change = ADD;
2735 tok++;
2736 } else if (*tok == '-') {
2737 if (change == SET)
2738 goto out_badmix;
2739 change = REMOVE;
2740 tok++;
2741 } else {
2742 if (change != SET && change != DEFAULT)
2743 goto out_badmix;
2744 change = SET;
2745 }
2746
David Ahern745f43e2011-03-09 22:23:26 -07002747 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002748 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002749 break;
David Ahern745f43e2011-03-09 22:23:26 -07002750 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002751 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002752 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002753 continue;
2754 }
David Ahern745f43e2011-03-09 22:23:26 -07002755 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002756 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002757 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002758 goto out;
2759 }
2760
2761 if (type == -1) {
2762 /* add user option to all events types for
2763 * which it is valid
2764 */
Adrian Hunter14057202017-06-21 13:17:19 +03002765 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002766 if (output[j].invalid_fields & all_output_options[i].field) {
2767 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2768 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002769 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002770 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002771 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002772 output[j].user_set_fields &= ~all_output_options[i].field;
2773 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002774 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002775 output[j].user_set_fields |= all_output_options[i].field;
2776 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002777 output[j].user_set = true;
2778 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002779 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002780 }
2781 } else {
2782 if (output[type].invalid_fields & all_output_options[i].field) {
2783 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2784 all_output_options[i].str, event_type(type));
2785
2786 rc = -EINVAL;
2787 goto out;
2788 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002789 if (change == REMOVE)
2790 output[type].fields &= ~all_output_options[i].field;
2791 else
2792 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002793 output[type].user_set = true;
2794 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002795 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002796 }
2797
2798 if (type >= 0) {
2799 if (output[type].fields == 0) {
2800 pr_debug("No fields requested for %s type. "
2801 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002802 }
David Ahern1424dc92011-03-09 22:23:28 -07002803 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002804 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002805
Andi Kleen36ce5652017-06-02 08:48:10 -07002806out_badmix:
2807 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2808 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002809out:
David Ahern745f43e2011-03-09 22:23:26 -07002810 free(str);
2811 return rc;
2812}
2813
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002814#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2815 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2816 if ((lang_dirent->d_type == DT_DIR || \
2817 (lang_dirent->d_type == DT_UNKNOWN && \
2818 is_directory(scripts_path, lang_dirent))) && \
2819 (strcmp(lang_dirent->d_name, ".")) && \
2820 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002821
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002822#define for_each_script(lang_path, lang_dir, script_dirent) \
2823 while ((script_dirent = readdir(lang_dir)) != NULL) \
2824 if (script_dirent->d_type != DT_DIR && \
2825 (script_dirent->d_type != DT_UNKNOWN || \
2826 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002827
2828
2829#define RECORD_SUFFIX "-record"
2830#define REPORT_SUFFIX "-report"
2831
2832struct script_desc {
2833 struct list_head node;
2834 char *name;
2835 char *half_liner;
2836 char *args;
2837};
2838
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002839static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002840
2841static struct script_desc *script_desc__new(const char *name)
2842{
2843 struct script_desc *s = zalloc(sizeof(*s));
2844
Tom Zanussib5b87312010-11-10 08:16:51 -06002845 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002846 s->name = strdup(name);
2847
2848 return s;
2849}
2850
2851static void script_desc__delete(struct script_desc *s)
2852{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002853 zfree(&s->name);
2854 zfree(&s->half_liner);
2855 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002856 free(s);
2857}
2858
2859static void script_desc__add(struct script_desc *s)
2860{
2861 list_add_tail(&s->node, &script_descs);
2862}
2863
2864static struct script_desc *script_desc__find(const char *name)
2865{
2866 struct script_desc *s;
2867
2868 list_for_each_entry(s, &script_descs, node)
2869 if (strcasecmp(s->name, name) == 0)
2870 return s;
2871 return NULL;
2872}
2873
2874static struct script_desc *script_desc__findnew(const char *name)
2875{
2876 struct script_desc *s = script_desc__find(name);
2877
2878 if (s)
2879 return s;
2880
2881 s = script_desc__new(name);
2882 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002883 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002884
2885 script_desc__add(s);
2886
2887 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002888}
2889
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002890static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002891{
2892 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002893 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002894
2895 if (strlen(str) > suffix_len) {
2896 p = str + strlen(str) - suffix_len;
2897 if (!strncmp(p, suffix, suffix_len))
2898 return p;
2899 }
2900
2901 return NULL;
2902}
2903
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002904static int read_script_info(struct script_desc *desc, const char *filename)
2905{
2906 char line[BUFSIZ], *p;
2907 FILE *fp;
2908
2909 fp = fopen(filename, "r");
2910 if (!fp)
2911 return -1;
2912
2913 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002914 p = skip_spaces(line);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002915 if (strlen(p) == 0)
2916 continue;
2917 if (*p != '#')
2918 continue;
2919 p++;
2920 if (strlen(p) && *p == '!')
2921 continue;
2922
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002923 p = skip_spaces(p);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002924 if (strlen(p) && p[strlen(p) - 1] == '\n')
2925 p[strlen(p) - 1] = '\0';
2926
2927 if (!strncmp(p, "description:", strlen("description:"))) {
2928 p += strlen("description:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002929 desc->half_liner = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002930 continue;
2931 }
2932
2933 if (!strncmp(p, "args:", strlen("args:"))) {
2934 p += strlen("args:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002935 desc->args = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002936 continue;
2937 }
2938 }
2939
2940 fclose(fp);
2941
2942 return 0;
2943}
2944
Robert Richter38efb532011-11-25 11:38:40 +01002945static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2946{
2947 char *script_root, *str;
2948
2949 script_root = strdup(script_dirent->d_name);
2950 if (!script_root)
2951 return NULL;
2952
2953 str = (char *)ends_with(script_root, suffix);
2954 if (!str) {
2955 free(script_root);
2956 return NULL;
2957 }
2958
2959 *str = '\0';
2960 return script_root;
2961}
2962
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002963static int list_available_scripts(const struct option *opt __maybe_unused,
2964 const char *s __maybe_unused,
2965 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002966{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002967 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002968 char scripts_path[MAXPATHLEN];
2969 DIR *scripts_dir, *lang_dir;
2970 char script_path[MAXPATHLEN];
2971 char lang_path[MAXPATHLEN];
2972 struct script_desc *desc;
2973 char first_half[BUFSIZ];
2974 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002975
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002976 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002977
2978 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002979 if (!scripts_dir) {
2980 fprintf(stdout,
2981 "open(%s) failed.\n"
2982 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2983 scripts_path);
2984 exit(-1);
2985 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002986
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002987 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002988 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2989 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002990 lang_dir = opendir(lang_path);
2991 if (!lang_dir)
2992 continue;
2993
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002994 for_each_script(lang_path, lang_dir, script_dirent) {
2995 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002996 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002997 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002998 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2999 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003000 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01003001 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003002 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003003 }
3004 }
3005
3006 fprintf(stdout, "List of available trace scripts:\n");
3007 list_for_each_entry(desc, &script_descs, node) {
3008 sprintf(first_half, "%s %s", desc->name,
3009 desc->args ? desc->args : "");
3010 fprintf(stdout, " %-36s %s\n", first_half,
3011 desc->half_liner ? desc->half_liner : "");
3012 }
3013
3014 exit(0);
3015}
3016
Feng Tange5f37052012-09-07 16:42:26 +08003017/*
Feng Tang49e639e2012-10-30 11:56:03 +08003018 * Some scripts specify the required events in their "xxx-record" file,
3019 * this function will check if the events in perf.data match those
3020 * mentioned in the "xxx-record".
3021 *
3022 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3023 * which is covered well now. And new parsing code should be added to
3024 * cover the future complexing formats like event groups etc.
3025 */
3026static int check_ev_match(char *dir_name, char *scriptname,
3027 struct perf_session *session)
3028{
3029 char filename[MAXPATHLEN], evname[128];
3030 char line[BUFSIZ], *p;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003031 struct evsel *pos;
Feng Tang49e639e2012-10-30 11:56:03 +08003032 int match, len;
3033 FILE *fp;
3034
Jiri Olsa77f18152018-03-19 09:29:01 +01003035 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08003036
3037 fp = fopen(filename, "r");
3038 if (!fp)
3039 return -1;
3040
3041 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003042 p = skip_spaces(line);
Feng Tang49e639e2012-10-30 11:56:03 +08003043 if (*p == '#')
3044 continue;
3045
3046 while (strlen(p)) {
3047 p = strstr(p, "-e");
3048 if (!p)
3049 break;
3050
3051 p += 2;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003052 p = skip_spaces(p);
Feng Tang49e639e2012-10-30 11:56:03 +08003053 len = strcspn(p, " \t");
3054 if (!len)
3055 break;
3056
3057 snprintf(evname, len + 1, "%s", p);
3058
3059 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003060 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08003061 if (!strcmp(perf_evsel__name(pos), evname)) {
3062 match = 1;
3063 break;
3064 }
3065 }
3066
3067 if (!match) {
3068 fclose(fp);
3069 return -1;
3070 }
3071 }
3072 }
3073
3074 fclose(fp);
3075 return 0;
3076}
3077
3078/*
Feng Tange5f37052012-09-07 16:42:26 +08003079 * Return -1 if none is found, otherwise the actual scripts number.
3080 *
3081 * Currently the only user of this function is the script browser, which
3082 * will list all statically runnable scripts, select one, execute it and
3083 * show the output in a perf browser.
3084 */
Andi Kleen905e4af2019-03-11 07:45:01 -07003085int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3086 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08003087{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003088 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08003089 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08003090 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08003091 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003092 struct perf_data data = {
Jiri Olsa2d4f27992019-02-21 10:41:30 +01003093 .path = input_name,
3094 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003095 };
Feng Tange5f37052012-09-07 16:42:26 +08003096 char *temp;
3097 int i = 0;
3098
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003099 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08003100 if (!session)
3101 return -1;
3102
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003103 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003104
3105 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003106 if (!scripts_dir) {
3107 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003108 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003109 }
Feng Tange5f37052012-09-07 16:42:26 +08003110
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003111 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003112 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3113 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003114#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003115 if (strstr(lang_path, "perl"))
3116 continue;
3117#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003118#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003119 if (strstr(lang_path, "python"))
3120 continue;
3121#endif
3122
3123 lang_dir = opendir(lang_path);
3124 if (!lang_dir)
3125 continue;
3126
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003127 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003128 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003129 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003130 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003131 if (i >= num)
3132 break;
3133 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3134 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003135 script_dirent->d_name);
3136 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003137 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003138 (temp - script_dirent->d_name) + 1,
3139 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003140
3141 if (check_ev_match(lang_path,
3142 scripts_array[i], session))
3143 continue;
3144
Feng Tange5f37052012-09-07 16:42:26 +08003145 i++;
3146 }
Feng Tang49e639e2012-10-30 11:56:03 +08003147 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003148 }
3149
Feng Tang49e639e2012-10-30 11:56:03 +08003150 closedir(scripts_dir);
3151 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003152 return i;
3153}
3154
Tom Zanussi38752942009-12-15 02:53:39 -06003155static char *get_script_path(const char *script_root, const char *suffix)
3156{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003157 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003158 char scripts_path[MAXPATHLEN];
3159 char script_path[MAXPATHLEN];
3160 DIR *scripts_dir, *lang_dir;
3161 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003162 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003163
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003164 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003165
3166 scripts_dir = opendir(scripts_path);
3167 if (!scripts_dir)
3168 return NULL;
3169
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003170 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003171 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3172 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003173 lang_dir = opendir(lang_path);
3174 if (!lang_dir)
3175 continue;
3176
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003177 for_each_script(lang_path, lang_dir, script_dirent) {
3178 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003179 if (__script_root && !strcmp(script_root, __script_root)) {
3180 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003181 closedir(lang_dir);
3182 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003183 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3184 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003185 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003186 }
3187 free(__script_root);
3188 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003189 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003190 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003191 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003192
Robert Richter38efb532011-11-25 11:38:40 +01003193 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003194}
3195
Tom Zanussib5b87312010-11-10 08:16:51 -06003196static bool is_top_script(const char *script_path)
3197{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003198 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003199}
3200
3201static int has_required_arg(char *script_path)
3202{
3203 struct script_desc *desc;
3204 int n_args = 0;
3205 char *p;
3206
3207 desc = script_desc__new(NULL);
3208
3209 if (read_script_info(desc, script_path))
3210 goto out;
3211
3212 if (!desc->args)
3213 goto out;
3214
3215 for (p = desc->args; *p; p++)
3216 if (*p == '<')
3217 n_args++;
3218out:
3219 script_desc__delete(desc);
3220
3221 return n_args;
3222}
3223
David Ahernd54b1a92012-08-26 12:24:46 -06003224static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003225{
3226 char **__argv = malloc(sizeof(const char *) * argc);
3227
David Ahernd54b1a92012-08-26 12:24:46 -06003228 if (!__argv) {
3229 pr_err("malloc failed\n");
3230 return -1;
3231 }
3232
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003233 memcpy(__argv, argv, sizeof(const char *) * argc);
3234 argc = parse_options(argc, (const char **)__argv, record_options,
3235 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3236 free(__argv);
3237
David Ahernd54b1a92012-08-26 12:24:46 -06003238 system_wide = (argc == 0);
3239
3240 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003241}
3242
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003243static void script__setup_sample_type(struct perf_script *script)
3244{
3245 struct perf_session *session = script->session;
3246 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3247
3248 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3249 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003250 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003251 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003252 dwarf_callchain_users = true;
3253 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003254 callchain_param.record_mode = CALLCHAIN_LBR;
3255 else
3256 callchain_param.record_mode = CALLCHAIN_FP;
3257 }
3258}
3259
Jiri Olsa89f16882018-09-13 14:54:03 +02003260static int process_stat_round_event(struct perf_session *session,
3261 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003262{
3263 struct stat_round_event *round = &event->stat_round;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003264 struct evsel *counter;
Jiri Olsae099eba2016-01-05 22:09:09 +01003265
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003266 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003267 perf_stat_process_counter(&stat_config, counter);
3268 process_stat(counter, round->time);
3269 }
3270
3271 process_stat_interval(round->time);
3272 return 0;
3273}
3274
Jiri Olsa89f16882018-09-13 14:54:03 +02003275static int process_stat_config_event(struct perf_session *session __maybe_unused,
3276 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003277{
3278 perf_event__read_stat_config(&stat_config, &event->stat_config);
3279 return 0;
3280}
3281
Jiri Olsacfc88742016-01-05 22:09:06 +01003282static int set_maps(struct perf_script *script)
3283{
Jiri Olsa63503db2019-07-21 13:23:52 +02003284 struct evlist *evlist = script->session->evlist;
Jiri Olsacfc88742016-01-05 22:09:06 +01003285
3286 if (!script->cpus || !script->threads)
3287 return 0;
3288
3289 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3290 return -EINVAL;
3291
Jiri Olsa453fa032019-07-21 13:24:43 +02003292 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
Jiri Olsacfc88742016-01-05 22:09:06 +01003293
3294 if (perf_evlist__alloc_stats(evlist, true))
3295 return -ENOMEM;
3296
3297 script->allocated = true;
3298 return 0;
3299}
3300
3301static
Jiri Olsa89f16882018-09-13 14:54:03 +02003302int process_thread_map_event(struct perf_session *session,
3303 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003304{
Jiri Olsa89f16882018-09-13 14:54:03 +02003305 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003306 struct perf_script *script = container_of(tool, struct perf_script, tool);
3307
3308 if (script->threads) {
3309 pr_warning("Extra thread map event, ignoring.\n");
3310 return 0;
3311 }
3312
3313 script->threads = thread_map__new_event(&event->thread_map);
3314 if (!script->threads)
3315 return -ENOMEM;
3316
3317 return set_maps(script);
3318}
3319
3320static
Jiri Olsa89f16882018-09-13 14:54:03 +02003321int process_cpu_map_event(struct perf_session *session,
3322 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003323{
Jiri Olsa89f16882018-09-13 14:54:03 +02003324 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003325 struct perf_script *script = container_of(tool, struct perf_script, tool);
3326
3327 if (script->cpus) {
3328 pr_warning("Extra cpu map event, ignoring.\n");
3329 return 0;
3330 }
3331
3332 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3333 if (!script->cpus)
3334 return -ENOMEM;
3335
3336 return set_maps(script);
3337}
3338
Jiri Olsa89f16882018-09-13 14:54:03 +02003339static int process_feature_event(struct perf_session *session,
3340 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303341{
3342 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003343 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303344 return 0;
3345}
3346
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003347#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003348static int perf_script__process_auxtrace_info(struct perf_session *session,
3349 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003350{
Jiri Olsa89f16882018-09-13 14:54:03 +02003351 struct perf_tool *tool = session->tool;
3352
3353 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003354
3355 if (ret == 0) {
3356 struct perf_script *script = container_of(tool, struct perf_script, tool);
3357
3358 ret = perf_script__setup_per_event_dump(script);
3359 }
3360
3361 return ret;
3362}
3363#else
3364#define perf_script__process_auxtrace_info 0
3365#endif
3366
Andi Kleenb585ebd2018-09-20 11:05:36 -07003367static int parse_insn_trace(const struct option *opt __maybe_unused,
3368 const char *str __maybe_unused,
3369 int unset __maybe_unused)
3370{
3371 parse_output_fields(NULL, "+insn,-event,-period", 0);
3372 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003373 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003374 return 0;
3375}
3376
3377static int parse_xed(const struct option *opt __maybe_unused,
3378 const char *str __maybe_unused,
3379 int unset __maybe_unused)
3380{
3381 force_pager("xed -F insn: -A -64 | less");
3382 return 0;
3383}
3384
Andi Kleend1b15522018-09-20 11:05:38 -07003385static int parse_call_trace(const struct option *opt __maybe_unused,
3386 const char *str __maybe_unused,
3387 int unset __maybe_unused)
3388{
3389 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3390 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003391 symbol_conf.nanosecs = true;
Jiri Olsa1c492422019-05-08 15:20:05 +02003392 symbol_conf.pad_output_len_dso = 50;
Andi Kleend1b15522018-09-20 11:05:38 -07003393 return 0;
3394}
3395
3396static int parse_callret_trace(const struct option *opt __maybe_unused,
3397 const char *str __maybe_unused,
3398 int unset __maybe_unused)
3399{
3400 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3401 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003402 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003403 return 0;
3404}
3405
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003406int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003407{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003408 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003409 bool header = false;
3410 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003411 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003412 char *rec_script_path = NULL;
3413 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003414 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003415 struct itrace_synth_opts itrace_synth_opts = {
3416 .set = false,
3417 .default_no_sample = true,
3418 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003419 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003420 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003421 const char **__argv;
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003422 const char *event_switch_on = NULL,
3423 *event_switch_off = NULL;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003424 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003425 struct perf_script script = {
3426 .tool = {
3427 .sample = process_sample_event,
3428 .mmap = perf_event__process_mmap,
3429 .mmap2 = perf_event__process_mmap2,
3430 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303431 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003432 .exit = perf_event__process_exit,
3433 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003434 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003435 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003436 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303437 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003438 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003439 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003440 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003441 .auxtrace = perf_event__process_auxtrace,
3442 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003443 .stat = perf_event__process_stat_event,
3444 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003445 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003446 .thread_map = process_thread_map_event,
3447 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003448 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003449 .ordering_requires_timestamps = true,
3450 },
3451 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003452 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003453 .mode = PERF_DATA_MODE_READ,
3454 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003455 const struct option options[] = {
3456 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3457 "dump raw trace in ASCII"),
3458 OPT_INCR('v', "verbose", &verbose,
3459 "be more verbose (show symbol address, etc)"),
3460 OPT_BOOLEAN('L', "Latency", &latency_format,
3461 "show latency attributes (irqs/preemption disabled, etc)"),
3462 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3463 list_available_scripts),
3464 OPT_CALLBACK('s', "script", NULL, "name",
3465 "script file name (lang:script name, script name, or *)",
3466 parse_scriptname),
3467 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3468 "generate perf-script.xx script in specified language"),
3469 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3470 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3471 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003472 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3473 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003474 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3475 "file", "vmlinux pathname"),
3476 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3477 "file", "kallsyms pathname"),
3478 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3479 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003480 OPT_CALLBACK(0, "symfs", NULL, "directory",
3481 "Look for files with symbols relative to this directory",
3482 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003483 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003484 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003485 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003486 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003487 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303488 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3489 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
Adrian Hunter68fb45b2019-05-20 14:37:14 +03003490 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003491 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003492 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3493 "system-wide collection from all CPUs"),
3494 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3495 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003496 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3497 "Decode instructions from itrace", parse_insn_trace),
3498 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3499 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003500 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3501 "Decode calls from from itrace", parse_call_trace),
3502 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3503 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003504 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3505 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003506 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3507 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003508 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3509 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3510 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003511 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3512 "only consider symbols in these pids"),
3513 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3514 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003515 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3516 "Set the maximum stack depth when parsing the callchain, "
3517 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003518 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Andi Kleen90b10f42019-03-14 15:50:00 -07003519 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003520 OPT_BOOLEAN('I', "show-info", &show_full_info,
3521 "display extended information from perf.data file"),
3522 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3523 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003524 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3525 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003526 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3527 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003528 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3529 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303530 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3531 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003532 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3533 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003534 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3535 "Show round events (if recorded)"),
Jiri Olsa490c8cc2019-05-08 15:20:08 +02003536 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3537 "Show bpf related events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003538 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3539 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003540 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003541 OPT_INTEGER(0, "max-blocks", &max_blocks,
3542 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003543 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003544 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003545 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003546 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003547 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003548 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3549 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003550 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3551 "Enable symbol demangling"),
3552 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3553 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003554 OPT_STRING(0, "time", &script.time_str, "str",
3555 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003556 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3557 "Show inline function"),
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003558 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3559 "guest mount directory under which every guest os"
3560 " instance has a subdir"),
3561 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3562 "file", "file saving guest os vmlinux"),
3563 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3564 "file", "file saving guest os /proc/kallsyms"),
3565 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3566 "file", "file saving guest os /proc/modules"),
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03003567 OPT_STRING(0, "switch-on", &event_switch_on,
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003568 "event", "Consider events after the ocurrence of this event"),
3569 OPT_STRING(0, "switch-off", &event_switch_off,
3570 "event", "Stop considering events after the ocurrence of this event"),
Arnaldo Carvalho de Melo6469eb62019-08-14 16:40:58 -03003571 OPT_BOOLEAN(0, "show-on-off-events", &script.evswitch.show_on_off_events,
3572 "Show the on/off switch events, used with --switch-on"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003573 OPT_END()
3574 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003575 const char * const script_subcommands[] = { "record", "report", NULL };
3576 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003577 "perf script [<options>]",
3578 "perf script [<options>] record <script> [<record-options>] <command>",
3579 "perf script [<options>] report <script> [script-args]",
3580 "perf script [<options>] <script> [<record-options>] <command>",
3581 "perf script [<options>] <top-script> [script-args]",
3582 NULL
3583 };
Tom Zanussi38752942009-12-15 02:53:39 -06003584
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003585 perf_set_singlethreaded();
3586
Tom Zanussib5b87312010-11-10 08:16:51 -06003587 setup_scripting();
3588
Yunlong Song40cae2b2015-03-18 21:35:54 +08003589 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003590 PARSE_OPT_STOP_AT_NON_OPTION);
3591
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003592 if (symbol_conf.guestmount ||
3593 symbol_conf.default_guest_vmlinux_name ||
3594 symbol_conf.default_guest_kallsyms ||
3595 symbol_conf.default_guest_modules) {
3596 /*
3597 * Enable guest sample processing.
3598 */
3599 perf_guest = true;
3600 }
3601
Jiri Olsa2d4f27992019-02-21 10:41:30 +01003602 data.path = input_name;
3603 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003604
Tom Zanussib5b87312010-11-10 08:16:51 -06003605 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3606 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3607 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003608 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003609 }
3610
Tom Zanussib5b87312010-11-10 08:16:51 -06003611 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3612 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3613 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003614 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003615 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003616 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003617 return -1;
3618 }
Tom Zanussi38752942009-12-15 02:53:39 -06003619 }
3620
Andi Kleen90b10f42019-03-14 15:50:00 -07003621 if (script.time_str && reltime) {
3622 fprintf(stderr, "Don't combine --reltime with --time\n");
3623 return -1;
3624 }
3625
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003626 if (itrace_synth_opts.callchain &&
3627 itrace_synth_opts.callchain_sz > scripting_max_stack)
3628 scripting_max_stack = itrace_synth_opts.callchain_sz;
3629
Ben Hutchings44e668c2010-10-10 16:10:03 +01003630 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003631 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003632
Tom Zanussib5b87312010-11-10 08:16:51 -06003633 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003634 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003635 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003636 pid_t pid;
3637
Tom Zanussib5b87312010-11-10 08:16:51 -06003638 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3639 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3640
3641 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003642 usage_with_options_msg(script_usage, options,
3643 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003644 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003645 }
3646
Tom Zanussib5b87312010-11-10 08:16:51 -06003647 if (is_top_script(argv[0])) {
3648 rep_args = argc - 1;
3649 } else {
3650 int rec_args;
3651
3652 rep_args = has_required_arg(rep_script_path);
3653 rec_args = (argc - 1) - rep_args;
3654 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003655 usage_with_options_msg(script_usage, options,
3656 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003657 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003658 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003659 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003660 }
3661
3662 if (pipe(live_pipe) < 0) {
3663 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003664 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003665 }
3666
3667 pid = fork();
3668 if (pid < 0) {
3669 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003670 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003671 }
3672
3673 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003674 j = 0;
3675
Tom Zanussia0cccc22010-04-01 23:59:25 -05003676 dup2(live_pipe[1], 1);
3677 close(live_pipe[0]);
3678
Robert Richter317df652011-11-25 15:05:25 +01003679 if (is_top_script(argv[0])) {
3680 system_wide = true;
3681 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003682 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3683 err = -1;
3684 goto out;
3685 }
Robert Richter317df652011-11-25 15:05:25 +01003686 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003687
3688 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003689 if (!__argv) {
3690 pr_err("malloc failed\n");
3691 err = -ENOMEM;
3692 goto out;
3693 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003694
Tom Zanussib5b87312010-11-10 08:16:51 -06003695 __argv[j++] = "/bin/sh";
3696 __argv[j++] = rec_script_path;
3697 if (system_wide)
3698 __argv[j++] = "-a";
3699 __argv[j++] = "-q";
3700 __argv[j++] = "-o";
3701 __argv[j++] = "-";
3702 for (i = rep_args + 1; i < argc; i++)
3703 __argv[j++] = argv[i];
3704 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003705
3706 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003707 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003708 exit(-1);
3709 }
3710
3711 dup2(live_pipe[0], 0);
3712 close(live_pipe[1]);
3713
Tom Zanussib5b87312010-11-10 08:16:51 -06003714 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003715 if (!__argv) {
3716 pr_err("malloc failed\n");
3717 err = -ENOMEM;
3718 goto out;
3719 }
3720
Tom Zanussib5b87312010-11-10 08:16:51 -06003721 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003722 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003723 __argv[j++] = rep_script_path;
3724 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003725 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003726 __argv[j++] = "-i";
3727 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003728 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003729
3730 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003731 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003732 exit(-1);
3733 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003734
Tom Zanussib5b87312010-11-10 08:16:51 -06003735 if (rec_script_path)
3736 script_path = rec_script_path;
3737 if (rep_script_path)
3738 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003739
Tom Zanussib5b87312010-11-10 08:16:51 -06003740 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003741 j = 0;
3742
Robert Richter317df652011-11-25 15:05:25 +01003743 if (!rec_script_path)
3744 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003745 else if (!system_wide) {
3746 if (have_cmd(argc - 1, &argv[1]) != 0) {
3747 err = -1;
3748 goto out;
3749 }
3750 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003751
3752 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003753 if (!__argv) {
3754 pr_err("malloc failed\n");
3755 err = -ENOMEM;
3756 goto out;
3757 }
3758
Tom Zanussib5b87312010-11-10 08:16:51 -06003759 __argv[j++] = "/bin/sh";
3760 __argv[j++] = script_path;
3761 if (system_wide)
3762 __argv[j++] = "-a";
3763 for (i = 2; i < argc; i++)
3764 __argv[j++] = argv[i];
3765 __argv[j++] = NULL;
3766
3767 execvp("/bin/sh", (char **)__argv);
3768 free(__argv);
3769 exit(-1);
3770 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003771
Milian Wolffc1c9b962018-10-21 21:14:23 +02003772 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003773 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003774 use_browser = 0;
3775 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003776
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003777 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003778 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003779 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003780
Jiri Olsae90debd2013-12-09 11:02:50 +01003781 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003782 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003783 perf_session__fprintf_info(session, stdout, show_full_info);
3784 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003785 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003786 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003787 if (show_full_info)
3788 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003789
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003790 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003791 goto out_delete;
3792
Andi Kleen3ab481a2019-03-05 06:47:45 -08003793 uname(&uts);
Song Liu9d491692019-06-20 18:44:38 -07003794 if (data.is_pipe || /* assume pipe_mode indicates native_arch */
3795 !strcmp(uts.machine, session->header.env.arch) ||
Andi Kleen3ab481a2019-03-05 06:47:45 -08003796 (!strcmp(uts.machine, "x86_64") &&
3797 !strcmp(session->header.env.arch, "i386")))
3798 native_arch = true;
3799
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003800 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003801 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003802
Andi Kleen99f753f2018-09-20 11:05:39 -07003803 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3804 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003805 itrace_synth_opts.thread_stack = true;
3806
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003807 session->itrace_synth_opts = &itrace_synth_opts;
3808
Anton Blanchard5d67be92011-07-04 21:57:50 +10003809 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003810 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3811 if (err < 0)
3812 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003813 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003814 }
3815
David Ahern1424dc92011-03-09 22:23:28 -07003816 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003817 symbol_conf.use_callchain = true;
3818 else
3819 symbol_conf.use_callchain = false;
3820
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003821 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003822 tep_set_function_resolver(session->tevent.pevent,
3823 machine__resolve_kernel_addr,
3824 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003825 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003826 err = -1;
3827 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003828 }
3829
Tom Zanussi956ffd02009-11-25 01:15:46 -06003830 if (generate_script_lang) {
3831 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003832 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003833
David Ahern2c9e45f72011-03-17 10:03:21 -06003834 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003835 fprintf(stderr,
3836 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003837 err = -EINVAL;
3838 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003839 }
3840
Jiri Olsa2d4f27992019-02-21 10:41:30 +01003841 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003842 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003843 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003844 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003845 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003846 }
3847
3848 err = fstat(input, &perf_stat);
3849 if (err < 0) {
3850 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003851 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003852 }
3853
3854 if (!perf_stat.st_size) {
3855 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003856 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003857 }
3858
3859 scripting_ops = script_spec__lookup(generate_script_lang);
3860 if (!scripting_ops) {
3861 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003862 err = -ENOENT;
3863 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003864 }
3865
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003866 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003867 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003868 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003869 }
3870
3871 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003872 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003873 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003874 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003875 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003876 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003877 }
3878
David Ahern9cbdb702011-04-06 21:54:20 -06003879
3880 err = perf_session__check_output_opt(session);
3881 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003882 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003883
Jin Yao284c4e12019-03-01 18:13:06 +08003884 if (script.time_str) {
3885 err = perf_time__parse_for_ranges(script.time_str, session,
3886 &script.ptime_range,
3887 &script.range_size,
3888 &script.range_num);
3889 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08003890 goto out_delete;
Adrian Hunter400ae982019-06-04 16:00:00 +03003891
3892 itrace_synth_opts__set_time_range(&itrace_synth_opts,
3893 script.ptime_range,
3894 script.range_num);
David Aherna91f4c42016-11-29 10:15:43 -07003895 }
3896
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03003897 if (event_switch_on) {
3898 script.evswitch.on = perf_evlist__find_evsel_by_str(session->evlist, event_switch_on);
3899 if (script.evswitch.on == NULL) {
3900 fprintf(stderr, "switch-on event not found (%s)\n", event_switch_on);
3901 err = -ENOENT;
3902 goto out_delete;
3903 }
3904 script.evswitch.discarding = true;
3905 }
3906
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003907 if (event_switch_off) {
3908 script.evswitch.off = perf_evlist__find_evsel_by_str(session->evlist, event_switch_off);
3909 if (script.evswitch.off == NULL) {
3910 fprintf(stderr, "switch-off event not found (%s)\n", event_switch_off);
3911 err = -ENOENT;
3912 goto out_delete;
3913 }
3914 }
3915
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003916 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003917
Adrian Hunterd445dd22014-08-15 22:08:37 +03003918 flush_scripting();
3919
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003920out_delete:
Adrian Hunter400ae982019-06-04 16:00:00 +03003921 if (script.ptime_range) {
3922 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
Jin Yao284c4e12019-03-01 18:13:06 +08003923 zfree(&script.ptime_range);
Adrian Hunter400ae982019-06-04 16:00:00 +03003924 }
Jin Yaocc2ef582018-01-10 23:00:33 +08003925
Jiri Olsacfc88742016-01-05 22:09:06 +01003926 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003927 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003928
3929 if (script_started)
3930 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003931out:
3932 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003933}