blob: 31a529ec139f39c68f5168c461597c09b1800b00 [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
Jiri Olsa809e9422015-11-26 18:55:21 +01001619struct perf_script {
1620 struct perf_tool tool;
1621 struct perf_session *session;
1622 bool show_task_events;
1623 bool show_mmap_events;
1624 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301625 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001626 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001627 bool show_round_events;
Jiri Olsa490c8cc2019-05-08 15:20:08 +02001628 bool show_bpf_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001629 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001630 bool per_event_dump;
Jiri Olsaf8548392019-07-21 13:23:49 +02001631 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +02001632 struct perf_thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001633 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001634 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001635 struct perf_time_interval *ptime_range;
1636 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001637 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001638};
1639
Jiri Olsa63503db2019-07-21 13:23:52 +02001640static int perf_evlist__max_name_len(struct evlist *evlist)
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001641{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001642 struct evsel *evsel;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001643 int max = 0;
1644
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001645 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001646 int len = strlen(perf_evsel__name(evsel));
1647
1648 max = MAX(len, max);
1649 }
1650
1651 return max;
1652}
1653
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001654static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001655{
1656 struct mem_info mi = { .data_src.val = data_src };
1657 char decode[100];
1658 char out[100];
1659 static int maxlen;
1660 int len;
1661
1662 perf_script__meminfo_scnprintf(decode, 100, &mi);
1663
1664 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1665 if (maxlen < len)
1666 maxlen = len;
1667
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001668 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001669}
1670
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001671struct metric_ctx {
1672 struct perf_sample *sample;
1673 struct thread *thread;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001674 struct evsel *evsel;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001675 FILE *fp;
1676};
1677
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001678static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1679 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001680 const char *fmt,
1681 const char *unit, double val)
1682{
1683 struct metric_ctx *mctx = ctx;
1684
1685 if (!fmt)
1686 return;
1687 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001688 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001689 fputs("\tmetric: ", mctx->fp);
1690 if (color)
1691 color_fprintf(mctx->fp, color, fmt, val);
1692 else
1693 printf(fmt, val);
1694 fprintf(mctx->fp, " %s\n", unit);
1695}
1696
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001697static void script_new_line(struct perf_stat_config *config __maybe_unused,
1698 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001699{
1700 struct metric_ctx *mctx = ctx;
1701
1702 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001703 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001704 fputs("\tmetric: ", mctx->fp);
1705}
1706
1707static void perf_sample__fprint_metric(struct perf_script *script,
1708 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001709 struct evsel *evsel,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001710 struct perf_sample *sample,
1711 FILE *fp)
1712{
1713 struct perf_stat_output_ctx ctx = {
1714 .print_metric = script_print_metric,
1715 .new_line = script_new_line,
1716 .ctx = &(struct metric_ctx) {
1717 .sample = sample,
1718 .thread = thread,
1719 .evsel = evsel,
1720 .fp = fp,
1721 },
1722 .force_header = false,
1723 };
Jiri Olsa32dcd022019-07-21 13:23:51 +02001724 struct evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001725 u64 val;
1726
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001727 if (!evsel->stats)
1728 perf_evlist__alloc_stats(script->session->evlist, false);
1729 if (evsel_script(evsel->leader)->gnum++ == 0)
1730 perf_stat__reset_shadow_stats();
1731 val = sample->period * evsel->scale;
1732 perf_stat__update_shadow_stats(evsel,
1733 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001734 sample->cpu,
1735 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001736 evsel_script(evsel)->val = val;
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001737 if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) {
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001738 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001739 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001740 evsel_script(ev2)->val,
1741 sample->cpu,
1742 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001743 NULL,
1744 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001745 }
1746 evsel_script(evsel->leader)->gnum = 0;
1747 }
1748}
1749
Andi Kleen99f753f2018-09-20 11:05:39 -07001750static bool show_event(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001751 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001752 struct thread *thread,
1753 struct addr_location *al)
1754{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001755 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001756
1757 if (!symbol_conf.graph_function)
1758 return true;
1759
1760 if (thread->filter) {
1761 if (depth <= thread->filter_entry_depth) {
1762 thread->filter = false;
1763 return false;
1764 }
1765 return true;
1766 } else {
1767 const char *s = symbol_conf.graph_function;
1768 u64 ip;
1769 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1770 &ip);
1771 unsigned nlen;
1772
1773 if (!name)
1774 return false;
1775 nlen = strlen(name);
1776 while (*s) {
1777 unsigned len = strcspn(s, ",");
1778 if (nlen == len && !strncmp(name, s, len)) {
1779 thread->filter = true;
1780 thread->filter_entry_depth = depth;
1781 return true;
1782 }
1783 s += len;
1784 if (*s == ',')
1785 s++;
1786 }
1787 return false;
1788 }
1789}
1790
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001791static void process_event(struct perf_script *script,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001792 struct perf_sample *sample, struct evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001793 struct addr_location *al,
1794 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001795{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001796 struct thread *thread = al->thread;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001797 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001798 unsigned int type = output_type(attr->type);
Jiri Olsa32dcd022019-07-21 13:23:51 +02001799 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001800 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001801
Adrian Hunter14057202017-06-21 13:17:19 +03001802 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001803 return;
1804
Andi Kleen99f753f2018-09-20 11:05:39 -07001805 if (!show_event(sample, evsel, thread, al))
1806 return;
1807
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001808 ++es->samples;
1809
Jiri Olsa28a0b392018-01-07 17:03:52 +01001810 perf_sample__fprintf_start(sample, thread, evsel,
1811 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001812
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001813 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001814 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001815
Namhyung Kime944d3d2013-11-18 14:34:52 +09001816 if (PRINT_FIELD(EVNAME)) {
1817 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001818
1819 if (!script->name_width)
1820 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1821
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001822 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001823 }
1824
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001825 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001826 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001827
Akihiro Nagai95582592012-01-30 13:43:09 +09001828 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001829 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001830 return;
1831 }
1832
Andi Kleen96167162019-01-17 11:48:34 -08001833 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001834 event_format__fprintf(evsel->tp_format, sample->cpu,
1835 sample->raw_data, sample->raw_size, fp);
1836 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001837
1838 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001839 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001840
David Ahern7cec0922011-05-30 13:08:23 -06001841 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001842 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001843
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001844 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001845 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001846
1847 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001848 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001849
David Ahern787bef12011-05-27 14:28:43 -06001850 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001851 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001852
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001853 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001854 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001855 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001856 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001857
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001858 fputc(cursor ? '\n' : ' ', fp);
1859 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001860 }
1861
Stephane Eranianfc36f942015-08-31 18:41:10 +02001862 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001863 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001864
Andi Kleenb1491ac2017-09-05 11:40:57 -07001865 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001866 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001867
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001868 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001869 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001870 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001871 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001872 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001873 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001874
Wang Nan30372f02016-02-24 11:20:45 +00001875 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001876 perf_sample__fprintf_bpf_output(sample, fp);
1877 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001878
1879 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001880 fprintf(fp, "%16" PRIx64, sample->phys_addr);
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001881
1882 perf_sample__fprintf_ipc(sample, attr, fp);
1883
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001884 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001885
Andi Kleendd2e18e2018-12-03 16:18:48 -08001886 if (PRINT_FIELD(SRCCODE)) {
1887 if (map__fprintf_srccode(al->map, al->addr, stdout,
1888 &thread->srccode_state))
1889 printf("\n");
1890 }
1891
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001892 if (PRINT_FIELD(METRIC))
1893 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001894
1895 if (verbose)
1896 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001897}
1898
Tom Zanussi956ffd02009-11-25 01:15:46 -06001899static struct scripting_ops *scripting_ops;
1900
Jiri Olsa32dcd022019-07-21 13:23:51 +02001901static void __process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsa36e33c52016-01-06 11:49:56 +01001902{
Jiri Olsaaf663bd2019-07-21 13:24:39 +02001903 int nthreads = thread_map__nr(counter->core.threads);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001904 int ncpus = perf_evsel__nr_cpus(counter);
1905 int cpu, thread;
1906 static int header_printed;
1907
1908 if (counter->system_wide)
1909 nthreads = 1;
1910
1911 if (!header_printed) {
1912 printf("%3s %8s %15s %15s %15s %15s %s\n",
1913 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1914 header_printed = 1;
1915 }
1916
1917 for (thread = 0; thread < nthreads; thread++) {
1918 for (cpu = 0; cpu < ncpus; cpu++) {
1919 struct perf_counts_values *counts;
1920
1921 counts = perf_counts(counter->counts, cpu, thread);
1922
1923 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
Jiri Olsad400bd32019-07-21 13:24:37 +02001924 counter->core.cpus->map[cpu],
Jiri Olsaaf663bd2019-07-21 13:24:39 +02001925 thread_map__pid(counter->core.threads, thread),
Jiri Olsa36e33c52016-01-06 11:49:56 +01001926 counts->val,
1927 counts->ena,
1928 counts->run,
1929 tstamp,
1930 perf_evsel__name(counter));
1931 }
1932 }
1933}
1934
Jiri Olsa32dcd022019-07-21 13:23:51 +02001935static void process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsae099eba2016-01-05 22:09:09 +01001936{
1937 if (scripting_ops && scripting_ops->process_stat)
1938 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001939 else
1940 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001941}
1942
1943static void process_stat_interval(u64 tstamp)
1944{
1945 if (scripting_ops && scripting_ops->process_stat_interval)
1946 scripting_ops->process_stat_interval(tstamp);
1947}
1948
Tom Zanussi956ffd02009-11-25 01:15:46 -06001949static void setup_scripting(void)
1950{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001951 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001952 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001953}
1954
Adrian Hunterd445dd22014-08-15 22:08:37 +03001955static int flush_scripting(void)
1956{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001957 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001958}
1959
Tom Zanussi956ffd02009-11-25 01:15:46 -06001960static int cleanup_scripting(void)
1961{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001962 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001963
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001964 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001965}
1966
Andi Kleene87e5482019-03-11 07:44:52 -07001967static bool filter_cpu(struct perf_sample *sample)
1968{
1969 if (cpu_list)
1970 return !test_bit(sample->cpu, cpu_bitmap);
1971 return false;
1972}
1973
Jiri Olsa809e9422015-11-26 18:55:21 +01001974static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001975 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001976 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001977 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001978 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001979{
Jiri Olsa809e9422015-11-26 18:55:21 +01001980 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001981 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001982
Jin Yao2ab046c2017-12-08 21:13:46 +08001983 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1984 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001985 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001986 }
David Aherna91f4c42016-11-29 10:15:43 -07001987
David Ahern1424dc92011-03-09 22:23:28 -07001988 if (debug_mode) {
1989 if (sample->time < last_timestamp) {
1990 pr_err("Samples misordered, previous: %" PRIu64
1991 " this: %" PRIu64 "\n", last_timestamp,
1992 sample->time);
1993 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001994 }
David Ahern1424dc92011-03-09 22:23:28 -07001995 last_timestamp = sample->time;
1996 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001997 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001998
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001999 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07002000 pr_err("problem processing %d event, skipping it.\n",
2001 event->header.type);
2002 return -1;
2003 }
2004
2005 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002006 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07002007
Andi Kleene87e5482019-03-11 07:44:52 -07002008 if (filter_cpu(sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002009 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002010
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002011 if (scripting_ops)
2012 scripting_ops->process_event(event, sample, evsel, &al);
2013 else
Andi Kleen48d02a12017-02-23 15:46:34 -08002014 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002015
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002016out_put:
2017 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002018 return 0;
2019}
2020
Adrian Hunter7ea95722013-11-01 15:51:30 +02002021static int process_attr(struct perf_tool *tool, union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02002022 struct evlist **pevlist)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002023{
2024 struct perf_script *scr = container_of(tool, struct perf_script, tool);
Jiri Olsa63503db2019-07-21 13:23:52 +02002025 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002026 struct evsel *evsel, *pos;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002027 int err;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002028 static struct evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002029
2030 err = perf_event__process_attr(tool, event, pevlist);
2031 if (err)
2032 return err;
2033
2034 evlist = *pevlist;
2035 evsel = perf_evlist__last(*pevlist);
2036
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302037 if (!evsel->priv) {
2038 if (scr->per_event_dump) {
2039 evsel->priv = perf_evsel_script__new(evsel,
2040 scr->session->data);
2041 } else {
2042 es = zalloc(sizeof(*es));
2043 if (!es)
2044 return -ENOMEM;
2045 es->fp = stdout;
2046 evsel->priv = es;
2047 }
2048 }
2049
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002050 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2051 evsel->core.attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002052 return 0;
2053
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002054 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002055 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002056 return 0;
2057 }
2058
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002059 set_print_ip_opts(&evsel->core.attr);
Adrian Hunter7ea95722013-11-01 15:51:30 +02002060
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002061 if (evsel->core.attr.sample_type)
Jiri Olsad2b5a312015-10-16 12:41:25 +02002062 err = perf_evsel__check_attr(evsel, scr->session);
2063
2064 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002065}
2066
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002067static int process_comm_event(struct perf_tool *tool,
2068 union perf_event *event,
2069 struct perf_sample *sample,
2070 struct machine *machine)
2071{
2072 struct thread *thread;
2073 struct perf_script *script = container_of(tool, struct perf_script, tool);
2074 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002075 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002076 int ret = -1;
2077
2078 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2079 if (thread == NULL) {
2080 pr_debug("problem processing COMM event, skipping it.\n");
2081 return -1;
2082 }
2083
2084 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2085 goto out;
2086
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002087 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002088 sample->cpu = 0;
2089 sample->time = 0;
2090 sample->tid = event->comm.tid;
2091 sample->pid = event->comm.pid;
2092 }
Andi Kleene87e5482019-03-11 07:44:52 -07002093 if (!filter_cpu(sample)) {
2094 perf_sample__fprintf_start(sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01002095 PERF_RECORD_COMM, stdout);
Andi Kleene87e5482019-03-11 07:44:52 -07002096 perf_event__fprintf(event, stdout);
2097 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002098 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002099out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002100 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002101 return ret;
2102}
2103
Hari Bathini96a44bb2017-03-08 02:12:06 +05302104static int process_namespaces_event(struct perf_tool *tool,
2105 union perf_event *event,
2106 struct perf_sample *sample,
2107 struct machine *machine)
2108{
2109 struct thread *thread;
2110 struct perf_script *script = container_of(tool, struct perf_script, tool);
2111 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002112 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302113 int ret = -1;
2114
2115 thread = machine__findnew_thread(machine, event->namespaces.pid,
2116 event->namespaces.tid);
2117 if (thread == NULL) {
2118 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2119 return -1;
2120 }
2121
2122 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2123 goto out;
2124
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002125 if (!evsel->core.attr.sample_id_all) {
Hari Bathini96a44bb2017-03-08 02:12:06 +05302126 sample->cpu = 0;
2127 sample->time = 0;
2128 sample->tid = event->namespaces.tid;
2129 sample->pid = event->namespaces.pid;
2130 }
Andi Kleene87e5482019-03-11 07:44:52 -07002131 if (!filter_cpu(sample)) {
2132 perf_sample__fprintf_start(sample, thread, evsel,
2133 PERF_RECORD_NAMESPACES, stdout);
2134 perf_event__fprintf(event, stdout);
2135 }
Hari Bathini96a44bb2017-03-08 02:12:06 +05302136 ret = 0;
2137out:
2138 thread__put(thread);
2139 return ret;
2140}
2141
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002142static int process_fork_event(struct perf_tool *tool,
2143 union perf_event *event,
2144 struct perf_sample *sample,
2145 struct machine *machine)
2146{
2147 struct thread *thread;
2148 struct perf_script *script = container_of(tool, struct perf_script, tool);
2149 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002150 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002151
2152 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2153 return -1;
2154
2155 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2156 if (thread == NULL) {
2157 pr_debug("problem processing FORK event, skipping it.\n");
2158 return -1;
2159 }
2160
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002161 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002162 sample->cpu = 0;
2163 sample->time = event->fork.time;
2164 sample->tid = event->fork.tid;
2165 sample->pid = event->fork.pid;
2166 }
Andi Kleene87e5482019-03-11 07:44:52 -07002167 if (!filter_cpu(sample)) {
2168 perf_sample__fprintf_start(sample, thread, evsel,
2169 PERF_RECORD_FORK, stdout);
2170 perf_event__fprintf(event, stdout);
2171 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002172 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002173
2174 return 0;
2175}
2176static int process_exit_event(struct perf_tool *tool,
2177 union perf_event *event,
2178 struct perf_sample *sample,
2179 struct machine *machine)
2180{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002181 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002182 struct thread *thread;
2183 struct perf_script *script = container_of(tool, struct perf_script, tool);
2184 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002185 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002186
2187 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2188 if (thread == NULL) {
2189 pr_debug("problem processing EXIT event, skipping it.\n");
2190 return -1;
2191 }
2192
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002193 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002194 sample->cpu = 0;
2195 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002196 sample->tid = event->fork.tid;
2197 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002198 }
Andi Kleene87e5482019-03-11 07:44:52 -07002199 if (!filter_cpu(sample)) {
2200 perf_sample__fprintf_start(sample, thread, evsel,
2201 PERF_RECORD_EXIT, stdout);
2202 perf_event__fprintf(event, stdout);
2203 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002204
2205 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002206 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002207
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002208 thread__put(thread);
2209 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002210}
2211
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002212static int process_mmap_event(struct perf_tool *tool,
2213 union perf_event *event,
2214 struct perf_sample *sample,
2215 struct machine *machine)
2216{
2217 struct thread *thread;
2218 struct perf_script *script = container_of(tool, struct perf_script, tool);
2219 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002220 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002221
2222 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2223 return -1;
2224
2225 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2226 if (thread == NULL) {
2227 pr_debug("problem processing MMAP event, skipping it.\n");
2228 return -1;
2229 }
2230
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002231 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002232 sample->cpu = 0;
2233 sample->time = 0;
2234 sample->tid = event->mmap.tid;
2235 sample->pid = event->mmap.pid;
2236 }
Andi Kleene87e5482019-03-11 07:44:52 -07002237 if (!filter_cpu(sample)) {
2238 perf_sample__fprintf_start(sample, thread, evsel,
2239 PERF_RECORD_MMAP, stdout);
2240 perf_event__fprintf(event, stdout);
2241 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002242 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002243 return 0;
2244}
2245
2246static int process_mmap2_event(struct perf_tool *tool,
2247 union perf_event *event,
2248 struct perf_sample *sample,
2249 struct machine *machine)
2250{
2251 struct thread *thread;
2252 struct perf_script *script = container_of(tool, struct perf_script, tool);
2253 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002254 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002255
2256 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2257 return -1;
2258
2259 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2260 if (thread == NULL) {
2261 pr_debug("problem processing MMAP2 event, skipping it.\n");
2262 return -1;
2263 }
2264
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002265 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002266 sample->cpu = 0;
2267 sample->time = 0;
2268 sample->tid = event->mmap2.tid;
2269 sample->pid = event->mmap2.pid;
2270 }
Andi Kleene87e5482019-03-11 07:44:52 -07002271 if (!filter_cpu(sample)) {
2272 perf_sample__fprintf_start(sample, thread, evsel,
2273 PERF_RECORD_MMAP2, stdout);
2274 perf_event__fprintf(event, stdout);
2275 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002276 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002277 return 0;
2278}
2279
Adrian Hunter7c148982015-07-21 12:44:06 +03002280static int process_switch_event(struct perf_tool *tool,
2281 union perf_event *event,
2282 struct perf_sample *sample,
2283 struct machine *machine)
2284{
2285 struct thread *thread;
2286 struct perf_script *script = container_of(tool, struct perf_script, tool);
2287 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002288 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Adrian Hunter7c148982015-07-21 12:44:06 +03002289
2290 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2291 return -1;
2292
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002293 if (scripting_ops && scripting_ops->process_switch)
2294 scripting_ops->process_switch(event, sample, machine);
2295
2296 if (!script->show_switch_events)
2297 return 0;
2298
Adrian Hunter7c148982015-07-21 12:44:06 +03002299 thread = machine__findnew_thread(machine, sample->pid,
2300 sample->tid);
2301 if (thread == NULL) {
2302 pr_debug("problem processing SWITCH event, skipping it.\n");
2303 return -1;
2304 }
2305
Andi Kleene87e5482019-03-11 07:44:52 -07002306 if (!filter_cpu(sample)) {
2307 perf_sample__fprintf_start(sample, thread, evsel,
2308 PERF_RECORD_SWITCH, stdout);
2309 perf_event__fprintf(event, stdout);
2310 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002311 thread__put(thread);
2312 return 0;
2313}
2314
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002315static int
2316process_lost_event(struct perf_tool *tool,
2317 union perf_event *event,
2318 struct perf_sample *sample,
2319 struct machine *machine)
2320{
2321 struct perf_script *script = container_of(tool, struct perf_script, tool);
2322 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002323 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002324 struct thread *thread;
2325
2326 thread = machine__findnew_thread(machine, sample->pid,
2327 sample->tid);
2328 if (thread == NULL)
2329 return -1;
2330
Andi Kleene87e5482019-03-11 07:44:52 -07002331 if (!filter_cpu(sample)) {
2332 perf_sample__fprintf_start(sample, thread, evsel,
2333 PERF_RECORD_LOST, stdout);
2334 perf_event__fprintf(event, stdout);
2335 }
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002336 thread__put(thread);
2337 return 0;
2338}
2339
Jiri Olsa3233b372018-02-06 19:17:59 +01002340static int
2341process_finished_round_event(struct perf_tool *tool __maybe_unused,
2342 union perf_event *event,
2343 struct ordered_events *oe __maybe_unused)
2344
2345{
2346 perf_event__fprintf(event, stdout);
2347 return 0;
2348}
2349
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002350static int
2351process_bpf_events(struct perf_tool *tool __maybe_unused,
2352 union perf_event *event,
2353 struct perf_sample *sample,
2354 struct machine *machine)
2355{
2356 struct thread *thread;
2357 struct perf_script *script = container_of(tool, struct perf_script, tool);
2358 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002359 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002360
2361 if (machine__process_ksymbol(machine, event, sample) < 0)
2362 return -1;
2363
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002364 if (!evsel->core.attr.sample_id_all) {
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002365 perf_event__fprintf(event, stdout);
2366 return 0;
2367 }
2368
2369 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2370 if (thread == NULL) {
2371 pr_debug("problem processing MMAP event, skipping it.\n");
2372 return -1;
2373 }
2374
2375 if (!filter_cpu(sample)) {
2376 perf_sample__fprintf_start(sample, thread, evsel,
2377 event->header.type, stdout);
2378 perf_event__fprintf(event, stdout);
2379 }
2380
2381 thread__put(thread);
2382 return 0;
2383}
2384
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002385static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002386{
2387 session_done = 1;
2388}
2389
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002390static void perf_script__fclose_per_event_dump(struct perf_script *script)
2391{
Jiri Olsa63503db2019-07-21 13:23:52 +02002392 struct evlist *evlist = script->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002393 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002394
2395 evlist__for_each_entry(evlist, evsel) {
2396 if (!evsel->priv)
2397 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002398 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002399 evsel->priv = NULL;
2400 }
2401}
2402
2403static int perf_script__fopen_per_event_dump(struct perf_script *script)
2404{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002405 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002406
2407 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002408 /*
2409 * Already setup? I.e. we may be called twice in cases like
2410 * Intel PT, one for the intel_pt// and dummy events, then
2411 * for the evsels syntheized from the auxtrace info.
2412 *
2413 * Ses perf_script__process_auxtrace_info.
2414 */
2415 if (evsel->priv != NULL)
2416 continue;
2417
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002418 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002419 if (evsel->priv == NULL)
2420 goto out_err_fclose;
2421 }
2422
2423 return 0;
2424
2425out_err_fclose:
2426 perf_script__fclose_per_event_dump(script);
2427 return -1;
2428}
2429
2430static int perf_script__setup_per_event_dump(struct perf_script *script)
2431{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002432 struct evsel *evsel;
2433 static struct evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002434
2435 if (script->per_event_dump)
2436 return perf_script__fopen_per_event_dump(script);
2437
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002438 es_stdout.fp = stdout;
2439
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002440 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002441 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002442
2443 return 0;
2444}
2445
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002446static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2447{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002448 struct evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002449
2450 evlist__for_each_entry(script->session->evlist, evsel) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002451 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002452
2453 perf_evsel_script__fprintf(es, stdout);
2454 perf_evsel_script__delete(es);
2455 evsel->priv = NULL;
2456 }
2457}
2458
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002459static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002460{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002461 int ret;
2462
Tom Zanussic239da32010-04-01 23:59:18 -05002463 signal(SIGINT, sig_handler);
2464
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002465 perf_stat__init_shadow_stats();
2466
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002467 /* override event processing functions */
2468 if (script->show_task_events) {
2469 script->tool.comm = process_comm_event;
2470 script->tool.fork = process_fork_event;
2471 script->tool.exit = process_exit_event;
2472 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002473 if (script->show_mmap_events) {
2474 script->tool.mmap = process_mmap_event;
2475 script->tool.mmap2 = process_mmap2_event;
2476 }
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002477 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
Adrian Hunter7c148982015-07-21 12:44:06 +03002478 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302479 if (script->show_namespace_events)
2480 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002481 if (script->show_lost_events)
2482 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002483 if (script->show_round_events) {
2484 script->tool.ordered_events = false;
2485 script->tool.finished_round = process_finished_round_event;
2486 }
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002487 if (script->show_bpf_events) {
2488 script->tool.ksymbol = process_bpf_events;
2489 script->tool.bpf_event = process_bpf_events;
2490 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002491
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002492 if (perf_script__setup_per_event_dump(script)) {
2493 pr_err("Couldn't create the per event dump files\n");
2494 return -1;
2495 }
2496
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002497 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002498
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002499 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002500 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002501
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002502 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002503 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002504
2505 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002506}
2507
Tom Zanussi956ffd02009-11-25 01:15:46 -06002508struct script_spec {
2509 struct list_head node;
2510 struct scripting_ops *ops;
2511 char spec[0];
2512};
2513
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002514static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002515
2516static struct script_spec *script_spec__new(const char *spec,
2517 struct scripting_ops *ops)
2518{
2519 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2520
2521 if (s != NULL) {
2522 strcpy(s->spec, spec);
2523 s->ops = ops;
2524 }
2525
2526 return s;
2527}
2528
Tom Zanussi956ffd02009-11-25 01:15:46 -06002529static void script_spec__add(struct script_spec *s)
2530{
2531 list_add_tail(&s->node, &script_specs);
2532}
2533
2534static struct script_spec *script_spec__find(const char *spec)
2535{
2536 struct script_spec *s;
2537
2538 list_for_each_entry(s, &script_specs, node)
2539 if (strcasecmp(s->spec, spec) == 0)
2540 return s;
2541 return NULL;
2542}
2543
Tom Zanussi956ffd02009-11-25 01:15:46 -06002544int script_spec_register(const char *spec, struct scripting_ops *ops)
2545{
2546 struct script_spec *s;
2547
2548 s = script_spec__find(spec);
2549 if (s)
2550 return -1;
2551
Taeung Song8560bae2016-02-26 00:13:10 +09002552 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002553 if (!s)
2554 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002555 else
2556 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002557
2558 return 0;
2559}
2560
2561static struct scripting_ops *script_spec__lookup(const char *spec)
2562{
2563 struct script_spec *s = script_spec__find(spec);
2564 if (!s)
2565 return NULL;
2566
2567 return s->ops;
2568}
2569
2570static void list_available_languages(void)
2571{
2572 struct script_spec *s;
2573
2574 fprintf(stderr, "\n");
2575 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002576 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002577
2578 list_for_each_entry(s, &script_specs, node)
2579 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2580
2581 fprintf(stderr, "\n");
2582}
2583
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002584static int parse_scriptname(const struct option *opt __maybe_unused,
2585 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002586{
2587 char spec[PATH_MAX];
2588 const char *script, *ext;
2589 int len;
2590
Tom Zanussif526d682010-01-27 02:27:52 -06002591 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002592 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002593 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002594 }
2595
2596 script = strchr(str, ':');
2597 if (script) {
2598 len = script - str;
2599 if (len >= PATH_MAX) {
2600 fprintf(stderr, "invalid language specifier");
2601 return -1;
2602 }
2603 strncpy(spec, str, len);
2604 spec[len] = '\0';
2605 scripting_ops = script_spec__lookup(spec);
2606 if (!scripting_ops) {
2607 fprintf(stderr, "invalid language specifier");
2608 return -1;
2609 }
2610 script++;
2611 } else {
2612 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002613 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002614 if (!ext) {
2615 fprintf(stderr, "invalid script extension");
2616 return -1;
2617 }
2618 scripting_ops = script_spec__lookup(++ext);
2619 if (!scripting_ops) {
2620 fprintf(stderr, "invalid script extension");
2621 return -1;
2622 }
2623 }
2624
2625 script_name = strdup(script);
2626
2627 return 0;
2628}
2629
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002630static int parse_output_fields(const struct option *opt __maybe_unused,
2631 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002632{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002633 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002634 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002635 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002636 int rc = 0;
2637 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002638 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002639 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002640
2641 if (!str)
2642 return -ENOMEM;
2643
David Ahern2c9e45f72011-03-17 10:03:21 -06002644 /* first word can state for which event type the user is specifying
2645 * the fields. If no type exists, the specified fields apply to all
2646 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002647 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002648 tok = strchr(str, ':');
2649 if (tok) {
2650 *tok = '\0';
2651 tok++;
2652 if (!strcmp(str, "hw"))
2653 type = PERF_TYPE_HARDWARE;
2654 else if (!strcmp(str, "sw"))
2655 type = PERF_TYPE_SOFTWARE;
2656 else if (!strcmp(str, "trace"))
2657 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002658 else if (!strcmp(str, "raw"))
2659 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002660 else if (!strcmp(str, "break"))
2661 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002662 else if (!strcmp(str, "synth"))
2663 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002664 else {
2665 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002666 rc = -EINVAL;
2667 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002668 }
2669
2670 if (output[type].user_set)
2671 pr_warning("Overriding previous field request for %s events.\n",
2672 event_type(type));
2673
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002674 /* Don't override defaults for +- */
2675 if (strchr(tok, '+') || strchr(tok, '-'))
2676 goto parse;
2677
David Ahern2c9e45f72011-03-17 10:03:21 -06002678 output[type].fields = 0;
2679 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002680 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002681
2682 } else {
2683 tok = str;
2684 if (strlen(str) == 0) {
2685 fprintf(stderr,
2686 "Cannot set fields to 'none' for all event types.\n");
2687 rc = -EINVAL;
2688 goto out;
2689 }
2690
Andi Kleen36ce5652017-06-02 08:48:10 -07002691 /* Don't override defaults for +- */
2692 if (strchr(str, '+') || strchr(str, '-'))
2693 goto parse;
2694
David Ahern2c9e45f72011-03-17 10:03:21 -06002695 if (output_set_by_user())
2696 pr_warning("Overriding previous field request for all events.\n");
2697
Adrian Hunter14057202017-06-21 13:17:19 +03002698 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002699 output[j].fields = 0;
2700 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002701 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002702 }
David Ahern1424dc92011-03-09 22:23:28 -07002703 }
2704
Andi Kleen36ce5652017-06-02 08:48:10 -07002705parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002706 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002707 if (*tok == '+') {
2708 if (change == SET)
2709 goto out_badmix;
2710 change = ADD;
2711 tok++;
2712 } else if (*tok == '-') {
2713 if (change == SET)
2714 goto out_badmix;
2715 change = REMOVE;
2716 tok++;
2717 } else {
2718 if (change != SET && change != DEFAULT)
2719 goto out_badmix;
2720 change = SET;
2721 }
2722
David Ahern745f43e2011-03-09 22:23:26 -07002723 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002724 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002725 break;
David Ahern745f43e2011-03-09 22:23:26 -07002726 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002727 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002728 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002729 continue;
2730 }
David Ahern745f43e2011-03-09 22:23:26 -07002731 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002732 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002733 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002734 goto out;
2735 }
2736
2737 if (type == -1) {
2738 /* add user option to all events types for
2739 * which it is valid
2740 */
Adrian Hunter14057202017-06-21 13:17:19 +03002741 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002742 if (output[j].invalid_fields & all_output_options[i].field) {
2743 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2744 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002745 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002746 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002747 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002748 output[j].user_set_fields &= ~all_output_options[i].field;
2749 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002750 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002751 output[j].user_set_fields |= all_output_options[i].field;
2752 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002753 output[j].user_set = true;
2754 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002755 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002756 }
2757 } else {
2758 if (output[type].invalid_fields & all_output_options[i].field) {
2759 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2760 all_output_options[i].str, event_type(type));
2761
2762 rc = -EINVAL;
2763 goto out;
2764 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002765 if (change == REMOVE)
2766 output[type].fields &= ~all_output_options[i].field;
2767 else
2768 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002769 output[type].user_set = true;
2770 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002771 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002772 }
2773
2774 if (type >= 0) {
2775 if (output[type].fields == 0) {
2776 pr_debug("No fields requested for %s type. "
2777 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002778 }
David Ahern1424dc92011-03-09 22:23:28 -07002779 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002780 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002781
Andi Kleen36ce5652017-06-02 08:48:10 -07002782out_badmix:
2783 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2784 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002785out:
David Ahern745f43e2011-03-09 22:23:26 -07002786 free(str);
2787 return rc;
2788}
2789
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002790#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2791 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2792 if ((lang_dirent->d_type == DT_DIR || \
2793 (lang_dirent->d_type == DT_UNKNOWN && \
2794 is_directory(scripts_path, lang_dirent))) && \
2795 (strcmp(lang_dirent->d_name, ".")) && \
2796 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002797
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002798#define for_each_script(lang_path, lang_dir, script_dirent) \
2799 while ((script_dirent = readdir(lang_dir)) != NULL) \
2800 if (script_dirent->d_type != DT_DIR && \
2801 (script_dirent->d_type != DT_UNKNOWN || \
2802 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002803
2804
2805#define RECORD_SUFFIX "-record"
2806#define REPORT_SUFFIX "-report"
2807
2808struct script_desc {
2809 struct list_head node;
2810 char *name;
2811 char *half_liner;
2812 char *args;
2813};
2814
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002815static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002816
2817static struct script_desc *script_desc__new(const char *name)
2818{
2819 struct script_desc *s = zalloc(sizeof(*s));
2820
Tom Zanussib5b87312010-11-10 08:16:51 -06002821 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002822 s->name = strdup(name);
2823
2824 return s;
2825}
2826
2827static void script_desc__delete(struct script_desc *s)
2828{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002829 zfree(&s->name);
2830 zfree(&s->half_liner);
2831 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002832 free(s);
2833}
2834
2835static void script_desc__add(struct script_desc *s)
2836{
2837 list_add_tail(&s->node, &script_descs);
2838}
2839
2840static struct script_desc *script_desc__find(const char *name)
2841{
2842 struct script_desc *s;
2843
2844 list_for_each_entry(s, &script_descs, node)
2845 if (strcasecmp(s->name, name) == 0)
2846 return s;
2847 return NULL;
2848}
2849
2850static struct script_desc *script_desc__findnew(const char *name)
2851{
2852 struct script_desc *s = script_desc__find(name);
2853
2854 if (s)
2855 return s;
2856
2857 s = script_desc__new(name);
2858 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002859 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002860
2861 script_desc__add(s);
2862
2863 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002864}
2865
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002866static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002867{
2868 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002869 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002870
2871 if (strlen(str) > suffix_len) {
2872 p = str + strlen(str) - suffix_len;
2873 if (!strncmp(p, suffix, suffix_len))
2874 return p;
2875 }
2876
2877 return NULL;
2878}
2879
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002880static int read_script_info(struct script_desc *desc, const char *filename)
2881{
2882 char line[BUFSIZ], *p;
2883 FILE *fp;
2884
2885 fp = fopen(filename, "r");
2886 if (!fp)
2887 return -1;
2888
2889 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002890 p = skip_spaces(line);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002891 if (strlen(p) == 0)
2892 continue;
2893 if (*p != '#')
2894 continue;
2895 p++;
2896 if (strlen(p) && *p == '!')
2897 continue;
2898
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002899 p = skip_spaces(p);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002900 if (strlen(p) && p[strlen(p) - 1] == '\n')
2901 p[strlen(p) - 1] = '\0';
2902
2903 if (!strncmp(p, "description:", strlen("description:"))) {
2904 p += strlen("description:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002905 desc->half_liner = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002906 continue;
2907 }
2908
2909 if (!strncmp(p, "args:", strlen("args:"))) {
2910 p += strlen("args:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002911 desc->args = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002912 continue;
2913 }
2914 }
2915
2916 fclose(fp);
2917
2918 return 0;
2919}
2920
Robert Richter38efb532011-11-25 11:38:40 +01002921static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2922{
2923 char *script_root, *str;
2924
2925 script_root = strdup(script_dirent->d_name);
2926 if (!script_root)
2927 return NULL;
2928
2929 str = (char *)ends_with(script_root, suffix);
2930 if (!str) {
2931 free(script_root);
2932 return NULL;
2933 }
2934
2935 *str = '\0';
2936 return script_root;
2937}
2938
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002939static int list_available_scripts(const struct option *opt __maybe_unused,
2940 const char *s __maybe_unused,
2941 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002942{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002943 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002944 char scripts_path[MAXPATHLEN];
2945 DIR *scripts_dir, *lang_dir;
2946 char script_path[MAXPATHLEN];
2947 char lang_path[MAXPATHLEN];
2948 struct script_desc *desc;
2949 char first_half[BUFSIZ];
2950 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002951
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002952 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002953
2954 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002955 if (!scripts_dir) {
2956 fprintf(stdout,
2957 "open(%s) failed.\n"
2958 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2959 scripts_path);
2960 exit(-1);
2961 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002962
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002963 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002964 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2965 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002966 lang_dir = opendir(lang_path);
2967 if (!lang_dir)
2968 continue;
2969
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002970 for_each_script(lang_path, lang_dir, script_dirent) {
2971 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002972 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002973 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002974 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2975 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002976 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002977 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002978 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002979 }
2980 }
2981
2982 fprintf(stdout, "List of available trace scripts:\n");
2983 list_for_each_entry(desc, &script_descs, node) {
2984 sprintf(first_half, "%s %s", desc->name,
2985 desc->args ? desc->args : "");
2986 fprintf(stdout, " %-36s %s\n", first_half,
2987 desc->half_liner ? desc->half_liner : "");
2988 }
2989
2990 exit(0);
2991}
2992
Feng Tange5f37052012-09-07 16:42:26 +08002993/*
Feng Tang49e639e2012-10-30 11:56:03 +08002994 * Some scripts specify the required events in their "xxx-record" file,
2995 * this function will check if the events in perf.data match those
2996 * mentioned in the "xxx-record".
2997 *
2998 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2999 * which is covered well now. And new parsing code should be added to
3000 * cover the future complexing formats like event groups etc.
3001 */
3002static int check_ev_match(char *dir_name, char *scriptname,
3003 struct perf_session *session)
3004{
3005 char filename[MAXPATHLEN], evname[128];
3006 char line[BUFSIZ], *p;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003007 struct evsel *pos;
Feng Tang49e639e2012-10-30 11:56:03 +08003008 int match, len;
3009 FILE *fp;
3010
Jiri Olsa77f18152018-03-19 09:29:01 +01003011 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08003012
3013 fp = fopen(filename, "r");
3014 if (!fp)
3015 return -1;
3016
3017 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003018 p = skip_spaces(line);
Feng Tang49e639e2012-10-30 11:56:03 +08003019 if (*p == '#')
3020 continue;
3021
3022 while (strlen(p)) {
3023 p = strstr(p, "-e");
3024 if (!p)
3025 break;
3026
3027 p += 2;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003028 p = skip_spaces(p);
Feng Tang49e639e2012-10-30 11:56:03 +08003029 len = strcspn(p, " \t");
3030 if (!len)
3031 break;
3032
3033 snprintf(evname, len + 1, "%s", p);
3034
3035 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003036 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08003037 if (!strcmp(perf_evsel__name(pos), evname)) {
3038 match = 1;
3039 break;
3040 }
3041 }
3042
3043 if (!match) {
3044 fclose(fp);
3045 return -1;
3046 }
3047 }
3048 }
3049
3050 fclose(fp);
3051 return 0;
3052}
3053
3054/*
Feng Tange5f37052012-09-07 16:42:26 +08003055 * Return -1 if none is found, otherwise the actual scripts number.
3056 *
3057 * Currently the only user of this function is the script browser, which
3058 * will list all statically runnable scripts, select one, execute it and
3059 * show the output in a perf browser.
3060 */
Andi Kleen905e4af2019-03-11 07:45:01 -07003061int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3062 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08003063{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003064 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08003065 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08003066 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08003067 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003068 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003069 .path = input_name,
3070 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003071 };
Feng Tange5f37052012-09-07 16:42:26 +08003072 char *temp;
3073 int i = 0;
3074
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003075 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08003076 if (!session)
3077 return -1;
3078
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003079 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003080
3081 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003082 if (!scripts_dir) {
3083 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003084 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003085 }
Feng Tange5f37052012-09-07 16:42:26 +08003086
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003087 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003088 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3089 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003090#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003091 if (strstr(lang_path, "perl"))
3092 continue;
3093#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003094#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003095 if (strstr(lang_path, "python"))
3096 continue;
3097#endif
3098
3099 lang_dir = opendir(lang_path);
3100 if (!lang_dir)
3101 continue;
3102
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003103 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003104 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003105 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003106 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003107 if (i >= num)
3108 break;
3109 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3110 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003111 script_dirent->d_name);
3112 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003113 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003114 (temp - script_dirent->d_name) + 1,
3115 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003116
3117 if (check_ev_match(lang_path,
3118 scripts_array[i], session))
3119 continue;
3120
Feng Tange5f37052012-09-07 16:42:26 +08003121 i++;
3122 }
Feng Tang49e639e2012-10-30 11:56:03 +08003123 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003124 }
3125
Feng Tang49e639e2012-10-30 11:56:03 +08003126 closedir(scripts_dir);
3127 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003128 return i;
3129}
3130
Tom Zanussi38752942009-12-15 02:53:39 -06003131static char *get_script_path(const char *script_root, const char *suffix)
3132{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003133 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003134 char scripts_path[MAXPATHLEN];
3135 char script_path[MAXPATHLEN];
3136 DIR *scripts_dir, *lang_dir;
3137 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003138 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003139
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003140 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003141
3142 scripts_dir = opendir(scripts_path);
3143 if (!scripts_dir)
3144 return NULL;
3145
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003146 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003147 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3148 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003149 lang_dir = opendir(lang_path);
3150 if (!lang_dir)
3151 continue;
3152
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003153 for_each_script(lang_path, lang_dir, script_dirent) {
3154 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003155 if (__script_root && !strcmp(script_root, __script_root)) {
3156 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003157 closedir(lang_dir);
3158 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003159 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3160 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003161 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003162 }
3163 free(__script_root);
3164 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003165 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003166 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003167 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003168
Robert Richter38efb532011-11-25 11:38:40 +01003169 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003170}
3171
Tom Zanussib5b87312010-11-10 08:16:51 -06003172static bool is_top_script(const char *script_path)
3173{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003174 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003175}
3176
3177static int has_required_arg(char *script_path)
3178{
3179 struct script_desc *desc;
3180 int n_args = 0;
3181 char *p;
3182
3183 desc = script_desc__new(NULL);
3184
3185 if (read_script_info(desc, script_path))
3186 goto out;
3187
3188 if (!desc->args)
3189 goto out;
3190
3191 for (p = desc->args; *p; p++)
3192 if (*p == '<')
3193 n_args++;
3194out:
3195 script_desc__delete(desc);
3196
3197 return n_args;
3198}
3199
David Ahernd54b1a92012-08-26 12:24:46 -06003200static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003201{
3202 char **__argv = malloc(sizeof(const char *) * argc);
3203
David Ahernd54b1a92012-08-26 12:24:46 -06003204 if (!__argv) {
3205 pr_err("malloc failed\n");
3206 return -1;
3207 }
3208
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003209 memcpy(__argv, argv, sizeof(const char *) * argc);
3210 argc = parse_options(argc, (const char **)__argv, record_options,
3211 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3212 free(__argv);
3213
David Ahernd54b1a92012-08-26 12:24:46 -06003214 system_wide = (argc == 0);
3215
3216 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003217}
3218
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003219static void script__setup_sample_type(struct perf_script *script)
3220{
3221 struct perf_session *session = script->session;
3222 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3223
3224 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3225 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003226 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003227 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003228 dwarf_callchain_users = true;
3229 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003230 callchain_param.record_mode = CALLCHAIN_LBR;
3231 else
3232 callchain_param.record_mode = CALLCHAIN_FP;
3233 }
3234}
3235
Jiri Olsa89f16882018-09-13 14:54:03 +02003236static int process_stat_round_event(struct perf_session *session,
3237 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003238{
3239 struct stat_round_event *round = &event->stat_round;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003240 struct evsel *counter;
Jiri Olsae099eba2016-01-05 22:09:09 +01003241
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003242 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003243 perf_stat_process_counter(&stat_config, counter);
3244 process_stat(counter, round->time);
3245 }
3246
3247 process_stat_interval(round->time);
3248 return 0;
3249}
3250
Jiri Olsa89f16882018-09-13 14:54:03 +02003251static int process_stat_config_event(struct perf_session *session __maybe_unused,
3252 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003253{
3254 perf_event__read_stat_config(&stat_config, &event->stat_config);
3255 return 0;
3256}
3257
Jiri Olsacfc88742016-01-05 22:09:06 +01003258static int set_maps(struct perf_script *script)
3259{
Jiri Olsa63503db2019-07-21 13:23:52 +02003260 struct evlist *evlist = script->session->evlist;
Jiri Olsacfc88742016-01-05 22:09:06 +01003261
3262 if (!script->cpus || !script->threads)
3263 return 0;
3264
3265 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3266 return -EINVAL;
3267
Jiri Olsa453fa032019-07-21 13:24:43 +02003268 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
Jiri Olsacfc88742016-01-05 22:09:06 +01003269
3270 if (perf_evlist__alloc_stats(evlist, true))
3271 return -ENOMEM;
3272
3273 script->allocated = true;
3274 return 0;
3275}
3276
3277static
Jiri Olsa89f16882018-09-13 14:54:03 +02003278int process_thread_map_event(struct perf_session *session,
3279 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003280{
Jiri Olsa89f16882018-09-13 14:54:03 +02003281 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003282 struct perf_script *script = container_of(tool, struct perf_script, tool);
3283
3284 if (script->threads) {
3285 pr_warning("Extra thread map event, ignoring.\n");
3286 return 0;
3287 }
3288
3289 script->threads = thread_map__new_event(&event->thread_map);
3290 if (!script->threads)
3291 return -ENOMEM;
3292
3293 return set_maps(script);
3294}
3295
3296static
Jiri Olsa89f16882018-09-13 14:54:03 +02003297int process_cpu_map_event(struct perf_session *session,
3298 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003299{
Jiri Olsa89f16882018-09-13 14:54:03 +02003300 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003301 struct perf_script *script = container_of(tool, struct perf_script, tool);
3302
3303 if (script->cpus) {
3304 pr_warning("Extra cpu map event, ignoring.\n");
3305 return 0;
3306 }
3307
3308 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3309 if (!script->cpus)
3310 return -ENOMEM;
3311
3312 return set_maps(script);
3313}
3314
Jiri Olsa89f16882018-09-13 14:54:03 +02003315static int process_feature_event(struct perf_session *session,
3316 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303317{
3318 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003319 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303320 return 0;
3321}
3322
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003323#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003324static int perf_script__process_auxtrace_info(struct perf_session *session,
3325 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003326{
Jiri Olsa89f16882018-09-13 14:54:03 +02003327 struct perf_tool *tool = session->tool;
3328
3329 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003330
3331 if (ret == 0) {
3332 struct perf_script *script = container_of(tool, struct perf_script, tool);
3333
3334 ret = perf_script__setup_per_event_dump(script);
3335 }
3336
3337 return ret;
3338}
3339#else
3340#define perf_script__process_auxtrace_info 0
3341#endif
3342
Andi Kleenb585ebd2018-09-20 11:05:36 -07003343static int parse_insn_trace(const struct option *opt __maybe_unused,
3344 const char *str __maybe_unused,
3345 int unset __maybe_unused)
3346{
3347 parse_output_fields(NULL, "+insn,-event,-period", 0);
3348 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003349 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003350 return 0;
3351}
3352
3353static int parse_xed(const struct option *opt __maybe_unused,
3354 const char *str __maybe_unused,
3355 int unset __maybe_unused)
3356{
3357 force_pager("xed -F insn: -A -64 | less");
3358 return 0;
3359}
3360
Andi Kleend1b15522018-09-20 11:05:38 -07003361static int parse_call_trace(const struct option *opt __maybe_unused,
3362 const char *str __maybe_unused,
3363 int unset __maybe_unused)
3364{
3365 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3366 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003367 symbol_conf.nanosecs = true;
Jiri Olsa1c492422019-05-08 15:20:05 +02003368 symbol_conf.pad_output_len_dso = 50;
Andi Kleend1b15522018-09-20 11:05:38 -07003369 return 0;
3370}
3371
3372static int parse_callret_trace(const struct option *opt __maybe_unused,
3373 const char *str __maybe_unused,
3374 int unset __maybe_unused)
3375{
3376 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3377 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003378 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003379 return 0;
3380}
3381
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003382int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003383{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003384 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003385 bool header = false;
3386 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003387 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003388 char *rec_script_path = NULL;
3389 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003390 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003391 struct itrace_synth_opts itrace_synth_opts = {
3392 .set = false,
3393 .default_no_sample = true,
3394 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003395 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003396 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003397 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003398 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003399 struct perf_script script = {
3400 .tool = {
3401 .sample = process_sample_event,
3402 .mmap = perf_event__process_mmap,
3403 .mmap2 = perf_event__process_mmap2,
3404 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303405 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003406 .exit = perf_event__process_exit,
3407 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003408 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003409 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003410 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303411 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003412 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003413 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003414 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003415 .auxtrace = perf_event__process_auxtrace,
3416 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003417 .stat = perf_event__process_stat_event,
3418 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003419 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003420 .thread_map = process_thread_map_event,
3421 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003422 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003423 .ordering_requires_timestamps = true,
3424 },
3425 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003426 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003427 .mode = PERF_DATA_MODE_READ,
3428 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003429 const struct option options[] = {
3430 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3431 "dump raw trace in ASCII"),
3432 OPT_INCR('v', "verbose", &verbose,
3433 "be more verbose (show symbol address, etc)"),
3434 OPT_BOOLEAN('L', "Latency", &latency_format,
3435 "show latency attributes (irqs/preemption disabled, etc)"),
3436 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3437 list_available_scripts),
3438 OPT_CALLBACK('s', "script", NULL, "name",
3439 "script file name (lang:script name, script name, or *)",
3440 parse_scriptname),
3441 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3442 "generate perf-script.xx script in specified language"),
3443 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3444 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3445 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003446 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3447 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003448 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3449 "file", "vmlinux pathname"),
3450 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3451 "file", "kallsyms pathname"),
3452 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3453 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003454 OPT_CALLBACK(0, "symfs", NULL, "directory",
3455 "Look for files with symbols relative to this directory",
3456 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003457 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003458 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003459 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003460 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003461 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303462 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3463 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
Adrian Hunter68fb45b2019-05-20 14:37:14 +03003464 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003465 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003466 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3467 "system-wide collection from all CPUs"),
3468 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3469 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003470 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3471 "Decode instructions from itrace", parse_insn_trace),
3472 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3473 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003474 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3475 "Decode calls from from itrace", parse_call_trace),
3476 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3477 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003478 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3479 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003480 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3481 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003482 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3483 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3484 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003485 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3486 "only consider symbols in these pids"),
3487 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3488 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003489 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3490 "Set the maximum stack depth when parsing the callchain, "
3491 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003492 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Andi Kleen90b10f42019-03-14 15:50:00 -07003493 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003494 OPT_BOOLEAN('I', "show-info", &show_full_info,
3495 "display extended information from perf.data file"),
3496 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3497 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003498 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3499 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003500 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3501 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003502 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3503 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303504 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3505 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003506 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3507 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003508 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3509 "Show round events (if recorded)"),
Jiri Olsa490c8cc2019-05-08 15:20:08 +02003510 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3511 "Show bpf related events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003512 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3513 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003514 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003515 OPT_INTEGER(0, "max-blocks", &max_blocks,
3516 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003517 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003518 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003519 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003520 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003521 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003522 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3523 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003524 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3525 "Enable symbol demangling"),
3526 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3527 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003528 OPT_STRING(0, "time", &script.time_str, "str",
3529 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003530 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3531 "Show inline function"),
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003532 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3533 "guest mount directory under which every guest os"
3534 " instance has a subdir"),
3535 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3536 "file", "file saving guest os vmlinux"),
3537 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3538 "file", "file saving guest os /proc/kallsyms"),
3539 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3540 "file", "file saving guest os /proc/modules"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003541 OPT_END()
3542 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003543 const char * const script_subcommands[] = { "record", "report", NULL };
3544 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003545 "perf script [<options>]",
3546 "perf script [<options>] record <script> [<record-options>] <command>",
3547 "perf script [<options>] report <script> [script-args]",
3548 "perf script [<options>] <script> [<record-options>] <command>",
3549 "perf script [<options>] <top-script> [script-args]",
3550 NULL
3551 };
Tom Zanussi38752942009-12-15 02:53:39 -06003552
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003553 perf_set_singlethreaded();
3554
Tom Zanussib5b87312010-11-10 08:16:51 -06003555 setup_scripting();
3556
Yunlong Song40cae2b2015-03-18 21:35:54 +08003557 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003558 PARSE_OPT_STOP_AT_NON_OPTION);
3559
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003560 if (symbol_conf.guestmount ||
3561 symbol_conf.default_guest_vmlinux_name ||
3562 symbol_conf.default_guest_kallsyms ||
3563 symbol_conf.default_guest_modules) {
3564 /*
3565 * Enable guest sample processing.
3566 */
3567 perf_guest = true;
3568 }
3569
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003570 data.path = input_name;
3571 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003572
Tom Zanussib5b87312010-11-10 08:16:51 -06003573 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3574 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3575 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003576 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003577 }
3578
Tom Zanussib5b87312010-11-10 08:16:51 -06003579 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3580 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3581 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003582 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003583 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003584 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003585 return -1;
3586 }
Tom Zanussi38752942009-12-15 02:53:39 -06003587 }
3588
Andi Kleen90b10f42019-03-14 15:50:00 -07003589 if (script.time_str && reltime) {
3590 fprintf(stderr, "Don't combine --reltime with --time\n");
3591 return -1;
3592 }
3593
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003594 if (itrace_synth_opts.callchain &&
3595 itrace_synth_opts.callchain_sz > scripting_max_stack)
3596 scripting_max_stack = itrace_synth_opts.callchain_sz;
3597
Ben Hutchings44e668c2010-10-10 16:10:03 +01003598 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003599 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003600
Tom Zanussib5b87312010-11-10 08:16:51 -06003601 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003602 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003603 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003604 pid_t pid;
3605
Tom Zanussib5b87312010-11-10 08:16:51 -06003606 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3607 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3608
3609 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003610 usage_with_options_msg(script_usage, options,
3611 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003612 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003613 }
3614
Tom Zanussib5b87312010-11-10 08:16:51 -06003615 if (is_top_script(argv[0])) {
3616 rep_args = argc - 1;
3617 } else {
3618 int rec_args;
3619
3620 rep_args = has_required_arg(rep_script_path);
3621 rec_args = (argc - 1) - rep_args;
3622 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003623 usage_with_options_msg(script_usage, options,
3624 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003625 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003626 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003627 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003628 }
3629
3630 if (pipe(live_pipe) < 0) {
3631 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003632 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003633 }
3634
3635 pid = fork();
3636 if (pid < 0) {
3637 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003638 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003639 }
3640
3641 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003642 j = 0;
3643
Tom Zanussia0cccc22010-04-01 23:59:25 -05003644 dup2(live_pipe[1], 1);
3645 close(live_pipe[0]);
3646
Robert Richter317df652011-11-25 15:05:25 +01003647 if (is_top_script(argv[0])) {
3648 system_wide = true;
3649 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003650 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3651 err = -1;
3652 goto out;
3653 }
Robert Richter317df652011-11-25 15:05:25 +01003654 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003655
3656 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003657 if (!__argv) {
3658 pr_err("malloc failed\n");
3659 err = -ENOMEM;
3660 goto out;
3661 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003662
Tom Zanussib5b87312010-11-10 08:16:51 -06003663 __argv[j++] = "/bin/sh";
3664 __argv[j++] = rec_script_path;
3665 if (system_wide)
3666 __argv[j++] = "-a";
3667 __argv[j++] = "-q";
3668 __argv[j++] = "-o";
3669 __argv[j++] = "-";
3670 for (i = rep_args + 1; i < argc; i++)
3671 __argv[j++] = argv[i];
3672 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003673
3674 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003675 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003676 exit(-1);
3677 }
3678
3679 dup2(live_pipe[0], 0);
3680 close(live_pipe[1]);
3681
Tom Zanussib5b87312010-11-10 08:16:51 -06003682 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003683 if (!__argv) {
3684 pr_err("malloc failed\n");
3685 err = -ENOMEM;
3686 goto out;
3687 }
3688
Tom Zanussib5b87312010-11-10 08:16:51 -06003689 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003690 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003691 __argv[j++] = rep_script_path;
3692 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003693 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003694 __argv[j++] = "-i";
3695 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003696 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003697
3698 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003699 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003700 exit(-1);
3701 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003702
Tom Zanussib5b87312010-11-10 08:16:51 -06003703 if (rec_script_path)
3704 script_path = rec_script_path;
3705 if (rep_script_path)
3706 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003707
Tom Zanussib5b87312010-11-10 08:16:51 -06003708 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003709 j = 0;
3710
Robert Richter317df652011-11-25 15:05:25 +01003711 if (!rec_script_path)
3712 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003713 else if (!system_wide) {
3714 if (have_cmd(argc - 1, &argv[1]) != 0) {
3715 err = -1;
3716 goto out;
3717 }
3718 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003719
3720 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003721 if (!__argv) {
3722 pr_err("malloc failed\n");
3723 err = -ENOMEM;
3724 goto out;
3725 }
3726
Tom Zanussib5b87312010-11-10 08:16:51 -06003727 __argv[j++] = "/bin/sh";
3728 __argv[j++] = script_path;
3729 if (system_wide)
3730 __argv[j++] = "-a";
3731 for (i = 2; i < argc; i++)
3732 __argv[j++] = argv[i];
3733 __argv[j++] = NULL;
3734
3735 execvp("/bin/sh", (char **)__argv);
3736 free(__argv);
3737 exit(-1);
3738 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003739
Milian Wolffc1c9b962018-10-21 21:14:23 +02003740 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003741 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003742 use_browser = 0;
3743 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003744
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003745 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003746 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003747 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003748
Jiri Olsae90debd2013-12-09 11:02:50 +01003749 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003750 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003751 perf_session__fprintf_info(session, stdout, show_full_info);
3752 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003753 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003754 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003755 if (show_full_info)
3756 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003757
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003758 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003759 goto out_delete;
3760
Andi Kleen3ab481a2019-03-05 06:47:45 -08003761 uname(&uts);
Song Liu9d491692019-06-20 18:44:38 -07003762 if (data.is_pipe || /* assume pipe_mode indicates native_arch */
3763 !strcmp(uts.machine, session->header.env.arch) ||
Andi Kleen3ab481a2019-03-05 06:47:45 -08003764 (!strcmp(uts.machine, "x86_64") &&
3765 !strcmp(session->header.env.arch, "i386")))
3766 native_arch = true;
3767
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003768 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003769 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003770
Andi Kleen99f753f2018-09-20 11:05:39 -07003771 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3772 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003773 itrace_synth_opts.thread_stack = true;
3774
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003775 session->itrace_synth_opts = &itrace_synth_opts;
3776
Anton Blanchard5d67be92011-07-04 21:57:50 +10003777 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003778 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3779 if (err < 0)
3780 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003781 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003782 }
3783
David Ahern1424dc92011-03-09 22:23:28 -07003784 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003785 symbol_conf.use_callchain = true;
3786 else
3787 symbol_conf.use_callchain = false;
3788
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003789 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003790 tep_set_function_resolver(session->tevent.pevent,
3791 machine__resolve_kernel_addr,
3792 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003793 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003794 err = -1;
3795 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003796 }
3797
Tom Zanussi956ffd02009-11-25 01:15:46 -06003798 if (generate_script_lang) {
3799 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003800 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003801
David Ahern2c9e45f72011-03-17 10:03:21 -06003802 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003803 fprintf(stderr,
3804 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003805 err = -EINVAL;
3806 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003807 }
3808
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003809 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003810 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003811 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003812 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003813 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003814 }
3815
3816 err = fstat(input, &perf_stat);
3817 if (err < 0) {
3818 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003819 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003820 }
3821
3822 if (!perf_stat.st_size) {
3823 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003824 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003825 }
3826
3827 scripting_ops = script_spec__lookup(generate_script_lang);
3828 if (!scripting_ops) {
3829 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003830 err = -ENOENT;
3831 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003832 }
3833
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003834 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003835 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003836 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003837 }
3838
3839 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003840 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003841 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003842 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003843 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003844 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003845 }
3846
David Ahern9cbdb702011-04-06 21:54:20 -06003847
3848 err = perf_session__check_output_opt(session);
3849 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003850 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003851
Jin Yao284c4e12019-03-01 18:13:06 +08003852 if (script.time_str) {
3853 err = perf_time__parse_for_ranges(script.time_str, session,
3854 &script.ptime_range,
3855 &script.range_size,
3856 &script.range_num);
3857 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08003858 goto out_delete;
Adrian Hunter400ae982019-06-04 16:00:00 +03003859
3860 itrace_synth_opts__set_time_range(&itrace_synth_opts,
3861 script.ptime_range,
3862 script.range_num);
David Aherna91f4c42016-11-29 10:15:43 -07003863 }
3864
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003865 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003866
Adrian Hunterd445dd22014-08-15 22:08:37 +03003867 flush_scripting();
3868
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003869out_delete:
Adrian Hunter400ae982019-06-04 16:00:00 +03003870 if (script.ptime_range) {
3871 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
Jin Yao284c4e12019-03-01 18:13:06 +08003872 zfree(&script.ptime_range);
Adrian Hunter400ae982019-06-04 16:00:00 +03003873 }
Jin Yaocc2ef582018-01-10 23:00:33 +08003874
Jiri Olsacfc88742016-01-05 22:09:06 +01003875 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003876 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003877
3878 if (script_started)
3879 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003880out:
3881 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003882}