blob: fa0cc8b0ecccad71bd640bc4e6db21ac86e259f1 [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 {
1620 struct evsel *on;
1621 bool discarding;
Arnaldo Carvalho de Melo6469eb62019-08-14 16:40:58 -03001622 bool show_on_off_events;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001623};
1624
Jiri Olsa809e9422015-11-26 18:55:21 +01001625struct perf_script {
1626 struct perf_tool tool;
1627 struct perf_session *session;
1628 bool show_task_events;
1629 bool show_mmap_events;
1630 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301631 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001632 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001633 bool show_round_events;
Jiri Olsa490c8cc2019-05-08 15:20:08 +02001634 bool show_bpf_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001635 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001636 bool per_event_dump;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001637 struct evswitch evswitch;
Jiri Olsaf8548392019-07-21 13:23:49 +02001638 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +02001639 struct perf_thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001640 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001641 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001642 struct perf_time_interval *ptime_range;
1643 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001644 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001645};
1646
Jiri Olsa63503db2019-07-21 13:23:52 +02001647static int perf_evlist__max_name_len(struct evlist *evlist)
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001648{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001649 struct evsel *evsel;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001650 int max = 0;
1651
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001652 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001653 int len = strlen(perf_evsel__name(evsel));
1654
1655 max = MAX(len, max);
1656 }
1657
1658 return max;
1659}
1660
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001661static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001662{
1663 struct mem_info mi = { .data_src.val = data_src };
1664 char decode[100];
1665 char out[100];
1666 static int maxlen;
1667 int len;
1668
1669 perf_script__meminfo_scnprintf(decode, 100, &mi);
1670
1671 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1672 if (maxlen < len)
1673 maxlen = len;
1674
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001675 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001676}
1677
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001678struct metric_ctx {
1679 struct perf_sample *sample;
1680 struct thread *thread;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001681 struct evsel *evsel;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001682 FILE *fp;
1683};
1684
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001685static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1686 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001687 const char *fmt,
1688 const char *unit, double val)
1689{
1690 struct metric_ctx *mctx = ctx;
1691
1692 if (!fmt)
1693 return;
1694 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001695 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001696 fputs("\tmetric: ", mctx->fp);
1697 if (color)
1698 color_fprintf(mctx->fp, color, fmt, val);
1699 else
1700 printf(fmt, val);
1701 fprintf(mctx->fp, " %s\n", unit);
1702}
1703
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001704static void script_new_line(struct perf_stat_config *config __maybe_unused,
1705 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001706{
1707 struct metric_ctx *mctx = ctx;
1708
1709 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001710 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001711 fputs("\tmetric: ", mctx->fp);
1712}
1713
1714static void perf_sample__fprint_metric(struct perf_script *script,
1715 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001716 struct evsel *evsel,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001717 struct perf_sample *sample,
1718 FILE *fp)
1719{
1720 struct perf_stat_output_ctx ctx = {
1721 .print_metric = script_print_metric,
1722 .new_line = script_new_line,
1723 .ctx = &(struct metric_ctx) {
1724 .sample = sample,
1725 .thread = thread,
1726 .evsel = evsel,
1727 .fp = fp,
1728 },
1729 .force_header = false,
1730 };
Jiri Olsa32dcd022019-07-21 13:23:51 +02001731 struct evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001732 u64 val;
1733
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001734 if (!evsel->stats)
1735 perf_evlist__alloc_stats(script->session->evlist, false);
1736 if (evsel_script(evsel->leader)->gnum++ == 0)
1737 perf_stat__reset_shadow_stats();
1738 val = sample->period * evsel->scale;
1739 perf_stat__update_shadow_stats(evsel,
1740 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001741 sample->cpu,
1742 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001743 evsel_script(evsel)->val = val;
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001744 if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) {
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001745 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001746 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001747 evsel_script(ev2)->val,
1748 sample->cpu,
1749 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001750 NULL,
1751 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001752 }
1753 evsel_script(evsel->leader)->gnum = 0;
1754 }
1755}
1756
Andi Kleen99f753f2018-09-20 11:05:39 -07001757static bool show_event(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001758 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001759 struct thread *thread,
1760 struct addr_location *al)
1761{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001762 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001763
1764 if (!symbol_conf.graph_function)
1765 return true;
1766
1767 if (thread->filter) {
1768 if (depth <= thread->filter_entry_depth) {
1769 thread->filter = false;
1770 return false;
1771 }
1772 return true;
1773 } else {
1774 const char *s = symbol_conf.graph_function;
1775 u64 ip;
1776 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1777 &ip);
1778 unsigned nlen;
1779
1780 if (!name)
1781 return false;
1782 nlen = strlen(name);
1783 while (*s) {
1784 unsigned len = strcspn(s, ",");
1785 if (nlen == len && !strncmp(name, s, len)) {
1786 thread->filter = true;
1787 thread->filter_entry_depth = depth;
1788 return true;
1789 }
1790 s += len;
1791 if (*s == ',')
1792 s++;
1793 }
1794 return false;
1795 }
1796}
1797
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001798static void process_event(struct perf_script *script,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001799 struct perf_sample *sample, struct evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001800 struct addr_location *al,
1801 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001802{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001803 struct thread *thread = al->thread;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001804 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001805 unsigned int type = output_type(attr->type);
Jiri Olsa32dcd022019-07-21 13:23:51 +02001806 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001807 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001808
Adrian Hunter14057202017-06-21 13:17:19 +03001809 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001810 return;
1811
Andi Kleen99f753f2018-09-20 11:05:39 -07001812 if (!show_event(sample, evsel, thread, al))
1813 return;
1814
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001815 if (script->evswitch.on && script->evswitch.discarding) {
1816 if (script->evswitch.on != evsel)
1817 return;
1818
1819 script->evswitch.discarding = false;
Arnaldo Carvalho de Melo6469eb62019-08-14 16:40:58 -03001820
1821 if (!script->evswitch.show_on_off_events)
1822 return;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001823 }
1824
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001825 ++es->samples;
1826
Jiri Olsa28a0b392018-01-07 17:03:52 +01001827 perf_sample__fprintf_start(sample, thread, evsel,
1828 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001829
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001830 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001831 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001832
Namhyung Kime944d3d2013-11-18 14:34:52 +09001833 if (PRINT_FIELD(EVNAME)) {
1834 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001835
1836 if (!script->name_width)
1837 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1838
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001839 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001840 }
1841
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001842 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001843 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001844
Akihiro Nagai95582592012-01-30 13:43:09 +09001845 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001846 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001847 return;
1848 }
1849
Andi Kleen96167162019-01-17 11:48:34 -08001850 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001851 event_format__fprintf(evsel->tp_format, sample->cpu,
1852 sample->raw_data, sample->raw_size, fp);
1853 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001854
1855 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001856 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001857
David Ahern7cec0922011-05-30 13:08:23 -06001858 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001859 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001860
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001861 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001862 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001863
1864 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001865 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001866
David Ahern787bef12011-05-27 14:28:43 -06001867 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001868 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001869
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001870 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001871 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001872 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001873 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001874
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001875 fputc(cursor ? '\n' : ' ', fp);
1876 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001877 }
1878
Stephane Eranianfc36f942015-08-31 18:41:10 +02001879 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001880 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001881
Andi Kleenb1491ac2017-09-05 11:40:57 -07001882 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001883 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001884
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001885 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001886 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001887 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001888 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001889 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001890 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001891
Wang Nan30372f02016-02-24 11:20:45 +00001892 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001893 perf_sample__fprintf_bpf_output(sample, fp);
1894 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001895
1896 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001897 fprintf(fp, "%16" PRIx64, sample->phys_addr);
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001898
1899 perf_sample__fprintf_ipc(sample, attr, fp);
1900
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001901 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001902
Andi Kleendd2e18e2018-12-03 16:18:48 -08001903 if (PRINT_FIELD(SRCCODE)) {
1904 if (map__fprintf_srccode(al->map, al->addr, stdout,
1905 &thread->srccode_state))
1906 printf("\n");
1907 }
1908
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001909 if (PRINT_FIELD(METRIC))
1910 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001911
1912 if (verbose)
1913 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001914}
1915
Tom Zanussi956ffd02009-11-25 01:15:46 -06001916static struct scripting_ops *scripting_ops;
1917
Jiri Olsa32dcd022019-07-21 13:23:51 +02001918static void __process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsa36e33c52016-01-06 11:49:56 +01001919{
Jiri Olsaaf663bd2019-07-21 13:24:39 +02001920 int nthreads = thread_map__nr(counter->core.threads);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001921 int ncpus = perf_evsel__nr_cpus(counter);
1922 int cpu, thread;
1923 static int header_printed;
1924
1925 if (counter->system_wide)
1926 nthreads = 1;
1927
1928 if (!header_printed) {
1929 printf("%3s %8s %15s %15s %15s %15s %s\n",
1930 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1931 header_printed = 1;
1932 }
1933
1934 for (thread = 0; thread < nthreads; thread++) {
1935 for (cpu = 0; cpu < ncpus; cpu++) {
1936 struct perf_counts_values *counts;
1937
1938 counts = perf_counts(counter->counts, cpu, thread);
1939
1940 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
Jiri Olsad400bd32019-07-21 13:24:37 +02001941 counter->core.cpus->map[cpu],
Jiri Olsaaf663bd2019-07-21 13:24:39 +02001942 thread_map__pid(counter->core.threads, thread),
Jiri Olsa36e33c52016-01-06 11:49:56 +01001943 counts->val,
1944 counts->ena,
1945 counts->run,
1946 tstamp,
1947 perf_evsel__name(counter));
1948 }
1949 }
1950}
1951
Jiri Olsa32dcd022019-07-21 13:23:51 +02001952static void process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsae099eba2016-01-05 22:09:09 +01001953{
1954 if (scripting_ops && scripting_ops->process_stat)
1955 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001956 else
1957 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001958}
1959
1960static void process_stat_interval(u64 tstamp)
1961{
1962 if (scripting_ops && scripting_ops->process_stat_interval)
1963 scripting_ops->process_stat_interval(tstamp);
1964}
1965
Tom Zanussi956ffd02009-11-25 01:15:46 -06001966static void setup_scripting(void)
1967{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001968 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001969 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001970}
1971
Adrian Hunterd445dd22014-08-15 22:08:37 +03001972static int flush_scripting(void)
1973{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001974 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001975}
1976
Tom Zanussi956ffd02009-11-25 01:15:46 -06001977static int cleanup_scripting(void)
1978{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001979 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001980
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001981 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001982}
1983
Andi Kleene87e5482019-03-11 07:44:52 -07001984static bool filter_cpu(struct perf_sample *sample)
1985{
1986 if (cpu_list)
1987 return !test_bit(sample->cpu, cpu_bitmap);
1988 return false;
1989}
1990
Jiri Olsa809e9422015-11-26 18:55:21 +01001991static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001992 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001993 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001994 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001995 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001996{
Jiri Olsa809e9422015-11-26 18:55:21 +01001997 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001998 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001999
Jin Yao2ab046c2017-12-08 21:13:46 +08002000 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
2001 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07002002 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08002003 }
David Aherna91f4c42016-11-29 10:15:43 -07002004
David Ahern1424dc92011-03-09 22:23:28 -07002005 if (debug_mode) {
2006 if (sample->time < last_timestamp) {
2007 pr_err("Samples misordered, previous: %" PRIu64
2008 " this: %" PRIu64 "\n", last_timestamp,
2009 sample->time);
2010 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02002011 }
David Ahern1424dc92011-03-09 22:23:28 -07002012 last_timestamp = sample->time;
2013 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002014 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002015
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03002016 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07002017 pr_err("problem processing %d event, skipping it.\n",
2018 event->header.type);
2019 return -1;
2020 }
2021
2022 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002023 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07002024
Andi Kleene87e5482019-03-11 07:44:52 -07002025 if (filter_cpu(sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002026 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002027
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002028 if (scripting_ops)
2029 scripting_ops->process_event(event, sample, evsel, &al);
2030 else
Andi Kleen48d02a12017-02-23 15:46:34 -08002031 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002032
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002033out_put:
2034 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002035 return 0;
2036}
2037
Adrian Hunter7ea95722013-11-01 15:51:30 +02002038static int process_attr(struct perf_tool *tool, union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02002039 struct evlist **pevlist)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002040{
2041 struct perf_script *scr = container_of(tool, struct perf_script, tool);
Jiri Olsa63503db2019-07-21 13:23:52 +02002042 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002043 struct evsel *evsel, *pos;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002044 int err;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002045 static struct evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002046
2047 err = perf_event__process_attr(tool, event, pevlist);
2048 if (err)
2049 return err;
2050
2051 evlist = *pevlist;
2052 evsel = perf_evlist__last(*pevlist);
2053
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302054 if (!evsel->priv) {
2055 if (scr->per_event_dump) {
2056 evsel->priv = perf_evsel_script__new(evsel,
2057 scr->session->data);
2058 } else {
2059 es = zalloc(sizeof(*es));
2060 if (!es)
2061 return -ENOMEM;
2062 es->fp = stdout;
2063 evsel->priv = es;
2064 }
2065 }
2066
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002067 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2068 evsel->core.attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002069 return 0;
2070
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002071 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002072 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002073 return 0;
2074 }
2075
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002076 set_print_ip_opts(&evsel->core.attr);
Adrian Hunter7ea95722013-11-01 15:51:30 +02002077
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002078 if (evsel->core.attr.sample_type)
Jiri Olsad2b5a312015-10-16 12:41:25 +02002079 err = perf_evsel__check_attr(evsel, scr->session);
2080
2081 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002082}
2083
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002084static int process_comm_event(struct perf_tool *tool,
2085 union perf_event *event,
2086 struct perf_sample *sample,
2087 struct machine *machine)
2088{
2089 struct thread *thread;
2090 struct perf_script *script = container_of(tool, struct perf_script, tool);
2091 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002092 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002093 int ret = -1;
2094
2095 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2096 if (thread == NULL) {
2097 pr_debug("problem processing COMM event, skipping it.\n");
2098 return -1;
2099 }
2100
2101 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2102 goto out;
2103
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002104 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002105 sample->cpu = 0;
2106 sample->time = 0;
2107 sample->tid = event->comm.tid;
2108 sample->pid = event->comm.pid;
2109 }
Andi Kleene87e5482019-03-11 07:44:52 -07002110 if (!filter_cpu(sample)) {
2111 perf_sample__fprintf_start(sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01002112 PERF_RECORD_COMM, stdout);
Andi Kleene87e5482019-03-11 07:44:52 -07002113 perf_event__fprintf(event, stdout);
2114 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002115 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002116out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002117 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002118 return ret;
2119}
2120
Hari Bathini96a44bb2017-03-08 02:12:06 +05302121static int process_namespaces_event(struct perf_tool *tool,
2122 union perf_event *event,
2123 struct perf_sample *sample,
2124 struct machine *machine)
2125{
2126 struct thread *thread;
2127 struct perf_script *script = container_of(tool, struct perf_script, tool);
2128 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002129 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302130 int ret = -1;
2131
2132 thread = machine__findnew_thread(machine, event->namespaces.pid,
2133 event->namespaces.tid);
2134 if (thread == NULL) {
2135 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2136 return -1;
2137 }
2138
2139 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2140 goto out;
2141
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002142 if (!evsel->core.attr.sample_id_all) {
Hari Bathini96a44bb2017-03-08 02:12:06 +05302143 sample->cpu = 0;
2144 sample->time = 0;
2145 sample->tid = event->namespaces.tid;
2146 sample->pid = event->namespaces.pid;
2147 }
Andi Kleene87e5482019-03-11 07:44:52 -07002148 if (!filter_cpu(sample)) {
2149 perf_sample__fprintf_start(sample, thread, evsel,
2150 PERF_RECORD_NAMESPACES, stdout);
2151 perf_event__fprintf(event, stdout);
2152 }
Hari Bathini96a44bb2017-03-08 02:12:06 +05302153 ret = 0;
2154out:
2155 thread__put(thread);
2156 return ret;
2157}
2158
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002159static int process_fork_event(struct perf_tool *tool,
2160 union perf_event *event,
2161 struct perf_sample *sample,
2162 struct machine *machine)
2163{
2164 struct thread *thread;
2165 struct perf_script *script = container_of(tool, struct perf_script, tool);
2166 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002167 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002168
2169 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2170 return -1;
2171
2172 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2173 if (thread == NULL) {
2174 pr_debug("problem processing FORK event, skipping it.\n");
2175 return -1;
2176 }
2177
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002178 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002179 sample->cpu = 0;
2180 sample->time = event->fork.time;
2181 sample->tid = event->fork.tid;
2182 sample->pid = event->fork.pid;
2183 }
Andi Kleene87e5482019-03-11 07:44:52 -07002184 if (!filter_cpu(sample)) {
2185 perf_sample__fprintf_start(sample, thread, evsel,
2186 PERF_RECORD_FORK, stdout);
2187 perf_event__fprintf(event, stdout);
2188 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002189 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002190
2191 return 0;
2192}
2193static int process_exit_event(struct perf_tool *tool,
2194 union perf_event *event,
2195 struct perf_sample *sample,
2196 struct machine *machine)
2197{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002198 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002199 struct thread *thread;
2200 struct perf_script *script = container_of(tool, struct perf_script, tool);
2201 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002202 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002203
2204 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2205 if (thread == NULL) {
2206 pr_debug("problem processing EXIT event, skipping it.\n");
2207 return -1;
2208 }
2209
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002210 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002211 sample->cpu = 0;
2212 sample->time = 0;
Adrian Hunter53ff6bc32015-08-18 12:07:05 +03002213 sample->tid = event->fork.tid;
2214 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002215 }
Andi Kleene87e5482019-03-11 07:44:52 -07002216 if (!filter_cpu(sample)) {
2217 perf_sample__fprintf_start(sample, thread, evsel,
2218 PERF_RECORD_EXIT, stdout);
2219 perf_event__fprintf(event, stdout);
2220 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002221
2222 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002223 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002224
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002225 thread__put(thread);
2226 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002227}
2228
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002229static int process_mmap_event(struct perf_tool *tool,
2230 union perf_event *event,
2231 struct perf_sample *sample,
2232 struct machine *machine)
2233{
2234 struct thread *thread;
2235 struct perf_script *script = container_of(tool, struct perf_script, tool);
2236 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002237 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002238
2239 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2240 return -1;
2241
2242 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2243 if (thread == NULL) {
2244 pr_debug("problem processing MMAP event, skipping it.\n");
2245 return -1;
2246 }
2247
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002248 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002249 sample->cpu = 0;
2250 sample->time = 0;
2251 sample->tid = event->mmap.tid;
2252 sample->pid = event->mmap.pid;
2253 }
Andi Kleene87e5482019-03-11 07:44:52 -07002254 if (!filter_cpu(sample)) {
2255 perf_sample__fprintf_start(sample, thread, evsel,
2256 PERF_RECORD_MMAP, stdout);
2257 perf_event__fprintf(event, stdout);
2258 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002259 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002260 return 0;
2261}
2262
2263static int process_mmap2_event(struct perf_tool *tool,
2264 union perf_event *event,
2265 struct perf_sample *sample,
2266 struct machine *machine)
2267{
2268 struct thread *thread;
2269 struct perf_script *script = container_of(tool, struct perf_script, tool);
2270 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002271 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002272
2273 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2274 return -1;
2275
2276 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2277 if (thread == NULL) {
2278 pr_debug("problem processing MMAP2 event, skipping it.\n");
2279 return -1;
2280 }
2281
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002282 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002283 sample->cpu = 0;
2284 sample->time = 0;
2285 sample->tid = event->mmap2.tid;
2286 sample->pid = event->mmap2.pid;
2287 }
Andi Kleene87e5482019-03-11 07:44:52 -07002288 if (!filter_cpu(sample)) {
2289 perf_sample__fprintf_start(sample, thread, evsel,
2290 PERF_RECORD_MMAP2, stdout);
2291 perf_event__fprintf(event, stdout);
2292 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002293 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002294 return 0;
2295}
2296
Adrian Hunter7c148982015-07-21 12:44:06 +03002297static int process_switch_event(struct perf_tool *tool,
2298 union perf_event *event,
2299 struct perf_sample *sample,
2300 struct machine *machine)
2301{
2302 struct thread *thread;
2303 struct perf_script *script = container_of(tool, struct perf_script, tool);
2304 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002305 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Adrian Hunter7c148982015-07-21 12:44:06 +03002306
2307 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2308 return -1;
2309
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002310 if (scripting_ops && scripting_ops->process_switch)
2311 scripting_ops->process_switch(event, sample, machine);
2312
2313 if (!script->show_switch_events)
2314 return 0;
2315
Adrian Hunter7c148982015-07-21 12:44:06 +03002316 thread = machine__findnew_thread(machine, sample->pid,
2317 sample->tid);
2318 if (thread == NULL) {
2319 pr_debug("problem processing SWITCH event, skipping it.\n");
2320 return -1;
2321 }
2322
Andi Kleene87e5482019-03-11 07:44:52 -07002323 if (!filter_cpu(sample)) {
2324 perf_sample__fprintf_start(sample, thread, evsel,
2325 PERF_RECORD_SWITCH, stdout);
2326 perf_event__fprintf(event, stdout);
2327 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002328 thread__put(thread);
2329 return 0;
2330}
2331
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002332static int
2333process_lost_event(struct perf_tool *tool,
2334 union perf_event *event,
2335 struct perf_sample *sample,
2336 struct machine *machine)
2337{
2338 struct perf_script *script = container_of(tool, struct perf_script, tool);
2339 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002340 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002341 struct thread *thread;
2342
2343 thread = machine__findnew_thread(machine, sample->pid,
2344 sample->tid);
2345 if (thread == NULL)
2346 return -1;
2347
Andi Kleene87e5482019-03-11 07:44:52 -07002348 if (!filter_cpu(sample)) {
2349 perf_sample__fprintf_start(sample, thread, evsel,
2350 PERF_RECORD_LOST, stdout);
2351 perf_event__fprintf(event, stdout);
2352 }
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002353 thread__put(thread);
2354 return 0;
2355}
2356
Jiri Olsa3233b372018-02-06 19:17:59 +01002357static int
2358process_finished_round_event(struct perf_tool *tool __maybe_unused,
2359 union perf_event *event,
2360 struct ordered_events *oe __maybe_unused)
2361
2362{
2363 perf_event__fprintf(event, stdout);
2364 return 0;
2365}
2366
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002367static int
2368process_bpf_events(struct perf_tool *tool __maybe_unused,
2369 union perf_event *event,
2370 struct perf_sample *sample,
2371 struct machine *machine)
2372{
2373 struct thread *thread;
2374 struct perf_script *script = container_of(tool, struct perf_script, tool);
2375 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002376 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002377
2378 if (machine__process_ksymbol(machine, event, sample) < 0)
2379 return -1;
2380
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002381 if (!evsel->core.attr.sample_id_all) {
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002382 perf_event__fprintf(event, stdout);
2383 return 0;
2384 }
2385
2386 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2387 if (thread == NULL) {
2388 pr_debug("problem processing MMAP event, skipping it.\n");
2389 return -1;
2390 }
2391
2392 if (!filter_cpu(sample)) {
2393 perf_sample__fprintf_start(sample, thread, evsel,
2394 event->header.type, stdout);
2395 perf_event__fprintf(event, stdout);
2396 }
2397
2398 thread__put(thread);
2399 return 0;
2400}
2401
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002402static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002403{
2404 session_done = 1;
2405}
2406
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002407static void perf_script__fclose_per_event_dump(struct perf_script *script)
2408{
Jiri Olsa63503db2019-07-21 13:23:52 +02002409 struct evlist *evlist = script->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002410 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002411
2412 evlist__for_each_entry(evlist, evsel) {
2413 if (!evsel->priv)
2414 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002415 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002416 evsel->priv = NULL;
2417 }
2418}
2419
2420static int perf_script__fopen_per_event_dump(struct perf_script *script)
2421{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002422 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002423
2424 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002425 /*
2426 * Already setup? I.e. we may be called twice in cases like
2427 * Intel PT, one for the intel_pt// and dummy events, then
2428 * for the evsels syntheized from the auxtrace info.
2429 *
2430 * Ses perf_script__process_auxtrace_info.
2431 */
2432 if (evsel->priv != NULL)
2433 continue;
2434
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002435 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002436 if (evsel->priv == NULL)
2437 goto out_err_fclose;
2438 }
2439
2440 return 0;
2441
2442out_err_fclose:
2443 perf_script__fclose_per_event_dump(script);
2444 return -1;
2445}
2446
2447static int perf_script__setup_per_event_dump(struct perf_script *script)
2448{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002449 struct evsel *evsel;
2450 static struct evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002451
2452 if (script->per_event_dump)
2453 return perf_script__fopen_per_event_dump(script);
2454
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002455 es_stdout.fp = stdout;
2456
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002457 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002458 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002459
2460 return 0;
2461}
2462
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002463static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2464{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002465 struct evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002466
2467 evlist__for_each_entry(script->session->evlist, evsel) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002468 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002469
2470 perf_evsel_script__fprintf(es, stdout);
2471 perf_evsel_script__delete(es);
2472 evsel->priv = NULL;
2473 }
2474}
2475
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002476static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002477{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002478 int ret;
2479
Tom Zanussic239da32010-04-01 23:59:18 -05002480 signal(SIGINT, sig_handler);
2481
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002482 perf_stat__init_shadow_stats();
2483
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002484 /* override event processing functions */
2485 if (script->show_task_events) {
2486 script->tool.comm = process_comm_event;
2487 script->tool.fork = process_fork_event;
2488 script->tool.exit = process_exit_event;
2489 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002490 if (script->show_mmap_events) {
2491 script->tool.mmap = process_mmap_event;
2492 script->tool.mmap2 = process_mmap2_event;
2493 }
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002494 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
Adrian Hunter7c148982015-07-21 12:44:06 +03002495 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302496 if (script->show_namespace_events)
2497 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002498 if (script->show_lost_events)
2499 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002500 if (script->show_round_events) {
2501 script->tool.ordered_events = false;
2502 script->tool.finished_round = process_finished_round_event;
2503 }
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002504 if (script->show_bpf_events) {
2505 script->tool.ksymbol = process_bpf_events;
2506 script->tool.bpf_event = process_bpf_events;
2507 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002508
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002509 if (perf_script__setup_per_event_dump(script)) {
2510 pr_err("Couldn't create the per event dump files\n");
2511 return -1;
2512 }
2513
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002514 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002515
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002516 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002517 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002518
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002519 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002520 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002521
2522 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002523}
2524
Tom Zanussi956ffd02009-11-25 01:15:46 -06002525struct script_spec {
2526 struct list_head node;
2527 struct scripting_ops *ops;
2528 char spec[0];
2529};
2530
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002531static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002532
2533static struct script_spec *script_spec__new(const char *spec,
2534 struct scripting_ops *ops)
2535{
2536 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2537
2538 if (s != NULL) {
2539 strcpy(s->spec, spec);
2540 s->ops = ops;
2541 }
2542
2543 return s;
2544}
2545
Tom Zanussi956ffd02009-11-25 01:15:46 -06002546static void script_spec__add(struct script_spec *s)
2547{
2548 list_add_tail(&s->node, &script_specs);
2549}
2550
2551static struct script_spec *script_spec__find(const char *spec)
2552{
2553 struct script_spec *s;
2554
2555 list_for_each_entry(s, &script_specs, node)
2556 if (strcasecmp(s->spec, spec) == 0)
2557 return s;
2558 return NULL;
2559}
2560
Tom Zanussi956ffd02009-11-25 01:15:46 -06002561int script_spec_register(const char *spec, struct scripting_ops *ops)
2562{
2563 struct script_spec *s;
2564
2565 s = script_spec__find(spec);
2566 if (s)
2567 return -1;
2568
Taeung Song8560bae2016-02-26 00:13:10 +09002569 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002570 if (!s)
2571 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002572 else
2573 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002574
2575 return 0;
2576}
2577
2578static struct scripting_ops *script_spec__lookup(const char *spec)
2579{
2580 struct script_spec *s = script_spec__find(spec);
2581 if (!s)
2582 return NULL;
2583
2584 return s->ops;
2585}
2586
2587static void list_available_languages(void)
2588{
2589 struct script_spec *s;
2590
2591 fprintf(stderr, "\n");
2592 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002593 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002594
2595 list_for_each_entry(s, &script_specs, node)
2596 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2597
2598 fprintf(stderr, "\n");
2599}
2600
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002601static int parse_scriptname(const struct option *opt __maybe_unused,
2602 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002603{
2604 char spec[PATH_MAX];
2605 const char *script, *ext;
2606 int len;
2607
Tom Zanussif526d682010-01-27 02:27:52 -06002608 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002609 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002610 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002611 }
2612
2613 script = strchr(str, ':');
2614 if (script) {
2615 len = script - str;
2616 if (len >= PATH_MAX) {
2617 fprintf(stderr, "invalid language specifier");
2618 return -1;
2619 }
2620 strncpy(spec, str, len);
2621 spec[len] = '\0';
2622 scripting_ops = script_spec__lookup(spec);
2623 if (!scripting_ops) {
2624 fprintf(stderr, "invalid language specifier");
2625 return -1;
2626 }
2627 script++;
2628 } else {
2629 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002630 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002631 if (!ext) {
2632 fprintf(stderr, "invalid script extension");
2633 return -1;
2634 }
2635 scripting_ops = script_spec__lookup(++ext);
2636 if (!scripting_ops) {
2637 fprintf(stderr, "invalid script extension");
2638 return -1;
2639 }
2640 }
2641
2642 script_name = strdup(script);
2643
2644 return 0;
2645}
2646
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002647static int parse_output_fields(const struct option *opt __maybe_unused,
2648 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002649{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002650 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002651 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002652 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002653 int rc = 0;
2654 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002655 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002656 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002657
2658 if (!str)
2659 return -ENOMEM;
2660
David Ahern2c9e45f72011-03-17 10:03:21 -06002661 /* first word can state for which event type the user is specifying
2662 * the fields. If no type exists, the specified fields apply to all
2663 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002664 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002665 tok = strchr(str, ':');
2666 if (tok) {
2667 *tok = '\0';
2668 tok++;
2669 if (!strcmp(str, "hw"))
2670 type = PERF_TYPE_HARDWARE;
2671 else if (!strcmp(str, "sw"))
2672 type = PERF_TYPE_SOFTWARE;
2673 else if (!strcmp(str, "trace"))
2674 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002675 else if (!strcmp(str, "raw"))
2676 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002677 else if (!strcmp(str, "break"))
2678 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002679 else if (!strcmp(str, "synth"))
2680 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002681 else {
2682 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002683 rc = -EINVAL;
2684 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002685 }
2686
2687 if (output[type].user_set)
2688 pr_warning("Overriding previous field request for %s events.\n",
2689 event_type(type));
2690
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002691 /* Don't override defaults for +- */
2692 if (strchr(tok, '+') || strchr(tok, '-'))
2693 goto parse;
2694
David Ahern2c9e45f72011-03-17 10:03:21 -06002695 output[type].fields = 0;
2696 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002697 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002698
2699 } else {
2700 tok = str;
2701 if (strlen(str) == 0) {
2702 fprintf(stderr,
2703 "Cannot set fields to 'none' for all event types.\n");
2704 rc = -EINVAL;
2705 goto out;
2706 }
2707
Andi Kleen36ce5652017-06-02 08:48:10 -07002708 /* Don't override defaults for +- */
2709 if (strchr(str, '+') || strchr(str, '-'))
2710 goto parse;
2711
David Ahern2c9e45f72011-03-17 10:03:21 -06002712 if (output_set_by_user())
2713 pr_warning("Overriding previous field request for all events.\n");
2714
Adrian Hunter14057202017-06-21 13:17:19 +03002715 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002716 output[j].fields = 0;
2717 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002718 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002719 }
David Ahern1424dc92011-03-09 22:23:28 -07002720 }
2721
Andi Kleen36ce5652017-06-02 08:48:10 -07002722parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002723 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002724 if (*tok == '+') {
2725 if (change == SET)
2726 goto out_badmix;
2727 change = ADD;
2728 tok++;
2729 } else if (*tok == '-') {
2730 if (change == SET)
2731 goto out_badmix;
2732 change = REMOVE;
2733 tok++;
2734 } else {
2735 if (change != SET && change != DEFAULT)
2736 goto out_badmix;
2737 change = SET;
2738 }
2739
David Ahern745f43e2011-03-09 22:23:26 -07002740 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002741 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002742 break;
David Ahern745f43e2011-03-09 22:23:26 -07002743 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002744 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002745 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002746 continue;
2747 }
David Ahern745f43e2011-03-09 22:23:26 -07002748 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002749 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002750 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002751 goto out;
2752 }
2753
2754 if (type == -1) {
2755 /* add user option to all events types for
2756 * which it is valid
2757 */
Adrian Hunter14057202017-06-21 13:17:19 +03002758 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002759 if (output[j].invalid_fields & all_output_options[i].field) {
2760 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2761 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002762 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002763 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002764 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002765 output[j].user_set_fields &= ~all_output_options[i].field;
2766 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002767 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002768 output[j].user_set_fields |= all_output_options[i].field;
2769 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002770 output[j].user_set = true;
2771 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002772 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002773 }
2774 } else {
2775 if (output[type].invalid_fields & all_output_options[i].field) {
2776 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2777 all_output_options[i].str, event_type(type));
2778
2779 rc = -EINVAL;
2780 goto out;
2781 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002782 if (change == REMOVE)
2783 output[type].fields &= ~all_output_options[i].field;
2784 else
2785 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002786 output[type].user_set = true;
2787 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002788 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002789 }
2790
2791 if (type >= 0) {
2792 if (output[type].fields == 0) {
2793 pr_debug("No fields requested for %s type. "
2794 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002795 }
David Ahern1424dc92011-03-09 22:23:28 -07002796 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002797 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002798
Andi Kleen36ce5652017-06-02 08:48:10 -07002799out_badmix:
2800 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2801 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002802out:
David Ahern745f43e2011-03-09 22:23:26 -07002803 free(str);
2804 return rc;
2805}
2806
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002807#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2808 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2809 if ((lang_dirent->d_type == DT_DIR || \
2810 (lang_dirent->d_type == DT_UNKNOWN && \
2811 is_directory(scripts_path, lang_dirent))) && \
2812 (strcmp(lang_dirent->d_name, ".")) && \
2813 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002814
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002815#define for_each_script(lang_path, lang_dir, script_dirent) \
2816 while ((script_dirent = readdir(lang_dir)) != NULL) \
2817 if (script_dirent->d_type != DT_DIR && \
2818 (script_dirent->d_type != DT_UNKNOWN || \
2819 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002820
2821
2822#define RECORD_SUFFIX "-record"
2823#define REPORT_SUFFIX "-report"
2824
2825struct script_desc {
2826 struct list_head node;
2827 char *name;
2828 char *half_liner;
2829 char *args;
2830};
2831
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002832static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002833
2834static struct script_desc *script_desc__new(const char *name)
2835{
2836 struct script_desc *s = zalloc(sizeof(*s));
2837
Tom Zanussib5b87312010-11-10 08:16:51 -06002838 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002839 s->name = strdup(name);
2840
2841 return s;
2842}
2843
2844static void script_desc__delete(struct script_desc *s)
2845{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002846 zfree(&s->name);
2847 zfree(&s->half_liner);
2848 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002849 free(s);
2850}
2851
2852static void script_desc__add(struct script_desc *s)
2853{
2854 list_add_tail(&s->node, &script_descs);
2855}
2856
2857static struct script_desc *script_desc__find(const char *name)
2858{
2859 struct script_desc *s;
2860
2861 list_for_each_entry(s, &script_descs, node)
2862 if (strcasecmp(s->name, name) == 0)
2863 return s;
2864 return NULL;
2865}
2866
2867static struct script_desc *script_desc__findnew(const char *name)
2868{
2869 struct script_desc *s = script_desc__find(name);
2870
2871 if (s)
2872 return s;
2873
2874 s = script_desc__new(name);
2875 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002876 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002877
2878 script_desc__add(s);
2879
2880 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002881}
2882
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002883static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002884{
2885 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002886 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002887
2888 if (strlen(str) > suffix_len) {
2889 p = str + strlen(str) - suffix_len;
2890 if (!strncmp(p, suffix, suffix_len))
2891 return p;
2892 }
2893
2894 return NULL;
2895}
2896
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002897static int read_script_info(struct script_desc *desc, const char *filename)
2898{
2899 char line[BUFSIZ], *p;
2900 FILE *fp;
2901
2902 fp = fopen(filename, "r");
2903 if (!fp)
2904 return -1;
2905
2906 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002907 p = skip_spaces(line);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002908 if (strlen(p) == 0)
2909 continue;
2910 if (*p != '#')
2911 continue;
2912 p++;
2913 if (strlen(p) && *p == '!')
2914 continue;
2915
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002916 p = skip_spaces(p);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002917 if (strlen(p) && p[strlen(p) - 1] == '\n')
2918 p[strlen(p) - 1] = '\0';
2919
2920 if (!strncmp(p, "description:", strlen("description:"))) {
2921 p += strlen("description:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002922 desc->half_liner = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002923 continue;
2924 }
2925
2926 if (!strncmp(p, "args:", strlen("args:"))) {
2927 p += strlen("args:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002928 desc->args = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002929 continue;
2930 }
2931 }
2932
2933 fclose(fp);
2934
2935 return 0;
2936}
2937
Robert Richter38efb532011-11-25 11:38:40 +01002938static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2939{
2940 char *script_root, *str;
2941
2942 script_root = strdup(script_dirent->d_name);
2943 if (!script_root)
2944 return NULL;
2945
2946 str = (char *)ends_with(script_root, suffix);
2947 if (!str) {
2948 free(script_root);
2949 return NULL;
2950 }
2951
2952 *str = '\0';
2953 return script_root;
2954}
2955
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002956static int list_available_scripts(const struct option *opt __maybe_unused,
2957 const char *s __maybe_unused,
2958 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002959{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002960 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002961 char scripts_path[MAXPATHLEN];
2962 DIR *scripts_dir, *lang_dir;
2963 char script_path[MAXPATHLEN];
2964 char lang_path[MAXPATHLEN];
2965 struct script_desc *desc;
2966 char first_half[BUFSIZ];
2967 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002968
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002969 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002970
2971 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002972 if (!scripts_dir) {
2973 fprintf(stdout,
2974 "open(%s) failed.\n"
2975 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2976 scripts_path);
2977 exit(-1);
2978 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002979
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002980 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002981 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2982 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002983 lang_dir = opendir(lang_path);
2984 if (!lang_dir)
2985 continue;
2986
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002987 for_each_script(lang_path, lang_dir, script_dirent) {
2988 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002989 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002990 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002991 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2992 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002993 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002994 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002995 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002996 }
2997 }
2998
2999 fprintf(stdout, "List of available trace scripts:\n");
3000 list_for_each_entry(desc, &script_descs, node) {
3001 sprintf(first_half, "%s %s", desc->name,
3002 desc->args ? desc->args : "");
3003 fprintf(stdout, " %-36s %s\n", first_half,
3004 desc->half_liner ? desc->half_liner : "");
3005 }
3006
3007 exit(0);
3008}
3009
Feng Tange5f37052012-09-07 16:42:26 +08003010/*
Feng Tang49e639e2012-10-30 11:56:03 +08003011 * Some scripts specify the required events in their "xxx-record" file,
3012 * this function will check if the events in perf.data match those
3013 * mentioned in the "xxx-record".
3014 *
3015 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3016 * which is covered well now. And new parsing code should be added to
3017 * cover the future complexing formats like event groups etc.
3018 */
3019static int check_ev_match(char *dir_name, char *scriptname,
3020 struct perf_session *session)
3021{
3022 char filename[MAXPATHLEN], evname[128];
3023 char line[BUFSIZ], *p;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003024 struct evsel *pos;
Feng Tang49e639e2012-10-30 11:56:03 +08003025 int match, len;
3026 FILE *fp;
3027
Jiri Olsa77f18152018-03-19 09:29:01 +01003028 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08003029
3030 fp = fopen(filename, "r");
3031 if (!fp)
3032 return -1;
3033
3034 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003035 p = skip_spaces(line);
Feng Tang49e639e2012-10-30 11:56:03 +08003036 if (*p == '#')
3037 continue;
3038
3039 while (strlen(p)) {
3040 p = strstr(p, "-e");
3041 if (!p)
3042 break;
3043
3044 p += 2;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003045 p = skip_spaces(p);
Feng Tang49e639e2012-10-30 11:56:03 +08003046 len = strcspn(p, " \t");
3047 if (!len)
3048 break;
3049
3050 snprintf(evname, len + 1, "%s", p);
3051
3052 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003053 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08003054 if (!strcmp(perf_evsel__name(pos), evname)) {
3055 match = 1;
3056 break;
3057 }
3058 }
3059
3060 if (!match) {
3061 fclose(fp);
3062 return -1;
3063 }
3064 }
3065 }
3066
3067 fclose(fp);
3068 return 0;
3069}
3070
3071/*
Feng Tange5f37052012-09-07 16:42:26 +08003072 * Return -1 if none is found, otherwise the actual scripts number.
3073 *
3074 * Currently the only user of this function is the script browser, which
3075 * will list all statically runnable scripts, select one, execute it and
3076 * show the output in a perf browser.
3077 */
Andi Kleen905e4af2019-03-11 07:45:01 -07003078int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3079 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08003080{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003081 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08003082 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08003083 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08003084 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003085 struct perf_data data = {
Jiri Olsa2d4f27992019-02-21 10:41:30 +01003086 .path = input_name,
3087 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003088 };
Feng Tange5f37052012-09-07 16:42:26 +08003089 char *temp;
3090 int i = 0;
3091
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003092 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08003093 if (!session)
3094 return -1;
3095
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003096 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003097
3098 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003099 if (!scripts_dir) {
3100 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003101 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003102 }
Feng Tange5f37052012-09-07 16:42:26 +08003103
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003104 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003105 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3106 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003107#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003108 if (strstr(lang_path, "perl"))
3109 continue;
3110#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003111#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003112 if (strstr(lang_path, "python"))
3113 continue;
3114#endif
3115
3116 lang_dir = opendir(lang_path);
3117 if (!lang_dir)
3118 continue;
3119
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003120 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003121 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003122 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003123 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003124 if (i >= num)
3125 break;
3126 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3127 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003128 script_dirent->d_name);
3129 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003130 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003131 (temp - script_dirent->d_name) + 1,
3132 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003133
3134 if (check_ev_match(lang_path,
3135 scripts_array[i], session))
3136 continue;
3137
Feng Tange5f37052012-09-07 16:42:26 +08003138 i++;
3139 }
Feng Tang49e639e2012-10-30 11:56:03 +08003140 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003141 }
3142
Feng Tang49e639e2012-10-30 11:56:03 +08003143 closedir(scripts_dir);
3144 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003145 return i;
3146}
3147
Tom Zanussi38752942009-12-15 02:53:39 -06003148static char *get_script_path(const char *script_root, const char *suffix)
3149{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003150 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003151 char scripts_path[MAXPATHLEN];
3152 char script_path[MAXPATHLEN];
3153 DIR *scripts_dir, *lang_dir;
3154 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003155 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003156
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003157 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003158
3159 scripts_dir = opendir(scripts_path);
3160 if (!scripts_dir)
3161 return NULL;
3162
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003163 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003164 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3165 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003166 lang_dir = opendir(lang_path);
3167 if (!lang_dir)
3168 continue;
3169
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003170 for_each_script(lang_path, lang_dir, script_dirent) {
3171 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003172 if (__script_root && !strcmp(script_root, __script_root)) {
3173 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003174 closedir(lang_dir);
3175 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003176 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3177 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003178 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003179 }
3180 free(__script_root);
3181 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003182 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003183 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003184 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003185
Robert Richter38efb532011-11-25 11:38:40 +01003186 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003187}
3188
Tom Zanussib5b87312010-11-10 08:16:51 -06003189static bool is_top_script(const char *script_path)
3190{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003191 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003192}
3193
3194static int has_required_arg(char *script_path)
3195{
3196 struct script_desc *desc;
3197 int n_args = 0;
3198 char *p;
3199
3200 desc = script_desc__new(NULL);
3201
3202 if (read_script_info(desc, script_path))
3203 goto out;
3204
3205 if (!desc->args)
3206 goto out;
3207
3208 for (p = desc->args; *p; p++)
3209 if (*p == '<')
3210 n_args++;
3211out:
3212 script_desc__delete(desc);
3213
3214 return n_args;
3215}
3216
David Ahernd54b1a92012-08-26 12:24:46 -06003217static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003218{
3219 char **__argv = malloc(sizeof(const char *) * argc);
3220
David Ahernd54b1a92012-08-26 12:24:46 -06003221 if (!__argv) {
3222 pr_err("malloc failed\n");
3223 return -1;
3224 }
3225
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003226 memcpy(__argv, argv, sizeof(const char *) * argc);
3227 argc = parse_options(argc, (const char **)__argv, record_options,
3228 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3229 free(__argv);
3230
David Ahernd54b1a92012-08-26 12:24:46 -06003231 system_wide = (argc == 0);
3232
3233 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003234}
3235
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003236static void script__setup_sample_type(struct perf_script *script)
3237{
3238 struct perf_session *session = script->session;
3239 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3240
3241 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3242 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003243 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003244 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003245 dwarf_callchain_users = true;
3246 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003247 callchain_param.record_mode = CALLCHAIN_LBR;
3248 else
3249 callchain_param.record_mode = CALLCHAIN_FP;
3250 }
3251}
3252
Jiri Olsa89f16882018-09-13 14:54:03 +02003253static int process_stat_round_event(struct perf_session *session,
3254 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003255{
3256 struct stat_round_event *round = &event->stat_round;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003257 struct evsel *counter;
Jiri Olsae099eba2016-01-05 22:09:09 +01003258
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003259 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003260 perf_stat_process_counter(&stat_config, counter);
3261 process_stat(counter, round->time);
3262 }
3263
3264 process_stat_interval(round->time);
3265 return 0;
3266}
3267
Jiri Olsa89f16882018-09-13 14:54:03 +02003268static int process_stat_config_event(struct perf_session *session __maybe_unused,
3269 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003270{
3271 perf_event__read_stat_config(&stat_config, &event->stat_config);
3272 return 0;
3273}
3274
Jiri Olsacfc88742016-01-05 22:09:06 +01003275static int set_maps(struct perf_script *script)
3276{
Jiri Olsa63503db2019-07-21 13:23:52 +02003277 struct evlist *evlist = script->session->evlist;
Jiri Olsacfc88742016-01-05 22:09:06 +01003278
3279 if (!script->cpus || !script->threads)
3280 return 0;
3281
3282 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3283 return -EINVAL;
3284
Jiri Olsa453fa032019-07-21 13:24:43 +02003285 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
Jiri Olsacfc88742016-01-05 22:09:06 +01003286
3287 if (perf_evlist__alloc_stats(evlist, true))
3288 return -ENOMEM;
3289
3290 script->allocated = true;
3291 return 0;
3292}
3293
3294static
Jiri Olsa89f16882018-09-13 14:54:03 +02003295int process_thread_map_event(struct perf_session *session,
3296 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003297{
Jiri Olsa89f16882018-09-13 14:54:03 +02003298 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003299 struct perf_script *script = container_of(tool, struct perf_script, tool);
3300
3301 if (script->threads) {
3302 pr_warning("Extra thread map event, ignoring.\n");
3303 return 0;
3304 }
3305
3306 script->threads = thread_map__new_event(&event->thread_map);
3307 if (!script->threads)
3308 return -ENOMEM;
3309
3310 return set_maps(script);
3311}
3312
3313static
Jiri Olsa89f16882018-09-13 14:54:03 +02003314int process_cpu_map_event(struct perf_session *session,
3315 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003316{
Jiri Olsa89f16882018-09-13 14:54:03 +02003317 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003318 struct perf_script *script = container_of(tool, struct perf_script, tool);
3319
3320 if (script->cpus) {
3321 pr_warning("Extra cpu map event, ignoring.\n");
3322 return 0;
3323 }
3324
3325 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3326 if (!script->cpus)
3327 return -ENOMEM;
3328
3329 return set_maps(script);
3330}
3331
Jiri Olsa89f16882018-09-13 14:54:03 +02003332static int process_feature_event(struct perf_session *session,
3333 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303334{
3335 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003336 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303337 return 0;
3338}
3339
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003340#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003341static int perf_script__process_auxtrace_info(struct perf_session *session,
3342 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003343{
Jiri Olsa89f16882018-09-13 14:54:03 +02003344 struct perf_tool *tool = session->tool;
3345
3346 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003347
3348 if (ret == 0) {
3349 struct perf_script *script = container_of(tool, struct perf_script, tool);
3350
3351 ret = perf_script__setup_per_event_dump(script);
3352 }
3353
3354 return ret;
3355}
3356#else
3357#define perf_script__process_auxtrace_info 0
3358#endif
3359
Andi Kleenb585ebd2018-09-20 11:05:36 -07003360static int parse_insn_trace(const struct option *opt __maybe_unused,
3361 const char *str __maybe_unused,
3362 int unset __maybe_unused)
3363{
3364 parse_output_fields(NULL, "+insn,-event,-period", 0);
3365 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003366 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003367 return 0;
3368}
3369
3370static int parse_xed(const struct option *opt __maybe_unused,
3371 const char *str __maybe_unused,
3372 int unset __maybe_unused)
3373{
3374 force_pager("xed -F insn: -A -64 | less");
3375 return 0;
3376}
3377
Andi Kleend1b15522018-09-20 11:05:38 -07003378static int parse_call_trace(const struct option *opt __maybe_unused,
3379 const char *str __maybe_unused,
3380 int unset __maybe_unused)
3381{
3382 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3383 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003384 symbol_conf.nanosecs = true;
Jiri Olsa1c492422019-05-08 15:20:05 +02003385 symbol_conf.pad_output_len_dso = 50;
Andi Kleend1b15522018-09-20 11:05:38 -07003386 return 0;
3387}
3388
3389static int parse_callret_trace(const struct option *opt __maybe_unused,
3390 const char *str __maybe_unused,
3391 int unset __maybe_unused)
3392{
3393 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3394 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003395 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003396 return 0;
3397}
3398
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003399int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003400{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003401 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003402 bool header = false;
3403 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003404 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003405 char *rec_script_path = NULL;
3406 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003407 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003408 struct itrace_synth_opts itrace_synth_opts = {
3409 .set = false,
3410 .default_no_sample = true,
3411 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003412 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003413 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003414 const char **__argv;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03003415 const char *event_switch_on = NULL;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003416 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003417 struct perf_script script = {
3418 .tool = {
3419 .sample = process_sample_event,
3420 .mmap = perf_event__process_mmap,
3421 .mmap2 = perf_event__process_mmap2,
3422 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303423 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003424 .exit = perf_event__process_exit,
3425 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003426 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003427 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003428 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303429 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003430 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003431 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003432 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003433 .auxtrace = perf_event__process_auxtrace,
3434 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003435 .stat = perf_event__process_stat_event,
3436 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003437 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003438 .thread_map = process_thread_map_event,
3439 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003440 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003441 .ordering_requires_timestamps = true,
3442 },
3443 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003444 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003445 .mode = PERF_DATA_MODE_READ,
3446 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003447 const struct option options[] = {
3448 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3449 "dump raw trace in ASCII"),
3450 OPT_INCR('v', "verbose", &verbose,
3451 "be more verbose (show symbol address, etc)"),
3452 OPT_BOOLEAN('L', "Latency", &latency_format,
3453 "show latency attributes (irqs/preemption disabled, etc)"),
3454 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3455 list_available_scripts),
3456 OPT_CALLBACK('s', "script", NULL, "name",
3457 "script file name (lang:script name, script name, or *)",
3458 parse_scriptname),
3459 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3460 "generate perf-script.xx script in specified language"),
3461 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3462 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3463 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003464 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3465 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003466 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3467 "file", "vmlinux pathname"),
3468 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3469 "file", "kallsyms pathname"),
3470 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3471 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003472 OPT_CALLBACK(0, "symfs", NULL, "directory",
3473 "Look for files with symbols relative to this directory",
3474 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003475 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003476 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003477 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003478 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003479 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303480 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3481 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
Adrian Hunter68fb45b2019-05-20 14:37:14 +03003482 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003483 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003484 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3485 "system-wide collection from all CPUs"),
3486 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3487 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003488 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3489 "Decode instructions from itrace", parse_insn_trace),
3490 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3491 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003492 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3493 "Decode calls from from itrace", parse_call_trace),
3494 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3495 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003496 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3497 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003498 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3499 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003500 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3501 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3502 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003503 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3504 "only consider symbols in these pids"),
3505 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3506 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003507 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3508 "Set the maximum stack depth when parsing the callchain, "
3509 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003510 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Andi Kleen90b10f42019-03-14 15:50:00 -07003511 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003512 OPT_BOOLEAN('I', "show-info", &show_full_info,
3513 "display extended information from perf.data file"),
3514 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3515 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003516 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3517 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003518 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3519 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003520 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3521 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303522 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3523 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003524 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3525 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003526 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3527 "Show round events (if recorded)"),
Jiri Olsa490c8cc2019-05-08 15:20:08 +02003528 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3529 "Show bpf related events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003530 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3531 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003532 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003533 OPT_INTEGER(0, "max-blocks", &max_blocks,
3534 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003535 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003536 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003537 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003538 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003539 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003540 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3541 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003542 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3543 "Enable symbol demangling"),
3544 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3545 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003546 OPT_STRING(0, "time", &script.time_str, "str",
3547 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003548 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3549 "Show inline function"),
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003550 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3551 "guest mount directory under which every guest os"
3552 " instance has a subdir"),
3553 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3554 "file", "file saving guest os vmlinux"),
3555 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3556 "file", "file saving guest os /proc/kallsyms"),
3557 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3558 "file", "file saving guest os /proc/modules"),
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03003559 OPT_STRING(0, "switch-on", &event_switch_on,
3560 "event", "Consider events from the first ocurrence of this event"),
Arnaldo Carvalho de Melo6469eb62019-08-14 16:40:58 -03003561 OPT_BOOLEAN(0, "show-on-off-events", &script.evswitch.show_on_off_events,
3562 "Show the on/off switch events, used with --switch-on"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003563 OPT_END()
3564 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003565 const char * const script_subcommands[] = { "record", "report", NULL };
3566 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003567 "perf script [<options>]",
3568 "perf script [<options>] record <script> [<record-options>] <command>",
3569 "perf script [<options>] report <script> [script-args]",
3570 "perf script [<options>] <script> [<record-options>] <command>",
3571 "perf script [<options>] <top-script> [script-args]",
3572 NULL
3573 };
Tom Zanussi38752942009-12-15 02:53:39 -06003574
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003575 perf_set_singlethreaded();
3576
Tom Zanussib5b87312010-11-10 08:16:51 -06003577 setup_scripting();
3578
Yunlong Song40cae2b2015-03-18 21:35:54 +08003579 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003580 PARSE_OPT_STOP_AT_NON_OPTION);
3581
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003582 if (symbol_conf.guestmount ||
3583 symbol_conf.default_guest_vmlinux_name ||
3584 symbol_conf.default_guest_kallsyms ||
3585 symbol_conf.default_guest_modules) {
3586 /*
3587 * Enable guest sample processing.
3588 */
3589 perf_guest = true;
3590 }
3591
Jiri Olsa2d4f27992019-02-21 10:41:30 +01003592 data.path = input_name;
3593 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003594
Tom Zanussib5b87312010-11-10 08:16:51 -06003595 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3596 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3597 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003598 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003599 }
3600
Tom Zanussib5b87312010-11-10 08:16:51 -06003601 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3602 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3603 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003604 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003605 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003606 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003607 return -1;
3608 }
Tom Zanussi38752942009-12-15 02:53:39 -06003609 }
3610
Andi Kleen90b10f42019-03-14 15:50:00 -07003611 if (script.time_str && reltime) {
3612 fprintf(stderr, "Don't combine --reltime with --time\n");
3613 return -1;
3614 }
3615
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003616 if (itrace_synth_opts.callchain &&
3617 itrace_synth_opts.callchain_sz > scripting_max_stack)
3618 scripting_max_stack = itrace_synth_opts.callchain_sz;
3619
Ben Hutchings44e668c2010-10-10 16:10:03 +01003620 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003621 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003622
Tom Zanussib5b87312010-11-10 08:16:51 -06003623 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003624 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003625 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003626 pid_t pid;
3627
Tom Zanussib5b87312010-11-10 08:16:51 -06003628 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3629 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3630
3631 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003632 usage_with_options_msg(script_usage, options,
3633 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003634 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003635 }
3636
Tom Zanussib5b87312010-11-10 08:16:51 -06003637 if (is_top_script(argv[0])) {
3638 rep_args = argc - 1;
3639 } else {
3640 int rec_args;
3641
3642 rep_args = has_required_arg(rep_script_path);
3643 rec_args = (argc - 1) - rep_args;
3644 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003645 usage_with_options_msg(script_usage, options,
3646 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003647 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003648 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003649 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003650 }
3651
3652 if (pipe(live_pipe) < 0) {
3653 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003654 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003655 }
3656
3657 pid = fork();
3658 if (pid < 0) {
3659 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003660 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003661 }
3662
3663 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003664 j = 0;
3665
Tom Zanussia0cccc22010-04-01 23:59:25 -05003666 dup2(live_pipe[1], 1);
3667 close(live_pipe[0]);
3668
Robert Richter317df652011-11-25 15:05:25 +01003669 if (is_top_script(argv[0])) {
3670 system_wide = true;
3671 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003672 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3673 err = -1;
3674 goto out;
3675 }
Robert Richter317df652011-11-25 15:05:25 +01003676 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003677
3678 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003679 if (!__argv) {
3680 pr_err("malloc failed\n");
3681 err = -ENOMEM;
3682 goto out;
3683 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003684
Tom Zanussib5b87312010-11-10 08:16:51 -06003685 __argv[j++] = "/bin/sh";
3686 __argv[j++] = rec_script_path;
3687 if (system_wide)
3688 __argv[j++] = "-a";
3689 __argv[j++] = "-q";
3690 __argv[j++] = "-o";
3691 __argv[j++] = "-";
3692 for (i = rep_args + 1; i < argc; i++)
3693 __argv[j++] = argv[i];
3694 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003695
3696 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003697 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003698 exit(-1);
3699 }
3700
3701 dup2(live_pipe[0], 0);
3702 close(live_pipe[1]);
3703
Tom Zanussib5b87312010-11-10 08:16:51 -06003704 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003705 if (!__argv) {
3706 pr_err("malloc failed\n");
3707 err = -ENOMEM;
3708 goto out;
3709 }
3710
Tom Zanussib5b87312010-11-10 08:16:51 -06003711 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003712 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003713 __argv[j++] = rep_script_path;
3714 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003715 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003716 __argv[j++] = "-i";
3717 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003718 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003719
3720 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003721 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003722 exit(-1);
3723 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003724
Tom Zanussib5b87312010-11-10 08:16:51 -06003725 if (rec_script_path)
3726 script_path = rec_script_path;
3727 if (rep_script_path)
3728 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003729
Tom Zanussib5b87312010-11-10 08:16:51 -06003730 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003731 j = 0;
3732
Robert Richter317df652011-11-25 15:05:25 +01003733 if (!rec_script_path)
3734 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003735 else if (!system_wide) {
3736 if (have_cmd(argc - 1, &argv[1]) != 0) {
3737 err = -1;
3738 goto out;
3739 }
3740 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003741
3742 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003743 if (!__argv) {
3744 pr_err("malloc failed\n");
3745 err = -ENOMEM;
3746 goto out;
3747 }
3748
Tom Zanussib5b87312010-11-10 08:16:51 -06003749 __argv[j++] = "/bin/sh";
3750 __argv[j++] = script_path;
3751 if (system_wide)
3752 __argv[j++] = "-a";
3753 for (i = 2; i < argc; i++)
3754 __argv[j++] = argv[i];
3755 __argv[j++] = NULL;
3756
3757 execvp("/bin/sh", (char **)__argv);
3758 free(__argv);
3759 exit(-1);
3760 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003761
Milian Wolffc1c9b962018-10-21 21:14:23 +02003762 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003763 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003764 use_browser = 0;
3765 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003766
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003767 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003768 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003769 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003770
Jiri Olsae90debd2013-12-09 11:02:50 +01003771 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003772 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003773 perf_session__fprintf_info(session, stdout, show_full_info);
3774 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003775 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003776 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003777 if (show_full_info)
3778 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003779
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003780 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003781 goto out_delete;
3782
Andi Kleen3ab481a2019-03-05 06:47:45 -08003783 uname(&uts);
Song Liu9d491692019-06-20 18:44:38 -07003784 if (data.is_pipe || /* assume pipe_mode indicates native_arch */
3785 !strcmp(uts.machine, session->header.env.arch) ||
Andi Kleen3ab481a2019-03-05 06:47:45 -08003786 (!strcmp(uts.machine, "x86_64") &&
3787 !strcmp(session->header.env.arch, "i386")))
3788 native_arch = true;
3789
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003790 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003791 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003792
Andi Kleen99f753f2018-09-20 11:05:39 -07003793 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3794 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003795 itrace_synth_opts.thread_stack = true;
3796
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003797 session->itrace_synth_opts = &itrace_synth_opts;
3798
Anton Blanchard5d67be92011-07-04 21:57:50 +10003799 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003800 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3801 if (err < 0)
3802 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003803 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003804 }
3805
David Ahern1424dc92011-03-09 22:23:28 -07003806 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003807 symbol_conf.use_callchain = true;
3808 else
3809 symbol_conf.use_callchain = false;
3810
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003811 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003812 tep_set_function_resolver(session->tevent.pevent,
3813 machine__resolve_kernel_addr,
3814 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003815 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003816 err = -1;
3817 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003818 }
3819
Tom Zanussi956ffd02009-11-25 01:15:46 -06003820 if (generate_script_lang) {
3821 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003822 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003823
David Ahern2c9e45f72011-03-17 10:03:21 -06003824 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003825 fprintf(stderr,
3826 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003827 err = -EINVAL;
3828 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003829 }
3830
Jiri Olsa2d4f27992019-02-21 10:41:30 +01003831 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003832 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003833 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003834 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003835 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003836 }
3837
3838 err = fstat(input, &perf_stat);
3839 if (err < 0) {
3840 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003841 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003842 }
3843
3844 if (!perf_stat.st_size) {
3845 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003846 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003847 }
3848
3849 scripting_ops = script_spec__lookup(generate_script_lang);
3850 if (!scripting_ops) {
3851 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003852 err = -ENOENT;
3853 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003854 }
3855
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003856 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003857 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003858 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003859 }
3860
3861 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003862 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003863 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003864 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003865 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003866 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003867 }
3868
David Ahern9cbdb702011-04-06 21:54:20 -06003869
3870 err = perf_session__check_output_opt(session);
3871 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003872 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003873
Jin Yao284c4e12019-03-01 18:13:06 +08003874 if (script.time_str) {
3875 err = perf_time__parse_for_ranges(script.time_str, session,
3876 &script.ptime_range,
3877 &script.range_size,
3878 &script.range_num);
3879 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08003880 goto out_delete;
Adrian Hunter400ae982019-06-04 16:00:00 +03003881
3882 itrace_synth_opts__set_time_range(&itrace_synth_opts,
3883 script.ptime_range,
3884 script.range_num);
David Aherna91f4c42016-11-29 10:15:43 -07003885 }
3886
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03003887 if (event_switch_on) {
3888 script.evswitch.on = perf_evlist__find_evsel_by_str(session->evlist, event_switch_on);
3889 if (script.evswitch.on == NULL) {
3890 fprintf(stderr, "switch-on event not found (%s)\n", event_switch_on);
3891 err = -ENOENT;
3892 goto out_delete;
3893 }
3894 script.evswitch.discarding = true;
3895 }
3896
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003897 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003898
Adrian Hunterd445dd22014-08-15 22:08:37 +03003899 flush_scripting();
3900
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003901out_delete:
Adrian Hunter400ae982019-06-04 16:00:00 +03003902 if (script.ptime_range) {
3903 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
Jin Yao284c4e12019-03-01 18:13:06 +08003904 zfree(&script.ptime_range);
Adrian Hunter400ae982019-06-04 16:00:00 +03003905 }
Jin Yaocc2ef582018-01-10 23:00:33 +08003906
Jiri Olsacfc88742016-01-05 22:09:06 +01003907 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003908 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003909
3910 if (script_started)
3911 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003912out:
3913 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003914}