blob: e7b950e977a9bc70cc944507dd58acfa50a0db79 [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 Melo8829e562019-08-15 11:00:11 -03001810 if (evswitch__discard(&script->evswitch, evsel))
1811 return;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001812
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001813 ++es->samples;
1814
Jiri Olsa28a0b392018-01-07 17:03:52 +01001815 perf_sample__fprintf_start(sample, thread, evsel,
1816 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001817
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001818 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001819 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001820
Namhyung Kime944d3d2013-11-18 14:34:52 +09001821 if (PRINT_FIELD(EVNAME)) {
1822 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001823
1824 if (!script->name_width)
1825 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1826
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001827 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001828 }
1829
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001830 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001831 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001832
Akihiro Nagai95582592012-01-30 13:43:09 +09001833 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001834 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001835 return;
1836 }
1837
Andi Kleen96167162019-01-17 11:48:34 -08001838 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001839 event_format__fprintf(evsel->tp_format, sample->cpu,
1840 sample->raw_data, sample->raw_size, fp);
1841 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001842
1843 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001844 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001845
David Ahern7cec0922011-05-30 13:08:23 -06001846 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001847 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001848
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001849 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001850 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001851
1852 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001853 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001854
David Ahern787bef12011-05-27 14:28:43 -06001855 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001856 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001857
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001858 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001859 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001860 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001861 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001862
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001863 fputc(cursor ? '\n' : ' ', fp);
1864 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001865 }
1866
Stephane Eranianfc36f942015-08-31 18:41:10 +02001867 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001868 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001869
Andi Kleenb1491ac2017-09-05 11:40:57 -07001870 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001871 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001872
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001873 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001874 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001875 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001876 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001877 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001878 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001879
Wang Nan30372f02016-02-24 11:20:45 +00001880 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001881 perf_sample__fprintf_bpf_output(sample, fp);
1882 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001883
1884 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001885 fprintf(fp, "%16" PRIx64, sample->phys_addr);
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001886
1887 perf_sample__fprintf_ipc(sample, attr, fp);
1888
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001889 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001890
Andi Kleendd2e18e2018-12-03 16:18:48 -08001891 if (PRINT_FIELD(SRCCODE)) {
1892 if (map__fprintf_srccode(al->map, al->addr, stdout,
1893 &thread->srccode_state))
1894 printf("\n");
1895 }
1896
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001897 if (PRINT_FIELD(METRIC))
1898 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001899
1900 if (verbose)
1901 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001902}
1903
Tom Zanussi956ffd02009-11-25 01:15:46 -06001904static struct scripting_ops *scripting_ops;
1905
Jiri Olsa32dcd022019-07-21 13:23:51 +02001906static void __process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsa36e33c52016-01-06 11:49:56 +01001907{
Jiri Olsaaf663bd2019-07-21 13:24:39 +02001908 int nthreads = thread_map__nr(counter->core.threads);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001909 int ncpus = perf_evsel__nr_cpus(counter);
1910 int cpu, thread;
1911 static int header_printed;
1912
1913 if (counter->system_wide)
1914 nthreads = 1;
1915
1916 if (!header_printed) {
1917 printf("%3s %8s %15s %15s %15s %15s %s\n",
1918 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1919 header_printed = 1;
1920 }
1921
1922 for (thread = 0; thread < nthreads; thread++) {
1923 for (cpu = 0; cpu < ncpus; cpu++) {
1924 struct perf_counts_values *counts;
1925
1926 counts = perf_counts(counter->counts, cpu, thread);
1927
1928 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
Jiri Olsad400bd32019-07-21 13:24:37 +02001929 counter->core.cpus->map[cpu],
Jiri Olsaaf663bd2019-07-21 13:24:39 +02001930 thread_map__pid(counter->core.threads, thread),
Jiri Olsa36e33c52016-01-06 11:49:56 +01001931 counts->val,
1932 counts->ena,
1933 counts->run,
1934 tstamp,
1935 perf_evsel__name(counter));
1936 }
1937 }
1938}
1939
Jiri Olsa32dcd022019-07-21 13:23:51 +02001940static void process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsae099eba2016-01-05 22:09:09 +01001941{
1942 if (scripting_ops && scripting_ops->process_stat)
1943 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001944 else
1945 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001946}
1947
1948static void process_stat_interval(u64 tstamp)
1949{
1950 if (scripting_ops && scripting_ops->process_stat_interval)
1951 scripting_ops->process_stat_interval(tstamp);
1952}
1953
Tom Zanussi956ffd02009-11-25 01:15:46 -06001954static void setup_scripting(void)
1955{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001956 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001957 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001958}
1959
Adrian Hunterd445dd22014-08-15 22:08:37 +03001960static int flush_scripting(void)
1961{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001962 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001963}
1964
Tom Zanussi956ffd02009-11-25 01:15:46 -06001965static int cleanup_scripting(void)
1966{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001967 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001968
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001969 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001970}
1971
Andi Kleene87e5482019-03-11 07:44:52 -07001972static bool filter_cpu(struct perf_sample *sample)
1973{
1974 if (cpu_list)
1975 return !test_bit(sample->cpu, cpu_bitmap);
1976 return false;
1977}
1978
Jiri Olsa809e9422015-11-26 18:55:21 +01001979static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001980 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001981 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001982 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001983 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001984{
Jiri Olsa809e9422015-11-26 18:55:21 +01001985 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001986 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001987
Jin Yao2ab046c2017-12-08 21:13:46 +08001988 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1989 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001990 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001991 }
David Aherna91f4c42016-11-29 10:15:43 -07001992
David Ahern1424dc92011-03-09 22:23:28 -07001993 if (debug_mode) {
1994 if (sample->time < last_timestamp) {
1995 pr_err("Samples misordered, previous: %" PRIu64
1996 " this: %" PRIu64 "\n", last_timestamp,
1997 sample->time);
1998 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001999 }
David Ahern1424dc92011-03-09 22:23:28 -07002000 last_timestamp = sample->time;
2001 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002002 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002003
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03002004 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07002005 pr_err("problem processing %d event, skipping it.\n",
2006 event->header.type);
2007 return -1;
2008 }
2009
2010 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002011 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07002012
Andi Kleene87e5482019-03-11 07:44:52 -07002013 if (filter_cpu(sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002014 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002015
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002016 if (scripting_ops)
2017 scripting_ops->process_event(event, sample, evsel, &al);
2018 else
Andi Kleen48d02a12017-02-23 15:46:34 -08002019 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002020
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002021out_put:
2022 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002023 return 0;
2024}
2025
Adrian Hunter7ea95722013-11-01 15:51:30 +02002026static int process_attr(struct perf_tool *tool, union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02002027 struct evlist **pevlist)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002028{
2029 struct perf_script *scr = container_of(tool, struct perf_script, tool);
Jiri Olsa63503db2019-07-21 13:23:52 +02002030 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002031 struct evsel *evsel, *pos;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002032 int err;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002033 static struct evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002034
2035 err = perf_event__process_attr(tool, event, pevlist);
2036 if (err)
2037 return err;
2038
2039 evlist = *pevlist;
2040 evsel = perf_evlist__last(*pevlist);
2041
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302042 if (!evsel->priv) {
2043 if (scr->per_event_dump) {
2044 evsel->priv = perf_evsel_script__new(evsel,
2045 scr->session->data);
2046 } else {
2047 es = zalloc(sizeof(*es));
2048 if (!es)
2049 return -ENOMEM;
2050 es->fp = stdout;
2051 evsel->priv = es;
2052 }
2053 }
2054
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002055 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2056 evsel->core.attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002057 return 0;
2058
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002059 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002060 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002061 return 0;
2062 }
2063
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002064 set_print_ip_opts(&evsel->core.attr);
Adrian Hunter7ea95722013-11-01 15:51:30 +02002065
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002066 if (evsel->core.attr.sample_type)
Jiri Olsad2b5a312015-10-16 12:41:25 +02002067 err = perf_evsel__check_attr(evsel, scr->session);
2068
2069 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002070}
2071
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002072static int process_comm_event(struct perf_tool *tool,
2073 union perf_event *event,
2074 struct perf_sample *sample,
2075 struct machine *machine)
2076{
2077 struct thread *thread;
2078 struct perf_script *script = container_of(tool, struct perf_script, tool);
2079 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002080 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002081 int ret = -1;
2082
2083 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2084 if (thread == NULL) {
2085 pr_debug("problem processing COMM event, skipping it.\n");
2086 return -1;
2087 }
2088
2089 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2090 goto out;
2091
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002092 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002093 sample->cpu = 0;
2094 sample->time = 0;
2095 sample->tid = event->comm.tid;
2096 sample->pid = event->comm.pid;
2097 }
Andi Kleene87e5482019-03-11 07:44:52 -07002098 if (!filter_cpu(sample)) {
2099 perf_sample__fprintf_start(sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01002100 PERF_RECORD_COMM, stdout);
Andi Kleene87e5482019-03-11 07:44:52 -07002101 perf_event__fprintf(event, stdout);
2102 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002103 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002104out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002105 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002106 return ret;
2107}
2108
Hari Bathini96a44bb2017-03-08 02:12:06 +05302109static int process_namespaces_event(struct perf_tool *tool,
2110 union perf_event *event,
2111 struct perf_sample *sample,
2112 struct machine *machine)
2113{
2114 struct thread *thread;
2115 struct perf_script *script = container_of(tool, struct perf_script, tool);
2116 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002117 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302118 int ret = -1;
2119
2120 thread = machine__findnew_thread(machine, event->namespaces.pid,
2121 event->namespaces.tid);
2122 if (thread == NULL) {
2123 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2124 return -1;
2125 }
2126
2127 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2128 goto out;
2129
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002130 if (!evsel->core.attr.sample_id_all) {
Hari Bathini96a44bb2017-03-08 02:12:06 +05302131 sample->cpu = 0;
2132 sample->time = 0;
2133 sample->tid = event->namespaces.tid;
2134 sample->pid = event->namespaces.pid;
2135 }
Andi Kleene87e5482019-03-11 07:44:52 -07002136 if (!filter_cpu(sample)) {
2137 perf_sample__fprintf_start(sample, thread, evsel,
2138 PERF_RECORD_NAMESPACES, stdout);
2139 perf_event__fprintf(event, stdout);
2140 }
Hari Bathini96a44bb2017-03-08 02:12:06 +05302141 ret = 0;
2142out:
2143 thread__put(thread);
2144 return ret;
2145}
2146
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002147static int process_fork_event(struct perf_tool *tool,
2148 union perf_event *event,
2149 struct perf_sample *sample,
2150 struct machine *machine)
2151{
2152 struct thread *thread;
2153 struct perf_script *script = container_of(tool, struct perf_script, tool);
2154 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002155 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002156
2157 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2158 return -1;
2159
2160 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2161 if (thread == NULL) {
2162 pr_debug("problem processing FORK event, skipping it.\n");
2163 return -1;
2164 }
2165
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002166 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002167 sample->cpu = 0;
2168 sample->time = event->fork.time;
2169 sample->tid = event->fork.tid;
2170 sample->pid = event->fork.pid;
2171 }
Andi Kleene87e5482019-03-11 07:44:52 -07002172 if (!filter_cpu(sample)) {
2173 perf_sample__fprintf_start(sample, thread, evsel,
2174 PERF_RECORD_FORK, stdout);
2175 perf_event__fprintf(event, stdout);
2176 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002177 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002178
2179 return 0;
2180}
2181static int process_exit_event(struct perf_tool *tool,
2182 union perf_event *event,
2183 struct perf_sample *sample,
2184 struct machine *machine)
2185{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002186 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002187 struct thread *thread;
2188 struct perf_script *script = container_of(tool, struct perf_script, tool);
2189 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002190 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002191
2192 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2193 if (thread == NULL) {
2194 pr_debug("problem processing EXIT event, skipping it.\n");
2195 return -1;
2196 }
2197
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002198 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002199 sample->cpu = 0;
2200 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002201 sample->tid = event->fork.tid;
2202 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002203 }
Andi Kleene87e5482019-03-11 07:44:52 -07002204 if (!filter_cpu(sample)) {
2205 perf_sample__fprintf_start(sample, thread, evsel,
2206 PERF_RECORD_EXIT, stdout);
2207 perf_event__fprintf(event, stdout);
2208 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002209
2210 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002211 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002212
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002213 thread__put(thread);
2214 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002215}
2216
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002217static int process_mmap_event(struct perf_tool *tool,
2218 union perf_event *event,
2219 struct perf_sample *sample,
2220 struct machine *machine)
2221{
2222 struct thread *thread;
2223 struct perf_script *script = container_of(tool, struct perf_script, tool);
2224 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002225 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002226
2227 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2228 return -1;
2229
2230 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2231 if (thread == NULL) {
2232 pr_debug("problem processing MMAP event, skipping it.\n");
2233 return -1;
2234 }
2235
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002236 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002237 sample->cpu = 0;
2238 sample->time = 0;
2239 sample->tid = event->mmap.tid;
2240 sample->pid = event->mmap.pid;
2241 }
Andi Kleene87e5482019-03-11 07:44:52 -07002242 if (!filter_cpu(sample)) {
2243 perf_sample__fprintf_start(sample, thread, evsel,
2244 PERF_RECORD_MMAP, stdout);
2245 perf_event__fprintf(event, stdout);
2246 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002247 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002248 return 0;
2249}
2250
2251static int process_mmap2_event(struct perf_tool *tool,
2252 union perf_event *event,
2253 struct perf_sample *sample,
2254 struct machine *machine)
2255{
2256 struct thread *thread;
2257 struct perf_script *script = container_of(tool, struct perf_script, tool);
2258 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002259 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002260
2261 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2262 return -1;
2263
2264 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2265 if (thread == NULL) {
2266 pr_debug("problem processing MMAP2 event, skipping it.\n");
2267 return -1;
2268 }
2269
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002270 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002271 sample->cpu = 0;
2272 sample->time = 0;
2273 sample->tid = event->mmap2.tid;
2274 sample->pid = event->mmap2.pid;
2275 }
Andi Kleene87e5482019-03-11 07:44:52 -07002276 if (!filter_cpu(sample)) {
2277 perf_sample__fprintf_start(sample, thread, evsel,
2278 PERF_RECORD_MMAP2, stdout);
2279 perf_event__fprintf(event, stdout);
2280 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002281 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002282 return 0;
2283}
2284
Adrian Hunter7c148982015-07-21 12:44:06 +03002285static int process_switch_event(struct perf_tool *tool,
2286 union perf_event *event,
2287 struct perf_sample *sample,
2288 struct machine *machine)
2289{
2290 struct thread *thread;
2291 struct perf_script *script = container_of(tool, struct perf_script, tool);
2292 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002293 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Adrian Hunter7c148982015-07-21 12:44:06 +03002294
2295 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2296 return -1;
2297
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002298 if (scripting_ops && scripting_ops->process_switch)
2299 scripting_ops->process_switch(event, sample, machine);
2300
2301 if (!script->show_switch_events)
2302 return 0;
2303
Adrian Hunter7c148982015-07-21 12:44:06 +03002304 thread = machine__findnew_thread(machine, sample->pid,
2305 sample->tid);
2306 if (thread == NULL) {
2307 pr_debug("problem processing SWITCH event, skipping it.\n");
2308 return -1;
2309 }
2310
Andi Kleene87e5482019-03-11 07:44:52 -07002311 if (!filter_cpu(sample)) {
2312 perf_sample__fprintf_start(sample, thread, evsel,
2313 PERF_RECORD_SWITCH, stdout);
2314 perf_event__fprintf(event, stdout);
2315 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002316 thread__put(thread);
2317 return 0;
2318}
2319
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002320static int
2321process_lost_event(struct perf_tool *tool,
2322 union perf_event *event,
2323 struct perf_sample *sample,
2324 struct machine *machine)
2325{
2326 struct perf_script *script = container_of(tool, struct perf_script, tool);
2327 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002328 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002329 struct thread *thread;
2330
2331 thread = machine__findnew_thread(machine, sample->pid,
2332 sample->tid);
2333 if (thread == NULL)
2334 return -1;
2335
Andi Kleene87e5482019-03-11 07:44:52 -07002336 if (!filter_cpu(sample)) {
2337 perf_sample__fprintf_start(sample, thread, evsel,
2338 PERF_RECORD_LOST, stdout);
2339 perf_event__fprintf(event, stdout);
2340 }
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002341 thread__put(thread);
2342 return 0;
2343}
2344
Jiri Olsa3233b372018-02-06 19:17:59 +01002345static int
2346process_finished_round_event(struct perf_tool *tool __maybe_unused,
2347 union perf_event *event,
2348 struct ordered_events *oe __maybe_unused)
2349
2350{
2351 perf_event__fprintf(event, stdout);
2352 return 0;
2353}
2354
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002355static int
2356process_bpf_events(struct perf_tool *tool __maybe_unused,
2357 union perf_event *event,
2358 struct perf_sample *sample,
2359 struct machine *machine)
2360{
2361 struct thread *thread;
2362 struct perf_script *script = container_of(tool, struct perf_script, tool);
2363 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002364 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002365
2366 if (machine__process_ksymbol(machine, event, sample) < 0)
2367 return -1;
2368
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002369 if (!evsel->core.attr.sample_id_all) {
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002370 perf_event__fprintf(event, stdout);
2371 return 0;
2372 }
2373
2374 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2375 if (thread == NULL) {
2376 pr_debug("problem processing MMAP event, skipping it.\n");
2377 return -1;
2378 }
2379
2380 if (!filter_cpu(sample)) {
2381 perf_sample__fprintf_start(sample, thread, evsel,
2382 event->header.type, stdout);
2383 perf_event__fprintf(event, stdout);
2384 }
2385
2386 thread__put(thread);
2387 return 0;
2388}
2389
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002390static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002391{
2392 session_done = 1;
2393}
2394
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002395static void perf_script__fclose_per_event_dump(struct perf_script *script)
2396{
Jiri Olsa63503db2019-07-21 13:23:52 +02002397 struct evlist *evlist = script->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002398 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002399
2400 evlist__for_each_entry(evlist, evsel) {
2401 if (!evsel->priv)
2402 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002403 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002404 evsel->priv = NULL;
2405 }
2406}
2407
2408static int perf_script__fopen_per_event_dump(struct perf_script *script)
2409{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002410 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002411
2412 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002413 /*
2414 * Already setup? I.e. we may be called twice in cases like
2415 * Intel PT, one for the intel_pt// and dummy events, then
2416 * for the evsels syntheized from the auxtrace info.
2417 *
2418 * Ses perf_script__process_auxtrace_info.
2419 */
2420 if (evsel->priv != NULL)
2421 continue;
2422
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002423 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002424 if (evsel->priv == NULL)
2425 goto out_err_fclose;
2426 }
2427
2428 return 0;
2429
2430out_err_fclose:
2431 perf_script__fclose_per_event_dump(script);
2432 return -1;
2433}
2434
2435static int perf_script__setup_per_event_dump(struct perf_script *script)
2436{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002437 struct evsel *evsel;
2438 static struct evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002439
2440 if (script->per_event_dump)
2441 return perf_script__fopen_per_event_dump(script);
2442
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002443 es_stdout.fp = stdout;
2444
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002445 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002446 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002447
2448 return 0;
2449}
2450
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002451static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2452{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002453 struct evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002454
2455 evlist__for_each_entry(script->session->evlist, evsel) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002456 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002457
2458 perf_evsel_script__fprintf(es, stdout);
2459 perf_evsel_script__delete(es);
2460 evsel->priv = NULL;
2461 }
2462}
2463
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002464static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002465{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002466 int ret;
2467
Tom Zanussic239da32010-04-01 23:59:18 -05002468 signal(SIGINT, sig_handler);
2469
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002470 perf_stat__init_shadow_stats();
2471
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002472 /* override event processing functions */
2473 if (script->show_task_events) {
2474 script->tool.comm = process_comm_event;
2475 script->tool.fork = process_fork_event;
2476 script->tool.exit = process_exit_event;
2477 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002478 if (script->show_mmap_events) {
2479 script->tool.mmap = process_mmap_event;
2480 script->tool.mmap2 = process_mmap2_event;
2481 }
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002482 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
Adrian Hunter7c148982015-07-21 12:44:06 +03002483 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302484 if (script->show_namespace_events)
2485 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002486 if (script->show_lost_events)
2487 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002488 if (script->show_round_events) {
2489 script->tool.ordered_events = false;
2490 script->tool.finished_round = process_finished_round_event;
2491 }
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002492 if (script->show_bpf_events) {
2493 script->tool.ksymbol = process_bpf_events;
2494 script->tool.bpf_event = process_bpf_events;
2495 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002496
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002497 if (perf_script__setup_per_event_dump(script)) {
2498 pr_err("Couldn't create the per event dump files\n");
2499 return -1;
2500 }
2501
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002502 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002503
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002504 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002505 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002506
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002507 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002508 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002509
2510 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002511}
2512
Tom Zanussi956ffd02009-11-25 01:15:46 -06002513struct script_spec {
2514 struct list_head node;
2515 struct scripting_ops *ops;
2516 char spec[0];
2517};
2518
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002519static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002520
2521static struct script_spec *script_spec__new(const char *spec,
2522 struct scripting_ops *ops)
2523{
2524 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2525
2526 if (s != NULL) {
2527 strcpy(s->spec, spec);
2528 s->ops = ops;
2529 }
2530
2531 return s;
2532}
2533
Tom Zanussi956ffd02009-11-25 01:15:46 -06002534static void script_spec__add(struct script_spec *s)
2535{
2536 list_add_tail(&s->node, &script_specs);
2537}
2538
2539static struct script_spec *script_spec__find(const char *spec)
2540{
2541 struct script_spec *s;
2542
2543 list_for_each_entry(s, &script_specs, node)
2544 if (strcasecmp(s->spec, spec) == 0)
2545 return s;
2546 return NULL;
2547}
2548
Tom Zanussi956ffd02009-11-25 01:15:46 -06002549int script_spec_register(const char *spec, struct scripting_ops *ops)
2550{
2551 struct script_spec *s;
2552
2553 s = script_spec__find(spec);
2554 if (s)
2555 return -1;
2556
Taeung Song8560bae2016-02-26 00:13:10 +09002557 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002558 if (!s)
2559 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002560 else
2561 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002562
2563 return 0;
2564}
2565
2566static struct scripting_ops *script_spec__lookup(const char *spec)
2567{
2568 struct script_spec *s = script_spec__find(spec);
2569 if (!s)
2570 return NULL;
2571
2572 return s->ops;
2573}
2574
2575static void list_available_languages(void)
2576{
2577 struct script_spec *s;
2578
2579 fprintf(stderr, "\n");
2580 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002581 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002582
2583 list_for_each_entry(s, &script_specs, node)
2584 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2585
2586 fprintf(stderr, "\n");
2587}
2588
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002589static int parse_scriptname(const struct option *opt __maybe_unused,
2590 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002591{
2592 char spec[PATH_MAX];
2593 const char *script, *ext;
2594 int len;
2595
Tom Zanussif526d682010-01-27 02:27:52 -06002596 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002597 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002598 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002599 }
2600
2601 script = strchr(str, ':');
2602 if (script) {
2603 len = script - str;
2604 if (len >= PATH_MAX) {
2605 fprintf(stderr, "invalid language specifier");
2606 return -1;
2607 }
2608 strncpy(spec, str, len);
2609 spec[len] = '\0';
2610 scripting_ops = script_spec__lookup(spec);
2611 if (!scripting_ops) {
2612 fprintf(stderr, "invalid language specifier");
2613 return -1;
2614 }
2615 script++;
2616 } else {
2617 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002618 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002619 if (!ext) {
2620 fprintf(stderr, "invalid script extension");
2621 return -1;
2622 }
2623 scripting_ops = script_spec__lookup(++ext);
2624 if (!scripting_ops) {
2625 fprintf(stderr, "invalid script extension");
2626 return -1;
2627 }
2628 }
2629
2630 script_name = strdup(script);
2631
2632 return 0;
2633}
2634
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002635static int parse_output_fields(const struct option *opt __maybe_unused,
2636 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002637{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002638 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002639 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002640 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002641 int rc = 0;
2642 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002643 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002644 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002645
2646 if (!str)
2647 return -ENOMEM;
2648
David Ahern2c9e45f72011-03-17 10:03:21 -06002649 /* first word can state for which event type the user is specifying
2650 * the fields. If no type exists, the specified fields apply to all
2651 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002652 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002653 tok = strchr(str, ':');
2654 if (tok) {
2655 *tok = '\0';
2656 tok++;
2657 if (!strcmp(str, "hw"))
2658 type = PERF_TYPE_HARDWARE;
2659 else if (!strcmp(str, "sw"))
2660 type = PERF_TYPE_SOFTWARE;
2661 else if (!strcmp(str, "trace"))
2662 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002663 else if (!strcmp(str, "raw"))
2664 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002665 else if (!strcmp(str, "break"))
2666 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002667 else if (!strcmp(str, "synth"))
2668 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002669 else {
2670 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002671 rc = -EINVAL;
2672 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002673 }
2674
2675 if (output[type].user_set)
2676 pr_warning("Overriding previous field request for %s events.\n",
2677 event_type(type));
2678
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002679 /* Don't override defaults for +- */
2680 if (strchr(tok, '+') || strchr(tok, '-'))
2681 goto parse;
2682
David Ahern2c9e45f72011-03-17 10:03:21 -06002683 output[type].fields = 0;
2684 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002685 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002686
2687 } else {
2688 tok = str;
2689 if (strlen(str) == 0) {
2690 fprintf(stderr,
2691 "Cannot set fields to 'none' for all event types.\n");
2692 rc = -EINVAL;
2693 goto out;
2694 }
2695
Andi Kleen36ce5652017-06-02 08:48:10 -07002696 /* Don't override defaults for +- */
2697 if (strchr(str, '+') || strchr(str, '-'))
2698 goto parse;
2699
David Ahern2c9e45f72011-03-17 10:03:21 -06002700 if (output_set_by_user())
2701 pr_warning("Overriding previous field request for all events.\n");
2702
Adrian Hunter14057202017-06-21 13:17:19 +03002703 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002704 output[j].fields = 0;
2705 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002706 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002707 }
David Ahern1424dc92011-03-09 22:23:28 -07002708 }
2709
Andi Kleen36ce5652017-06-02 08:48:10 -07002710parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002711 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002712 if (*tok == '+') {
2713 if (change == SET)
2714 goto out_badmix;
2715 change = ADD;
2716 tok++;
2717 } else if (*tok == '-') {
2718 if (change == SET)
2719 goto out_badmix;
2720 change = REMOVE;
2721 tok++;
2722 } else {
2723 if (change != SET && change != DEFAULT)
2724 goto out_badmix;
2725 change = SET;
2726 }
2727
David Ahern745f43e2011-03-09 22:23:26 -07002728 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002729 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002730 break;
David Ahern745f43e2011-03-09 22:23:26 -07002731 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002732 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002733 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002734 continue;
2735 }
David Ahern745f43e2011-03-09 22:23:26 -07002736 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002737 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002738 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002739 goto out;
2740 }
2741
2742 if (type == -1) {
2743 /* add user option to all events types for
2744 * which it is valid
2745 */
Adrian Hunter14057202017-06-21 13:17:19 +03002746 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002747 if (output[j].invalid_fields & all_output_options[i].field) {
2748 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2749 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002750 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002751 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002752 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002753 output[j].user_set_fields &= ~all_output_options[i].field;
2754 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002755 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002756 output[j].user_set_fields |= all_output_options[i].field;
2757 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002758 output[j].user_set = true;
2759 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002760 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002761 }
2762 } else {
2763 if (output[type].invalid_fields & all_output_options[i].field) {
2764 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2765 all_output_options[i].str, event_type(type));
2766
2767 rc = -EINVAL;
2768 goto out;
2769 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002770 if (change == REMOVE)
2771 output[type].fields &= ~all_output_options[i].field;
2772 else
2773 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002774 output[type].user_set = true;
2775 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002776 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002777 }
2778
2779 if (type >= 0) {
2780 if (output[type].fields == 0) {
2781 pr_debug("No fields requested for %s type. "
2782 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002783 }
David Ahern1424dc92011-03-09 22:23:28 -07002784 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002785 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002786
Andi Kleen36ce5652017-06-02 08:48:10 -07002787out_badmix:
2788 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2789 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002790out:
David Ahern745f43e2011-03-09 22:23:26 -07002791 free(str);
2792 return rc;
2793}
2794
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002795#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2796 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2797 if ((lang_dirent->d_type == DT_DIR || \
2798 (lang_dirent->d_type == DT_UNKNOWN && \
2799 is_directory(scripts_path, lang_dirent))) && \
2800 (strcmp(lang_dirent->d_name, ".")) && \
2801 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002802
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002803#define for_each_script(lang_path, lang_dir, script_dirent) \
2804 while ((script_dirent = readdir(lang_dir)) != NULL) \
2805 if (script_dirent->d_type != DT_DIR && \
2806 (script_dirent->d_type != DT_UNKNOWN || \
2807 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002808
2809
2810#define RECORD_SUFFIX "-record"
2811#define REPORT_SUFFIX "-report"
2812
2813struct script_desc {
2814 struct list_head node;
2815 char *name;
2816 char *half_liner;
2817 char *args;
2818};
2819
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002820static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002821
2822static struct script_desc *script_desc__new(const char *name)
2823{
2824 struct script_desc *s = zalloc(sizeof(*s));
2825
Tom Zanussib5b87312010-11-10 08:16:51 -06002826 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002827 s->name = strdup(name);
2828
2829 return s;
2830}
2831
2832static void script_desc__delete(struct script_desc *s)
2833{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002834 zfree(&s->name);
2835 zfree(&s->half_liner);
2836 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002837 free(s);
2838}
2839
2840static void script_desc__add(struct script_desc *s)
2841{
2842 list_add_tail(&s->node, &script_descs);
2843}
2844
2845static struct script_desc *script_desc__find(const char *name)
2846{
2847 struct script_desc *s;
2848
2849 list_for_each_entry(s, &script_descs, node)
2850 if (strcasecmp(s->name, name) == 0)
2851 return s;
2852 return NULL;
2853}
2854
2855static struct script_desc *script_desc__findnew(const char *name)
2856{
2857 struct script_desc *s = script_desc__find(name);
2858
2859 if (s)
2860 return s;
2861
2862 s = script_desc__new(name);
2863 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002864 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002865
2866 script_desc__add(s);
2867
2868 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002869}
2870
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002871static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002872{
2873 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002874 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002875
2876 if (strlen(str) > suffix_len) {
2877 p = str + strlen(str) - suffix_len;
2878 if (!strncmp(p, suffix, suffix_len))
2879 return p;
2880 }
2881
2882 return NULL;
2883}
2884
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002885static int read_script_info(struct script_desc *desc, const char *filename)
2886{
2887 char line[BUFSIZ], *p;
2888 FILE *fp;
2889
2890 fp = fopen(filename, "r");
2891 if (!fp)
2892 return -1;
2893
2894 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002895 p = skip_spaces(line);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002896 if (strlen(p) == 0)
2897 continue;
2898 if (*p != '#')
2899 continue;
2900 p++;
2901 if (strlen(p) && *p == '!')
2902 continue;
2903
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002904 p = skip_spaces(p);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002905 if (strlen(p) && p[strlen(p) - 1] == '\n')
2906 p[strlen(p) - 1] = '\0';
2907
2908 if (!strncmp(p, "description:", strlen("description:"))) {
2909 p += strlen("description:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002910 desc->half_liner = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002911 continue;
2912 }
2913
2914 if (!strncmp(p, "args:", strlen("args:"))) {
2915 p += strlen("args:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002916 desc->args = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002917 continue;
2918 }
2919 }
2920
2921 fclose(fp);
2922
2923 return 0;
2924}
2925
Robert Richter38efb532011-11-25 11:38:40 +01002926static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2927{
2928 char *script_root, *str;
2929
2930 script_root = strdup(script_dirent->d_name);
2931 if (!script_root)
2932 return NULL;
2933
2934 str = (char *)ends_with(script_root, suffix);
2935 if (!str) {
2936 free(script_root);
2937 return NULL;
2938 }
2939
2940 *str = '\0';
2941 return script_root;
2942}
2943
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002944static int list_available_scripts(const struct option *opt __maybe_unused,
2945 const char *s __maybe_unused,
2946 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002947{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002948 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002949 char scripts_path[MAXPATHLEN];
2950 DIR *scripts_dir, *lang_dir;
2951 char script_path[MAXPATHLEN];
2952 char lang_path[MAXPATHLEN];
2953 struct script_desc *desc;
2954 char first_half[BUFSIZ];
2955 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002956
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002957 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002958
2959 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002960 if (!scripts_dir) {
2961 fprintf(stdout,
2962 "open(%s) failed.\n"
2963 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2964 scripts_path);
2965 exit(-1);
2966 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002967
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002968 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002969 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2970 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002971 lang_dir = opendir(lang_path);
2972 if (!lang_dir)
2973 continue;
2974
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002975 for_each_script(lang_path, lang_dir, script_dirent) {
2976 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002977 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002978 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002979 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2980 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002981 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002982 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002983 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002984 }
2985 }
2986
2987 fprintf(stdout, "List of available trace scripts:\n");
2988 list_for_each_entry(desc, &script_descs, node) {
2989 sprintf(first_half, "%s %s", desc->name,
2990 desc->args ? desc->args : "");
2991 fprintf(stdout, " %-36s %s\n", first_half,
2992 desc->half_liner ? desc->half_liner : "");
2993 }
2994
2995 exit(0);
2996}
2997
Feng Tange5f37052012-09-07 16:42:26 +08002998/*
Feng Tang49e639e2012-10-30 11:56:03 +08002999 * Some scripts specify the required events in their "xxx-record" file,
3000 * this function will check if the events in perf.data match those
3001 * mentioned in the "xxx-record".
3002 *
3003 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3004 * which is covered well now. And new parsing code should be added to
3005 * cover the future complexing formats like event groups etc.
3006 */
3007static int check_ev_match(char *dir_name, char *scriptname,
3008 struct perf_session *session)
3009{
3010 char filename[MAXPATHLEN], evname[128];
3011 char line[BUFSIZ], *p;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003012 struct evsel *pos;
Feng Tang49e639e2012-10-30 11:56:03 +08003013 int match, len;
3014 FILE *fp;
3015
Jiri Olsa77f18152018-03-19 09:29:01 +01003016 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08003017
3018 fp = fopen(filename, "r");
3019 if (!fp)
3020 return -1;
3021
3022 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003023 p = skip_spaces(line);
Feng Tang49e639e2012-10-30 11:56:03 +08003024 if (*p == '#')
3025 continue;
3026
3027 while (strlen(p)) {
3028 p = strstr(p, "-e");
3029 if (!p)
3030 break;
3031
3032 p += 2;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003033 p = skip_spaces(p);
Feng Tang49e639e2012-10-30 11:56:03 +08003034 len = strcspn(p, " \t");
3035 if (!len)
3036 break;
3037
3038 snprintf(evname, len + 1, "%s", p);
3039
3040 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003041 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08003042 if (!strcmp(perf_evsel__name(pos), evname)) {
3043 match = 1;
3044 break;
3045 }
3046 }
3047
3048 if (!match) {
3049 fclose(fp);
3050 return -1;
3051 }
3052 }
3053 }
3054
3055 fclose(fp);
3056 return 0;
3057}
3058
3059/*
Feng Tange5f37052012-09-07 16:42:26 +08003060 * Return -1 if none is found, otherwise the actual scripts number.
3061 *
3062 * Currently the only user of this function is the script browser, which
3063 * will list all statically runnable scripts, select one, execute it and
3064 * show the output in a perf browser.
3065 */
Andi Kleen905e4af2019-03-11 07:45:01 -07003066int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3067 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08003068{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003069 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08003070 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08003071 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08003072 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003073 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003074 .path = input_name,
3075 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003076 };
Feng Tange5f37052012-09-07 16:42:26 +08003077 char *temp;
3078 int i = 0;
3079
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003080 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08003081 if (!session)
3082 return -1;
3083
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003084 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003085
3086 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003087 if (!scripts_dir) {
3088 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003089 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003090 }
Feng Tange5f37052012-09-07 16:42:26 +08003091
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003092 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003093 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3094 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003095#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003096 if (strstr(lang_path, "perl"))
3097 continue;
3098#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003099#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003100 if (strstr(lang_path, "python"))
3101 continue;
3102#endif
3103
3104 lang_dir = opendir(lang_path);
3105 if (!lang_dir)
3106 continue;
3107
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003108 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003109 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003110 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003111 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003112 if (i >= num)
3113 break;
3114 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3115 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003116 script_dirent->d_name);
3117 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003118 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003119 (temp - script_dirent->d_name) + 1,
3120 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003121
3122 if (check_ev_match(lang_path,
3123 scripts_array[i], session))
3124 continue;
3125
Feng Tange5f37052012-09-07 16:42:26 +08003126 i++;
3127 }
Feng Tang49e639e2012-10-30 11:56:03 +08003128 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003129 }
3130
Feng Tang49e639e2012-10-30 11:56:03 +08003131 closedir(scripts_dir);
3132 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003133 return i;
3134}
3135
Tom Zanussi38752942009-12-15 02:53:39 -06003136static char *get_script_path(const char *script_root, const char *suffix)
3137{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003138 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003139 char scripts_path[MAXPATHLEN];
3140 char script_path[MAXPATHLEN];
3141 DIR *scripts_dir, *lang_dir;
3142 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003143 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003144
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003145 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003146
3147 scripts_dir = opendir(scripts_path);
3148 if (!scripts_dir)
3149 return NULL;
3150
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003151 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003152 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3153 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003154 lang_dir = opendir(lang_path);
3155 if (!lang_dir)
3156 continue;
3157
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003158 for_each_script(lang_path, lang_dir, script_dirent) {
3159 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003160 if (__script_root && !strcmp(script_root, __script_root)) {
3161 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003162 closedir(lang_dir);
3163 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003164 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3165 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003166 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003167 }
3168 free(__script_root);
3169 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003170 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003171 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003172 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003173
Robert Richter38efb532011-11-25 11:38:40 +01003174 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003175}
3176
Tom Zanussib5b87312010-11-10 08:16:51 -06003177static bool is_top_script(const char *script_path)
3178{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003179 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003180}
3181
3182static int has_required_arg(char *script_path)
3183{
3184 struct script_desc *desc;
3185 int n_args = 0;
3186 char *p;
3187
3188 desc = script_desc__new(NULL);
3189
3190 if (read_script_info(desc, script_path))
3191 goto out;
3192
3193 if (!desc->args)
3194 goto out;
3195
3196 for (p = desc->args; *p; p++)
3197 if (*p == '<')
3198 n_args++;
3199out:
3200 script_desc__delete(desc);
3201
3202 return n_args;
3203}
3204
David Ahernd54b1a92012-08-26 12:24:46 -06003205static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003206{
3207 char **__argv = malloc(sizeof(const char *) * argc);
3208
David Ahernd54b1a92012-08-26 12:24:46 -06003209 if (!__argv) {
3210 pr_err("malloc failed\n");
3211 return -1;
3212 }
3213
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003214 memcpy(__argv, argv, sizeof(const char *) * argc);
3215 argc = parse_options(argc, (const char **)__argv, record_options,
3216 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3217 free(__argv);
3218
David Ahernd54b1a92012-08-26 12:24:46 -06003219 system_wide = (argc == 0);
3220
3221 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003222}
3223
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003224static void script__setup_sample_type(struct perf_script *script)
3225{
3226 struct perf_session *session = script->session;
3227 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3228
3229 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3230 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003231 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003232 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003233 dwarf_callchain_users = true;
3234 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003235 callchain_param.record_mode = CALLCHAIN_LBR;
3236 else
3237 callchain_param.record_mode = CALLCHAIN_FP;
3238 }
3239}
3240
Jiri Olsa89f16882018-09-13 14:54:03 +02003241static int process_stat_round_event(struct perf_session *session,
3242 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003243{
3244 struct stat_round_event *round = &event->stat_round;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003245 struct evsel *counter;
Jiri Olsae099eba2016-01-05 22:09:09 +01003246
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003247 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003248 perf_stat_process_counter(&stat_config, counter);
3249 process_stat(counter, round->time);
3250 }
3251
3252 process_stat_interval(round->time);
3253 return 0;
3254}
3255
Jiri Olsa89f16882018-09-13 14:54:03 +02003256static int process_stat_config_event(struct perf_session *session __maybe_unused,
3257 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003258{
3259 perf_event__read_stat_config(&stat_config, &event->stat_config);
3260 return 0;
3261}
3262
Jiri Olsacfc88742016-01-05 22:09:06 +01003263static int set_maps(struct perf_script *script)
3264{
Jiri Olsa63503db2019-07-21 13:23:52 +02003265 struct evlist *evlist = script->session->evlist;
Jiri Olsacfc88742016-01-05 22:09:06 +01003266
3267 if (!script->cpus || !script->threads)
3268 return 0;
3269
3270 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3271 return -EINVAL;
3272
Jiri Olsa453fa032019-07-21 13:24:43 +02003273 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
Jiri Olsacfc88742016-01-05 22:09:06 +01003274
3275 if (perf_evlist__alloc_stats(evlist, true))
3276 return -ENOMEM;
3277
3278 script->allocated = true;
3279 return 0;
3280}
3281
3282static
Jiri Olsa89f16882018-09-13 14:54:03 +02003283int process_thread_map_event(struct perf_session *session,
3284 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003285{
Jiri Olsa89f16882018-09-13 14:54:03 +02003286 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003287 struct perf_script *script = container_of(tool, struct perf_script, tool);
3288
3289 if (script->threads) {
3290 pr_warning("Extra thread map event, ignoring.\n");
3291 return 0;
3292 }
3293
3294 script->threads = thread_map__new_event(&event->thread_map);
3295 if (!script->threads)
3296 return -ENOMEM;
3297
3298 return set_maps(script);
3299}
3300
3301static
Jiri Olsa89f16882018-09-13 14:54:03 +02003302int process_cpu_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->cpus) {
3309 pr_warning("Extra cpu map event, ignoring.\n");
3310 return 0;
3311 }
3312
3313 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3314 if (!script->cpus)
3315 return -ENOMEM;
3316
3317 return set_maps(script);
3318}
3319
Jiri Olsa89f16882018-09-13 14:54:03 +02003320static int process_feature_event(struct perf_session *session,
3321 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303322{
3323 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003324 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303325 return 0;
3326}
3327
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003328#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003329static int perf_script__process_auxtrace_info(struct perf_session *session,
3330 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003331{
Jiri Olsa89f16882018-09-13 14:54:03 +02003332 struct perf_tool *tool = session->tool;
3333
3334 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003335
3336 if (ret == 0) {
3337 struct perf_script *script = container_of(tool, struct perf_script, tool);
3338
3339 ret = perf_script__setup_per_event_dump(script);
3340 }
3341
3342 return ret;
3343}
3344#else
3345#define perf_script__process_auxtrace_info 0
3346#endif
3347
Andi Kleenb585ebd2018-09-20 11:05:36 -07003348static int parse_insn_trace(const struct option *opt __maybe_unused,
3349 const char *str __maybe_unused,
3350 int unset __maybe_unused)
3351{
3352 parse_output_fields(NULL, "+insn,-event,-period", 0);
3353 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003354 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003355 return 0;
3356}
3357
3358static int parse_xed(const struct option *opt __maybe_unused,
3359 const char *str __maybe_unused,
3360 int unset __maybe_unused)
3361{
3362 force_pager("xed -F insn: -A -64 | less");
3363 return 0;
3364}
3365
Andi Kleend1b15522018-09-20 11:05:38 -07003366static int parse_call_trace(const struct option *opt __maybe_unused,
3367 const char *str __maybe_unused,
3368 int unset __maybe_unused)
3369{
3370 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3371 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003372 symbol_conf.nanosecs = true;
Jiri Olsa1c492422019-05-08 15:20:05 +02003373 symbol_conf.pad_output_len_dso = 50;
Andi Kleend1b15522018-09-20 11:05:38 -07003374 return 0;
3375}
3376
3377static int parse_callret_trace(const struct option *opt __maybe_unused,
3378 const char *str __maybe_unused,
3379 int unset __maybe_unused)
3380{
3381 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3382 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003383 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003384 return 0;
3385}
3386
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003387int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003388{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003389 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003390 bool header = false;
3391 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003392 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003393 char *rec_script_path = NULL;
3394 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003395 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003396 struct itrace_synth_opts itrace_synth_opts = {
3397 .set = false,
3398 .default_no_sample = true,
3399 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003400 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003401 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003402 const char **__argv;
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003403 const char *event_switch_on = NULL,
3404 *event_switch_off = NULL;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003405 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003406 struct perf_script script = {
3407 .tool = {
3408 .sample = process_sample_event,
3409 .mmap = perf_event__process_mmap,
3410 .mmap2 = perf_event__process_mmap2,
3411 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303412 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003413 .exit = perf_event__process_exit,
3414 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003415 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003416 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003417 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303418 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003419 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003420 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003421 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003422 .auxtrace = perf_event__process_auxtrace,
3423 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003424 .stat = perf_event__process_stat_event,
3425 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003426 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003427 .thread_map = process_thread_map_event,
3428 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003429 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003430 .ordering_requires_timestamps = true,
3431 },
3432 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003433 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003434 .mode = PERF_DATA_MODE_READ,
3435 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003436 const struct option options[] = {
3437 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3438 "dump raw trace in ASCII"),
3439 OPT_INCR('v', "verbose", &verbose,
3440 "be more verbose (show symbol address, etc)"),
3441 OPT_BOOLEAN('L', "Latency", &latency_format,
3442 "show latency attributes (irqs/preemption disabled, etc)"),
3443 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3444 list_available_scripts),
3445 OPT_CALLBACK('s', "script", NULL, "name",
3446 "script file name (lang:script name, script name, or *)",
3447 parse_scriptname),
3448 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3449 "generate perf-script.xx script in specified language"),
3450 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3451 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3452 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003453 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3454 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003455 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3456 "file", "vmlinux pathname"),
3457 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3458 "file", "kallsyms pathname"),
3459 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3460 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003461 OPT_CALLBACK(0, "symfs", NULL, "directory",
3462 "Look for files with symbols relative to this directory",
3463 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003464 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003465 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003466 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003467 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003468 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303469 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3470 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
Adrian Hunter68fb45b2019-05-20 14:37:14 +03003471 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003472 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003473 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3474 "system-wide collection from all CPUs"),
3475 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3476 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003477 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3478 "Decode instructions from itrace", parse_insn_trace),
3479 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3480 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003481 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3482 "Decode calls from from itrace", parse_call_trace),
3483 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3484 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003485 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3486 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003487 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3488 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003489 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3490 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3491 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003492 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3493 "only consider symbols in these pids"),
3494 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3495 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003496 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3497 "Set the maximum stack depth when parsing the callchain, "
3498 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003499 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Andi Kleen90b10f42019-03-14 15:50:00 -07003500 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003501 OPT_BOOLEAN('I', "show-info", &show_full_info,
3502 "display extended information from perf.data file"),
3503 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3504 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003505 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3506 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003507 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3508 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003509 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3510 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303511 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3512 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003513 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3514 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003515 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3516 "Show round events (if recorded)"),
Jiri Olsa490c8cc2019-05-08 15:20:08 +02003517 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3518 "Show bpf related events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003519 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3520 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003521 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003522 OPT_INTEGER(0, "max-blocks", &max_blocks,
3523 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003524 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003525 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003526 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003527 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003528 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003529 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3530 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003531 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3532 "Enable symbol demangling"),
3533 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3534 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003535 OPT_STRING(0, "time", &script.time_str, "str",
3536 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003537 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3538 "Show inline function"),
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003539 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3540 "guest mount directory under which every guest os"
3541 " instance has a subdir"),
3542 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3543 "file", "file saving guest os vmlinux"),
3544 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3545 "file", "file saving guest os /proc/kallsyms"),
3546 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3547 "file", "file saving guest os /proc/modules"),
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03003548 OPT_STRING(0, "switch-on", &event_switch_on,
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003549 "event", "Consider events after the ocurrence of this event"),
3550 OPT_STRING(0, "switch-off", &event_switch_off,
3551 "event", "Stop considering events after the ocurrence of this event"),
Arnaldo Carvalho de Melo6469eb62019-08-14 16:40:58 -03003552 OPT_BOOLEAN(0, "show-on-off-events", &script.evswitch.show_on_off_events,
3553 "Show the on/off switch events, used with --switch-on"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003554 OPT_END()
3555 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003556 const char * const script_subcommands[] = { "record", "report", NULL };
3557 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003558 "perf script [<options>]",
3559 "perf script [<options>] record <script> [<record-options>] <command>",
3560 "perf script [<options>] report <script> [script-args]",
3561 "perf script [<options>] <script> [<record-options>] <command>",
3562 "perf script [<options>] <top-script> [script-args]",
3563 NULL
3564 };
Tom Zanussi38752942009-12-15 02:53:39 -06003565
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003566 perf_set_singlethreaded();
3567
Tom Zanussib5b87312010-11-10 08:16:51 -06003568 setup_scripting();
3569
Yunlong Song40cae2b2015-03-18 21:35:54 +08003570 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003571 PARSE_OPT_STOP_AT_NON_OPTION);
3572
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003573 if (symbol_conf.guestmount ||
3574 symbol_conf.default_guest_vmlinux_name ||
3575 symbol_conf.default_guest_kallsyms ||
3576 symbol_conf.default_guest_modules) {
3577 /*
3578 * Enable guest sample processing.
3579 */
3580 perf_guest = true;
3581 }
3582
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003583 data.path = input_name;
3584 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003585
Tom Zanussib5b87312010-11-10 08:16:51 -06003586 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3587 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3588 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003589 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003590 }
3591
Tom Zanussib5b87312010-11-10 08:16:51 -06003592 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3593 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3594 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003595 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003596 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003597 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003598 return -1;
3599 }
Tom Zanussi38752942009-12-15 02:53:39 -06003600 }
3601
Andi Kleen90b10f42019-03-14 15:50:00 -07003602 if (script.time_str && reltime) {
3603 fprintf(stderr, "Don't combine --reltime with --time\n");
3604 return -1;
3605 }
3606
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003607 if (itrace_synth_opts.callchain &&
3608 itrace_synth_opts.callchain_sz > scripting_max_stack)
3609 scripting_max_stack = itrace_synth_opts.callchain_sz;
3610
Ben Hutchings44e668c2010-10-10 16:10:03 +01003611 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003612 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003613
Tom Zanussib5b87312010-11-10 08:16:51 -06003614 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003615 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003616 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003617 pid_t pid;
3618
Tom Zanussib5b87312010-11-10 08:16:51 -06003619 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3620 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3621
3622 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003623 usage_with_options_msg(script_usage, options,
3624 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003625 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003626 }
3627
Tom Zanussib5b87312010-11-10 08:16:51 -06003628 if (is_top_script(argv[0])) {
3629 rep_args = argc - 1;
3630 } else {
3631 int rec_args;
3632
3633 rep_args = has_required_arg(rep_script_path);
3634 rec_args = (argc - 1) - rep_args;
3635 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003636 usage_with_options_msg(script_usage, options,
3637 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003638 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003639 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003640 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003641 }
3642
3643 if (pipe(live_pipe) < 0) {
3644 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003645 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003646 }
3647
3648 pid = fork();
3649 if (pid < 0) {
3650 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003651 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003652 }
3653
3654 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003655 j = 0;
3656
Tom Zanussia0cccc22010-04-01 23:59:25 -05003657 dup2(live_pipe[1], 1);
3658 close(live_pipe[0]);
3659
Robert Richter317df652011-11-25 15:05:25 +01003660 if (is_top_script(argv[0])) {
3661 system_wide = true;
3662 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003663 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3664 err = -1;
3665 goto out;
3666 }
Robert Richter317df652011-11-25 15:05:25 +01003667 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003668
3669 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003670 if (!__argv) {
3671 pr_err("malloc failed\n");
3672 err = -ENOMEM;
3673 goto out;
3674 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003675
Tom Zanussib5b87312010-11-10 08:16:51 -06003676 __argv[j++] = "/bin/sh";
3677 __argv[j++] = rec_script_path;
3678 if (system_wide)
3679 __argv[j++] = "-a";
3680 __argv[j++] = "-q";
3681 __argv[j++] = "-o";
3682 __argv[j++] = "-";
3683 for (i = rep_args + 1; i < argc; i++)
3684 __argv[j++] = argv[i];
3685 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003686
3687 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003688 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003689 exit(-1);
3690 }
3691
3692 dup2(live_pipe[0], 0);
3693 close(live_pipe[1]);
3694
Tom Zanussib5b87312010-11-10 08:16:51 -06003695 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003696 if (!__argv) {
3697 pr_err("malloc failed\n");
3698 err = -ENOMEM;
3699 goto out;
3700 }
3701
Tom Zanussib5b87312010-11-10 08:16:51 -06003702 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003703 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003704 __argv[j++] = rep_script_path;
3705 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003706 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003707 __argv[j++] = "-i";
3708 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003709 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003710
3711 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003712 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003713 exit(-1);
3714 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003715
Tom Zanussib5b87312010-11-10 08:16:51 -06003716 if (rec_script_path)
3717 script_path = rec_script_path;
3718 if (rep_script_path)
3719 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003720
Tom Zanussib5b87312010-11-10 08:16:51 -06003721 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003722 j = 0;
3723
Robert Richter317df652011-11-25 15:05:25 +01003724 if (!rec_script_path)
3725 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003726 else if (!system_wide) {
3727 if (have_cmd(argc - 1, &argv[1]) != 0) {
3728 err = -1;
3729 goto out;
3730 }
3731 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003732
3733 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003734 if (!__argv) {
3735 pr_err("malloc failed\n");
3736 err = -ENOMEM;
3737 goto out;
3738 }
3739
Tom Zanussib5b87312010-11-10 08:16:51 -06003740 __argv[j++] = "/bin/sh";
3741 __argv[j++] = script_path;
3742 if (system_wide)
3743 __argv[j++] = "-a";
3744 for (i = 2; i < argc; i++)
3745 __argv[j++] = argv[i];
3746 __argv[j++] = NULL;
3747
3748 execvp("/bin/sh", (char **)__argv);
3749 free(__argv);
3750 exit(-1);
3751 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003752
Milian Wolffc1c9b962018-10-21 21:14:23 +02003753 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003754 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003755 use_browser = 0;
3756 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003757
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003758 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003759 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003760 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003761
Jiri Olsae90debd2013-12-09 11:02:50 +01003762 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003763 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003764 perf_session__fprintf_info(session, stdout, show_full_info);
3765 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003766 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003767 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003768 if (show_full_info)
3769 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003770
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003771 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003772 goto out_delete;
3773
Andi Kleen3ab481a2019-03-05 06:47:45 -08003774 uname(&uts);
Song Liu9d491692019-06-20 18:44:38 -07003775 if (data.is_pipe || /* assume pipe_mode indicates native_arch */
3776 !strcmp(uts.machine, session->header.env.arch) ||
Andi Kleen3ab481a2019-03-05 06:47:45 -08003777 (!strcmp(uts.machine, "x86_64") &&
3778 !strcmp(session->header.env.arch, "i386")))
3779 native_arch = true;
3780
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003781 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003782 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003783
Andi Kleen99f753f2018-09-20 11:05:39 -07003784 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3785 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003786 itrace_synth_opts.thread_stack = true;
3787
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003788 session->itrace_synth_opts = &itrace_synth_opts;
3789
Anton Blanchard5d67be92011-07-04 21:57:50 +10003790 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003791 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3792 if (err < 0)
3793 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003794 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003795 }
3796
David Ahern1424dc92011-03-09 22:23:28 -07003797 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003798 symbol_conf.use_callchain = true;
3799 else
3800 symbol_conf.use_callchain = false;
3801
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003802 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003803 tep_set_function_resolver(session->tevent.pevent,
3804 machine__resolve_kernel_addr,
3805 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003806 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003807 err = -1;
3808 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003809 }
3810
Tom Zanussi956ffd02009-11-25 01:15:46 -06003811 if (generate_script_lang) {
3812 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003813 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003814
David Ahern2c9e45f72011-03-17 10:03:21 -06003815 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003816 fprintf(stderr,
3817 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003818 err = -EINVAL;
3819 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003820 }
3821
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003822 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003823 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003824 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003825 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003826 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003827 }
3828
3829 err = fstat(input, &perf_stat);
3830 if (err < 0) {
3831 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003832 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003833 }
3834
3835 if (!perf_stat.st_size) {
3836 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003837 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003838 }
3839
3840 scripting_ops = script_spec__lookup(generate_script_lang);
3841 if (!scripting_ops) {
3842 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003843 err = -ENOENT;
3844 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003845 }
3846
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003847 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003848 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003849 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003850 }
3851
3852 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003853 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003854 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003855 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003856 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003857 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003858 }
3859
David Ahern9cbdb702011-04-06 21:54:20 -06003860
3861 err = perf_session__check_output_opt(session);
3862 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003863 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003864
Jin Yao284c4e12019-03-01 18:13:06 +08003865 if (script.time_str) {
3866 err = perf_time__parse_for_ranges(script.time_str, session,
3867 &script.ptime_range,
3868 &script.range_size,
3869 &script.range_num);
3870 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08003871 goto out_delete;
Adrian Hunter400ae982019-06-04 16:00:00 +03003872
3873 itrace_synth_opts__set_time_range(&itrace_synth_opts,
3874 script.ptime_range,
3875 script.range_num);
David Aherna91f4c42016-11-29 10:15:43 -07003876 }
3877
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03003878 if (event_switch_on) {
3879 script.evswitch.on = perf_evlist__find_evsel_by_str(session->evlist, event_switch_on);
3880 if (script.evswitch.on == NULL) {
3881 fprintf(stderr, "switch-on event not found (%s)\n", event_switch_on);
3882 err = -ENOENT;
3883 goto out_delete;
3884 }
3885 script.evswitch.discarding = true;
3886 }
3887
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003888 if (event_switch_off) {
3889 script.evswitch.off = perf_evlist__find_evsel_by_str(session->evlist, event_switch_off);
3890 if (script.evswitch.off == NULL) {
3891 fprintf(stderr, "switch-off event not found (%s)\n", event_switch_off);
3892 err = -ENOENT;
3893 goto out_delete;
3894 }
3895 }
3896
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003897 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003898
Adrian Hunterd445dd22014-08-15 22:08:37 +03003899 flush_scripting();
3900
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003901out_delete:
Adrian Hunter400ae982019-06-04 16:00:00 +03003902 if (script.ptime_range) {
3903 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
Jin Yao284c4e12019-03-01 18:13:06 +08003904 zfree(&script.ptime_range);
Adrian Hunter400ae982019-06-04 16:00:00 +03003905 }
Jin Yaocc2ef582018-01-10 23:00:33 +08003906
Jiri Olsacfc88742016-01-05 22:09:06 +01003907 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003908 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003909
3910 if (script_started)
3911 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003912out:
3913 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003914}