blob: b6eed0f7e8358f514353b61782aa3cd97c3a673c [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"
Feng Tang36385be2012-09-07 16:42:24 +080019#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020020#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030021#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010022#include "util/cpumap.h"
23#include "util/thread_map.h"
24#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080025#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030026#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030027#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070028#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010029#include "util/path.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030030#include "print_binary.h"
Andi Kleen3ab481a2019-03-05 06:47:45 -080031#include "archinsn.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100032#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030033#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030034#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030035#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030036#include <linux/zalloc.h>
Andi Kleen3ab481a2019-03-05 06:47:45 -080037#include <sys/utsname.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010038#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010039#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080040#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030041#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030042#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030043#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030044#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030045#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030046#include <sys/types.h>
47#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030048#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030049#include <unistd.h>
Andi Kleenb585ebd2018-09-20 11:05:36 -070050#include <subcmd/pager.h>
Jiri Olsa453fa032019-07-21 13:24:43 +020051#include <perf/evlist.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020052
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030053#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030054
Tom Zanussi956ffd02009-11-25 01:15:46 -060055static char const *script_name;
56static char const *generate_script_lang;
Andi Kleen90b10f42019-03-14 15:50:00 -070057static bool reltime;
58static u64 initial_time;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020059static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020060static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020061static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070062static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090063static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010064static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030065static bool print_flags;
Anton Blanchard5d67be92011-07-04 21:57:50 +100066static const char *cpu_list;
67static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010068static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080069static int max_blocks;
Andi Kleen3ab481a2019-03-05 06:47:45 -080070static bool native_arch;
Tom Zanussi956ffd02009-11-25 01:15:46 -060071
Adrian Hunter44cbe722015-09-25 16:15:50 +030072unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030073
David Ahern745f43e2011-03-09 22:23:26 -070074enum perf_output_field {
75 PERF_OUTPUT_COMM = 1U << 0,
76 PERF_OUTPUT_TID = 1U << 1,
77 PERF_OUTPUT_PID = 1U << 2,
78 PERF_OUTPUT_TIME = 1U << 3,
79 PERF_OUTPUT_CPU = 1U << 4,
80 PERF_OUTPUT_EVNAME = 1U << 5,
81 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060082 PERF_OUTPUT_IP = 1U << 7,
83 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060084 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060085 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090086 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020087 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020088 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020089 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020090 PERF_OUTPUT_BRSTACK = 1U << 15,
91 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010092 PERF_OUTPUT_DATA_SRC = 1U << 17,
93 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000094 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030095 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030096 PERF_OUTPUT_INSN = 1U << 21,
97 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080098 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070099 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +0300100 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -0400101 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -0700102 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800103 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100104 PERF_OUTPUT_MISC = 1U << 29,
Andi Kleendd2e18e2018-12-03 16:18:48 -0800105 PERF_OUTPUT_SRCCODE = 1U << 30,
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300106 PERF_OUTPUT_IPC = 1U << 31,
David Ahern745f43e2011-03-09 22:23:26 -0700107};
108
109struct output_option {
110 const char *str;
111 enum perf_output_field field;
112} all_output_options[] = {
113 {.str = "comm", .field = PERF_OUTPUT_COMM},
114 {.str = "tid", .field = PERF_OUTPUT_TID},
115 {.str = "pid", .field = PERF_OUTPUT_PID},
116 {.str = "time", .field = PERF_OUTPUT_TIME},
117 {.str = "cpu", .field = PERF_OUTPUT_CPU},
118 {.str = "event", .field = PERF_OUTPUT_EVNAME},
119 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600120 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700121 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600122 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600123 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900124 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200125 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200126 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200127 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700128 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200129 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
130 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100131 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
132 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000133 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300134 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300135 {.str = "insn", .field = PERF_OUTPUT_INSN},
136 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800137 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700138 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300139 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400140 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800141 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100142 {.str = "misc", .field = PERF_OUTPUT_MISC},
Andi Kleendd2e18e2018-12-03 16:18:48 -0800143 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300144 {.str = "ipc", .field = PERF_OUTPUT_IPC},
David Ahern745f43e2011-03-09 22:23:26 -0700145};
146
Adrian Hunter14057202017-06-21 13:17:19 +0300147enum {
148 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
149 OUTPUT_TYPE_MAX
150};
151
David Ahern745f43e2011-03-09 22:23:26 -0700152/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600153static struct {
154 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600155 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400156 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600157 u64 fields;
158 u64 invalid_fields;
Andi Kleen4b6ac812019-02-24 07:37:12 -0800159 u64 user_set_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300160} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700161
David Ahern2c9e45f72011-03-17 10:03:21 -0600162 [PERF_TYPE_HARDWARE] = {
163 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700164
David Ahern2c9e45f72011-03-17 10:03:21 -0600165 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
166 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600167 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530168 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
169 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600170
Wang Nan30372f02016-02-24 11:20:45 +0000171 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600172 },
173
174 [PERF_TYPE_SOFTWARE] = {
175 .user_set = false,
176
177 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
178 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600179 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530180 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
181 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
182 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600183
184 .invalid_fields = PERF_OUTPUT_TRACE,
185 },
186
187 [PERF_TYPE_TRACEPOINT] = {
188 .user_set = false,
189
190 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
191 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000192 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600193 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700194
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530195 [PERF_TYPE_HW_CACHE] = {
196 .user_set = false,
197
198 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
199 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
200 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
201 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
202 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
203
204 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
205 },
206
Arun Sharma0817a6a2011-04-14 10:38:18 -0700207 [PERF_TYPE_RAW] = {
208 .user_set = false,
209
210 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
211 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600212 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530213 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
214 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
215 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
216 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700217
Wang Nan30372f02016-02-24 11:20:45 +0000218 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700219 },
Wang Nan27cfef02015-12-08 02:25:43 +0000220
221 [PERF_TYPE_BREAKPOINT] = {
222 .user_set = false,
223
224 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
225 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
226 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530227 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
228 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000229
Wang Nan30372f02016-02-24 11:20:45 +0000230 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000231 },
Adrian Hunter14057202017-06-21 13:17:19 +0300232
233 [OUTPUT_TYPE_SYNTH] = {
234 .user_set = false,
235
236 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
237 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
238 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530239 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
240 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300241
242 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
243 },
David Ahern1424dc92011-03-09 22:23:28 -0700244};
David Ahern745f43e2011-03-09 22:23:26 -0700245
Jiri Olsa32dcd022019-07-21 13:23:51 +0200246struct evsel_script {
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300247 char *filename;
248 FILE *fp;
249 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800250 /* For metric output */
251 u64 val;
252 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300253};
254
Jiri Olsa32dcd022019-07-21 13:23:51 +0200255static inline struct evsel_script *evsel_script(struct evsel *evsel)
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800256{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200257 return (struct evsel_script *)evsel->priv;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800258}
259
Jiri Olsa32dcd022019-07-21 13:23:51 +0200260static struct evsel_script *perf_evsel_script__new(struct evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100261 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300262{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200263 struct evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300264
265 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100266 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300267 goto out_free;
268 es->fp = fopen(es->filename, "w");
269 if (es->fp == NULL)
270 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300271 }
272
273 return es;
274out_free_filename:
275 zfree(&es->filename);
276out_free:
277 free(es);
278 return NULL;
279}
280
Jiri Olsa32dcd022019-07-21 13:23:51 +0200281static void perf_evsel_script__delete(struct evsel_script *es)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300282{
283 zfree(&es->filename);
284 fclose(es->fp);
285 es->fp = NULL;
286 free(es);
287}
288
Jiri Olsa32dcd022019-07-21 13:23:51 +0200289static int perf_evsel_script__fprintf(struct evsel_script *es, FILE *fp)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300290{
291 struct stat st;
292
293 fstat(fileno(es->fp), &st);
294 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
295 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
296}
297
Adrian Hunter14057202017-06-21 13:17:19 +0300298static inline int output_type(unsigned int type)
299{
300 switch (type) {
301 case PERF_TYPE_SYNTH:
302 return OUTPUT_TYPE_SYNTH;
303 default:
304 return type;
305 }
306}
307
308static inline unsigned int attr_type(unsigned int type)
309{
310 switch (type) {
311 case OUTPUT_TYPE_SYNTH:
312 return PERF_TYPE_SYNTH;
313 default:
314 return type;
315 }
316}
317
David Ahern2c9e45f72011-03-17 10:03:21 -0600318static bool output_set_by_user(void)
319{
320 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300321 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600322 if (output[j].user_set)
323 return true;
324 }
325 return false;
326}
David Ahern745f43e2011-03-09 22:23:26 -0700327
David Ahern9cbdb702011-04-06 21:54:20 -0600328static const char *output_field2str(enum perf_output_field field)
329{
330 int i, imax = ARRAY_SIZE(all_output_options);
331 const char *str = "";
332
333 for (i = 0; i < imax; ++i) {
334 if (all_output_options[i].field == field) {
335 str = all_output_options[i].str;
336 break;
337 }
338 }
339 return str;
340}
341
Adrian Hunter14057202017-06-21 13:17:19 +0300342#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700343
Jiri Olsa32dcd022019-07-21 13:23:51 +0200344static int perf_evsel__do_check_stype(struct evsel *evsel,
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300345 u64 sample_type, const char *sample_msg,
346 enum perf_output_field field,
347 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700348{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200349 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300350 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600351 const char *evname;
352
353 if (attr->sample_type & sample_type)
354 return 0;
355
Andi Kleen4b6ac812019-02-24 07:37:12 -0800356 if (output[type].user_set_fields & field) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300357 if (allow_user_set)
358 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300359 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600360 pr_err("Samples for '%s' event do not have %s attribute set. "
361 "Cannot print '%s' field.\n",
362 evname, sample_msg, output_field2str(field));
363 return -1;
364 }
365
366 /* user did not ask for it explicitly so remove from the default list */
367 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300368 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600369 pr_debug("Samples for '%s' event do not have %s attribute set. "
370 "Skipping '%s' field.\n",
371 evname, sample_msg, output_field2str(field));
372
373 return 0;
374}
375
Jiri Olsa32dcd022019-07-21 13:23:51 +0200376static int perf_evsel__check_stype(struct evsel *evsel,
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300377 u64 sample_type, const char *sample_msg,
378 enum perf_output_field field)
379{
380 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
381 false);
382}
383
Jiri Olsa32dcd022019-07-21 13:23:51 +0200384static int perf_evsel__check_attr(struct evsel *evsel,
David Ahern9cbdb702011-04-06 21:54:20 -0600385 struct perf_session *session)
386{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200387 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300388 bool allow_user_set;
389
Jiri Olsae099eba2016-01-05 22:09:09 +0100390 if (perf_header__has_feat(&session->header, HEADER_STAT))
391 return 0;
392
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300393 allow_user_set = perf_header__has_feat(&session->header,
394 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600395
David Ahern1424dc92011-03-09 22:23:28 -0700396 if (PRINT_FIELD(TRACE) &&
397 !perf_session__has_traces(session, "record -R"))
398 return -EINVAL;
399
David Ahern787bef12011-05-27 14:28:43 -0600400 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300401 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
402 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700403 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700404 }
David Ahern7cec0922011-05-30 13:08:23 -0600405
406 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300407 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
408 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600409 return -EINVAL;
410
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100411 if (PRINT_FIELD(DATA_SRC) &&
412 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
413 PERF_OUTPUT_DATA_SRC))
414 return -EINVAL;
415
416 if (PRINT_FIELD(WEIGHT) &&
417 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
418 PERF_OUTPUT_WEIGHT))
419 return -EINVAL;
420
Andi Kleen37fed3d2018-09-18 05:32:09 -0700421 if (PRINT_FIELD(SYM) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200422 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600423 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700424 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600425 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600426 return -EINVAL;
427 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900428 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
429 pr_err("Display of offsets requested but symbol is not"
430 "selected.\n");
431 return -EINVAL;
432 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700433 if (PRINT_FIELD(DSO) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200434 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
Andi Kleen37fed3d2018-09-18 05:32:09 -0700435 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600436 return -EINVAL;
437 }
Andi Kleendd2e18e2018-12-03 16:18:48 -0800438 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
Adrian Huntercc8fae12013-12-06 09:42:57 +0200439 pr_err("Display of source line number requested but sample IP is not\n"
440 "selected. Hence, no address to lookup the source line number.\n");
441 return -EINVAL;
442 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800443 if (PRINT_FIELD(BRSTACKINSN) &&
444 !(perf_evlist__combined_branch_type(session->evlist) &
445 PERF_SAMPLE_BRANCH_ANY)) {
446 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
447 "Hint: run 'perf record -b ...'\n");
448 return -EINVAL;
449 }
David Ahern1424dc92011-03-09 22:23:28 -0700450 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300451 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
452 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700453 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700454
455 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300456 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
457 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700458 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700459
460 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300461 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
462 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700463 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600464
Stephane Eranianfc36f942015-08-31 18:41:10 +0200465 if (PRINT_FIELD(IREGS) &&
466 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
467 PERF_OUTPUT_IREGS))
468 return -EINVAL;
469
Andi Kleenb1491ac2017-09-05 11:40:57 -0700470 if (PRINT_FIELD(UREGS) &&
471 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
472 PERF_OUTPUT_UREGS))
473 return -EINVAL;
474
Kan Liang49d58f02017-08-29 13:11:11 -0400475 if (PRINT_FIELD(PHYS_ADDR) &&
476 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
477 PERF_OUTPUT_PHYS_ADDR))
478 return -EINVAL;
479
David Ahern9cbdb702011-04-06 21:54:20 -0600480 return 0;
481}
482
Adrian Hunter7ea95722013-11-01 15:51:30 +0200483static void set_print_ip_opts(struct perf_event_attr *attr)
484{
Adrian Hunter14057202017-06-21 13:17:19 +0300485 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200486
487 output[type].print_ip_opts = 0;
488 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300489 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200490
491 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300492 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200493
494 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300495 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200496
497 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300498 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200499
500 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300501 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200502}
503
David Ahern9cbdb702011-04-06 21:54:20 -0600504/*
505 * verify all user requested events exist and the samples
506 * have the expected data
507 */
508static int perf_session__check_output_opt(struct perf_session *session)
509{
He Kuang40f20e52016-05-16 04:51:19 +0000510 unsigned int j;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200511 struct evsel *evsel;
David Ahern9cbdb702011-04-06 21:54:20 -0600512
Adrian Hunter14057202017-06-21 13:17:19 +0300513 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
514 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600515
516 /*
517 * even if fields is set to 0 (ie., show nothing) event must
518 * exist if user explicitly includes it on the command line
519 */
Adrian Hunter14057202017-06-21 13:17:19 +0300520 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
521 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600522 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300523 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600524 event_type(j));
525 return -1;
526 }
527
528 if (evsel && output[j].fields &&
529 perf_evsel__check_attr(evsel, session))
530 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400531
532 if (evsel == NULL)
533 continue;
534
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200535 set_print_ip_opts(&evsel->core.attr);
David Ahern1424dc92011-03-09 22:23:28 -0700536 }
537
Adrian Hunter98526ee2014-07-31 09:00:59 +0300538 if (!no_callchain) {
539 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000540 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300541
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300542 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000543 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300544 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300545 use_callchain = true;
546 break;
547 }
548 }
He Kuang71ac8992016-08-04 11:25:43 +0000549 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300550 symbol_conf.use_callchain = false;
551 }
552
David Ahern80b8b492013-11-19 21:07:37 -0700553 /*
554 * set default for tracepoints to print symbols only
555 * if callchains are present
556 */
557 if (symbol_conf.use_callchain &&
558 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700559 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700560
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300561 evlist__for_each_entry(session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200562 if (evsel->core.attr.type != j)
He Kuang40f20e52016-05-16 04:51:19 +0000563 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700564
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300565 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000566 output[j].fields |= PERF_OUTPUT_IP;
567 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530568 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000569 output[j].fields |= PERF_OUTPUT_DSO;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200570 set_print_ip_opts(&evsel->core.attr);
He Kuang40f20e52016-05-16 04:51:19 +0000571 goto out;
572 }
David Ahern80b8b492013-11-19 21:07:37 -0700573 }
574 }
575
576out:
David Ahern1424dc92011-03-09 22:23:28 -0700577 return 0;
578}
David Ahern745f43e2011-03-09 22:23:26 -0700579
Milian Wolff9add8fe2018-11-07 23:34:37 +0100580static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
581 FILE *fp
582)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200583{
Andi Kleenb1491ac2017-09-05 11:40:57 -0700584 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300585 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700586
587 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300588 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700589
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300590 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700591
592 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
593 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300594 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700595 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300596
Milian Wolffb07d16f2018-11-07 10:37:05 +0100597 fprintf(fp, "\n");
598
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300599 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700600}
601
Milian Wolff9add8fe2018-11-07 23:34:37 +0100602static int perf_sample__fprintf_iregs(struct perf_sample *sample,
603 struct perf_event_attr *attr, FILE *fp)
604{
605 return perf_sample__fprintf_regs(&sample->intr_regs,
606 attr->sample_regs_intr, fp);
607}
608
609static int perf_sample__fprintf_uregs(struct perf_sample *sample,
610 struct perf_event_attr *attr, FILE *fp)
611{
612 return perf_sample__fprintf_regs(&sample->user_regs,
613 attr->sample_regs_user, fp);
614}
615
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300616static int perf_sample__fprintf_start(struct perf_sample *sample,
617 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200618 struct evsel *evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100619 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700620{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200621 struct perf_event_attr *attr = &evsel->core.attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700622 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700623 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300624 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700625
David Ahern745f43e2011-03-09 22:23:26 -0700626 if (PRINT_FIELD(COMM)) {
627 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300628 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300629 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300630 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700631 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300632 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700633 }
David Ahernc70c94b2011-03-09 22:23:25 -0700634
David Ahern745f43e2011-03-09 22:23:26 -0700635 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300636 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700637 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300638 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700639 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300640 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700641
David Ahern745f43e2011-03-09 22:23:26 -0700642 if (PRINT_FIELD(CPU)) {
643 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300644 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700645 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300646 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700647 }
David Ahernc70c94b2011-03-09 22:23:25 -0700648
Jiri Olsa28a0b392018-01-07 17:03:52 +0100649 if (PRINT_FIELD(MISC)) {
650 int ret = 0;
651
652 #define has(m) \
653 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
654
655 if (has(KERNEL))
656 ret += fprintf(fp, "K");
657 if (has(USER))
658 ret += fprintf(fp, "U");
659 if (has(HYPERVISOR))
660 ret += fprintf(fp, "H");
661 if (has(GUEST_KERNEL))
662 ret += fprintf(fp, "G");
663 if (has(GUEST_USER))
664 ret += fprintf(fp, "g");
665
666 switch (type) {
667 case PERF_RECORD_MMAP:
668 case PERF_RECORD_MMAP2:
669 if (has(MMAP_DATA))
670 ret += fprintf(fp, "M");
671 break;
672 case PERF_RECORD_COMM:
673 if (has(COMM_EXEC))
674 ret += fprintf(fp, "E");
675 break;
676 case PERF_RECORD_SWITCH:
677 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300678 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100679 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300680 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
681 ret += fprintf(fp, "p");
682 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100683 default:
684 break;
685 }
686
687 #undef has
688
689 ret += fprintf(fp, "%*s", 6 - ret, " ");
690 printed += ret;
691 }
692
David Ahern745f43e2011-03-09 22:23:26 -0700693 if (PRINT_FIELD(TIME)) {
Andi Kleen90b10f42019-03-14 15:50:00 -0700694 u64 t = sample->time;
695 if (reltime) {
696 if (!initial_time)
697 initial_time = sample->time;
698 t = sample->time - initial_time;
699 }
700 nsecs = t;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300701 secs = nsecs / NSEC_PER_SEC;
702 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900703
Andi Kleen52bab882019-03-05 06:47:47 -0800704 if (symbol_conf.nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300705 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900706 else {
707 char sample_time[32];
Andi Kleen90b10f42019-03-14 15:50:00 -0700708 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300709 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900710 }
David Ahern745f43e2011-03-09 22:23:26 -0700711 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300712
713 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700714}
715
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200716static inline char
717mispred_str(struct branch_entry *br)
718{
719 if (!(br->flags.mispred || br->flags.predicted))
720 return '-';
721
722 return br->flags.predicted ? 'P' : 'M';
723}
724
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300725static int perf_sample__fprintf_brstack(struct perf_sample *sample,
726 struct thread *thread,
727 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200728{
729 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700730 struct addr_location alf, alt;
731 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300732 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200733
734 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300735 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200736
737 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700738 from = br->entries[i].from;
739 to = br->entries[i].to;
740
741 if (PRINT_FIELD(DSO)) {
742 memset(&alf, 0, sizeof(alf));
743 memset(&alt, 0, sizeof(alt));
Adrian Hunter692d0e62018-11-06 23:07:12 +0200744 thread__find_map_fb(thread, sample->cpumode, from, &alf);
745 thread__find_map_fb(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700746 }
747
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300748 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700749 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300750 printed += fprintf(fp, "(");
751 printed += map__fprintf_dsoname(alf.map, fp);
752 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700753 }
754
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300755 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700756 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300757 printed += fprintf(fp, "(");
758 printed += map__fprintf_dsoname(alt.map, fp);
759 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700760 }
761
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300762 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200763 mispred_str( br->entries + i),
764 br->entries[i].flags.in_tx? 'X' : '-',
765 br->entries[i].flags.abort? 'A' : '-',
766 br->entries[i].flags.cycles);
767 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300768
769 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200770}
771
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300772static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
773 struct thread *thread,
774 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200775{
776 struct branch_stack *br = sample->branch_stack;
777 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200778 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300779 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200780
781 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300782 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200783
784 for (i = 0; i < br->nr; i++) {
785
786 memset(&alf, 0, sizeof(alf));
787 memset(&alt, 0, sizeof(alt));
788 from = br->entries[i].from;
789 to = br->entries[i].to;
790
Adrian Hunter692d0e62018-11-06 23:07:12 +0200791 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
792 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200793
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300794 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700795 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300796 printed += fprintf(fp, "(");
797 printed += map__fprintf_dsoname(alf.map, fp);
798 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700799 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300800 printed += fprintf(fp, "%c", '/');
801 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700802 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300803 printed += fprintf(fp, "(");
804 printed += map__fprintf_dsoname(alt.map, fp);
805 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700806 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300807 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200808 mispred_str( br->entries + i),
809 br->entries[i].flags.in_tx? 'X' : '-',
810 br->entries[i].flags.abort? 'A' : '-',
811 br->entries[i].flags.cycles);
812 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300813
814 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200815}
816
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300817static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
818 struct thread *thread,
819 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700820{
821 struct branch_stack *br = sample->branch_stack;
822 struct addr_location alf, alt;
823 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300824 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700825
826 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300827 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700828
829 for (i = 0; i < br->nr; i++) {
830
831 memset(&alf, 0, sizeof(alf));
832 memset(&alt, 0, sizeof(alt));
833 from = br->entries[i].from;
834 to = br->entries[i].to;
835
Adrian Hunter692d0e62018-11-06 23:07:12 +0200836 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300837 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700838 from = map__map_ip(alf.map, from);
839
Adrian Hunter692d0e62018-11-06 23:07:12 +0200840 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300841 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700842 to = map__map_ip(alt.map, to);
843
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300844 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700845 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300846 printed += fprintf(fp, "(");
847 printed += map__fprintf_dsoname(alf.map, fp);
848 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700849 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300850 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700851 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300852 printed += fprintf(fp, "(");
853 printed += map__fprintf_dsoname(alt.map, fp);
854 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700855 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300856 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700857 mispred_str(br->entries + i),
858 br->entries[i].flags.in_tx ? 'X' : '-',
859 br->entries[i].flags.abort ? 'A' : '-',
860 br->entries[i].flags.cycles);
861 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300862
863 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700864}
Andi Kleen48d02a12017-02-23 15:46:34 -0800865#define MAXBB 16384UL
866
867static int grab_bb(u8 *buffer, u64 start, u64 end,
868 struct machine *machine, struct thread *thread,
869 bool *is64bit, u8 *cpumode, bool last)
870{
871 long offset, len;
872 struct addr_location al;
873 bool kernel;
874
875 if (!start || !end)
876 return 0;
877
878 kernel = machine__kernel_ip(machine, start);
879 if (kernel)
880 *cpumode = PERF_RECORD_MISC_KERNEL;
881 else
882 *cpumode = PERF_RECORD_MISC_USER;
883
884 /*
885 * Block overlaps between kernel and user.
886 * This can happen due to ring filtering
887 * On Intel CPUs the entry into the kernel is filtered,
888 * but the exit is not. Let the caller patch it up.
889 */
890 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300891 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800892 return -ENXIO;
893 }
894
895 memset(&al, 0, sizeof(al));
896 if (end - start > MAXBB - MAXINSN) {
897 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300898 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800899 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300900 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800901 return 0;
902 }
903
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300904 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300905 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800906 return 0;
907 }
908 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300909 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800910 return 0;
911 }
912
913 /* Load maps to ensure dso->is_64_bit has been updated */
914 map__load(al.map);
915
916 offset = al.map->map_ip(al.map, start);
917 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
918 end - start + MAXINSN);
919
920 *is64bit = al.map->dso->is_64_bit;
921 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300922 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800923 start, end);
924 return len;
925}
926
Andi Kleendd2e18e2018-12-03 16:18:48 -0800927static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
928{
929 struct addr_location al;
930 int ret = 0;
931
932 memset(&al, 0, sizeof(al));
933 thread__find_map(thread, cpumode, addr, &al);
934 if (!al.map)
935 return 0;
936 ret = map__fprintf_srccode(al.map, al.addr, stdout,
937 &thread->srccode_state);
938 if (ret)
939 ret += printf("\n");
940 return ret;
941}
942
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300943static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
944 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -0700945 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -0800946{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300947 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
948 dump_insn(x, ip, inbuf, len, NULL),
949 en->flags.predicted ? " PRED" : "",
950 en->flags.mispred ? " MISPRED" : "",
951 en->flags.in_tx ? " INTX" : "",
952 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800953 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -0700954 *total_cycles += en->flags.cycles;
955 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800956 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300957 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800958 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300959 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800960}
961
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300962static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
963 u8 cpumode, int cpu, struct symbol **lastsym,
964 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800965{
966 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300967 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800968
969 memset(&al, 0, sizeof(al));
970
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300971 thread__find_map(thread, cpumode, addr, &al);
972
Andi Kleen48d02a12017-02-23 15:46:34 -0800973 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300974 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800975
976 al.cpu = cpu;
977 al.sym = NULL;
978 if (al.map)
979 al.sym = map__find_symbol(al.map, al.addr);
980
981 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300982 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800983
984 if (al.addr < al.sym->end)
985 off = al.addr - al.sym->start;
986 else
987 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300988 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800989 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300990 printed += fprintf(fp, "%+d", off);
991 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800992 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300993 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
994 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800995 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300996
997 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800998}
999
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001000static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1001 struct thread *thread,
1002 struct perf_event_attr *attr,
1003 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -08001004{
1005 struct branch_stack *br = sample->branch_stack;
1006 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001007 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001008 struct perf_insn x;
1009 u8 buffer[MAXBB];
1010 unsigned off;
1011 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -07001012 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001013
1014 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001015 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001016 nr = br->nr;
1017 if (max_blocks && nr > max_blocks + 1)
1018 nr = max_blocks + 1;
1019
1020 x.thread = thread;
1021 x.cpu = sample->cpu;
1022
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001023 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -08001024
1025 /* Handle first from jump, of which we don't know the entry. */
1026 len = grab_bb(buffer, br->entries[nr-1].from,
1027 br->entries[nr-1].from,
1028 machine, thread, &x.is64bit, &x.cpumode, false);
1029 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001030 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1031 x.cpumode, x.cpu, &lastsym, attr, fp);
1032 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001033 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001034 if (PRINT_FIELD(SRCCODE))
1035 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
Andi Kleen48d02a12017-02-23 15:46:34 -08001036 }
1037
1038 /* Print all blocks */
1039 for (i = nr - 2; i >= 0; i--) {
1040 if (br->entries[i].from || br->entries[i].to)
1041 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1042 br->entries[i].from,
1043 br->entries[i].to);
1044 start = br->entries[i + 1].to;
1045 end = br->entries[i].from;
1046
1047 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1048 /* Patch up missing kernel transfers due to ring filters */
1049 if (len == -ENXIO && i > 0) {
1050 end = br->entries[--i].from;
1051 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1052 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1053 }
1054 if (len <= 0)
1055 continue;
1056
1057 insn = 0;
1058 for (off = 0;; off += ilen) {
1059 uint64_t ip = start + off;
1060
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001061 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001062 if (ip == end) {
Andi Kleendde4e732019-07-11 11:19:21 -07001063 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, ++insn, fp,
Andi Kleenfe571202018-09-24 10:07:32 -07001064 &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001065 if (PRINT_FIELD(SRCCODE))
1066 printed += print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001067 break;
1068 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001069 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1070 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001071 if (ilen == 0)
1072 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001073 if (PRINT_FIELD(SRCCODE))
1074 print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001075 insn++;
1076 }
1077 }
1078 }
1079
1080 /*
1081 * Hit the branch? In this case we are already done, and the target
1082 * has not been executed yet.
1083 */
1084 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001085 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001086 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001087 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001088
1089 /*
1090 * Print final block upto sample
Andi Kleen61f61152018-11-19 21:06:17 -08001091 *
1092 * Due to pipeline delays the LBRs might be missing a branch
1093 * or two, which can result in very large or negative blocks
1094 * between final branch and sample. When this happens just
1095 * continue walking after the last TO until we hit a branch.
Andi Kleen48d02a12017-02-23 15:46:34 -08001096 */
1097 start = br->entries[0].to;
1098 end = sample->ip;
Andi Kleen61f61152018-11-19 21:06:17 -08001099 if (end < start) {
1100 /* Missing jump. Scan 128 bytes for the next branch */
1101 end = start + 128;
1102 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001103 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001104 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001105 if (len <= 0) {
1106 /* Print at least last IP if basic block did not work */
1107 len = grab_bb(buffer, sample->ip, sample->ip,
1108 machine, thread, &x.is64bit, &x.cpumode, false);
1109 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001110 goto out;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001111 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001112 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001113 if (PRINT_FIELD(SRCCODE))
1114 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001115 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001116 }
1117 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001118 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1119 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001120 if (ilen == 0)
1121 break;
Andi Kleen61f61152018-11-19 21:06:17 -08001122 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1123 /*
1124 * Hit a missing branch. Just stop.
1125 */
1126 printed += fprintf(fp, "\t... not reaching sample ...\n");
1127 break;
1128 }
Andi Kleendd2e18e2018-12-03 16:18:48 -08001129 if (PRINT_FIELD(SRCCODE))
1130 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001131 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001132out:
1133 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001134}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001135
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001136static int perf_sample__fprintf_addr(struct perf_sample *sample,
1137 struct thread *thread,
1138 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001139{
1140 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001141 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001142
1143 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001144 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001145
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001146 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001147
1148 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001149 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001150 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001151 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001152 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001153 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001154 }
1155
1156 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001157 printed += fprintf(fp, " (");
1158 printed += map__fprintf_dsoname(al.map, fp);
1159 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001160 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001161out:
1162 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001163}
1164
Andi Kleen99f753f2018-09-20 11:05:39 -07001165static const char *resolve_branch_sym(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001166 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001167 struct thread *thread,
1168 struct addr_location *al,
1169 u64 *ip)
1170{
1171 struct addr_location addr_al;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001172 struct perf_event_attr *attr = &evsel->core.attr;
Andi Kleen99f753f2018-09-20 11:05:39 -07001173 const char *name = NULL;
1174
1175 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1176 if (sample_addr_correlates_sym(attr)) {
1177 thread__resolve(thread, &addr_al, sample);
1178 if (addr_al.sym)
1179 name = addr_al.sym->name;
1180 else
1181 *ip = sample->addr;
1182 } else {
1183 *ip = sample->addr;
1184 }
1185 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1186 if (al->sym)
1187 name = al->sym->name;
1188 else
1189 *ip = sample->ip;
1190 }
1191 return name;
1192}
1193
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001194static int perf_sample__fprintf_callindent(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001195 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001196 struct thread *thread,
1197 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001198{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001199 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter256d92b2018-12-21 14:06:19 +02001200 size_t depth = thread_stack__depth(thread, sample->cpu);
Adrian Huntere2167082016-06-23 16:40:58 +03001201 const char *name = NULL;
1202 static int spacing;
1203 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001204 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001205 u64 ip = 0;
1206
1207 /*
1208 * The 'return' has already been popped off the stack so the depth has
1209 * to be adjusted to match the 'call'.
1210 */
1211 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1212 depth += 1;
1213
Andi Kleen99f753f2018-09-20 11:05:39 -07001214 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001215
Andi Kleena78cdee2018-09-18 05:32:10 -07001216 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1217 dlen += fprintf(fp, "(");
1218 dlen += map__fprintf_dsoname(al->map, fp);
1219 dlen += fprintf(fp, ")\t");
1220 }
1221
Adrian Huntere2167082016-06-23 16:40:58 +03001222 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001223 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001224 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001225 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001226
1227 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001228 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001229
1230 /*
1231 * Try to keep the output length from changing frequently so that the
1232 * output lines up more nicely.
1233 */
1234 if (len > spacing || (len && len < spacing - 52))
1235 spacing = round_up(len + 4, 32);
1236
1237 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001238 len += fprintf(fp, "%*s", spacing - len, "");
1239
Andi Kleena78cdee2018-09-18 05:32:10 -07001240 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001241}
1242
Andi Kleen3ab481a2019-03-05 06:47:45 -08001243__weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1244 struct thread *thread __maybe_unused,
1245 struct machine *machine __maybe_unused)
1246{
1247}
1248
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001249static int perf_sample__fprintf_insn(struct perf_sample *sample,
1250 struct perf_event_attr *attr,
1251 struct thread *thread,
1252 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001253{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001254 int printed = 0;
1255
Andi Kleen3ab481a2019-03-05 06:47:45 -08001256 if (sample->insn_len == 0 && native_arch)
1257 arch_fetch_insn(sample, thread, machine);
1258
Andi Kleen224e2c92016-10-07 16:42:27 +03001259 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001260 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen3ab481a2019-03-05 06:47:45 -08001261 if (PRINT_FIELD(INSN) && sample->insn_len) {
Andi Kleen224e2c92016-10-07 16:42:27 +03001262 int i;
1263
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001264 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001265 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001266 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001267 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001268 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001269 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1270
1271 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001272}
1273
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001274static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1275 struct perf_event_attr *attr, FILE *fp)
1276{
1277 unsigned int ipc;
1278
1279 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1280 return 0;
1281
1282 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1283
1284 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1285 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1286}
1287
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001288static int perf_sample__fprintf_bts(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001289 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001290 struct thread *thread,
1291 struct addr_location *al,
1292 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001293{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001294 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001295 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001296 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001297 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001298
Adrian Huntere2167082016-06-23 16:40:58 +03001299 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001300 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001301
Akihiro Nagai95582592012-01-30 13:43:09 +09001302 /* print branch_from information */
1303 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001304 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001305 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001306
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001307 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001308 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001309 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001310 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001311
1312 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001313 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001314 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001315 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001316 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001317 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001318 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001319 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001320
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001321 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001322 }
1323
Akihiro Nagai95582592012-01-30 13:43:09 +09001324 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001325 if (PRINT_FIELD(ADDR) ||
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001326 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001327 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001328 printed += fprintf(fp, " => ");
1329 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001330 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001331
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001332 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1333
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001334 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001335 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001336
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001337 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001338 printed += fprintf(fp, "\n");
1339 if (PRINT_FIELD(SRCCODE)) {
1340 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1341 &thread->srccode_state);
1342 if (ret) {
1343 printed += ret;
1344 printed += printf("\n");
1345 }
1346 }
1347 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001348}
1349
Adrian Hunter055cd332016-06-23 16:40:56 +03001350static struct {
1351 u32 flags;
1352 const char *name;
1353} sample_flags[] = {
1354 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1355 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1356 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1357 {PERF_IP_FLAG_BRANCH, "jmp"},
1358 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1359 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1360 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1361 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1362 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1363 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1364 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1365 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1366 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1367 {0, NULL}
1368};
1369
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001370static const char *sample_flags_to_name(u32 flags)
1371{
1372 int i;
1373
1374 for (i = 0; sample_flags[i].name ; i++) {
1375 if (sample_flags[i].flags == flags)
1376 return sample_flags[i].name;
1377 }
1378
1379 return NULL;
1380}
1381
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001382static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001383{
1384 const char *chars = PERF_IP_FLAG_CHARS;
1385 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001386 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1387 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001388 char str[33];
1389 int i, pos = 0;
1390
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001391 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1392 if (name)
1393 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1394
1395 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1396 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1397 if (name)
1398 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1399 }
1400
1401 if (flags & PERF_IP_FLAG_TRACE_END) {
1402 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1403 if (name)
1404 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001405 }
1406
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001407 for (i = 0; i < n; i++, flags >>= 1) {
1408 if (flags & 1)
1409 str[pos++] = chars[i];
1410 }
1411 for (; i < 32; i++, flags >>= 1) {
1412 if (flags & 1)
1413 str[pos++] = '?';
1414 }
1415 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001416
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001417 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001418}
1419
Wang Nan30372f02016-02-24 11:20:45 +00001420struct printer_data {
1421 int line_no;
1422 bool hit_nul;
1423 bool is_printable;
1424};
1425
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001426static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1427 unsigned int val,
1428 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001429{
1430 unsigned char ch = (unsigned char)val;
1431 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001432 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001433
1434 switch (op) {
1435 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001436 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001437 break;
1438 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001439 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001440 " ");
1441 break;
1442 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001443 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001444 break;
1445 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001446 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001447 break;
1448 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001449 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001450 break;
1451 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001452 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001453 break;
1454 case BINARY_PRINT_CHAR_DATA:
1455 if (printer_data->hit_nul && ch)
1456 printer_data->is_printable = false;
1457
1458 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001459 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001460
1461 if (!printer_data->is_printable)
1462 break;
1463
1464 if (ch == '\0')
1465 printer_data->hit_nul = true;
1466 else
1467 printer_data->is_printable = false;
1468 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001469 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001470 }
1471 break;
1472 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001473 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001474 break;
1475 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001476 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001477 printer_data->line_no++;
1478 break;
1479 case BINARY_PRINT_DATA_END:
1480 default:
1481 break;
1482 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001483
1484 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001485}
1486
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001487static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001488{
1489 unsigned int nr_bytes = sample->raw_size;
1490 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001491 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1492 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001493
1494 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001495 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1496
1497 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001498}
1499
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001500static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001501{
1502 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001503 return fprintf(fp, "%*s", spacing - len, "");
1504
1505 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001506}
1507
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001508static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001509{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001510 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001511}
1512
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001513static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001514{
1515 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1516 int len;
1517
1518 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001519 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001520
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001521 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001522 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001523 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001524}
1525
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001526static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001527{
1528 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1529 int len;
1530
1531 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001532 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001533
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001534 len = fprintf(fp, " hints: %#x extensions: %#x ",
1535 data->hints, data->extensions);
1536 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001537}
1538
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001539static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001540{
1541 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1542 int len;
1543
1544 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001545 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001546
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001547 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1548 data->hw, data->cstate, data->subcstate);
1549 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001550}
1551
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001552static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001553{
1554 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1555 int len;
1556
1557 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001558 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001559
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001560 len = fprintf(fp, " IP: %u ", data->ip);
1561 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001562}
1563
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001564static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001565{
1566 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1567 int len;
1568
1569 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001570 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001571
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001572 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001573 data->deepest_cstate, data->last_cstate,
1574 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001575 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001576}
1577
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001578static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001579{
1580 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1581 unsigned int percent, freq;
1582 int len;
1583
1584 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001585 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001586
1587 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001588 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001589 if (data->max_nonturbo) {
1590 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001591 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001592 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001593 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001594}
1595
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001596static int perf_sample__fprintf_synth(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001597 struct evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001598{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001599 switch (evsel->core.attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001600 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001601 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001602 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001603 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001604 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001605 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001606 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001607 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001608 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001609 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001610 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001611 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001612 default:
1613 break;
1614 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001615
1616 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001617}
1618
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001619struct evswitch {
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03001620 struct evsel *on,
1621 *off;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001622 bool discarding;
Arnaldo Carvalho de Melo6469eb62019-08-14 16:40:58 -03001623 bool show_on_off_events;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001624};
1625
Jiri Olsa809e9422015-11-26 18:55:21 +01001626struct perf_script {
1627 struct perf_tool tool;
1628 struct perf_session *session;
1629 bool show_task_events;
1630 bool show_mmap_events;
1631 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301632 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001633 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001634 bool show_round_events;
Jiri Olsa490c8cc2019-05-08 15:20:08 +02001635 bool show_bpf_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001636 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001637 bool per_event_dump;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001638 struct evswitch evswitch;
Jiri Olsaf8548392019-07-21 13:23:49 +02001639 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +02001640 struct perf_thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001641 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001642 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001643 struct perf_time_interval *ptime_range;
1644 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001645 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001646};
1647
Jiri Olsa63503db2019-07-21 13:23:52 +02001648static int perf_evlist__max_name_len(struct evlist *evlist)
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001649{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001650 struct evsel *evsel;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001651 int max = 0;
1652
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001653 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001654 int len = strlen(perf_evsel__name(evsel));
1655
1656 max = MAX(len, max);
1657 }
1658
1659 return max;
1660}
1661
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001662static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001663{
1664 struct mem_info mi = { .data_src.val = data_src };
1665 char decode[100];
1666 char out[100];
1667 static int maxlen;
1668 int len;
1669
1670 perf_script__meminfo_scnprintf(decode, 100, &mi);
1671
1672 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1673 if (maxlen < len)
1674 maxlen = len;
1675
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001676 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001677}
1678
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001679struct metric_ctx {
1680 struct perf_sample *sample;
1681 struct thread *thread;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001682 struct evsel *evsel;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001683 FILE *fp;
1684};
1685
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001686static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1687 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001688 const char *fmt,
1689 const char *unit, double val)
1690{
1691 struct metric_ctx *mctx = ctx;
1692
1693 if (!fmt)
1694 return;
1695 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001696 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001697 fputs("\tmetric: ", mctx->fp);
1698 if (color)
1699 color_fprintf(mctx->fp, color, fmt, val);
1700 else
1701 printf(fmt, val);
1702 fprintf(mctx->fp, " %s\n", unit);
1703}
1704
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001705static void script_new_line(struct perf_stat_config *config __maybe_unused,
1706 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001707{
1708 struct metric_ctx *mctx = ctx;
1709
1710 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001711 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001712 fputs("\tmetric: ", mctx->fp);
1713}
1714
1715static void perf_sample__fprint_metric(struct perf_script *script,
1716 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001717 struct evsel *evsel,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001718 struct perf_sample *sample,
1719 FILE *fp)
1720{
1721 struct perf_stat_output_ctx ctx = {
1722 .print_metric = script_print_metric,
1723 .new_line = script_new_line,
1724 .ctx = &(struct metric_ctx) {
1725 .sample = sample,
1726 .thread = thread,
1727 .evsel = evsel,
1728 .fp = fp,
1729 },
1730 .force_header = false,
1731 };
Jiri Olsa32dcd022019-07-21 13:23:51 +02001732 struct evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001733 u64 val;
1734
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001735 if (!evsel->stats)
1736 perf_evlist__alloc_stats(script->session->evlist, false);
1737 if (evsel_script(evsel->leader)->gnum++ == 0)
1738 perf_stat__reset_shadow_stats();
1739 val = sample->period * evsel->scale;
1740 perf_stat__update_shadow_stats(evsel,
1741 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001742 sample->cpu,
1743 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001744 evsel_script(evsel)->val = val;
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001745 if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) {
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001746 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001747 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001748 evsel_script(ev2)->val,
1749 sample->cpu,
1750 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001751 NULL,
1752 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001753 }
1754 evsel_script(evsel->leader)->gnum = 0;
1755 }
1756}
1757
Andi Kleen99f753f2018-09-20 11:05:39 -07001758static bool show_event(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001759 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001760 struct thread *thread,
1761 struct addr_location *al)
1762{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001763 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001764
1765 if (!symbol_conf.graph_function)
1766 return true;
1767
1768 if (thread->filter) {
1769 if (depth <= thread->filter_entry_depth) {
1770 thread->filter = false;
1771 return false;
1772 }
1773 return true;
1774 } else {
1775 const char *s = symbol_conf.graph_function;
1776 u64 ip;
1777 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1778 &ip);
1779 unsigned nlen;
1780
1781 if (!name)
1782 return false;
1783 nlen = strlen(name);
1784 while (*s) {
1785 unsigned len = strcspn(s, ",");
1786 if (nlen == len && !strncmp(name, s, len)) {
1787 thread->filter = true;
1788 thread->filter_entry_depth = depth;
1789 return true;
1790 }
1791 s += len;
1792 if (*s == ',')
1793 s++;
1794 }
1795 return false;
1796 }
1797}
1798
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001799static void process_event(struct perf_script *script,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001800 struct perf_sample *sample, struct evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001801 struct addr_location *al,
1802 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001803{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001804 struct thread *thread = al->thread;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001805 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001806 unsigned int type = output_type(attr->type);
Jiri Olsa32dcd022019-07-21 13:23:51 +02001807 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001808 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001809
Adrian Hunter14057202017-06-21 13:17:19 +03001810 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001811 return;
1812
Andi Kleen99f753f2018-09-20 11:05:39 -07001813 if (!show_event(sample, evsel, thread, al))
1814 return;
1815
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001816 if (script->evswitch.on && script->evswitch.discarding) {
1817 if (script->evswitch.on != evsel)
1818 return;
1819
1820 script->evswitch.discarding = false;
Arnaldo Carvalho de Melo6469eb62019-08-14 16:40:58 -03001821
1822 if (!script->evswitch.show_on_off_events)
1823 return;
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03001824
1825 goto print_it;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001826 }
1827
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03001828 if (script->evswitch.off && !script->evswitch.discarding) {
1829 if (script->evswitch.off != evsel)
1830 goto print_it;
1831
1832 script->evswitch.discarding = true;
1833
1834 if (!script->evswitch.show_on_off_events)
1835 return;
1836 }
1837print_it:
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001838 ++es->samples;
1839
Jiri Olsa28a0b392018-01-07 17:03:52 +01001840 perf_sample__fprintf_start(sample, thread, evsel,
1841 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001842
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001843 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001844 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001845
Namhyung Kime944d3d2013-11-18 14:34:52 +09001846 if (PRINT_FIELD(EVNAME)) {
1847 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001848
1849 if (!script->name_width)
1850 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1851
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001852 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001853 }
1854
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001855 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001856 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001857
Akihiro Nagai95582592012-01-30 13:43:09 +09001858 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001859 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001860 return;
1861 }
1862
Andi Kleen96167162019-01-17 11:48:34 -08001863 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001864 event_format__fprintf(evsel->tp_format, sample->cpu,
1865 sample->raw_data, sample->raw_size, fp);
1866 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001867
1868 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001869 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001870
David Ahern7cec0922011-05-30 13:08:23 -06001871 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001872 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001873
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001874 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001875 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001876
1877 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001878 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001879
David Ahern787bef12011-05-27 14:28:43 -06001880 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001881 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001882
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001883 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001884 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001885 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001886 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001887
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001888 fputc(cursor ? '\n' : ' ', fp);
1889 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001890 }
1891
Stephane Eranianfc36f942015-08-31 18:41:10 +02001892 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001893 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001894
Andi Kleenb1491ac2017-09-05 11:40:57 -07001895 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001896 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001897
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001898 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001899 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001900 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001901 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001902 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001903 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001904
Wang Nan30372f02016-02-24 11:20:45 +00001905 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001906 perf_sample__fprintf_bpf_output(sample, fp);
1907 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001908
1909 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001910 fprintf(fp, "%16" PRIx64, sample->phys_addr);
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001911
1912 perf_sample__fprintf_ipc(sample, attr, fp);
1913
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001914 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001915
Andi Kleendd2e18e2018-12-03 16:18:48 -08001916 if (PRINT_FIELD(SRCCODE)) {
1917 if (map__fprintf_srccode(al->map, al->addr, stdout,
1918 &thread->srccode_state))
1919 printf("\n");
1920 }
1921
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001922 if (PRINT_FIELD(METRIC))
1923 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001924
1925 if (verbose)
1926 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001927}
1928
Tom Zanussi956ffd02009-11-25 01:15:46 -06001929static struct scripting_ops *scripting_ops;
1930
Jiri Olsa32dcd022019-07-21 13:23:51 +02001931static void __process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsa36e33c52016-01-06 11:49:56 +01001932{
Jiri Olsaaf663bd2019-07-21 13:24:39 +02001933 int nthreads = thread_map__nr(counter->core.threads);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001934 int ncpus = perf_evsel__nr_cpus(counter);
1935 int cpu, thread;
1936 static int header_printed;
1937
1938 if (counter->system_wide)
1939 nthreads = 1;
1940
1941 if (!header_printed) {
1942 printf("%3s %8s %15s %15s %15s %15s %s\n",
1943 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1944 header_printed = 1;
1945 }
1946
1947 for (thread = 0; thread < nthreads; thread++) {
1948 for (cpu = 0; cpu < ncpus; cpu++) {
1949 struct perf_counts_values *counts;
1950
1951 counts = perf_counts(counter->counts, cpu, thread);
1952
1953 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
Jiri Olsad400bd32019-07-21 13:24:37 +02001954 counter->core.cpus->map[cpu],
Jiri Olsaaf663bd2019-07-21 13:24:39 +02001955 thread_map__pid(counter->core.threads, thread),
Jiri Olsa36e33c52016-01-06 11:49:56 +01001956 counts->val,
1957 counts->ena,
1958 counts->run,
1959 tstamp,
1960 perf_evsel__name(counter));
1961 }
1962 }
1963}
1964
Jiri Olsa32dcd022019-07-21 13:23:51 +02001965static void process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsae099eba2016-01-05 22:09:09 +01001966{
1967 if (scripting_ops && scripting_ops->process_stat)
1968 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001969 else
1970 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001971}
1972
1973static void process_stat_interval(u64 tstamp)
1974{
1975 if (scripting_ops && scripting_ops->process_stat_interval)
1976 scripting_ops->process_stat_interval(tstamp);
1977}
1978
Tom Zanussi956ffd02009-11-25 01:15:46 -06001979static void setup_scripting(void)
1980{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001981 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001982 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001983}
1984
Adrian Hunterd445dd22014-08-15 22:08:37 +03001985static int flush_scripting(void)
1986{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001987 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001988}
1989
Tom Zanussi956ffd02009-11-25 01:15:46 -06001990static int cleanup_scripting(void)
1991{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001992 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001993
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001994 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001995}
1996
Andi Kleene87e5482019-03-11 07:44:52 -07001997static bool filter_cpu(struct perf_sample *sample)
1998{
1999 if (cpu_list)
2000 return !test_bit(sample->cpu, cpu_bitmap);
2001 return false;
2002}
2003
Jiri Olsa809e9422015-11-26 18:55:21 +01002004static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002005 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002006 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002007 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002008 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002009{
Jiri Olsa809e9422015-11-26 18:55:21 +01002010 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07002011 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002012
Jin Yao2ab046c2017-12-08 21:13:46 +08002013 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
2014 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07002015 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08002016 }
David Aherna91f4c42016-11-29 10:15:43 -07002017
David Ahern1424dc92011-03-09 22:23:28 -07002018 if (debug_mode) {
2019 if (sample->time < last_timestamp) {
2020 pr_err("Samples misordered, previous: %" PRIu64
2021 " this: %" PRIu64 "\n", last_timestamp,
2022 sample->time);
2023 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02002024 }
David Ahern1424dc92011-03-09 22:23:28 -07002025 last_timestamp = sample->time;
2026 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002027 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002028
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03002029 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07002030 pr_err("problem processing %d event, skipping it.\n",
2031 event->header.type);
2032 return -1;
2033 }
2034
2035 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002036 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07002037
Andi Kleene87e5482019-03-11 07:44:52 -07002038 if (filter_cpu(sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002039 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002040
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002041 if (scripting_ops)
2042 scripting_ops->process_event(event, sample, evsel, &al);
2043 else
Andi Kleen48d02a12017-02-23 15:46:34 -08002044 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002045
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002046out_put:
2047 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002048 return 0;
2049}
2050
Adrian Hunter7ea95722013-11-01 15:51:30 +02002051static int process_attr(struct perf_tool *tool, union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02002052 struct evlist **pevlist)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002053{
2054 struct perf_script *scr = container_of(tool, struct perf_script, tool);
Jiri Olsa63503db2019-07-21 13:23:52 +02002055 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002056 struct evsel *evsel, *pos;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002057 int err;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002058 static struct evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002059
2060 err = perf_event__process_attr(tool, event, pevlist);
2061 if (err)
2062 return err;
2063
2064 evlist = *pevlist;
2065 evsel = perf_evlist__last(*pevlist);
2066
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302067 if (!evsel->priv) {
2068 if (scr->per_event_dump) {
2069 evsel->priv = perf_evsel_script__new(evsel,
2070 scr->session->data);
2071 } else {
2072 es = zalloc(sizeof(*es));
2073 if (!es)
2074 return -ENOMEM;
2075 es->fp = stdout;
2076 evsel->priv = es;
2077 }
2078 }
2079
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002080 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2081 evsel->core.attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002082 return 0;
2083
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002084 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002085 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002086 return 0;
2087 }
2088
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002089 set_print_ip_opts(&evsel->core.attr);
Adrian Hunter7ea95722013-11-01 15:51:30 +02002090
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002091 if (evsel->core.attr.sample_type)
Jiri Olsad2b5a312015-10-16 12:41:25 +02002092 err = perf_evsel__check_attr(evsel, scr->session);
2093
2094 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002095}
2096
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002097static int process_comm_event(struct perf_tool *tool,
2098 union perf_event *event,
2099 struct perf_sample *sample,
2100 struct machine *machine)
2101{
2102 struct thread *thread;
2103 struct perf_script *script = container_of(tool, struct perf_script, tool);
2104 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002105 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002106 int ret = -1;
2107
2108 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2109 if (thread == NULL) {
2110 pr_debug("problem processing COMM event, skipping it.\n");
2111 return -1;
2112 }
2113
2114 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2115 goto out;
2116
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002117 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002118 sample->cpu = 0;
2119 sample->time = 0;
2120 sample->tid = event->comm.tid;
2121 sample->pid = event->comm.pid;
2122 }
Andi Kleene87e5482019-03-11 07:44:52 -07002123 if (!filter_cpu(sample)) {
2124 perf_sample__fprintf_start(sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01002125 PERF_RECORD_COMM, stdout);
Andi Kleene87e5482019-03-11 07:44:52 -07002126 perf_event__fprintf(event, stdout);
2127 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002128 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002129out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002130 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002131 return ret;
2132}
2133
Hari Bathini96a44bb2017-03-08 02:12:06 +05302134static int process_namespaces_event(struct perf_tool *tool,
2135 union perf_event *event,
2136 struct perf_sample *sample,
2137 struct machine *machine)
2138{
2139 struct thread *thread;
2140 struct perf_script *script = container_of(tool, struct perf_script, tool);
2141 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002142 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302143 int ret = -1;
2144
2145 thread = machine__findnew_thread(machine, event->namespaces.pid,
2146 event->namespaces.tid);
2147 if (thread == NULL) {
2148 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2149 return -1;
2150 }
2151
2152 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2153 goto out;
2154
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002155 if (!evsel->core.attr.sample_id_all) {
Hari Bathini96a44bb2017-03-08 02:12:06 +05302156 sample->cpu = 0;
2157 sample->time = 0;
2158 sample->tid = event->namespaces.tid;
2159 sample->pid = event->namespaces.pid;
2160 }
Andi Kleene87e5482019-03-11 07:44:52 -07002161 if (!filter_cpu(sample)) {
2162 perf_sample__fprintf_start(sample, thread, evsel,
2163 PERF_RECORD_NAMESPACES, stdout);
2164 perf_event__fprintf(event, stdout);
2165 }
Hari Bathini96a44bb2017-03-08 02:12:06 +05302166 ret = 0;
2167out:
2168 thread__put(thread);
2169 return ret;
2170}
2171
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002172static int process_fork_event(struct perf_tool *tool,
2173 union perf_event *event,
2174 struct perf_sample *sample,
2175 struct machine *machine)
2176{
2177 struct thread *thread;
2178 struct perf_script *script = container_of(tool, struct perf_script, tool);
2179 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002180 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002181
2182 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2183 return -1;
2184
2185 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2186 if (thread == NULL) {
2187 pr_debug("problem processing FORK event, skipping it.\n");
2188 return -1;
2189 }
2190
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002191 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002192 sample->cpu = 0;
2193 sample->time = event->fork.time;
2194 sample->tid = event->fork.tid;
2195 sample->pid = event->fork.pid;
2196 }
Andi Kleene87e5482019-03-11 07:44:52 -07002197 if (!filter_cpu(sample)) {
2198 perf_sample__fprintf_start(sample, thread, evsel,
2199 PERF_RECORD_FORK, stdout);
2200 perf_event__fprintf(event, stdout);
2201 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002202 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002203
2204 return 0;
2205}
2206static int process_exit_event(struct perf_tool *tool,
2207 union perf_event *event,
2208 struct perf_sample *sample,
2209 struct machine *machine)
2210{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002211 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002212 struct thread *thread;
2213 struct perf_script *script = container_of(tool, struct perf_script, tool);
2214 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002215 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002216
2217 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2218 if (thread == NULL) {
2219 pr_debug("problem processing EXIT event, skipping it.\n");
2220 return -1;
2221 }
2222
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002223 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002224 sample->cpu = 0;
2225 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002226 sample->tid = event->fork.tid;
2227 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002228 }
Andi Kleene87e5482019-03-11 07:44:52 -07002229 if (!filter_cpu(sample)) {
2230 perf_sample__fprintf_start(sample, thread, evsel,
2231 PERF_RECORD_EXIT, stdout);
2232 perf_event__fprintf(event, stdout);
2233 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002234
2235 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002236 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002237
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002238 thread__put(thread);
2239 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002240}
2241
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002242static int process_mmap_event(struct perf_tool *tool,
2243 union perf_event *event,
2244 struct perf_sample *sample,
2245 struct machine *machine)
2246{
2247 struct thread *thread;
2248 struct perf_script *script = container_of(tool, struct perf_script, tool);
2249 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002250 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002251
2252 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2253 return -1;
2254
2255 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2256 if (thread == NULL) {
2257 pr_debug("problem processing MMAP event, skipping it.\n");
2258 return -1;
2259 }
2260
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002261 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002262 sample->cpu = 0;
2263 sample->time = 0;
2264 sample->tid = event->mmap.tid;
2265 sample->pid = event->mmap.pid;
2266 }
Andi Kleene87e5482019-03-11 07:44:52 -07002267 if (!filter_cpu(sample)) {
2268 perf_sample__fprintf_start(sample, thread, evsel,
2269 PERF_RECORD_MMAP, stdout);
2270 perf_event__fprintf(event, stdout);
2271 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002272 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002273 return 0;
2274}
2275
2276static int process_mmap2_event(struct perf_tool *tool,
2277 union perf_event *event,
2278 struct perf_sample *sample,
2279 struct machine *machine)
2280{
2281 struct thread *thread;
2282 struct perf_script *script = container_of(tool, struct perf_script, tool);
2283 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002284 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002285
2286 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2287 return -1;
2288
2289 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2290 if (thread == NULL) {
2291 pr_debug("problem processing MMAP2 event, skipping it.\n");
2292 return -1;
2293 }
2294
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002295 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002296 sample->cpu = 0;
2297 sample->time = 0;
2298 sample->tid = event->mmap2.tid;
2299 sample->pid = event->mmap2.pid;
2300 }
Andi Kleene87e5482019-03-11 07:44:52 -07002301 if (!filter_cpu(sample)) {
2302 perf_sample__fprintf_start(sample, thread, evsel,
2303 PERF_RECORD_MMAP2, stdout);
2304 perf_event__fprintf(event, stdout);
2305 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002306 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002307 return 0;
2308}
2309
Adrian Hunter7c148982015-07-21 12:44:06 +03002310static int process_switch_event(struct perf_tool *tool,
2311 union perf_event *event,
2312 struct perf_sample *sample,
2313 struct machine *machine)
2314{
2315 struct thread *thread;
2316 struct perf_script *script = container_of(tool, struct perf_script, tool);
2317 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002318 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Adrian Hunter7c148982015-07-21 12:44:06 +03002319
2320 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2321 return -1;
2322
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002323 if (scripting_ops && scripting_ops->process_switch)
2324 scripting_ops->process_switch(event, sample, machine);
2325
2326 if (!script->show_switch_events)
2327 return 0;
2328
Adrian Hunter7c148982015-07-21 12:44:06 +03002329 thread = machine__findnew_thread(machine, sample->pid,
2330 sample->tid);
2331 if (thread == NULL) {
2332 pr_debug("problem processing SWITCH event, skipping it.\n");
2333 return -1;
2334 }
2335
Andi Kleene87e5482019-03-11 07:44:52 -07002336 if (!filter_cpu(sample)) {
2337 perf_sample__fprintf_start(sample, thread, evsel,
2338 PERF_RECORD_SWITCH, stdout);
2339 perf_event__fprintf(event, stdout);
2340 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002341 thread__put(thread);
2342 return 0;
2343}
2344
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002345static int
2346process_lost_event(struct perf_tool *tool,
2347 union perf_event *event,
2348 struct perf_sample *sample,
2349 struct machine *machine)
2350{
2351 struct perf_script *script = container_of(tool, struct perf_script, tool);
2352 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002353 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002354 struct thread *thread;
2355
2356 thread = machine__findnew_thread(machine, sample->pid,
2357 sample->tid);
2358 if (thread == NULL)
2359 return -1;
2360
Andi Kleene87e5482019-03-11 07:44:52 -07002361 if (!filter_cpu(sample)) {
2362 perf_sample__fprintf_start(sample, thread, evsel,
2363 PERF_RECORD_LOST, stdout);
2364 perf_event__fprintf(event, stdout);
2365 }
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002366 thread__put(thread);
2367 return 0;
2368}
2369
Jiri Olsa3233b372018-02-06 19:17:59 +01002370static int
2371process_finished_round_event(struct perf_tool *tool __maybe_unused,
2372 union perf_event *event,
2373 struct ordered_events *oe __maybe_unused)
2374
2375{
2376 perf_event__fprintf(event, stdout);
2377 return 0;
2378}
2379
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002380static int
2381process_bpf_events(struct perf_tool *tool __maybe_unused,
2382 union perf_event *event,
2383 struct perf_sample *sample,
2384 struct machine *machine)
2385{
2386 struct thread *thread;
2387 struct perf_script *script = container_of(tool, struct perf_script, tool);
2388 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002389 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002390
2391 if (machine__process_ksymbol(machine, event, sample) < 0)
2392 return -1;
2393
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002394 if (!evsel->core.attr.sample_id_all) {
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002395 perf_event__fprintf(event, stdout);
2396 return 0;
2397 }
2398
2399 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2400 if (thread == NULL) {
2401 pr_debug("problem processing MMAP event, skipping it.\n");
2402 return -1;
2403 }
2404
2405 if (!filter_cpu(sample)) {
2406 perf_sample__fprintf_start(sample, thread, evsel,
2407 event->header.type, stdout);
2408 perf_event__fprintf(event, stdout);
2409 }
2410
2411 thread__put(thread);
2412 return 0;
2413}
2414
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002415static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002416{
2417 session_done = 1;
2418}
2419
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002420static void perf_script__fclose_per_event_dump(struct perf_script *script)
2421{
Jiri Olsa63503db2019-07-21 13:23:52 +02002422 struct evlist *evlist = script->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002423 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002424
2425 evlist__for_each_entry(evlist, evsel) {
2426 if (!evsel->priv)
2427 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002428 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002429 evsel->priv = NULL;
2430 }
2431}
2432
2433static int perf_script__fopen_per_event_dump(struct perf_script *script)
2434{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002435 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002436
2437 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002438 /*
2439 * Already setup? I.e. we may be called twice in cases like
2440 * Intel PT, one for the intel_pt// and dummy events, then
2441 * for the evsels syntheized from the auxtrace info.
2442 *
2443 * Ses perf_script__process_auxtrace_info.
2444 */
2445 if (evsel->priv != NULL)
2446 continue;
2447
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002448 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002449 if (evsel->priv == NULL)
2450 goto out_err_fclose;
2451 }
2452
2453 return 0;
2454
2455out_err_fclose:
2456 perf_script__fclose_per_event_dump(script);
2457 return -1;
2458}
2459
2460static int perf_script__setup_per_event_dump(struct perf_script *script)
2461{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002462 struct evsel *evsel;
2463 static struct evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002464
2465 if (script->per_event_dump)
2466 return perf_script__fopen_per_event_dump(script);
2467
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002468 es_stdout.fp = stdout;
2469
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002470 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002471 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002472
2473 return 0;
2474}
2475
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002476static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2477{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002478 struct evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002479
2480 evlist__for_each_entry(script->session->evlist, evsel) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002481 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002482
2483 perf_evsel_script__fprintf(es, stdout);
2484 perf_evsel_script__delete(es);
2485 evsel->priv = NULL;
2486 }
2487}
2488
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002489static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002490{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002491 int ret;
2492
Tom Zanussic239da32010-04-01 23:59:18 -05002493 signal(SIGINT, sig_handler);
2494
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002495 perf_stat__init_shadow_stats();
2496
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002497 /* override event processing functions */
2498 if (script->show_task_events) {
2499 script->tool.comm = process_comm_event;
2500 script->tool.fork = process_fork_event;
2501 script->tool.exit = process_exit_event;
2502 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002503 if (script->show_mmap_events) {
2504 script->tool.mmap = process_mmap_event;
2505 script->tool.mmap2 = process_mmap2_event;
2506 }
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002507 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
Adrian Hunter7c148982015-07-21 12:44:06 +03002508 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302509 if (script->show_namespace_events)
2510 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002511 if (script->show_lost_events)
2512 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002513 if (script->show_round_events) {
2514 script->tool.ordered_events = false;
2515 script->tool.finished_round = process_finished_round_event;
2516 }
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002517 if (script->show_bpf_events) {
2518 script->tool.ksymbol = process_bpf_events;
2519 script->tool.bpf_event = process_bpf_events;
2520 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002521
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002522 if (perf_script__setup_per_event_dump(script)) {
2523 pr_err("Couldn't create the per event dump files\n");
2524 return -1;
2525 }
2526
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002527 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002528
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002529 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002530 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002531
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002532 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002533 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002534
2535 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002536}
2537
Tom Zanussi956ffd02009-11-25 01:15:46 -06002538struct script_spec {
2539 struct list_head node;
2540 struct scripting_ops *ops;
2541 char spec[0];
2542};
2543
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002544static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002545
2546static struct script_spec *script_spec__new(const char *spec,
2547 struct scripting_ops *ops)
2548{
2549 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2550
2551 if (s != NULL) {
2552 strcpy(s->spec, spec);
2553 s->ops = ops;
2554 }
2555
2556 return s;
2557}
2558
Tom Zanussi956ffd02009-11-25 01:15:46 -06002559static void script_spec__add(struct script_spec *s)
2560{
2561 list_add_tail(&s->node, &script_specs);
2562}
2563
2564static struct script_spec *script_spec__find(const char *spec)
2565{
2566 struct script_spec *s;
2567
2568 list_for_each_entry(s, &script_specs, node)
2569 if (strcasecmp(s->spec, spec) == 0)
2570 return s;
2571 return NULL;
2572}
2573
Tom Zanussi956ffd02009-11-25 01:15:46 -06002574int script_spec_register(const char *spec, struct scripting_ops *ops)
2575{
2576 struct script_spec *s;
2577
2578 s = script_spec__find(spec);
2579 if (s)
2580 return -1;
2581
Taeung Song8560bae2016-02-26 00:13:10 +09002582 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002583 if (!s)
2584 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002585 else
2586 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002587
2588 return 0;
2589}
2590
2591static struct scripting_ops *script_spec__lookup(const char *spec)
2592{
2593 struct script_spec *s = script_spec__find(spec);
2594 if (!s)
2595 return NULL;
2596
2597 return s->ops;
2598}
2599
2600static void list_available_languages(void)
2601{
2602 struct script_spec *s;
2603
2604 fprintf(stderr, "\n");
2605 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002606 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002607
2608 list_for_each_entry(s, &script_specs, node)
2609 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2610
2611 fprintf(stderr, "\n");
2612}
2613
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002614static int parse_scriptname(const struct option *opt __maybe_unused,
2615 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002616{
2617 char spec[PATH_MAX];
2618 const char *script, *ext;
2619 int len;
2620
Tom Zanussif526d682010-01-27 02:27:52 -06002621 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002622 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002623 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002624 }
2625
2626 script = strchr(str, ':');
2627 if (script) {
2628 len = script - str;
2629 if (len >= PATH_MAX) {
2630 fprintf(stderr, "invalid language specifier");
2631 return -1;
2632 }
2633 strncpy(spec, str, len);
2634 spec[len] = '\0';
2635 scripting_ops = script_spec__lookup(spec);
2636 if (!scripting_ops) {
2637 fprintf(stderr, "invalid language specifier");
2638 return -1;
2639 }
2640 script++;
2641 } else {
2642 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002643 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002644 if (!ext) {
2645 fprintf(stderr, "invalid script extension");
2646 return -1;
2647 }
2648 scripting_ops = script_spec__lookup(++ext);
2649 if (!scripting_ops) {
2650 fprintf(stderr, "invalid script extension");
2651 return -1;
2652 }
2653 }
2654
2655 script_name = strdup(script);
2656
2657 return 0;
2658}
2659
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002660static int parse_output_fields(const struct option *opt __maybe_unused,
2661 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002662{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002663 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002664 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002665 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002666 int rc = 0;
2667 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002668 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002669 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002670
2671 if (!str)
2672 return -ENOMEM;
2673
David Ahern2c9e45f72011-03-17 10:03:21 -06002674 /* first word can state for which event type the user is specifying
2675 * the fields. If no type exists, the specified fields apply to all
2676 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002677 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002678 tok = strchr(str, ':');
2679 if (tok) {
2680 *tok = '\0';
2681 tok++;
2682 if (!strcmp(str, "hw"))
2683 type = PERF_TYPE_HARDWARE;
2684 else if (!strcmp(str, "sw"))
2685 type = PERF_TYPE_SOFTWARE;
2686 else if (!strcmp(str, "trace"))
2687 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002688 else if (!strcmp(str, "raw"))
2689 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002690 else if (!strcmp(str, "break"))
2691 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002692 else if (!strcmp(str, "synth"))
2693 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002694 else {
2695 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002696 rc = -EINVAL;
2697 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002698 }
2699
2700 if (output[type].user_set)
2701 pr_warning("Overriding previous field request for %s events.\n",
2702 event_type(type));
2703
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002704 /* Don't override defaults for +- */
2705 if (strchr(tok, '+') || strchr(tok, '-'))
2706 goto parse;
2707
David Ahern2c9e45f72011-03-17 10:03:21 -06002708 output[type].fields = 0;
2709 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002710 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002711
2712 } else {
2713 tok = str;
2714 if (strlen(str) == 0) {
2715 fprintf(stderr,
2716 "Cannot set fields to 'none' for all event types.\n");
2717 rc = -EINVAL;
2718 goto out;
2719 }
2720
Andi Kleen36ce5652017-06-02 08:48:10 -07002721 /* Don't override defaults for +- */
2722 if (strchr(str, '+') || strchr(str, '-'))
2723 goto parse;
2724
David Ahern2c9e45f72011-03-17 10:03:21 -06002725 if (output_set_by_user())
2726 pr_warning("Overriding previous field request for all events.\n");
2727
Adrian Hunter14057202017-06-21 13:17:19 +03002728 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002729 output[j].fields = 0;
2730 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002731 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002732 }
David Ahern1424dc92011-03-09 22:23:28 -07002733 }
2734
Andi Kleen36ce5652017-06-02 08:48:10 -07002735parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002736 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002737 if (*tok == '+') {
2738 if (change == SET)
2739 goto out_badmix;
2740 change = ADD;
2741 tok++;
2742 } else if (*tok == '-') {
2743 if (change == SET)
2744 goto out_badmix;
2745 change = REMOVE;
2746 tok++;
2747 } else {
2748 if (change != SET && change != DEFAULT)
2749 goto out_badmix;
2750 change = SET;
2751 }
2752
David Ahern745f43e2011-03-09 22:23:26 -07002753 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002754 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002755 break;
David Ahern745f43e2011-03-09 22:23:26 -07002756 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002757 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002758 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002759 continue;
2760 }
David Ahern745f43e2011-03-09 22:23:26 -07002761 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002762 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002763 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002764 goto out;
2765 }
2766
2767 if (type == -1) {
2768 /* add user option to all events types for
2769 * which it is valid
2770 */
Adrian Hunter14057202017-06-21 13:17:19 +03002771 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002772 if (output[j].invalid_fields & all_output_options[i].field) {
2773 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2774 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002775 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002776 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002777 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002778 output[j].user_set_fields &= ~all_output_options[i].field;
2779 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002780 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002781 output[j].user_set_fields |= all_output_options[i].field;
2782 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002783 output[j].user_set = true;
2784 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002785 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002786 }
2787 } else {
2788 if (output[type].invalid_fields & all_output_options[i].field) {
2789 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2790 all_output_options[i].str, event_type(type));
2791
2792 rc = -EINVAL;
2793 goto out;
2794 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002795 if (change == REMOVE)
2796 output[type].fields &= ~all_output_options[i].field;
2797 else
2798 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002799 output[type].user_set = true;
2800 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002801 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002802 }
2803
2804 if (type >= 0) {
2805 if (output[type].fields == 0) {
2806 pr_debug("No fields requested for %s type. "
2807 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002808 }
David Ahern1424dc92011-03-09 22:23:28 -07002809 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002810 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002811
Andi Kleen36ce5652017-06-02 08:48:10 -07002812out_badmix:
2813 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2814 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002815out:
David Ahern745f43e2011-03-09 22:23:26 -07002816 free(str);
2817 return rc;
2818}
2819
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002820#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2821 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2822 if ((lang_dirent->d_type == DT_DIR || \
2823 (lang_dirent->d_type == DT_UNKNOWN && \
2824 is_directory(scripts_path, lang_dirent))) && \
2825 (strcmp(lang_dirent->d_name, ".")) && \
2826 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002827
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002828#define for_each_script(lang_path, lang_dir, script_dirent) \
2829 while ((script_dirent = readdir(lang_dir)) != NULL) \
2830 if (script_dirent->d_type != DT_DIR && \
2831 (script_dirent->d_type != DT_UNKNOWN || \
2832 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002833
2834
2835#define RECORD_SUFFIX "-record"
2836#define REPORT_SUFFIX "-report"
2837
2838struct script_desc {
2839 struct list_head node;
2840 char *name;
2841 char *half_liner;
2842 char *args;
2843};
2844
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002845static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002846
2847static struct script_desc *script_desc__new(const char *name)
2848{
2849 struct script_desc *s = zalloc(sizeof(*s));
2850
Tom Zanussib5b87312010-11-10 08:16:51 -06002851 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002852 s->name = strdup(name);
2853
2854 return s;
2855}
2856
2857static void script_desc__delete(struct script_desc *s)
2858{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002859 zfree(&s->name);
2860 zfree(&s->half_liner);
2861 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002862 free(s);
2863}
2864
2865static void script_desc__add(struct script_desc *s)
2866{
2867 list_add_tail(&s->node, &script_descs);
2868}
2869
2870static struct script_desc *script_desc__find(const char *name)
2871{
2872 struct script_desc *s;
2873
2874 list_for_each_entry(s, &script_descs, node)
2875 if (strcasecmp(s->name, name) == 0)
2876 return s;
2877 return NULL;
2878}
2879
2880static struct script_desc *script_desc__findnew(const char *name)
2881{
2882 struct script_desc *s = script_desc__find(name);
2883
2884 if (s)
2885 return s;
2886
2887 s = script_desc__new(name);
2888 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002889 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002890
2891 script_desc__add(s);
2892
2893 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002894}
2895
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002896static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002897{
2898 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002899 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002900
2901 if (strlen(str) > suffix_len) {
2902 p = str + strlen(str) - suffix_len;
2903 if (!strncmp(p, suffix, suffix_len))
2904 return p;
2905 }
2906
2907 return NULL;
2908}
2909
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002910static int read_script_info(struct script_desc *desc, const char *filename)
2911{
2912 char line[BUFSIZ], *p;
2913 FILE *fp;
2914
2915 fp = fopen(filename, "r");
2916 if (!fp)
2917 return -1;
2918
2919 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002920 p = skip_spaces(line);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002921 if (strlen(p) == 0)
2922 continue;
2923 if (*p != '#')
2924 continue;
2925 p++;
2926 if (strlen(p) && *p == '!')
2927 continue;
2928
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002929 p = skip_spaces(p);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002930 if (strlen(p) && p[strlen(p) - 1] == '\n')
2931 p[strlen(p) - 1] = '\0';
2932
2933 if (!strncmp(p, "description:", strlen("description:"))) {
2934 p += strlen("description:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002935 desc->half_liner = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002936 continue;
2937 }
2938
2939 if (!strncmp(p, "args:", strlen("args:"))) {
2940 p += strlen("args:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002941 desc->args = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002942 continue;
2943 }
2944 }
2945
2946 fclose(fp);
2947
2948 return 0;
2949}
2950
Robert Richter38efb532011-11-25 11:38:40 +01002951static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2952{
2953 char *script_root, *str;
2954
2955 script_root = strdup(script_dirent->d_name);
2956 if (!script_root)
2957 return NULL;
2958
2959 str = (char *)ends_with(script_root, suffix);
2960 if (!str) {
2961 free(script_root);
2962 return NULL;
2963 }
2964
2965 *str = '\0';
2966 return script_root;
2967}
2968
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002969static int list_available_scripts(const struct option *opt __maybe_unused,
2970 const char *s __maybe_unused,
2971 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002972{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002973 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002974 char scripts_path[MAXPATHLEN];
2975 DIR *scripts_dir, *lang_dir;
2976 char script_path[MAXPATHLEN];
2977 char lang_path[MAXPATHLEN];
2978 struct script_desc *desc;
2979 char first_half[BUFSIZ];
2980 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002981
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002982 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002983
2984 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002985 if (!scripts_dir) {
2986 fprintf(stdout,
2987 "open(%s) failed.\n"
2988 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2989 scripts_path);
2990 exit(-1);
2991 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002992
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002993 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002994 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2995 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002996 lang_dir = opendir(lang_path);
2997 if (!lang_dir)
2998 continue;
2999
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003000 for_each_script(lang_path, lang_dir, script_dirent) {
3001 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01003002 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003003 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01003004 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3005 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003006 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01003007 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003008 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06003009 }
3010 }
3011
3012 fprintf(stdout, "List of available trace scripts:\n");
3013 list_for_each_entry(desc, &script_descs, node) {
3014 sprintf(first_half, "%s %s", desc->name,
3015 desc->args ? desc->args : "");
3016 fprintf(stdout, " %-36s %s\n", first_half,
3017 desc->half_liner ? desc->half_liner : "");
3018 }
3019
3020 exit(0);
3021}
3022
Feng Tange5f37052012-09-07 16:42:26 +08003023/*
Feng Tang49e639e2012-10-30 11:56:03 +08003024 * Some scripts specify the required events in their "xxx-record" file,
3025 * this function will check if the events in perf.data match those
3026 * mentioned in the "xxx-record".
3027 *
3028 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3029 * which is covered well now. And new parsing code should be added to
3030 * cover the future complexing formats like event groups etc.
3031 */
3032static int check_ev_match(char *dir_name, char *scriptname,
3033 struct perf_session *session)
3034{
3035 char filename[MAXPATHLEN], evname[128];
3036 char line[BUFSIZ], *p;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003037 struct evsel *pos;
Feng Tang49e639e2012-10-30 11:56:03 +08003038 int match, len;
3039 FILE *fp;
3040
Jiri Olsa77f18152018-03-19 09:29:01 +01003041 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08003042
3043 fp = fopen(filename, "r");
3044 if (!fp)
3045 return -1;
3046
3047 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003048 p = skip_spaces(line);
Feng Tang49e639e2012-10-30 11:56:03 +08003049 if (*p == '#')
3050 continue;
3051
3052 while (strlen(p)) {
3053 p = strstr(p, "-e");
3054 if (!p)
3055 break;
3056
3057 p += 2;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003058 p = skip_spaces(p);
Feng Tang49e639e2012-10-30 11:56:03 +08003059 len = strcspn(p, " \t");
3060 if (!len)
3061 break;
3062
3063 snprintf(evname, len + 1, "%s", p);
3064
3065 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003066 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08003067 if (!strcmp(perf_evsel__name(pos), evname)) {
3068 match = 1;
3069 break;
3070 }
3071 }
3072
3073 if (!match) {
3074 fclose(fp);
3075 return -1;
3076 }
3077 }
3078 }
3079
3080 fclose(fp);
3081 return 0;
3082}
3083
3084/*
Feng Tange5f37052012-09-07 16:42:26 +08003085 * Return -1 if none is found, otherwise the actual scripts number.
3086 *
3087 * Currently the only user of this function is the script browser, which
3088 * will list all statically runnable scripts, select one, execute it and
3089 * show the output in a perf browser.
3090 */
Andi Kleen905e4af2019-03-11 07:45:01 -07003091int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3092 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08003093{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003094 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08003095 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08003096 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08003097 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003098 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003099 .path = input_name,
3100 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003101 };
Feng Tange5f37052012-09-07 16:42:26 +08003102 char *temp;
3103 int i = 0;
3104
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003105 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08003106 if (!session)
3107 return -1;
3108
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003109 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003110
3111 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003112 if (!scripts_dir) {
3113 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003114 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003115 }
Feng Tange5f37052012-09-07 16:42:26 +08003116
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003117 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003118 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3119 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003120#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003121 if (strstr(lang_path, "perl"))
3122 continue;
3123#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003124#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003125 if (strstr(lang_path, "python"))
3126 continue;
3127#endif
3128
3129 lang_dir = opendir(lang_path);
3130 if (!lang_dir)
3131 continue;
3132
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003133 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003134 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003135 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003136 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003137 if (i >= num)
3138 break;
3139 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3140 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003141 script_dirent->d_name);
3142 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003143 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003144 (temp - script_dirent->d_name) + 1,
3145 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003146
3147 if (check_ev_match(lang_path,
3148 scripts_array[i], session))
3149 continue;
3150
Feng Tange5f37052012-09-07 16:42:26 +08003151 i++;
3152 }
Feng Tang49e639e2012-10-30 11:56:03 +08003153 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003154 }
3155
Feng Tang49e639e2012-10-30 11:56:03 +08003156 closedir(scripts_dir);
3157 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003158 return i;
3159}
3160
Tom Zanussi38752942009-12-15 02:53:39 -06003161static char *get_script_path(const char *script_root, const char *suffix)
3162{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003163 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003164 char scripts_path[MAXPATHLEN];
3165 char script_path[MAXPATHLEN];
3166 DIR *scripts_dir, *lang_dir;
3167 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003168 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003169
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003170 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003171
3172 scripts_dir = opendir(scripts_path);
3173 if (!scripts_dir)
3174 return NULL;
3175
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003176 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003177 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3178 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003179 lang_dir = opendir(lang_path);
3180 if (!lang_dir)
3181 continue;
3182
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003183 for_each_script(lang_path, lang_dir, script_dirent) {
3184 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003185 if (__script_root && !strcmp(script_root, __script_root)) {
3186 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003187 closedir(lang_dir);
3188 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003189 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3190 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003191 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003192 }
3193 free(__script_root);
3194 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003195 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003196 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003197 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003198
Robert Richter38efb532011-11-25 11:38:40 +01003199 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003200}
3201
Tom Zanussib5b87312010-11-10 08:16:51 -06003202static bool is_top_script(const char *script_path)
3203{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003204 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003205}
3206
3207static int has_required_arg(char *script_path)
3208{
3209 struct script_desc *desc;
3210 int n_args = 0;
3211 char *p;
3212
3213 desc = script_desc__new(NULL);
3214
3215 if (read_script_info(desc, script_path))
3216 goto out;
3217
3218 if (!desc->args)
3219 goto out;
3220
3221 for (p = desc->args; *p; p++)
3222 if (*p == '<')
3223 n_args++;
3224out:
3225 script_desc__delete(desc);
3226
3227 return n_args;
3228}
3229
David Ahernd54b1a92012-08-26 12:24:46 -06003230static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003231{
3232 char **__argv = malloc(sizeof(const char *) * argc);
3233
David Ahernd54b1a92012-08-26 12:24:46 -06003234 if (!__argv) {
3235 pr_err("malloc failed\n");
3236 return -1;
3237 }
3238
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003239 memcpy(__argv, argv, sizeof(const char *) * argc);
3240 argc = parse_options(argc, (const char **)__argv, record_options,
3241 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3242 free(__argv);
3243
David Ahernd54b1a92012-08-26 12:24:46 -06003244 system_wide = (argc == 0);
3245
3246 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003247}
3248
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003249static void script__setup_sample_type(struct perf_script *script)
3250{
3251 struct perf_session *session = script->session;
3252 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3253
3254 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3255 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003256 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003257 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003258 dwarf_callchain_users = true;
3259 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003260 callchain_param.record_mode = CALLCHAIN_LBR;
3261 else
3262 callchain_param.record_mode = CALLCHAIN_FP;
3263 }
3264}
3265
Jiri Olsa89f16882018-09-13 14:54:03 +02003266static int process_stat_round_event(struct perf_session *session,
3267 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003268{
3269 struct stat_round_event *round = &event->stat_round;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003270 struct evsel *counter;
Jiri Olsae099eba2016-01-05 22:09:09 +01003271
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003272 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003273 perf_stat_process_counter(&stat_config, counter);
3274 process_stat(counter, round->time);
3275 }
3276
3277 process_stat_interval(round->time);
3278 return 0;
3279}
3280
Jiri Olsa89f16882018-09-13 14:54:03 +02003281static int process_stat_config_event(struct perf_session *session __maybe_unused,
3282 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003283{
3284 perf_event__read_stat_config(&stat_config, &event->stat_config);
3285 return 0;
3286}
3287
Jiri Olsacfc88742016-01-05 22:09:06 +01003288static int set_maps(struct perf_script *script)
3289{
Jiri Olsa63503db2019-07-21 13:23:52 +02003290 struct evlist *evlist = script->session->evlist;
Jiri Olsacfc88742016-01-05 22:09:06 +01003291
3292 if (!script->cpus || !script->threads)
3293 return 0;
3294
3295 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3296 return -EINVAL;
3297
Jiri Olsa453fa032019-07-21 13:24:43 +02003298 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
Jiri Olsacfc88742016-01-05 22:09:06 +01003299
3300 if (perf_evlist__alloc_stats(evlist, true))
3301 return -ENOMEM;
3302
3303 script->allocated = true;
3304 return 0;
3305}
3306
3307static
Jiri Olsa89f16882018-09-13 14:54:03 +02003308int process_thread_map_event(struct perf_session *session,
3309 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003310{
Jiri Olsa89f16882018-09-13 14:54:03 +02003311 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003312 struct perf_script *script = container_of(tool, struct perf_script, tool);
3313
3314 if (script->threads) {
3315 pr_warning("Extra thread map event, ignoring.\n");
3316 return 0;
3317 }
3318
3319 script->threads = thread_map__new_event(&event->thread_map);
3320 if (!script->threads)
3321 return -ENOMEM;
3322
3323 return set_maps(script);
3324}
3325
3326static
Jiri Olsa89f16882018-09-13 14:54:03 +02003327int process_cpu_map_event(struct perf_session *session,
3328 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003329{
Jiri Olsa89f16882018-09-13 14:54:03 +02003330 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003331 struct perf_script *script = container_of(tool, struct perf_script, tool);
3332
3333 if (script->cpus) {
3334 pr_warning("Extra cpu map event, ignoring.\n");
3335 return 0;
3336 }
3337
3338 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3339 if (!script->cpus)
3340 return -ENOMEM;
3341
3342 return set_maps(script);
3343}
3344
Jiri Olsa89f16882018-09-13 14:54:03 +02003345static int process_feature_event(struct perf_session *session,
3346 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303347{
3348 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003349 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303350 return 0;
3351}
3352
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003353#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003354static int perf_script__process_auxtrace_info(struct perf_session *session,
3355 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003356{
Jiri Olsa89f16882018-09-13 14:54:03 +02003357 struct perf_tool *tool = session->tool;
3358
3359 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003360
3361 if (ret == 0) {
3362 struct perf_script *script = container_of(tool, struct perf_script, tool);
3363
3364 ret = perf_script__setup_per_event_dump(script);
3365 }
3366
3367 return ret;
3368}
3369#else
3370#define perf_script__process_auxtrace_info 0
3371#endif
3372
Andi Kleenb585ebd2018-09-20 11:05:36 -07003373static int parse_insn_trace(const struct option *opt __maybe_unused,
3374 const char *str __maybe_unused,
3375 int unset __maybe_unused)
3376{
3377 parse_output_fields(NULL, "+insn,-event,-period", 0);
3378 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003379 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003380 return 0;
3381}
3382
3383static int parse_xed(const struct option *opt __maybe_unused,
3384 const char *str __maybe_unused,
3385 int unset __maybe_unused)
3386{
3387 force_pager("xed -F insn: -A -64 | less");
3388 return 0;
3389}
3390
Andi Kleend1b15522018-09-20 11:05:38 -07003391static int parse_call_trace(const struct option *opt __maybe_unused,
3392 const char *str __maybe_unused,
3393 int unset __maybe_unused)
3394{
3395 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3396 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003397 symbol_conf.nanosecs = true;
Jiri Olsa1c492422019-05-08 15:20:05 +02003398 symbol_conf.pad_output_len_dso = 50;
Andi Kleend1b15522018-09-20 11:05:38 -07003399 return 0;
3400}
3401
3402static int parse_callret_trace(const struct option *opt __maybe_unused,
3403 const char *str __maybe_unused,
3404 int unset __maybe_unused)
3405{
3406 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3407 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003408 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003409 return 0;
3410}
3411
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003412int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003413{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003414 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003415 bool header = false;
3416 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003417 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003418 char *rec_script_path = NULL;
3419 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003420 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003421 struct itrace_synth_opts itrace_synth_opts = {
3422 .set = false,
3423 .default_no_sample = true,
3424 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003425 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003426 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003427 const char **__argv;
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003428 const char *event_switch_on = NULL,
3429 *event_switch_off = NULL;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003430 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003431 struct perf_script script = {
3432 .tool = {
3433 .sample = process_sample_event,
3434 .mmap = perf_event__process_mmap,
3435 .mmap2 = perf_event__process_mmap2,
3436 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303437 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003438 .exit = perf_event__process_exit,
3439 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003440 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003441 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003442 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303443 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003444 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003445 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003446 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003447 .auxtrace = perf_event__process_auxtrace,
3448 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003449 .stat = perf_event__process_stat_event,
3450 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003451 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003452 .thread_map = process_thread_map_event,
3453 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003454 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003455 .ordering_requires_timestamps = true,
3456 },
3457 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003458 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003459 .mode = PERF_DATA_MODE_READ,
3460 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003461 const struct option options[] = {
3462 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3463 "dump raw trace in ASCII"),
3464 OPT_INCR('v', "verbose", &verbose,
3465 "be more verbose (show symbol address, etc)"),
3466 OPT_BOOLEAN('L', "Latency", &latency_format,
3467 "show latency attributes (irqs/preemption disabled, etc)"),
3468 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3469 list_available_scripts),
3470 OPT_CALLBACK('s', "script", NULL, "name",
3471 "script file name (lang:script name, script name, or *)",
3472 parse_scriptname),
3473 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3474 "generate perf-script.xx script in specified language"),
3475 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3476 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3477 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003478 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3479 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003480 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3481 "file", "vmlinux pathname"),
3482 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3483 "file", "kallsyms pathname"),
3484 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3485 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003486 OPT_CALLBACK(0, "symfs", NULL, "directory",
3487 "Look for files with symbols relative to this directory",
3488 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003489 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003490 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003491 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003492 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003493 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303494 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3495 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
Adrian Hunter68fb45b2019-05-20 14:37:14 +03003496 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003497 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003498 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3499 "system-wide collection from all CPUs"),
3500 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3501 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003502 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3503 "Decode instructions from itrace", parse_insn_trace),
3504 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3505 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003506 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3507 "Decode calls from from itrace", parse_call_trace),
3508 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3509 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003510 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3511 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003512 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3513 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003514 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3515 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3516 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003517 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3518 "only consider symbols in these pids"),
3519 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3520 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003521 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3522 "Set the maximum stack depth when parsing the callchain, "
3523 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003524 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Andi Kleen90b10f42019-03-14 15:50:00 -07003525 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003526 OPT_BOOLEAN('I', "show-info", &show_full_info,
3527 "display extended information from perf.data file"),
3528 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3529 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003530 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3531 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003532 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3533 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003534 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3535 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303536 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3537 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003538 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3539 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003540 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3541 "Show round events (if recorded)"),
Jiri Olsa490c8cc2019-05-08 15:20:08 +02003542 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3543 "Show bpf related events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003544 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3545 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003546 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003547 OPT_INTEGER(0, "max-blocks", &max_blocks,
3548 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003549 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003550 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003551 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003552 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003553 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003554 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3555 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003556 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3557 "Enable symbol demangling"),
3558 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3559 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003560 OPT_STRING(0, "time", &script.time_str, "str",
3561 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003562 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3563 "Show inline function"),
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003564 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3565 "guest mount directory under which every guest os"
3566 " instance has a subdir"),
3567 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3568 "file", "file saving guest os vmlinux"),
3569 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3570 "file", "file saving guest os /proc/kallsyms"),
3571 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3572 "file", "file saving guest os /proc/modules"),
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03003573 OPT_STRING(0, "switch-on", &event_switch_on,
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003574 "event", "Consider events after the ocurrence of this event"),
3575 OPT_STRING(0, "switch-off", &event_switch_off,
3576 "event", "Stop considering events after the ocurrence of this event"),
Arnaldo Carvalho de Melo6469eb62019-08-14 16:40:58 -03003577 OPT_BOOLEAN(0, "show-on-off-events", &script.evswitch.show_on_off_events,
3578 "Show the on/off switch events, used with --switch-on"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003579 OPT_END()
3580 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003581 const char * const script_subcommands[] = { "record", "report", NULL };
3582 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003583 "perf script [<options>]",
3584 "perf script [<options>] record <script> [<record-options>] <command>",
3585 "perf script [<options>] report <script> [script-args]",
3586 "perf script [<options>] <script> [<record-options>] <command>",
3587 "perf script [<options>] <top-script> [script-args]",
3588 NULL
3589 };
Tom Zanussi38752942009-12-15 02:53:39 -06003590
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003591 perf_set_singlethreaded();
3592
Tom Zanussib5b87312010-11-10 08:16:51 -06003593 setup_scripting();
3594
Yunlong Song40cae2b2015-03-18 21:35:54 +08003595 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003596 PARSE_OPT_STOP_AT_NON_OPTION);
3597
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003598 if (symbol_conf.guestmount ||
3599 symbol_conf.default_guest_vmlinux_name ||
3600 symbol_conf.default_guest_kallsyms ||
3601 symbol_conf.default_guest_modules) {
3602 /*
3603 * Enable guest sample processing.
3604 */
3605 perf_guest = true;
3606 }
3607
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003608 data.path = input_name;
3609 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003610
Tom Zanussib5b87312010-11-10 08:16:51 -06003611 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3612 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3613 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003614 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003615 }
3616
Tom Zanussib5b87312010-11-10 08:16:51 -06003617 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3618 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3619 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003620 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003621 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003622 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003623 return -1;
3624 }
Tom Zanussi38752942009-12-15 02:53:39 -06003625 }
3626
Andi Kleen90b10f42019-03-14 15:50:00 -07003627 if (script.time_str && reltime) {
3628 fprintf(stderr, "Don't combine --reltime with --time\n");
3629 return -1;
3630 }
3631
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003632 if (itrace_synth_opts.callchain &&
3633 itrace_synth_opts.callchain_sz > scripting_max_stack)
3634 scripting_max_stack = itrace_synth_opts.callchain_sz;
3635
Ben Hutchings44e668c2010-10-10 16:10:03 +01003636 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003637 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003638
Tom Zanussib5b87312010-11-10 08:16:51 -06003639 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003640 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003641 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003642 pid_t pid;
3643
Tom Zanussib5b87312010-11-10 08:16:51 -06003644 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3645 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3646
3647 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003648 usage_with_options_msg(script_usage, options,
3649 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003650 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003651 }
3652
Tom Zanussib5b87312010-11-10 08:16:51 -06003653 if (is_top_script(argv[0])) {
3654 rep_args = argc - 1;
3655 } else {
3656 int rec_args;
3657
3658 rep_args = has_required_arg(rep_script_path);
3659 rec_args = (argc - 1) - rep_args;
3660 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003661 usage_with_options_msg(script_usage, options,
3662 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003663 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003664 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003665 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003666 }
3667
3668 if (pipe(live_pipe) < 0) {
3669 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003670 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003671 }
3672
3673 pid = fork();
3674 if (pid < 0) {
3675 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003676 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003677 }
3678
3679 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003680 j = 0;
3681
Tom Zanussia0cccc22010-04-01 23:59:25 -05003682 dup2(live_pipe[1], 1);
3683 close(live_pipe[0]);
3684
Robert Richter317df652011-11-25 15:05:25 +01003685 if (is_top_script(argv[0])) {
3686 system_wide = true;
3687 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003688 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3689 err = -1;
3690 goto out;
3691 }
Robert Richter317df652011-11-25 15:05:25 +01003692 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003693
3694 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003695 if (!__argv) {
3696 pr_err("malloc failed\n");
3697 err = -ENOMEM;
3698 goto out;
3699 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003700
Tom Zanussib5b87312010-11-10 08:16:51 -06003701 __argv[j++] = "/bin/sh";
3702 __argv[j++] = rec_script_path;
3703 if (system_wide)
3704 __argv[j++] = "-a";
3705 __argv[j++] = "-q";
3706 __argv[j++] = "-o";
3707 __argv[j++] = "-";
3708 for (i = rep_args + 1; i < argc; i++)
3709 __argv[j++] = argv[i];
3710 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003711
3712 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003713 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003714 exit(-1);
3715 }
3716
3717 dup2(live_pipe[0], 0);
3718 close(live_pipe[1]);
3719
Tom Zanussib5b87312010-11-10 08:16:51 -06003720 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003721 if (!__argv) {
3722 pr_err("malloc failed\n");
3723 err = -ENOMEM;
3724 goto out;
3725 }
3726
Tom Zanussib5b87312010-11-10 08:16:51 -06003727 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003728 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003729 __argv[j++] = rep_script_path;
3730 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003731 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003732 __argv[j++] = "-i";
3733 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003734 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003735
3736 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003737 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003738 exit(-1);
3739 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003740
Tom Zanussib5b87312010-11-10 08:16:51 -06003741 if (rec_script_path)
3742 script_path = rec_script_path;
3743 if (rep_script_path)
3744 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003745
Tom Zanussib5b87312010-11-10 08:16:51 -06003746 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003747 j = 0;
3748
Robert Richter317df652011-11-25 15:05:25 +01003749 if (!rec_script_path)
3750 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003751 else if (!system_wide) {
3752 if (have_cmd(argc - 1, &argv[1]) != 0) {
3753 err = -1;
3754 goto out;
3755 }
3756 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003757
3758 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003759 if (!__argv) {
3760 pr_err("malloc failed\n");
3761 err = -ENOMEM;
3762 goto out;
3763 }
3764
Tom Zanussib5b87312010-11-10 08:16:51 -06003765 __argv[j++] = "/bin/sh";
3766 __argv[j++] = script_path;
3767 if (system_wide)
3768 __argv[j++] = "-a";
3769 for (i = 2; i < argc; i++)
3770 __argv[j++] = argv[i];
3771 __argv[j++] = NULL;
3772
3773 execvp("/bin/sh", (char **)__argv);
3774 free(__argv);
3775 exit(-1);
3776 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003777
Milian Wolffc1c9b962018-10-21 21:14:23 +02003778 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003779 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003780 use_browser = 0;
3781 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003782
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003783 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003784 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003785 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003786
Jiri Olsae90debd2013-12-09 11:02:50 +01003787 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003788 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003789 perf_session__fprintf_info(session, stdout, show_full_info);
3790 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003791 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003792 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003793 if (show_full_info)
3794 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003795
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003796 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003797 goto out_delete;
3798
Andi Kleen3ab481a2019-03-05 06:47:45 -08003799 uname(&uts);
Song Liu9d491692019-06-20 18:44:38 -07003800 if (data.is_pipe || /* assume pipe_mode indicates native_arch */
3801 !strcmp(uts.machine, session->header.env.arch) ||
Andi Kleen3ab481a2019-03-05 06:47:45 -08003802 (!strcmp(uts.machine, "x86_64") &&
3803 !strcmp(session->header.env.arch, "i386")))
3804 native_arch = true;
3805
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003806 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003807 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003808
Andi Kleen99f753f2018-09-20 11:05:39 -07003809 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3810 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003811 itrace_synth_opts.thread_stack = true;
3812
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003813 session->itrace_synth_opts = &itrace_synth_opts;
3814
Anton Blanchard5d67be92011-07-04 21:57:50 +10003815 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003816 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3817 if (err < 0)
3818 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003819 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003820 }
3821
David Ahern1424dc92011-03-09 22:23:28 -07003822 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003823 symbol_conf.use_callchain = true;
3824 else
3825 symbol_conf.use_callchain = false;
3826
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003827 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003828 tep_set_function_resolver(session->tevent.pevent,
3829 machine__resolve_kernel_addr,
3830 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003831 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003832 err = -1;
3833 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003834 }
3835
Tom Zanussi956ffd02009-11-25 01:15:46 -06003836 if (generate_script_lang) {
3837 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003838 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003839
David Ahern2c9e45f72011-03-17 10:03:21 -06003840 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003841 fprintf(stderr,
3842 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003843 err = -EINVAL;
3844 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003845 }
3846
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003847 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003848 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003849 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003850 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003851 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003852 }
3853
3854 err = fstat(input, &perf_stat);
3855 if (err < 0) {
3856 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003857 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003858 }
3859
3860 if (!perf_stat.st_size) {
3861 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003862 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003863 }
3864
3865 scripting_ops = script_spec__lookup(generate_script_lang);
3866 if (!scripting_ops) {
3867 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003868 err = -ENOENT;
3869 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003870 }
3871
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003872 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003873 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003874 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003875 }
3876
3877 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003878 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003879 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003880 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003881 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003882 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003883 }
3884
David Ahern9cbdb702011-04-06 21:54:20 -06003885
3886 err = perf_session__check_output_opt(session);
3887 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003888 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003889
Jin Yao284c4e12019-03-01 18:13:06 +08003890 if (script.time_str) {
3891 err = perf_time__parse_for_ranges(script.time_str, session,
3892 &script.ptime_range,
3893 &script.range_size,
3894 &script.range_num);
3895 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08003896 goto out_delete;
Adrian Hunter400ae982019-06-04 16:00:00 +03003897
3898 itrace_synth_opts__set_time_range(&itrace_synth_opts,
3899 script.ptime_range,
3900 script.range_num);
David Aherna91f4c42016-11-29 10:15:43 -07003901 }
3902
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03003903 if (event_switch_on) {
3904 script.evswitch.on = perf_evlist__find_evsel_by_str(session->evlist, event_switch_on);
3905 if (script.evswitch.on == NULL) {
3906 fprintf(stderr, "switch-on event not found (%s)\n", event_switch_on);
3907 err = -ENOENT;
3908 goto out_delete;
3909 }
3910 script.evswitch.discarding = true;
3911 }
3912
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003913 if (event_switch_off) {
3914 script.evswitch.off = perf_evlist__find_evsel_by_str(session->evlist, event_switch_off);
3915 if (script.evswitch.off == NULL) {
3916 fprintf(stderr, "switch-off event not found (%s)\n", event_switch_off);
3917 err = -ENOENT;
3918 goto out_delete;
3919 }
3920 }
3921
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003922 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003923
Adrian Hunterd445dd22014-08-15 22:08:37 +03003924 flush_scripting();
3925
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003926out_delete:
Adrian Hunter400ae982019-06-04 16:00:00 +03003927 if (script.ptime_range) {
3928 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
Jin Yao284c4e12019-03-01 18:13:06 +08003929 zfree(&script.ptime_range);
Adrian Hunter400ae982019-06-04 16:00:00 +03003930 }
Jin Yaocc2ef582018-01-10 23:00:33 +08003931
Jiri Olsacfc88742016-01-05 22:09:06 +01003932 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003933 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003934
3935 if (script_started)
3936 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003937out:
3938 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003939}