blob: 6f389b33fbe5f9cd9da605fa13bdaf312ec7c663 [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
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02004#include "util/cache.h"
Arnaldo Carvalho de Melo0f31c012019-08-21 14:00:17 -03005#include "util/counts.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"
Arnaldo Carvalho de Melo97b9d862019-08-22 17:10:08 -030014#include "util/srcline.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020015#include "util/symbol.h"
16#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010017#include "util/trace-event.h"
David Ahern1424dc92011-03-09 22:23:28 -070018#include "util/evlist.h"
19#include "util/evsel.h"
Arnaldo Carvalho de Melod2360442019-08-15 10:37:24 -030020#include "util/evswitch.h"
Feng Tang36385be2012-09-07 16:42:24 +080021#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020022#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030023#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010024#include "util/cpumap.h"
25#include "util/thread_map.h"
26#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080027#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030028#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030029#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070030#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010031#include "util/path.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030032#include "print_binary.h"
Andi Kleen3ab481a2019-03-05 06:47:45 -080033#include "archinsn.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100034#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030035#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030036#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030037#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030038#include <linux/zalloc.h>
Andi Kleen3ab481a2019-03-05 06:47:45 -080039#include <sys/utsname.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010040#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010041#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080042#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030043#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030044#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030045#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030046#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030047#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030048#include <sys/types.h>
49#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030050#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030051#include <unistd.h>
Andi Kleenb585ebd2018-09-20 11:05:36 -070052#include <subcmd/pager.h>
Jiri Olsa453fa032019-07-21 13:24:43 +020053#include <perf/evlist.h>
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030054#include "util/record.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020055
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030056#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030057
Tom Zanussi956ffd02009-11-25 01:15:46 -060058static char const *script_name;
59static char const *generate_script_lang;
Andi Kleen90b10f42019-03-14 15:50:00 -070060static bool reltime;
61static u64 initial_time;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020062static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020063static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020064static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070065static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090066static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010067static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030068static bool print_flags;
Anton Blanchard5d67be92011-07-04 21:57:50 +100069static const char *cpu_list;
70static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010071static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080072static int max_blocks;
Andi Kleen3ab481a2019-03-05 06:47:45 -080073static bool native_arch;
Tom Zanussi956ffd02009-11-25 01:15:46 -060074
Adrian Hunter44cbe722015-09-25 16:15:50 +030075unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030076
David Ahern745f43e2011-03-09 22:23:26 -070077enum perf_output_field {
78 PERF_OUTPUT_COMM = 1U << 0,
79 PERF_OUTPUT_TID = 1U << 1,
80 PERF_OUTPUT_PID = 1U << 2,
81 PERF_OUTPUT_TIME = 1U << 3,
82 PERF_OUTPUT_CPU = 1U << 4,
83 PERF_OUTPUT_EVNAME = 1U << 5,
84 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060085 PERF_OUTPUT_IP = 1U << 7,
86 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060087 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060088 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090089 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020090 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020091 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020092 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020093 PERF_OUTPUT_BRSTACK = 1U << 15,
94 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010095 PERF_OUTPUT_DATA_SRC = 1U << 17,
96 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000097 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030098 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030099 PERF_OUTPUT_INSN = 1U << 21,
100 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -0800101 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -0700102 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +0300103 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -0400104 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -0700105 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800106 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100107 PERF_OUTPUT_MISC = 1U << 29,
Andi Kleendd2e18e2018-12-03 16:18:48 -0800108 PERF_OUTPUT_SRCCODE = 1U << 30,
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300109 PERF_OUTPUT_IPC = 1U << 31,
David Ahern745f43e2011-03-09 22:23:26 -0700110};
111
112struct output_option {
113 const char *str;
114 enum perf_output_field field;
115} all_output_options[] = {
116 {.str = "comm", .field = PERF_OUTPUT_COMM},
117 {.str = "tid", .field = PERF_OUTPUT_TID},
118 {.str = "pid", .field = PERF_OUTPUT_PID},
119 {.str = "time", .field = PERF_OUTPUT_TIME},
120 {.str = "cpu", .field = PERF_OUTPUT_CPU},
121 {.str = "event", .field = PERF_OUTPUT_EVNAME},
122 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600123 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700124 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600125 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600126 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900127 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200128 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200129 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200130 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700131 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200132 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
133 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100134 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
135 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000136 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300137 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300138 {.str = "insn", .field = PERF_OUTPUT_INSN},
139 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800140 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700141 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300142 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400143 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800144 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100145 {.str = "misc", .field = PERF_OUTPUT_MISC},
Andi Kleendd2e18e2018-12-03 16:18:48 -0800146 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300147 {.str = "ipc", .field = PERF_OUTPUT_IPC},
David Ahern745f43e2011-03-09 22:23:26 -0700148};
149
Adrian Hunter14057202017-06-21 13:17:19 +0300150enum {
151 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
152 OUTPUT_TYPE_MAX
153};
154
David Ahern745f43e2011-03-09 22:23:26 -0700155/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600156static struct {
157 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600158 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400159 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600160 u64 fields;
161 u64 invalid_fields;
Andi Kleen4b6ac812019-02-24 07:37:12 -0800162 u64 user_set_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300163} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700164
David Ahern2c9e45f72011-03-17 10:03:21 -0600165 [PERF_TYPE_HARDWARE] = {
166 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700167
David Ahern2c9e45f72011-03-17 10:03:21 -0600168 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
169 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600170 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530171 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
172 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600173
Wang Nan30372f02016-02-24 11:20:45 +0000174 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600175 },
176
177 [PERF_TYPE_SOFTWARE] = {
178 .user_set = false,
179
180 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
181 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600182 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530183 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
184 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
185 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600186
187 .invalid_fields = PERF_OUTPUT_TRACE,
188 },
189
190 [PERF_TYPE_TRACEPOINT] = {
191 .user_set = false,
192
193 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
194 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000195 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600196 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700197
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530198 [PERF_TYPE_HW_CACHE] = {
199 .user_set = false,
200
201 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
202 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
203 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
204 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
205 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
206
207 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
208 },
209
Arun Sharma0817a6a2011-04-14 10:38:18 -0700210 [PERF_TYPE_RAW] = {
211 .user_set = false,
212
213 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
214 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600215 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530216 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
217 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
218 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
219 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700220
Wang Nan30372f02016-02-24 11:20:45 +0000221 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700222 },
Wang Nan27cfef02015-12-08 02:25:43 +0000223
224 [PERF_TYPE_BREAKPOINT] = {
225 .user_set = false,
226
227 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
228 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
229 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530230 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
231 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000232
Wang Nan30372f02016-02-24 11:20:45 +0000233 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000234 },
Adrian Hunter14057202017-06-21 13:17:19 +0300235
236 [OUTPUT_TYPE_SYNTH] = {
237 .user_set = false,
238
239 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
240 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
241 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530242 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
243 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300244
245 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
246 },
David Ahern1424dc92011-03-09 22:23:28 -0700247};
David Ahern745f43e2011-03-09 22:23:26 -0700248
Jiri Olsa32dcd022019-07-21 13:23:51 +0200249struct evsel_script {
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300250 char *filename;
251 FILE *fp;
252 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800253 /* For metric output */
254 u64 val;
255 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300256};
257
Jiri Olsa32dcd022019-07-21 13:23:51 +0200258static inline struct evsel_script *evsel_script(struct evsel *evsel)
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800259{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200260 return (struct evsel_script *)evsel->priv;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800261}
262
Jiri Olsa32dcd022019-07-21 13:23:51 +0200263static struct evsel_script *perf_evsel_script__new(struct evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100264 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300265{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200266 struct evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300267
268 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100269 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300270 goto out_free;
271 es->fp = fopen(es->filename, "w");
272 if (es->fp == NULL)
273 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300274 }
275
276 return es;
277out_free_filename:
278 zfree(&es->filename);
279out_free:
280 free(es);
281 return NULL;
282}
283
Jiri Olsa32dcd022019-07-21 13:23:51 +0200284static void perf_evsel_script__delete(struct evsel_script *es)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300285{
286 zfree(&es->filename);
287 fclose(es->fp);
288 es->fp = NULL;
289 free(es);
290}
291
Jiri Olsa32dcd022019-07-21 13:23:51 +0200292static int perf_evsel_script__fprintf(struct evsel_script *es, FILE *fp)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300293{
294 struct stat st;
295
296 fstat(fileno(es->fp), &st);
297 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
298 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
299}
300
Adrian Hunter14057202017-06-21 13:17:19 +0300301static inline int output_type(unsigned int type)
302{
303 switch (type) {
304 case PERF_TYPE_SYNTH:
305 return OUTPUT_TYPE_SYNTH;
306 default:
307 return type;
308 }
309}
310
311static inline unsigned int attr_type(unsigned int type)
312{
313 switch (type) {
314 case OUTPUT_TYPE_SYNTH:
315 return PERF_TYPE_SYNTH;
316 default:
317 return type;
318 }
319}
320
David Ahern2c9e45f72011-03-17 10:03:21 -0600321static bool output_set_by_user(void)
322{
323 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300324 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600325 if (output[j].user_set)
326 return true;
327 }
328 return false;
329}
David Ahern745f43e2011-03-09 22:23:26 -0700330
David Ahern9cbdb702011-04-06 21:54:20 -0600331static const char *output_field2str(enum perf_output_field field)
332{
333 int i, imax = ARRAY_SIZE(all_output_options);
334 const char *str = "";
335
336 for (i = 0; i < imax; ++i) {
337 if (all_output_options[i].field == field) {
338 str = all_output_options[i].str;
339 break;
340 }
341 }
342 return str;
343}
344
Adrian Hunter14057202017-06-21 13:17:19 +0300345#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700346
Jiri Olsa32dcd022019-07-21 13:23:51 +0200347static int perf_evsel__do_check_stype(struct evsel *evsel,
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300348 u64 sample_type, const char *sample_msg,
349 enum perf_output_field field,
350 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700351{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200352 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300353 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600354 const char *evname;
355
356 if (attr->sample_type & sample_type)
357 return 0;
358
Andi Kleen4b6ac812019-02-24 07:37:12 -0800359 if (output[type].user_set_fields & field) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300360 if (allow_user_set)
361 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300362 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600363 pr_err("Samples for '%s' event do not have %s attribute set. "
364 "Cannot print '%s' field.\n",
365 evname, sample_msg, output_field2str(field));
366 return -1;
367 }
368
369 /* user did not ask for it explicitly so remove from the default list */
370 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300371 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600372 pr_debug("Samples for '%s' event do not have %s attribute set. "
373 "Skipping '%s' field.\n",
374 evname, sample_msg, output_field2str(field));
375
376 return 0;
377}
378
Jiri Olsa32dcd022019-07-21 13:23:51 +0200379static int perf_evsel__check_stype(struct evsel *evsel,
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300380 u64 sample_type, const char *sample_msg,
381 enum perf_output_field field)
382{
383 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
384 false);
385}
386
Jiri Olsa32dcd022019-07-21 13:23:51 +0200387static int perf_evsel__check_attr(struct evsel *evsel,
David Ahern9cbdb702011-04-06 21:54:20 -0600388 struct perf_session *session)
389{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200390 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300391 bool allow_user_set;
392
Jiri Olsae099eba2016-01-05 22:09:09 +0100393 if (perf_header__has_feat(&session->header, HEADER_STAT))
394 return 0;
395
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300396 allow_user_set = perf_header__has_feat(&session->header,
397 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600398
David Ahern1424dc92011-03-09 22:23:28 -0700399 if (PRINT_FIELD(TRACE) &&
400 !perf_session__has_traces(session, "record -R"))
401 return -EINVAL;
402
David Ahern787bef12011-05-27 14:28:43 -0600403 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300404 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
405 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700406 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700407 }
David Ahern7cec0922011-05-30 13:08:23 -0600408
409 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300410 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
411 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600412 return -EINVAL;
413
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100414 if (PRINT_FIELD(DATA_SRC) &&
415 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
416 PERF_OUTPUT_DATA_SRC))
417 return -EINVAL;
418
419 if (PRINT_FIELD(WEIGHT) &&
420 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
421 PERF_OUTPUT_WEIGHT))
422 return -EINVAL;
423
Andi Kleen37fed3d2018-09-18 05:32:09 -0700424 if (PRINT_FIELD(SYM) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200425 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600426 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700427 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600428 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600429 return -EINVAL;
430 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900431 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
432 pr_err("Display of offsets requested but symbol is not"
433 "selected.\n");
434 return -EINVAL;
435 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700436 if (PRINT_FIELD(DSO) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200437 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
Andi Kleen37fed3d2018-09-18 05:32:09 -0700438 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600439 return -EINVAL;
440 }
Andi Kleendd2e18e2018-12-03 16:18:48 -0800441 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
Adrian Huntercc8fae12013-12-06 09:42:57 +0200442 pr_err("Display of source line number requested but sample IP is not\n"
443 "selected. Hence, no address to lookup the source line number.\n");
444 return -EINVAL;
445 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800446 if (PRINT_FIELD(BRSTACKINSN) &&
447 !(perf_evlist__combined_branch_type(session->evlist) &
448 PERF_SAMPLE_BRANCH_ANY)) {
449 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
450 "Hint: run 'perf record -b ...'\n");
451 return -EINVAL;
452 }
David Ahern1424dc92011-03-09 22:23:28 -0700453 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300454 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
455 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700456 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700457
458 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300459 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
460 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700461 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700462
463 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300464 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
465 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700466 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600467
Stephane Eranianfc36f942015-08-31 18:41:10 +0200468 if (PRINT_FIELD(IREGS) &&
469 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
470 PERF_OUTPUT_IREGS))
471 return -EINVAL;
472
Andi Kleenb1491ac2017-09-05 11:40:57 -0700473 if (PRINT_FIELD(UREGS) &&
474 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
475 PERF_OUTPUT_UREGS))
476 return -EINVAL;
477
Kan Liang49d58f02017-08-29 13:11:11 -0400478 if (PRINT_FIELD(PHYS_ADDR) &&
479 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
480 PERF_OUTPUT_PHYS_ADDR))
481 return -EINVAL;
482
David Ahern9cbdb702011-04-06 21:54:20 -0600483 return 0;
484}
485
Adrian Hunter7ea95722013-11-01 15:51:30 +0200486static void set_print_ip_opts(struct perf_event_attr *attr)
487{
Adrian Hunter14057202017-06-21 13:17:19 +0300488 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200489
490 output[type].print_ip_opts = 0;
491 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300492 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200493
494 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300495 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200496
497 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300498 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200499
500 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300501 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200502
503 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300504 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200505}
506
David Ahern9cbdb702011-04-06 21:54:20 -0600507/*
508 * verify all user requested events exist and the samples
509 * have the expected data
510 */
511static int perf_session__check_output_opt(struct perf_session *session)
512{
He Kuang40f20e52016-05-16 04:51:19 +0000513 unsigned int j;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200514 struct evsel *evsel;
David Ahern9cbdb702011-04-06 21:54:20 -0600515
Adrian Hunter14057202017-06-21 13:17:19 +0300516 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
517 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600518
519 /*
520 * even if fields is set to 0 (ie., show nothing) event must
521 * exist if user explicitly includes it on the command line
522 */
Adrian Hunter14057202017-06-21 13:17:19 +0300523 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
524 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600525 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300526 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600527 event_type(j));
528 return -1;
529 }
530
531 if (evsel && output[j].fields &&
532 perf_evsel__check_attr(evsel, session))
533 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400534
535 if (evsel == NULL)
536 continue;
537
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200538 set_print_ip_opts(&evsel->core.attr);
David Ahern1424dc92011-03-09 22:23:28 -0700539 }
540
Adrian Hunter98526ee2014-07-31 09:00:59 +0300541 if (!no_callchain) {
542 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000543 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300544
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300545 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000546 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300547 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300548 use_callchain = true;
549 break;
550 }
551 }
He Kuang71ac8992016-08-04 11:25:43 +0000552 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300553 symbol_conf.use_callchain = false;
554 }
555
David Ahern80b8b492013-11-19 21:07:37 -0700556 /*
557 * set default for tracepoints to print symbols only
558 * if callchains are present
559 */
560 if (symbol_conf.use_callchain &&
561 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700562 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700563
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300564 evlist__for_each_entry(session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200565 if (evsel->core.attr.type != j)
He Kuang40f20e52016-05-16 04:51:19 +0000566 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700567
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300568 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000569 output[j].fields |= PERF_OUTPUT_IP;
570 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530571 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000572 output[j].fields |= PERF_OUTPUT_DSO;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200573 set_print_ip_opts(&evsel->core.attr);
He Kuang40f20e52016-05-16 04:51:19 +0000574 goto out;
575 }
David Ahern80b8b492013-11-19 21:07:37 -0700576 }
577 }
578
579out:
David Ahern1424dc92011-03-09 22:23:28 -0700580 return 0;
581}
David Ahern745f43e2011-03-09 22:23:26 -0700582
Milian Wolff9add8fe2018-11-07 23:34:37 +0100583static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
584 FILE *fp
585)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200586{
Andi Kleenb1491ac2017-09-05 11:40:57 -0700587 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300588 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700589
590 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300591 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700592
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300593 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700594
595 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
596 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300597 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700598 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300599
Milian Wolffb07d16f2018-11-07 10:37:05 +0100600 fprintf(fp, "\n");
601
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300602 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700603}
604
Milian Wolff9add8fe2018-11-07 23:34:37 +0100605static int perf_sample__fprintf_iregs(struct perf_sample *sample,
606 struct perf_event_attr *attr, FILE *fp)
607{
608 return perf_sample__fprintf_regs(&sample->intr_regs,
609 attr->sample_regs_intr, fp);
610}
611
612static int perf_sample__fprintf_uregs(struct perf_sample *sample,
613 struct perf_event_attr *attr, FILE *fp)
614{
615 return perf_sample__fprintf_regs(&sample->user_regs,
616 attr->sample_regs_user, fp);
617}
618
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300619static int perf_sample__fprintf_start(struct perf_sample *sample,
620 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200621 struct evsel *evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100622 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700623{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200624 struct perf_event_attr *attr = &evsel->core.attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700625 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700626 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300627 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700628
David Ahern745f43e2011-03-09 22:23:26 -0700629 if (PRINT_FIELD(COMM)) {
630 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300631 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300632 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300633 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700634 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300635 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700636 }
David Ahernc70c94b2011-03-09 22:23:25 -0700637
David Ahern745f43e2011-03-09 22:23:26 -0700638 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300639 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700640 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300641 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700642 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300643 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700644
David Ahern745f43e2011-03-09 22:23:26 -0700645 if (PRINT_FIELD(CPU)) {
646 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300647 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700648 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300649 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700650 }
David Ahernc70c94b2011-03-09 22:23:25 -0700651
Jiri Olsa28a0b392018-01-07 17:03:52 +0100652 if (PRINT_FIELD(MISC)) {
653 int ret = 0;
654
655 #define has(m) \
656 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
657
658 if (has(KERNEL))
659 ret += fprintf(fp, "K");
660 if (has(USER))
661 ret += fprintf(fp, "U");
662 if (has(HYPERVISOR))
663 ret += fprintf(fp, "H");
664 if (has(GUEST_KERNEL))
665 ret += fprintf(fp, "G");
666 if (has(GUEST_USER))
667 ret += fprintf(fp, "g");
668
669 switch (type) {
670 case PERF_RECORD_MMAP:
671 case PERF_RECORD_MMAP2:
672 if (has(MMAP_DATA))
673 ret += fprintf(fp, "M");
674 break;
675 case PERF_RECORD_COMM:
676 if (has(COMM_EXEC))
677 ret += fprintf(fp, "E");
678 break;
679 case PERF_RECORD_SWITCH:
680 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300681 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100682 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300683 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
684 ret += fprintf(fp, "p");
685 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100686 default:
687 break;
688 }
689
690 #undef has
691
692 ret += fprintf(fp, "%*s", 6 - ret, " ");
693 printed += ret;
694 }
695
David Ahern745f43e2011-03-09 22:23:26 -0700696 if (PRINT_FIELD(TIME)) {
Andi Kleen90b10f42019-03-14 15:50:00 -0700697 u64 t = sample->time;
698 if (reltime) {
699 if (!initial_time)
700 initial_time = sample->time;
701 t = sample->time - initial_time;
702 }
703 nsecs = t;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300704 secs = nsecs / NSEC_PER_SEC;
705 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900706
Andi Kleen52bab882019-03-05 06:47:47 -0800707 if (symbol_conf.nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300708 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900709 else {
710 char sample_time[32];
Andi Kleen90b10f42019-03-14 15:50:00 -0700711 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300712 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900713 }
David Ahern745f43e2011-03-09 22:23:26 -0700714 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300715
716 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700717}
718
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200719static inline char
720mispred_str(struct branch_entry *br)
721{
722 if (!(br->flags.mispred || br->flags.predicted))
723 return '-';
724
725 return br->flags.predicted ? 'P' : 'M';
726}
727
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300728static int perf_sample__fprintf_brstack(struct perf_sample *sample,
729 struct thread *thread,
730 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200731{
732 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700733 struct addr_location alf, alt;
734 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300735 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200736
737 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300738 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200739
740 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700741 from = br->entries[i].from;
742 to = br->entries[i].to;
743
744 if (PRINT_FIELD(DSO)) {
745 memset(&alf, 0, sizeof(alf));
746 memset(&alt, 0, sizeof(alt));
Adrian Hunter692d0e62018-11-06 23:07:12 +0200747 thread__find_map_fb(thread, sample->cpumode, from, &alf);
748 thread__find_map_fb(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700749 }
750
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300751 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700752 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300753 printed += fprintf(fp, "(");
754 printed += map__fprintf_dsoname(alf.map, fp);
755 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700756 }
757
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300758 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700759 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300760 printed += fprintf(fp, "(");
761 printed += map__fprintf_dsoname(alt.map, fp);
762 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700763 }
764
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300765 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200766 mispred_str( br->entries + i),
767 br->entries[i].flags.in_tx? 'X' : '-',
768 br->entries[i].flags.abort? 'A' : '-',
769 br->entries[i].flags.cycles);
770 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300771
772 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200773}
774
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300775static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
776 struct thread *thread,
777 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200778{
779 struct branch_stack *br = sample->branch_stack;
780 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200781 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300782 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200783
784 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300785 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200786
787 for (i = 0; i < br->nr; i++) {
788
789 memset(&alf, 0, sizeof(alf));
790 memset(&alt, 0, sizeof(alt));
791 from = br->entries[i].from;
792 to = br->entries[i].to;
793
Adrian Hunter692d0e62018-11-06 23:07:12 +0200794 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
795 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200796
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300797 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700798 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300799 printed += fprintf(fp, "(");
800 printed += map__fprintf_dsoname(alf.map, fp);
801 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700802 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300803 printed += fprintf(fp, "%c", '/');
804 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700805 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300806 printed += fprintf(fp, "(");
807 printed += map__fprintf_dsoname(alt.map, fp);
808 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700809 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300810 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200811 mispred_str( br->entries + i),
812 br->entries[i].flags.in_tx? 'X' : '-',
813 br->entries[i].flags.abort? 'A' : '-',
814 br->entries[i].flags.cycles);
815 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300816
817 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200818}
819
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300820static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
821 struct thread *thread,
822 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700823{
824 struct branch_stack *br = sample->branch_stack;
825 struct addr_location alf, alt;
826 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300827 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700828
829 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300830 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700831
832 for (i = 0; i < br->nr; i++) {
833
834 memset(&alf, 0, sizeof(alf));
835 memset(&alt, 0, sizeof(alt));
836 from = br->entries[i].from;
837 to = br->entries[i].to;
838
Adrian Hunter692d0e62018-11-06 23:07:12 +0200839 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300840 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700841 from = map__map_ip(alf.map, from);
842
Adrian Hunter692d0e62018-11-06 23:07:12 +0200843 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300844 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700845 to = map__map_ip(alt.map, to);
846
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300847 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700848 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300849 printed += fprintf(fp, "(");
850 printed += map__fprintf_dsoname(alf.map, fp);
851 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700852 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300853 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700854 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300855 printed += fprintf(fp, "(");
856 printed += map__fprintf_dsoname(alt.map, fp);
857 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700858 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300859 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700860 mispred_str(br->entries + i),
861 br->entries[i].flags.in_tx ? 'X' : '-',
862 br->entries[i].flags.abort ? 'A' : '-',
863 br->entries[i].flags.cycles);
864 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300865
866 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700867}
Andi Kleen48d02a12017-02-23 15:46:34 -0800868#define MAXBB 16384UL
869
870static int grab_bb(u8 *buffer, u64 start, u64 end,
871 struct machine *machine, struct thread *thread,
872 bool *is64bit, u8 *cpumode, bool last)
873{
874 long offset, len;
875 struct addr_location al;
876 bool kernel;
877
878 if (!start || !end)
879 return 0;
880
881 kernel = machine__kernel_ip(machine, start);
882 if (kernel)
883 *cpumode = PERF_RECORD_MISC_KERNEL;
884 else
885 *cpumode = PERF_RECORD_MISC_USER;
886
887 /*
888 * Block overlaps between kernel and user.
889 * This can happen due to ring filtering
890 * On Intel CPUs the entry into the kernel is filtered,
891 * but the exit is not. Let the caller patch it up.
892 */
893 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300894 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800895 return -ENXIO;
896 }
897
898 memset(&al, 0, sizeof(al));
899 if (end - start > MAXBB - MAXINSN) {
900 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300901 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800902 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300903 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800904 return 0;
905 }
906
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300907 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300908 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800909 return 0;
910 }
911 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300912 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800913 return 0;
914 }
915
916 /* Load maps to ensure dso->is_64_bit has been updated */
917 map__load(al.map);
918
919 offset = al.map->map_ip(al.map, start);
920 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
921 end - start + MAXINSN);
922
923 *is64bit = al.map->dso->is_64_bit;
924 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300925 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800926 start, end);
927 return len;
928}
929
Andi Kleendd2e18e2018-12-03 16:18:48 -0800930static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
931{
932 struct addr_location al;
933 int ret = 0;
934
935 memset(&al, 0, sizeof(al));
936 thread__find_map(thread, cpumode, addr, &al);
937 if (!al.map)
938 return 0;
939 ret = map__fprintf_srccode(al.map, al.addr, stdout,
940 &thread->srccode_state);
941 if (ret)
942 ret += printf("\n");
943 return ret;
944}
945
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300946static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
947 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -0700948 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -0800949{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300950 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
951 dump_insn(x, ip, inbuf, len, NULL),
952 en->flags.predicted ? " PRED" : "",
953 en->flags.mispred ? " MISPRED" : "",
954 en->flags.in_tx ? " INTX" : "",
955 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800956 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -0700957 *total_cycles += en->flags.cycles;
958 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800959 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300960 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800961 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300962 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800963}
964
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300965static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
966 u8 cpumode, int cpu, struct symbol **lastsym,
967 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800968{
969 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300970 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800971
972 memset(&al, 0, sizeof(al));
973
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300974 thread__find_map(thread, cpumode, addr, &al);
975
Andi Kleen48d02a12017-02-23 15:46:34 -0800976 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300977 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800978
979 al.cpu = cpu;
980 al.sym = NULL;
981 if (al.map)
982 al.sym = map__find_symbol(al.map, al.addr);
983
984 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300985 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800986
987 if (al.addr < al.sym->end)
988 off = al.addr - al.sym->start;
989 else
990 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300991 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800992 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300993 printed += fprintf(fp, "%+d", off);
994 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800995 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300996 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
997 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800998 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300999
1000 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001001}
1002
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001003static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1004 struct thread *thread,
1005 struct perf_event_attr *attr,
1006 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -08001007{
1008 struct branch_stack *br = sample->branch_stack;
1009 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001010 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001011 struct perf_insn x;
1012 u8 buffer[MAXBB];
1013 unsigned off;
1014 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -07001015 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001016
1017 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001018 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001019 nr = br->nr;
1020 if (max_blocks && nr > max_blocks + 1)
1021 nr = max_blocks + 1;
1022
1023 x.thread = thread;
1024 x.cpu = sample->cpu;
1025
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001026 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -08001027
1028 /* Handle first from jump, of which we don't know the entry. */
1029 len = grab_bb(buffer, br->entries[nr-1].from,
1030 br->entries[nr-1].from,
1031 machine, thread, &x.is64bit, &x.cpumode, false);
1032 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001033 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1034 x.cpumode, x.cpu, &lastsym, attr, fp);
1035 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001036 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001037 if (PRINT_FIELD(SRCCODE))
1038 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
Andi Kleen48d02a12017-02-23 15:46:34 -08001039 }
1040
1041 /* Print all blocks */
1042 for (i = nr - 2; i >= 0; i--) {
1043 if (br->entries[i].from || br->entries[i].to)
1044 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1045 br->entries[i].from,
1046 br->entries[i].to);
1047 start = br->entries[i + 1].to;
1048 end = br->entries[i].from;
1049
1050 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1051 /* Patch up missing kernel transfers due to ring filters */
1052 if (len == -ENXIO && i > 0) {
1053 end = br->entries[--i].from;
1054 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1055 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1056 }
1057 if (len <= 0)
1058 continue;
1059
1060 insn = 0;
1061 for (off = 0;; off += ilen) {
1062 uint64_t ip = start + off;
1063
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001064 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001065 if (ip == end) {
Andi Kleendde4e732019-07-11 11:19:21 -07001066 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, ++insn, fp,
Andi Kleenfe571202018-09-24 10:07:32 -07001067 &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001068 if (PRINT_FIELD(SRCCODE))
1069 printed += print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001070 break;
1071 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001072 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1073 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001074 if (ilen == 0)
1075 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001076 if (PRINT_FIELD(SRCCODE))
1077 print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001078 insn++;
1079 }
1080 }
1081 }
1082
1083 /*
1084 * Hit the branch? In this case we are already done, and the target
1085 * has not been executed yet.
1086 */
1087 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001088 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001089 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001090 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001091
1092 /*
1093 * Print final block upto sample
Andi Kleen61f61152018-11-19 21:06:17 -08001094 *
1095 * Due to pipeline delays the LBRs might be missing a branch
1096 * or two, which can result in very large or negative blocks
1097 * between final branch and sample. When this happens just
1098 * continue walking after the last TO until we hit a branch.
Andi Kleen48d02a12017-02-23 15:46:34 -08001099 */
1100 start = br->entries[0].to;
1101 end = sample->ip;
Andi Kleen61f61152018-11-19 21:06:17 -08001102 if (end < start) {
1103 /* Missing jump. Scan 128 bytes for the next branch */
1104 end = start + 128;
1105 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001106 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001107 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001108 if (len <= 0) {
1109 /* Print at least last IP if basic block did not work */
1110 len = grab_bb(buffer, sample->ip, sample->ip,
1111 machine, thread, &x.is64bit, &x.cpumode, false);
1112 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001113 goto out;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001114 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001115 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001116 if (PRINT_FIELD(SRCCODE))
1117 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001118 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001119 }
1120 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001121 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1122 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001123 if (ilen == 0)
1124 break;
Andi Kleen61f61152018-11-19 21:06:17 -08001125 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1126 /*
1127 * Hit a missing branch. Just stop.
1128 */
1129 printed += fprintf(fp, "\t... not reaching sample ...\n");
1130 break;
1131 }
Andi Kleendd2e18e2018-12-03 16:18:48 -08001132 if (PRINT_FIELD(SRCCODE))
1133 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001134 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001135out:
1136 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001137}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001138
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001139static int perf_sample__fprintf_addr(struct perf_sample *sample,
1140 struct thread *thread,
1141 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001142{
1143 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001144 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001145
1146 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001147 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001148
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001149 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001150
1151 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001152 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001153 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001154 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001155 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001156 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001157 }
1158
1159 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001160 printed += fprintf(fp, " (");
1161 printed += map__fprintf_dsoname(al.map, fp);
1162 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001163 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001164out:
1165 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001166}
1167
Andi Kleen99f753f2018-09-20 11:05:39 -07001168static const char *resolve_branch_sym(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001169 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001170 struct thread *thread,
1171 struct addr_location *al,
1172 u64 *ip)
1173{
1174 struct addr_location addr_al;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001175 struct perf_event_attr *attr = &evsel->core.attr;
Andi Kleen99f753f2018-09-20 11:05:39 -07001176 const char *name = NULL;
1177
1178 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1179 if (sample_addr_correlates_sym(attr)) {
1180 thread__resolve(thread, &addr_al, sample);
1181 if (addr_al.sym)
1182 name = addr_al.sym->name;
1183 else
1184 *ip = sample->addr;
1185 } else {
1186 *ip = sample->addr;
1187 }
1188 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1189 if (al->sym)
1190 name = al->sym->name;
1191 else
1192 *ip = sample->ip;
1193 }
1194 return name;
1195}
1196
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001197static int perf_sample__fprintf_callindent(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001198 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001199 struct thread *thread,
1200 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001201{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001202 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter256d92b2018-12-21 14:06:19 +02001203 size_t depth = thread_stack__depth(thread, sample->cpu);
Adrian Huntere2167082016-06-23 16:40:58 +03001204 const char *name = NULL;
1205 static int spacing;
1206 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001207 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001208 u64 ip = 0;
1209
1210 /*
1211 * The 'return' has already been popped off the stack so the depth has
1212 * to be adjusted to match the 'call'.
1213 */
1214 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1215 depth += 1;
1216
Andi Kleen99f753f2018-09-20 11:05:39 -07001217 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001218
Andi Kleena78cdee2018-09-18 05:32:10 -07001219 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1220 dlen += fprintf(fp, "(");
1221 dlen += map__fprintf_dsoname(al->map, fp);
1222 dlen += fprintf(fp, ")\t");
1223 }
1224
Adrian Huntere2167082016-06-23 16:40:58 +03001225 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001226 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001227 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001228 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001229
1230 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001231 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001232
1233 /*
1234 * Try to keep the output length from changing frequently so that the
1235 * output lines up more nicely.
1236 */
1237 if (len > spacing || (len && len < spacing - 52))
1238 spacing = round_up(len + 4, 32);
1239
1240 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001241 len += fprintf(fp, "%*s", spacing - len, "");
1242
Andi Kleena78cdee2018-09-18 05:32:10 -07001243 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001244}
1245
Andi Kleen3ab481a2019-03-05 06:47:45 -08001246__weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1247 struct thread *thread __maybe_unused,
1248 struct machine *machine __maybe_unused)
1249{
1250}
1251
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001252static int perf_sample__fprintf_insn(struct perf_sample *sample,
1253 struct perf_event_attr *attr,
1254 struct thread *thread,
1255 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001256{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001257 int printed = 0;
1258
Andi Kleen3ab481a2019-03-05 06:47:45 -08001259 if (sample->insn_len == 0 && native_arch)
1260 arch_fetch_insn(sample, thread, machine);
1261
Andi Kleen224e2c92016-10-07 16:42:27 +03001262 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001263 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen3ab481a2019-03-05 06:47:45 -08001264 if (PRINT_FIELD(INSN) && sample->insn_len) {
Andi Kleen224e2c92016-10-07 16:42:27 +03001265 int i;
1266
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001267 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001268 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001269 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001270 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001271 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001272 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1273
1274 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001275}
1276
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001277static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1278 struct perf_event_attr *attr, FILE *fp)
1279{
1280 unsigned int ipc;
1281
1282 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1283 return 0;
1284
1285 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1286
1287 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1288 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1289}
1290
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001291static int perf_sample__fprintf_bts(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001292 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001293 struct thread *thread,
1294 struct addr_location *al,
1295 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001296{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001297 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001298 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001299 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001300 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001301
Adrian Huntere2167082016-06-23 16:40:58 +03001302 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001303 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001304
Akihiro Nagai95582592012-01-30 13:43:09 +09001305 /* print branch_from information */
1306 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001307 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001308 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001309
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001310 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001311 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001312 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001313 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001314
1315 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001316 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001317 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001318 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001319 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001320 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001321 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001322 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001323
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001324 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001325 }
1326
Akihiro Nagai95582592012-01-30 13:43:09 +09001327 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001328 if (PRINT_FIELD(ADDR) ||
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001329 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001330 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001331 printed += fprintf(fp, " => ");
1332 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001333 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001334
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001335 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1336
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001337 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001338 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001339
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001340 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001341 printed += fprintf(fp, "\n");
1342 if (PRINT_FIELD(SRCCODE)) {
1343 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1344 &thread->srccode_state);
1345 if (ret) {
1346 printed += ret;
1347 printed += printf("\n");
1348 }
1349 }
1350 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001351}
1352
Adrian Hunter055cd332016-06-23 16:40:56 +03001353static struct {
1354 u32 flags;
1355 const char *name;
1356} sample_flags[] = {
1357 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1358 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1359 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1360 {PERF_IP_FLAG_BRANCH, "jmp"},
1361 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1362 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1363 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1364 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1365 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1366 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1367 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1368 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1369 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1370 {0, NULL}
1371};
1372
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001373static const char *sample_flags_to_name(u32 flags)
1374{
1375 int i;
1376
1377 for (i = 0; sample_flags[i].name ; i++) {
1378 if (sample_flags[i].flags == flags)
1379 return sample_flags[i].name;
1380 }
1381
1382 return NULL;
1383}
1384
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001385static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001386{
1387 const char *chars = PERF_IP_FLAG_CHARS;
1388 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001389 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1390 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001391 char str[33];
1392 int i, pos = 0;
1393
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001394 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1395 if (name)
1396 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1397
1398 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1399 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1400 if (name)
1401 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1402 }
1403
1404 if (flags & PERF_IP_FLAG_TRACE_END) {
1405 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1406 if (name)
1407 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001408 }
1409
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001410 for (i = 0; i < n; i++, flags >>= 1) {
1411 if (flags & 1)
1412 str[pos++] = chars[i];
1413 }
1414 for (; i < 32; i++, flags >>= 1) {
1415 if (flags & 1)
1416 str[pos++] = '?';
1417 }
1418 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001419
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001420 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001421}
1422
Wang Nan30372f02016-02-24 11:20:45 +00001423struct printer_data {
1424 int line_no;
1425 bool hit_nul;
1426 bool is_printable;
1427};
1428
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001429static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1430 unsigned int val,
1431 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001432{
1433 unsigned char ch = (unsigned char)val;
1434 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001435 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001436
1437 switch (op) {
1438 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001439 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001440 break;
1441 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001442 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001443 " ");
1444 break;
1445 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001446 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001447 break;
1448 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001449 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001450 break;
1451 case BINARY_PRINT_NUM_PAD:
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_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001455 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001456 break;
1457 case BINARY_PRINT_CHAR_DATA:
1458 if (printer_data->hit_nul && ch)
1459 printer_data->is_printable = false;
1460
1461 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001462 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001463
1464 if (!printer_data->is_printable)
1465 break;
1466
1467 if (ch == '\0')
1468 printer_data->hit_nul = true;
1469 else
1470 printer_data->is_printable = false;
1471 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001472 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001473 }
1474 break;
1475 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001476 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001477 break;
1478 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001479 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001480 printer_data->line_no++;
1481 break;
1482 case BINARY_PRINT_DATA_END:
1483 default:
1484 break;
1485 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001486
1487 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001488}
1489
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001490static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001491{
1492 unsigned int nr_bytes = sample->raw_size;
1493 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001494 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1495 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001496
1497 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001498 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1499
1500 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001501}
1502
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001503static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001504{
1505 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001506 return fprintf(fp, "%*s", spacing - len, "");
1507
1508 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001509}
1510
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001511static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001512{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001513 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001514}
1515
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001516static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001517{
1518 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1519 int len;
1520
1521 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001522 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001523
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001524 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001525 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001526 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001527}
1528
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001529static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001530{
1531 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1532 int len;
1533
1534 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001535 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001536
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001537 len = fprintf(fp, " hints: %#x extensions: %#x ",
1538 data->hints, data->extensions);
1539 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001540}
1541
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001542static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001543{
1544 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1545 int len;
1546
1547 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001548 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001549
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001550 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1551 data->hw, data->cstate, data->subcstate);
1552 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001553}
1554
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001555static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001556{
1557 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1558 int len;
1559
1560 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001561 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001562
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001563 len = fprintf(fp, " IP: %u ", data->ip);
1564 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001565}
1566
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001567static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001568{
1569 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1570 int len;
1571
1572 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001573 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001574
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001575 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001576 data->deepest_cstate, data->last_cstate,
1577 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001578 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001579}
1580
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001581static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001582{
1583 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1584 unsigned int percent, freq;
1585 int len;
1586
1587 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001588 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001589
1590 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001591 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001592 if (data->max_nonturbo) {
1593 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001594 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001595 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001596 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001597}
1598
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001599static int perf_sample__fprintf_synth(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001600 struct evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001601{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001602 switch (evsel->core.attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001603 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001604 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001605 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001606 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001607 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001608 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001609 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001610 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001611 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001612 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001613 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001614 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001615 default:
1616 break;
1617 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001618
1619 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001620}
1621
Jiri Olsa809e9422015-11-26 18:55:21 +01001622struct perf_script {
1623 struct perf_tool tool;
1624 struct perf_session *session;
1625 bool show_task_events;
1626 bool show_mmap_events;
1627 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301628 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001629 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001630 bool show_round_events;
Jiri Olsa490c8cc2019-05-08 15:20:08 +02001631 bool show_bpf_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001632 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001633 bool per_event_dump;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001634 struct evswitch evswitch;
Jiri Olsaf8548392019-07-21 13:23:49 +02001635 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +02001636 struct perf_thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001637 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001638 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001639 struct perf_time_interval *ptime_range;
1640 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001641 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001642};
1643
Jiri Olsa63503db2019-07-21 13:23:52 +02001644static int perf_evlist__max_name_len(struct evlist *evlist)
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001645{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001646 struct evsel *evsel;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001647 int max = 0;
1648
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001649 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001650 int len = strlen(perf_evsel__name(evsel));
1651
1652 max = MAX(len, max);
1653 }
1654
1655 return max;
1656}
1657
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001658static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001659{
1660 struct mem_info mi = { .data_src.val = data_src };
1661 char decode[100];
1662 char out[100];
1663 static int maxlen;
1664 int len;
1665
1666 perf_script__meminfo_scnprintf(decode, 100, &mi);
1667
1668 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1669 if (maxlen < len)
1670 maxlen = len;
1671
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001672 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001673}
1674
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001675struct metric_ctx {
1676 struct perf_sample *sample;
1677 struct thread *thread;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001678 struct evsel *evsel;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001679 FILE *fp;
1680};
1681
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001682static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1683 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001684 const char *fmt,
1685 const char *unit, double val)
1686{
1687 struct metric_ctx *mctx = ctx;
1688
1689 if (!fmt)
1690 return;
1691 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001692 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001693 fputs("\tmetric: ", mctx->fp);
1694 if (color)
1695 color_fprintf(mctx->fp, color, fmt, val);
1696 else
1697 printf(fmt, val);
1698 fprintf(mctx->fp, " %s\n", unit);
1699}
1700
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001701static void script_new_line(struct perf_stat_config *config __maybe_unused,
1702 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001703{
1704 struct metric_ctx *mctx = ctx;
1705
1706 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001707 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001708 fputs("\tmetric: ", mctx->fp);
1709}
1710
1711static void perf_sample__fprint_metric(struct perf_script *script,
1712 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001713 struct evsel *evsel,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001714 struct perf_sample *sample,
1715 FILE *fp)
1716{
1717 struct perf_stat_output_ctx ctx = {
1718 .print_metric = script_print_metric,
1719 .new_line = script_new_line,
1720 .ctx = &(struct metric_ctx) {
1721 .sample = sample,
1722 .thread = thread,
1723 .evsel = evsel,
1724 .fp = fp,
1725 },
1726 .force_header = false,
1727 };
Jiri Olsa32dcd022019-07-21 13:23:51 +02001728 struct evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001729 u64 val;
1730
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001731 if (!evsel->stats)
1732 perf_evlist__alloc_stats(script->session->evlist, false);
1733 if (evsel_script(evsel->leader)->gnum++ == 0)
1734 perf_stat__reset_shadow_stats();
1735 val = sample->period * evsel->scale;
1736 perf_stat__update_shadow_stats(evsel,
1737 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001738 sample->cpu,
1739 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001740 evsel_script(evsel)->val = val;
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001741 if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) {
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001742 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001743 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001744 evsel_script(ev2)->val,
1745 sample->cpu,
1746 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001747 NULL,
1748 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001749 }
1750 evsel_script(evsel->leader)->gnum = 0;
1751 }
1752}
1753
Andi Kleen99f753f2018-09-20 11:05:39 -07001754static bool show_event(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001755 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001756 struct thread *thread,
1757 struct addr_location *al)
1758{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001759 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001760
1761 if (!symbol_conf.graph_function)
1762 return true;
1763
1764 if (thread->filter) {
1765 if (depth <= thread->filter_entry_depth) {
1766 thread->filter = false;
1767 return false;
1768 }
1769 return true;
1770 } else {
1771 const char *s = symbol_conf.graph_function;
1772 u64 ip;
1773 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1774 &ip);
1775 unsigned nlen;
1776
1777 if (!name)
1778 return false;
1779 nlen = strlen(name);
1780 while (*s) {
1781 unsigned len = strcspn(s, ",");
1782 if (nlen == len && !strncmp(name, s, len)) {
1783 thread->filter = true;
1784 thread->filter_entry_depth = depth;
1785 return true;
1786 }
1787 s += len;
1788 if (*s == ',')
1789 s++;
1790 }
1791 return false;
1792 }
1793}
1794
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001795static void process_event(struct perf_script *script,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001796 struct perf_sample *sample, struct evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001797 struct addr_location *al,
1798 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001799{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001800 struct thread *thread = al->thread;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001801 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001802 unsigned int type = output_type(attr->type);
Jiri Olsa32dcd022019-07-21 13:23:51 +02001803 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001804 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001805
Adrian Hunter14057202017-06-21 13:17:19 +03001806 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001807 return;
1808
Andi Kleen99f753f2018-09-20 11:05:39 -07001809 if (!show_event(sample, evsel, thread, al))
1810 return;
1811
Arnaldo Carvalho de Melo8829e562019-08-15 11:00:11 -03001812 if (evswitch__discard(&script->evswitch, evsel))
1813 return;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001814
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001815 ++es->samples;
1816
Jiri Olsa28a0b392018-01-07 17:03:52 +01001817 perf_sample__fprintf_start(sample, thread, evsel,
1818 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001819
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001820 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001821 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001822
Namhyung Kime944d3d2013-11-18 14:34:52 +09001823 if (PRINT_FIELD(EVNAME)) {
1824 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001825
1826 if (!script->name_width)
1827 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1828
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001829 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001830 }
1831
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001832 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001833 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001834
Akihiro Nagai95582592012-01-30 13:43:09 +09001835 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001836 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001837 return;
1838 }
1839
Andi Kleen96167162019-01-17 11:48:34 -08001840 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001841 event_format__fprintf(evsel->tp_format, sample->cpu,
1842 sample->raw_data, sample->raw_size, fp);
1843 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001844
1845 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001846 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001847
David Ahern7cec0922011-05-30 13:08:23 -06001848 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001849 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001850
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001851 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001852 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001853
1854 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001855 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001856
David Ahern787bef12011-05-27 14:28:43 -06001857 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001858 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001859
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001860 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001861 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001862 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001863 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001864
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001865 fputc(cursor ? '\n' : ' ', fp);
1866 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001867 }
1868
Stephane Eranianfc36f942015-08-31 18:41:10 +02001869 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001870 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001871
Andi Kleenb1491ac2017-09-05 11:40:57 -07001872 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001873 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001874
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001875 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001876 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001877 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001878 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001879 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001880 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001881
Wang Nan30372f02016-02-24 11:20:45 +00001882 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001883 perf_sample__fprintf_bpf_output(sample, fp);
1884 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001885
1886 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001887 fprintf(fp, "%16" PRIx64, sample->phys_addr);
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001888
1889 perf_sample__fprintf_ipc(sample, attr, fp);
1890
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001891 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001892
Andi Kleendd2e18e2018-12-03 16:18:48 -08001893 if (PRINT_FIELD(SRCCODE)) {
1894 if (map__fprintf_srccode(al->map, al->addr, stdout,
1895 &thread->srccode_state))
1896 printf("\n");
1897 }
1898
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001899 if (PRINT_FIELD(METRIC))
1900 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001901
1902 if (verbose)
1903 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001904}
1905
Tom Zanussi956ffd02009-11-25 01:15:46 -06001906static struct scripting_ops *scripting_ops;
1907
Jiri Olsa32dcd022019-07-21 13:23:51 +02001908static void __process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsa36e33c52016-01-06 11:49:56 +01001909{
Jiri Olsaa2f354e2019-08-22 13:11:41 +02001910 int nthreads = perf_thread_map__nr(counter->core.threads);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001911 int ncpus = perf_evsel__nr_cpus(counter);
1912 int cpu, thread;
1913 static int header_printed;
1914
1915 if (counter->system_wide)
1916 nthreads = 1;
1917
1918 if (!header_printed) {
1919 printf("%3s %8s %15s %15s %15s %15s %s\n",
1920 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1921 header_printed = 1;
1922 }
1923
1924 for (thread = 0; thread < nthreads; thread++) {
1925 for (cpu = 0; cpu < ncpus; cpu++) {
1926 struct perf_counts_values *counts;
1927
1928 counts = perf_counts(counter->counts, cpu, thread);
1929
1930 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
Jiri Olsad400bd32019-07-21 13:24:37 +02001931 counter->core.cpus->map[cpu],
Jiri Olsaa2f354e2019-08-22 13:11:41 +02001932 perf_thread_map__pid(counter->core.threads, thread),
Jiri Olsa36e33c52016-01-06 11:49:56 +01001933 counts->val,
1934 counts->ena,
1935 counts->run,
1936 tstamp,
1937 perf_evsel__name(counter));
1938 }
1939 }
1940}
1941
Jiri Olsa32dcd022019-07-21 13:23:51 +02001942static void process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsae099eba2016-01-05 22:09:09 +01001943{
1944 if (scripting_ops && scripting_ops->process_stat)
1945 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001946 else
1947 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001948}
1949
1950static void process_stat_interval(u64 tstamp)
1951{
1952 if (scripting_ops && scripting_ops->process_stat_interval)
1953 scripting_ops->process_stat_interval(tstamp);
1954}
1955
Tom Zanussi956ffd02009-11-25 01:15:46 -06001956static void setup_scripting(void)
1957{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001958 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001959 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001960}
1961
Adrian Hunterd445dd22014-08-15 22:08:37 +03001962static int flush_scripting(void)
1963{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001964 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001965}
1966
Tom Zanussi956ffd02009-11-25 01:15:46 -06001967static int cleanup_scripting(void)
1968{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001969 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001970
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001971 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001972}
1973
Andi Kleene87e5482019-03-11 07:44:52 -07001974static bool filter_cpu(struct perf_sample *sample)
1975{
1976 if (cpu_list)
1977 return !test_bit(sample->cpu, cpu_bitmap);
1978 return false;
1979}
1980
Jiri Olsa809e9422015-11-26 18:55:21 +01001981static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001982 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001983 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001984 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001985 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001986{
Jiri Olsa809e9422015-11-26 18:55:21 +01001987 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001988 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001989
Jin Yao2ab046c2017-12-08 21:13:46 +08001990 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1991 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001992 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001993 }
David Aherna91f4c42016-11-29 10:15:43 -07001994
David Ahern1424dc92011-03-09 22:23:28 -07001995 if (debug_mode) {
1996 if (sample->time < last_timestamp) {
1997 pr_err("Samples misordered, previous: %" PRIu64
1998 " this: %" PRIu64 "\n", last_timestamp,
1999 sample->time);
2000 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02002001 }
David Ahern1424dc92011-03-09 22:23:28 -07002002 last_timestamp = sample->time;
2003 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002004 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002005
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03002006 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07002007 pr_err("problem processing %d event, skipping it.\n",
2008 event->header.type);
2009 return -1;
2010 }
2011
2012 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002013 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07002014
Andi Kleene87e5482019-03-11 07:44:52 -07002015 if (filter_cpu(sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002016 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002017
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002018 if (scripting_ops)
2019 scripting_ops->process_event(event, sample, evsel, &al);
2020 else
Andi Kleen48d02a12017-02-23 15:46:34 -08002021 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002022
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002023out_put:
2024 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002025 return 0;
2026}
2027
Adrian Hunter7ea95722013-11-01 15:51:30 +02002028static int process_attr(struct perf_tool *tool, union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02002029 struct evlist **pevlist)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002030{
2031 struct perf_script *scr = container_of(tool, struct perf_script, tool);
Jiri Olsa63503db2019-07-21 13:23:52 +02002032 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002033 struct evsel *evsel, *pos;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002034 int err;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002035 static struct evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002036
2037 err = perf_event__process_attr(tool, event, pevlist);
2038 if (err)
2039 return err;
2040
2041 evlist = *pevlist;
2042 evsel = perf_evlist__last(*pevlist);
2043
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302044 if (!evsel->priv) {
2045 if (scr->per_event_dump) {
2046 evsel->priv = perf_evsel_script__new(evsel,
2047 scr->session->data);
2048 } else {
2049 es = zalloc(sizeof(*es));
2050 if (!es)
2051 return -ENOMEM;
2052 es->fp = stdout;
2053 evsel->priv = es;
2054 }
2055 }
2056
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002057 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2058 evsel->core.attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002059 return 0;
2060
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002061 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002062 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002063 return 0;
2064 }
2065
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002066 set_print_ip_opts(&evsel->core.attr);
Adrian Hunter7ea95722013-11-01 15:51:30 +02002067
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002068 if (evsel->core.attr.sample_type)
Jiri Olsad2b5a312015-10-16 12:41:25 +02002069 err = perf_evsel__check_attr(evsel, scr->session);
2070
2071 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002072}
2073
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002074static int process_comm_event(struct perf_tool *tool,
2075 union perf_event *event,
2076 struct perf_sample *sample,
2077 struct machine *machine)
2078{
2079 struct thread *thread;
2080 struct perf_script *script = container_of(tool, struct perf_script, tool);
2081 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002082 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002083 int ret = -1;
2084
2085 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2086 if (thread == NULL) {
2087 pr_debug("problem processing COMM event, skipping it.\n");
2088 return -1;
2089 }
2090
2091 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2092 goto out;
2093
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002094 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002095 sample->cpu = 0;
2096 sample->time = 0;
2097 sample->tid = event->comm.tid;
2098 sample->pid = event->comm.pid;
2099 }
Andi Kleene87e5482019-03-11 07:44:52 -07002100 if (!filter_cpu(sample)) {
2101 perf_sample__fprintf_start(sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01002102 PERF_RECORD_COMM, stdout);
Andi Kleene87e5482019-03-11 07:44:52 -07002103 perf_event__fprintf(event, stdout);
2104 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002105 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002106out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002107 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002108 return ret;
2109}
2110
Hari Bathini96a44bb2017-03-08 02:12:06 +05302111static int process_namespaces_event(struct perf_tool *tool,
2112 union perf_event *event,
2113 struct perf_sample *sample,
2114 struct machine *machine)
2115{
2116 struct thread *thread;
2117 struct perf_script *script = container_of(tool, struct perf_script, tool);
2118 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002119 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302120 int ret = -1;
2121
2122 thread = machine__findnew_thread(machine, event->namespaces.pid,
2123 event->namespaces.tid);
2124 if (thread == NULL) {
2125 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2126 return -1;
2127 }
2128
2129 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2130 goto out;
2131
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002132 if (!evsel->core.attr.sample_id_all) {
Hari Bathini96a44bb2017-03-08 02:12:06 +05302133 sample->cpu = 0;
2134 sample->time = 0;
2135 sample->tid = event->namespaces.tid;
2136 sample->pid = event->namespaces.pid;
2137 }
Andi Kleene87e5482019-03-11 07:44:52 -07002138 if (!filter_cpu(sample)) {
2139 perf_sample__fprintf_start(sample, thread, evsel,
2140 PERF_RECORD_NAMESPACES, stdout);
2141 perf_event__fprintf(event, stdout);
2142 }
Hari Bathini96a44bb2017-03-08 02:12:06 +05302143 ret = 0;
2144out:
2145 thread__put(thread);
2146 return ret;
2147}
2148
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002149static int process_fork_event(struct perf_tool *tool,
2150 union perf_event *event,
2151 struct perf_sample *sample,
2152 struct machine *machine)
2153{
2154 struct thread *thread;
2155 struct perf_script *script = container_of(tool, struct perf_script, tool);
2156 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002157 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002158
2159 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2160 return -1;
2161
2162 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2163 if (thread == NULL) {
2164 pr_debug("problem processing FORK event, skipping it.\n");
2165 return -1;
2166 }
2167
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002168 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002169 sample->cpu = 0;
2170 sample->time = event->fork.time;
2171 sample->tid = event->fork.tid;
2172 sample->pid = event->fork.pid;
2173 }
Andi Kleene87e5482019-03-11 07:44:52 -07002174 if (!filter_cpu(sample)) {
2175 perf_sample__fprintf_start(sample, thread, evsel,
2176 PERF_RECORD_FORK, stdout);
2177 perf_event__fprintf(event, stdout);
2178 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002179 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002180
2181 return 0;
2182}
2183static int process_exit_event(struct perf_tool *tool,
2184 union perf_event *event,
2185 struct perf_sample *sample,
2186 struct machine *machine)
2187{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002188 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002189 struct thread *thread;
2190 struct perf_script *script = container_of(tool, struct perf_script, tool);
2191 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002192 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002193
2194 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2195 if (thread == NULL) {
2196 pr_debug("problem processing EXIT event, skipping it.\n");
2197 return -1;
2198 }
2199
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002200 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002201 sample->cpu = 0;
2202 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002203 sample->tid = event->fork.tid;
2204 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002205 }
Andi Kleene87e5482019-03-11 07:44:52 -07002206 if (!filter_cpu(sample)) {
2207 perf_sample__fprintf_start(sample, thread, evsel,
2208 PERF_RECORD_EXIT, stdout);
2209 perf_event__fprintf(event, stdout);
2210 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002211
2212 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002213 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002214
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002215 thread__put(thread);
2216 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002217}
2218
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002219static int process_mmap_event(struct perf_tool *tool,
2220 union perf_event *event,
2221 struct perf_sample *sample,
2222 struct machine *machine)
2223{
2224 struct thread *thread;
2225 struct perf_script *script = container_of(tool, struct perf_script, tool);
2226 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002227 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002228
2229 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2230 return -1;
2231
2232 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2233 if (thread == NULL) {
2234 pr_debug("problem processing MMAP event, skipping it.\n");
2235 return -1;
2236 }
2237
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002238 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002239 sample->cpu = 0;
2240 sample->time = 0;
2241 sample->tid = event->mmap.tid;
2242 sample->pid = event->mmap.pid;
2243 }
Andi Kleene87e5482019-03-11 07:44:52 -07002244 if (!filter_cpu(sample)) {
2245 perf_sample__fprintf_start(sample, thread, evsel,
2246 PERF_RECORD_MMAP, stdout);
2247 perf_event__fprintf(event, stdout);
2248 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002249 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002250 return 0;
2251}
2252
2253static int process_mmap2_event(struct perf_tool *tool,
2254 union perf_event *event,
2255 struct perf_sample *sample,
2256 struct machine *machine)
2257{
2258 struct thread *thread;
2259 struct perf_script *script = container_of(tool, struct perf_script, tool);
2260 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002261 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002262
2263 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2264 return -1;
2265
2266 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2267 if (thread == NULL) {
2268 pr_debug("problem processing MMAP2 event, skipping it.\n");
2269 return -1;
2270 }
2271
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002272 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002273 sample->cpu = 0;
2274 sample->time = 0;
2275 sample->tid = event->mmap2.tid;
2276 sample->pid = event->mmap2.pid;
2277 }
Andi Kleene87e5482019-03-11 07:44:52 -07002278 if (!filter_cpu(sample)) {
2279 perf_sample__fprintf_start(sample, thread, evsel,
2280 PERF_RECORD_MMAP2, stdout);
2281 perf_event__fprintf(event, stdout);
2282 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002283 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002284 return 0;
2285}
2286
Adrian Hunter7c148982015-07-21 12:44:06 +03002287static int process_switch_event(struct perf_tool *tool,
2288 union perf_event *event,
2289 struct perf_sample *sample,
2290 struct machine *machine)
2291{
2292 struct thread *thread;
2293 struct perf_script *script = container_of(tool, struct perf_script, tool);
2294 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002295 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Adrian Hunter7c148982015-07-21 12:44:06 +03002296
2297 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2298 return -1;
2299
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002300 if (scripting_ops && scripting_ops->process_switch)
2301 scripting_ops->process_switch(event, sample, machine);
2302
2303 if (!script->show_switch_events)
2304 return 0;
2305
Adrian Hunter7c148982015-07-21 12:44:06 +03002306 thread = machine__findnew_thread(machine, sample->pid,
2307 sample->tid);
2308 if (thread == NULL) {
2309 pr_debug("problem processing SWITCH event, skipping it.\n");
2310 return -1;
2311 }
2312
Andi Kleene87e5482019-03-11 07:44:52 -07002313 if (!filter_cpu(sample)) {
2314 perf_sample__fprintf_start(sample, thread, evsel,
2315 PERF_RECORD_SWITCH, stdout);
2316 perf_event__fprintf(event, stdout);
2317 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002318 thread__put(thread);
2319 return 0;
2320}
2321
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002322static int
2323process_lost_event(struct perf_tool *tool,
2324 union perf_event *event,
2325 struct perf_sample *sample,
2326 struct machine *machine)
2327{
2328 struct perf_script *script = container_of(tool, struct perf_script, tool);
2329 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002330 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002331 struct thread *thread;
2332
2333 thread = machine__findnew_thread(machine, sample->pid,
2334 sample->tid);
2335 if (thread == NULL)
2336 return -1;
2337
Andi Kleene87e5482019-03-11 07:44:52 -07002338 if (!filter_cpu(sample)) {
2339 perf_sample__fprintf_start(sample, thread, evsel,
2340 PERF_RECORD_LOST, stdout);
2341 perf_event__fprintf(event, stdout);
2342 }
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002343 thread__put(thread);
2344 return 0;
2345}
2346
Jiri Olsa3233b372018-02-06 19:17:59 +01002347static int
2348process_finished_round_event(struct perf_tool *tool __maybe_unused,
2349 union perf_event *event,
2350 struct ordered_events *oe __maybe_unused)
2351
2352{
2353 perf_event__fprintf(event, stdout);
2354 return 0;
2355}
2356
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002357static int
2358process_bpf_events(struct perf_tool *tool __maybe_unused,
2359 union perf_event *event,
2360 struct perf_sample *sample,
2361 struct machine *machine)
2362{
2363 struct thread *thread;
2364 struct perf_script *script = container_of(tool, struct perf_script, tool);
2365 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002366 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002367
2368 if (machine__process_ksymbol(machine, event, sample) < 0)
2369 return -1;
2370
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002371 if (!evsel->core.attr.sample_id_all) {
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002372 perf_event__fprintf(event, stdout);
2373 return 0;
2374 }
2375
2376 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2377 if (thread == NULL) {
2378 pr_debug("problem processing MMAP event, skipping it.\n");
2379 return -1;
2380 }
2381
2382 if (!filter_cpu(sample)) {
2383 perf_sample__fprintf_start(sample, thread, evsel,
2384 event->header.type, stdout);
2385 perf_event__fprintf(event, stdout);
2386 }
2387
2388 thread__put(thread);
2389 return 0;
2390}
2391
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002392static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002393{
2394 session_done = 1;
2395}
2396
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002397static void perf_script__fclose_per_event_dump(struct perf_script *script)
2398{
Jiri Olsa63503db2019-07-21 13:23:52 +02002399 struct evlist *evlist = script->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002400 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002401
2402 evlist__for_each_entry(evlist, evsel) {
2403 if (!evsel->priv)
2404 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002405 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002406 evsel->priv = NULL;
2407 }
2408}
2409
2410static int perf_script__fopen_per_event_dump(struct perf_script *script)
2411{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002412 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002413
2414 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002415 /*
2416 * Already setup? I.e. we may be called twice in cases like
2417 * Intel PT, one for the intel_pt// and dummy events, then
2418 * for the evsels syntheized from the auxtrace info.
2419 *
2420 * Ses perf_script__process_auxtrace_info.
2421 */
2422 if (evsel->priv != NULL)
2423 continue;
2424
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002425 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002426 if (evsel->priv == NULL)
2427 goto out_err_fclose;
2428 }
2429
2430 return 0;
2431
2432out_err_fclose:
2433 perf_script__fclose_per_event_dump(script);
2434 return -1;
2435}
2436
2437static int perf_script__setup_per_event_dump(struct perf_script *script)
2438{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002439 struct evsel *evsel;
2440 static struct evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002441
2442 if (script->per_event_dump)
2443 return perf_script__fopen_per_event_dump(script);
2444
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002445 es_stdout.fp = stdout;
2446
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002447 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002448 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002449
2450 return 0;
2451}
2452
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002453static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2454{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002455 struct evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002456
2457 evlist__for_each_entry(script->session->evlist, evsel) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002458 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002459
2460 perf_evsel_script__fprintf(es, stdout);
2461 perf_evsel_script__delete(es);
2462 evsel->priv = NULL;
2463 }
2464}
2465
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002466static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002467{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002468 int ret;
2469
Tom Zanussic239da32010-04-01 23:59:18 -05002470 signal(SIGINT, sig_handler);
2471
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002472 perf_stat__init_shadow_stats();
2473
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002474 /* override event processing functions */
2475 if (script->show_task_events) {
2476 script->tool.comm = process_comm_event;
2477 script->tool.fork = process_fork_event;
2478 script->tool.exit = process_exit_event;
2479 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002480 if (script->show_mmap_events) {
2481 script->tool.mmap = process_mmap_event;
2482 script->tool.mmap2 = process_mmap2_event;
2483 }
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002484 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
Adrian Hunter7c148982015-07-21 12:44:06 +03002485 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302486 if (script->show_namespace_events)
2487 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002488 if (script->show_lost_events)
2489 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002490 if (script->show_round_events) {
2491 script->tool.ordered_events = false;
2492 script->tool.finished_round = process_finished_round_event;
2493 }
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002494 if (script->show_bpf_events) {
2495 script->tool.ksymbol = process_bpf_events;
2496 script->tool.bpf_event = process_bpf_events;
2497 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002498
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002499 if (perf_script__setup_per_event_dump(script)) {
2500 pr_err("Couldn't create the per event dump files\n");
2501 return -1;
2502 }
2503
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002504 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002505
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002506 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002507 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002508
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002509 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002510 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002511
2512 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002513}
2514
Tom Zanussi956ffd02009-11-25 01:15:46 -06002515struct script_spec {
2516 struct list_head node;
2517 struct scripting_ops *ops;
2518 char spec[0];
2519};
2520
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002521static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002522
2523static struct script_spec *script_spec__new(const char *spec,
2524 struct scripting_ops *ops)
2525{
2526 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2527
2528 if (s != NULL) {
2529 strcpy(s->spec, spec);
2530 s->ops = ops;
2531 }
2532
2533 return s;
2534}
2535
Tom Zanussi956ffd02009-11-25 01:15:46 -06002536static void script_spec__add(struct script_spec *s)
2537{
2538 list_add_tail(&s->node, &script_specs);
2539}
2540
2541static struct script_spec *script_spec__find(const char *spec)
2542{
2543 struct script_spec *s;
2544
2545 list_for_each_entry(s, &script_specs, node)
2546 if (strcasecmp(s->spec, spec) == 0)
2547 return s;
2548 return NULL;
2549}
2550
Tom Zanussi956ffd02009-11-25 01:15:46 -06002551int script_spec_register(const char *spec, struct scripting_ops *ops)
2552{
2553 struct script_spec *s;
2554
2555 s = script_spec__find(spec);
2556 if (s)
2557 return -1;
2558
Taeung Song8560bae2016-02-26 00:13:10 +09002559 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002560 if (!s)
2561 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002562 else
2563 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002564
2565 return 0;
2566}
2567
2568static struct scripting_ops *script_spec__lookup(const char *spec)
2569{
2570 struct script_spec *s = script_spec__find(spec);
2571 if (!s)
2572 return NULL;
2573
2574 return s->ops;
2575}
2576
2577static void list_available_languages(void)
2578{
2579 struct script_spec *s;
2580
2581 fprintf(stderr, "\n");
2582 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002583 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002584
2585 list_for_each_entry(s, &script_specs, node)
2586 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2587
2588 fprintf(stderr, "\n");
2589}
2590
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002591static int parse_scriptname(const struct option *opt __maybe_unused,
2592 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002593{
2594 char spec[PATH_MAX];
2595 const char *script, *ext;
2596 int len;
2597
Tom Zanussif526d682010-01-27 02:27:52 -06002598 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002599 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002600 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002601 }
2602
2603 script = strchr(str, ':');
2604 if (script) {
2605 len = script - str;
2606 if (len >= PATH_MAX) {
2607 fprintf(stderr, "invalid language specifier");
2608 return -1;
2609 }
2610 strncpy(spec, str, len);
2611 spec[len] = '\0';
2612 scripting_ops = script_spec__lookup(spec);
2613 if (!scripting_ops) {
2614 fprintf(stderr, "invalid language specifier");
2615 return -1;
2616 }
2617 script++;
2618 } else {
2619 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002620 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002621 if (!ext) {
2622 fprintf(stderr, "invalid script extension");
2623 return -1;
2624 }
2625 scripting_ops = script_spec__lookup(++ext);
2626 if (!scripting_ops) {
2627 fprintf(stderr, "invalid script extension");
2628 return -1;
2629 }
2630 }
2631
2632 script_name = strdup(script);
2633
2634 return 0;
2635}
2636
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002637static int parse_output_fields(const struct option *opt __maybe_unused,
2638 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002639{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002640 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002641 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002642 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002643 int rc = 0;
2644 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002645 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002646 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002647
2648 if (!str)
2649 return -ENOMEM;
2650
David Ahern2c9e45f72011-03-17 10:03:21 -06002651 /* first word can state for which event type the user is specifying
2652 * the fields. If no type exists, the specified fields apply to all
2653 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002654 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002655 tok = strchr(str, ':');
2656 if (tok) {
2657 *tok = '\0';
2658 tok++;
2659 if (!strcmp(str, "hw"))
2660 type = PERF_TYPE_HARDWARE;
2661 else if (!strcmp(str, "sw"))
2662 type = PERF_TYPE_SOFTWARE;
2663 else if (!strcmp(str, "trace"))
2664 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002665 else if (!strcmp(str, "raw"))
2666 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002667 else if (!strcmp(str, "break"))
2668 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002669 else if (!strcmp(str, "synth"))
2670 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002671 else {
2672 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002673 rc = -EINVAL;
2674 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002675 }
2676
2677 if (output[type].user_set)
2678 pr_warning("Overriding previous field request for %s events.\n",
2679 event_type(type));
2680
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002681 /* Don't override defaults for +- */
2682 if (strchr(tok, '+') || strchr(tok, '-'))
2683 goto parse;
2684
David Ahern2c9e45f72011-03-17 10:03:21 -06002685 output[type].fields = 0;
2686 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002687 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002688
2689 } else {
2690 tok = str;
2691 if (strlen(str) == 0) {
2692 fprintf(stderr,
2693 "Cannot set fields to 'none' for all event types.\n");
2694 rc = -EINVAL;
2695 goto out;
2696 }
2697
Andi Kleen36ce5652017-06-02 08:48:10 -07002698 /* Don't override defaults for +- */
2699 if (strchr(str, '+') || strchr(str, '-'))
2700 goto parse;
2701
David Ahern2c9e45f72011-03-17 10:03:21 -06002702 if (output_set_by_user())
2703 pr_warning("Overriding previous field request for all events.\n");
2704
Adrian Hunter14057202017-06-21 13:17:19 +03002705 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002706 output[j].fields = 0;
2707 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002708 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002709 }
David Ahern1424dc92011-03-09 22:23:28 -07002710 }
2711
Andi Kleen36ce5652017-06-02 08:48:10 -07002712parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002713 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002714 if (*tok == '+') {
2715 if (change == SET)
2716 goto out_badmix;
2717 change = ADD;
2718 tok++;
2719 } else if (*tok == '-') {
2720 if (change == SET)
2721 goto out_badmix;
2722 change = REMOVE;
2723 tok++;
2724 } else {
2725 if (change != SET && change != DEFAULT)
2726 goto out_badmix;
2727 change = SET;
2728 }
2729
David Ahern745f43e2011-03-09 22:23:26 -07002730 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002731 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002732 break;
David Ahern745f43e2011-03-09 22:23:26 -07002733 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002734 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002735 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002736 continue;
2737 }
David Ahern745f43e2011-03-09 22:23:26 -07002738 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002739 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002740 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002741 goto out;
2742 }
2743
2744 if (type == -1) {
2745 /* add user option to all events types for
2746 * which it is valid
2747 */
Adrian Hunter14057202017-06-21 13:17:19 +03002748 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002749 if (output[j].invalid_fields & all_output_options[i].field) {
2750 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2751 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002752 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002753 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002754 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002755 output[j].user_set_fields &= ~all_output_options[i].field;
2756 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002757 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002758 output[j].user_set_fields |= all_output_options[i].field;
2759 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002760 output[j].user_set = true;
2761 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002762 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002763 }
2764 } else {
2765 if (output[type].invalid_fields & all_output_options[i].field) {
2766 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2767 all_output_options[i].str, event_type(type));
2768
2769 rc = -EINVAL;
2770 goto out;
2771 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002772 if (change == REMOVE)
2773 output[type].fields &= ~all_output_options[i].field;
2774 else
2775 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002776 output[type].user_set = true;
2777 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002778 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002779 }
2780
2781 if (type >= 0) {
2782 if (output[type].fields == 0) {
2783 pr_debug("No fields requested for %s type. "
2784 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002785 }
David Ahern1424dc92011-03-09 22:23:28 -07002786 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002787 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002788
Andi Kleen36ce5652017-06-02 08:48:10 -07002789out_badmix:
2790 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2791 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002792out:
David Ahern745f43e2011-03-09 22:23:26 -07002793 free(str);
2794 return rc;
2795}
2796
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002797#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2798 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2799 if ((lang_dirent->d_type == DT_DIR || \
2800 (lang_dirent->d_type == DT_UNKNOWN && \
2801 is_directory(scripts_path, lang_dirent))) && \
2802 (strcmp(lang_dirent->d_name, ".")) && \
2803 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002804
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002805#define for_each_script(lang_path, lang_dir, script_dirent) \
2806 while ((script_dirent = readdir(lang_dir)) != NULL) \
2807 if (script_dirent->d_type != DT_DIR && \
2808 (script_dirent->d_type != DT_UNKNOWN || \
2809 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002810
2811
2812#define RECORD_SUFFIX "-record"
2813#define REPORT_SUFFIX "-report"
2814
2815struct script_desc {
2816 struct list_head node;
2817 char *name;
2818 char *half_liner;
2819 char *args;
2820};
2821
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002822static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002823
2824static struct script_desc *script_desc__new(const char *name)
2825{
2826 struct script_desc *s = zalloc(sizeof(*s));
2827
Tom Zanussib5b87312010-11-10 08:16:51 -06002828 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002829 s->name = strdup(name);
2830
2831 return s;
2832}
2833
2834static void script_desc__delete(struct script_desc *s)
2835{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002836 zfree(&s->name);
2837 zfree(&s->half_liner);
2838 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002839 free(s);
2840}
2841
2842static void script_desc__add(struct script_desc *s)
2843{
2844 list_add_tail(&s->node, &script_descs);
2845}
2846
2847static struct script_desc *script_desc__find(const char *name)
2848{
2849 struct script_desc *s;
2850
2851 list_for_each_entry(s, &script_descs, node)
2852 if (strcasecmp(s->name, name) == 0)
2853 return s;
2854 return NULL;
2855}
2856
2857static struct script_desc *script_desc__findnew(const char *name)
2858{
2859 struct script_desc *s = script_desc__find(name);
2860
2861 if (s)
2862 return s;
2863
2864 s = script_desc__new(name);
2865 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002866 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002867
2868 script_desc__add(s);
2869
2870 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002871}
2872
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002873static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002874{
2875 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002876 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002877
2878 if (strlen(str) > suffix_len) {
2879 p = str + strlen(str) - suffix_len;
2880 if (!strncmp(p, suffix, suffix_len))
2881 return p;
2882 }
2883
2884 return NULL;
2885}
2886
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002887static int read_script_info(struct script_desc *desc, const char *filename)
2888{
2889 char line[BUFSIZ], *p;
2890 FILE *fp;
2891
2892 fp = fopen(filename, "r");
2893 if (!fp)
2894 return -1;
2895
2896 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002897 p = skip_spaces(line);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002898 if (strlen(p) == 0)
2899 continue;
2900 if (*p != '#')
2901 continue;
2902 p++;
2903 if (strlen(p) && *p == '!')
2904 continue;
2905
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002906 p = skip_spaces(p);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002907 if (strlen(p) && p[strlen(p) - 1] == '\n')
2908 p[strlen(p) - 1] = '\0';
2909
2910 if (!strncmp(p, "description:", strlen("description:"))) {
2911 p += strlen("description:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002912 desc->half_liner = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002913 continue;
2914 }
2915
2916 if (!strncmp(p, "args:", strlen("args:"))) {
2917 p += strlen("args:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002918 desc->args = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002919 continue;
2920 }
2921 }
2922
2923 fclose(fp);
2924
2925 return 0;
2926}
2927
Robert Richter38efb532011-11-25 11:38:40 +01002928static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2929{
2930 char *script_root, *str;
2931
2932 script_root = strdup(script_dirent->d_name);
2933 if (!script_root)
2934 return NULL;
2935
2936 str = (char *)ends_with(script_root, suffix);
2937 if (!str) {
2938 free(script_root);
2939 return NULL;
2940 }
2941
2942 *str = '\0';
2943 return script_root;
2944}
2945
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002946static int list_available_scripts(const struct option *opt __maybe_unused,
2947 const char *s __maybe_unused,
2948 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002949{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002950 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002951 char scripts_path[MAXPATHLEN];
2952 DIR *scripts_dir, *lang_dir;
2953 char script_path[MAXPATHLEN];
2954 char lang_path[MAXPATHLEN];
2955 struct script_desc *desc;
2956 char first_half[BUFSIZ];
2957 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002958
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002959 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002960
2961 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002962 if (!scripts_dir) {
2963 fprintf(stdout,
2964 "open(%s) failed.\n"
2965 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2966 scripts_path);
2967 exit(-1);
2968 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002969
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002970 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002971 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2972 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002973 lang_dir = opendir(lang_path);
2974 if (!lang_dir)
2975 continue;
2976
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002977 for_each_script(lang_path, lang_dir, script_dirent) {
2978 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002979 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002980 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002981 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2982 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002983 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002984 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002985 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002986 }
2987 }
2988
2989 fprintf(stdout, "List of available trace scripts:\n");
2990 list_for_each_entry(desc, &script_descs, node) {
2991 sprintf(first_half, "%s %s", desc->name,
2992 desc->args ? desc->args : "");
2993 fprintf(stdout, " %-36s %s\n", first_half,
2994 desc->half_liner ? desc->half_liner : "");
2995 }
2996
2997 exit(0);
2998}
2999
Feng Tange5f37052012-09-07 16:42:26 +08003000/*
Feng Tang49e639e2012-10-30 11:56:03 +08003001 * Some scripts specify the required events in their "xxx-record" file,
3002 * this function will check if the events in perf.data match those
3003 * mentioned in the "xxx-record".
3004 *
3005 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3006 * which is covered well now. And new parsing code should be added to
3007 * cover the future complexing formats like event groups etc.
3008 */
3009static int check_ev_match(char *dir_name, char *scriptname,
3010 struct perf_session *session)
3011{
3012 char filename[MAXPATHLEN], evname[128];
3013 char line[BUFSIZ], *p;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003014 struct evsel *pos;
Feng Tang49e639e2012-10-30 11:56:03 +08003015 int match, len;
3016 FILE *fp;
3017
Jiri Olsa77f18152018-03-19 09:29:01 +01003018 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08003019
3020 fp = fopen(filename, "r");
3021 if (!fp)
3022 return -1;
3023
3024 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003025 p = skip_spaces(line);
Feng Tang49e639e2012-10-30 11:56:03 +08003026 if (*p == '#')
3027 continue;
3028
3029 while (strlen(p)) {
3030 p = strstr(p, "-e");
3031 if (!p)
3032 break;
3033
3034 p += 2;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003035 p = skip_spaces(p);
Feng Tang49e639e2012-10-30 11:56:03 +08003036 len = strcspn(p, " \t");
3037 if (!len)
3038 break;
3039
3040 snprintf(evname, len + 1, "%s", p);
3041
3042 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003043 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08003044 if (!strcmp(perf_evsel__name(pos), evname)) {
3045 match = 1;
3046 break;
3047 }
3048 }
3049
3050 if (!match) {
3051 fclose(fp);
3052 return -1;
3053 }
3054 }
3055 }
3056
3057 fclose(fp);
3058 return 0;
3059}
3060
3061/*
Feng Tange5f37052012-09-07 16:42:26 +08003062 * Return -1 if none is found, otherwise the actual scripts number.
3063 *
3064 * Currently the only user of this function is the script browser, which
3065 * will list all statically runnable scripts, select one, execute it and
3066 * show the output in a perf browser.
3067 */
Andi Kleen905e4af2019-03-11 07:45:01 -07003068int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3069 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08003070{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003071 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08003072 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08003073 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08003074 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003075 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003076 .path = input_name,
3077 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003078 };
Feng Tange5f37052012-09-07 16:42:26 +08003079 char *temp;
3080 int i = 0;
3081
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003082 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08003083 if (!session)
3084 return -1;
3085
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003086 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003087
3088 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003089 if (!scripts_dir) {
3090 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003091 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003092 }
Feng Tange5f37052012-09-07 16:42:26 +08003093
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003094 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003095 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3096 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003097#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003098 if (strstr(lang_path, "perl"))
3099 continue;
3100#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003101#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003102 if (strstr(lang_path, "python"))
3103 continue;
3104#endif
3105
3106 lang_dir = opendir(lang_path);
3107 if (!lang_dir)
3108 continue;
3109
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003110 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003111 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003112 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003113 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003114 if (i >= num)
3115 break;
3116 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3117 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003118 script_dirent->d_name);
3119 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003120 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003121 (temp - script_dirent->d_name) + 1,
3122 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003123
3124 if (check_ev_match(lang_path,
3125 scripts_array[i], session))
3126 continue;
3127
Feng Tange5f37052012-09-07 16:42:26 +08003128 i++;
3129 }
Feng Tang49e639e2012-10-30 11:56:03 +08003130 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003131 }
3132
Feng Tang49e639e2012-10-30 11:56:03 +08003133 closedir(scripts_dir);
3134 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003135 return i;
3136}
3137
Tom Zanussi38752942009-12-15 02:53:39 -06003138static char *get_script_path(const char *script_root, const char *suffix)
3139{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003140 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003141 char scripts_path[MAXPATHLEN];
3142 char script_path[MAXPATHLEN];
3143 DIR *scripts_dir, *lang_dir;
3144 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003145 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003146
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003147 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003148
3149 scripts_dir = opendir(scripts_path);
3150 if (!scripts_dir)
3151 return NULL;
3152
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003153 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003154 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3155 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003156 lang_dir = opendir(lang_path);
3157 if (!lang_dir)
3158 continue;
3159
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003160 for_each_script(lang_path, lang_dir, script_dirent) {
3161 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003162 if (__script_root && !strcmp(script_root, __script_root)) {
3163 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003164 closedir(lang_dir);
3165 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003166 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3167 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003168 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003169 }
3170 free(__script_root);
3171 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003172 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003173 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003174 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003175
Robert Richter38efb532011-11-25 11:38:40 +01003176 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003177}
3178
Tom Zanussib5b87312010-11-10 08:16:51 -06003179static bool is_top_script(const char *script_path)
3180{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003181 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003182}
3183
3184static int has_required_arg(char *script_path)
3185{
3186 struct script_desc *desc;
3187 int n_args = 0;
3188 char *p;
3189
3190 desc = script_desc__new(NULL);
3191
3192 if (read_script_info(desc, script_path))
3193 goto out;
3194
3195 if (!desc->args)
3196 goto out;
3197
3198 for (p = desc->args; *p; p++)
3199 if (*p == '<')
3200 n_args++;
3201out:
3202 script_desc__delete(desc);
3203
3204 return n_args;
3205}
3206
David Ahernd54b1a92012-08-26 12:24:46 -06003207static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003208{
3209 char **__argv = malloc(sizeof(const char *) * argc);
3210
David Ahernd54b1a92012-08-26 12:24:46 -06003211 if (!__argv) {
3212 pr_err("malloc failed\n");
3213 return -1;
3214 }
3215
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003216 memcpy(__argv, argv, sizeof(const char *) * argc);
3217 argc = parse_options(argc, (const char **)__argv, record_options,
3218 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3219 free(__argv);
3220
David Ahernd54b1a92012-08-26 12:24:46 -06003221 system_wide = (argc == 0);
3222
3223 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003224}
3225
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003226static void script__setup_sample_type(struct perf_script *script)
3227{
3228 struct perf_session *session = script->session;
3229 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3230
3231 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3232 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003233 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003234 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003235 dwarf_callchain_users = true;
3236 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003237 callchain_param.record_mode = CALLCHAIN_LBR;
3238 else
3239 callchain_param.record_mode = CALLCHAIN_FP;
3240 }
3241}
3242
Jiri Olsa89f16882018-09-13 14:54:03 +02003243static int process_stat_round_event(struct perf_session *session,
3244 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003245{
3246 struct stat_round_event *round = &event->stat_round;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003247 struct evsel *counter;
Jiri Olsae099eba2016-01-05 22:09:09 +01003248
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003249 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003250 perf_stat_process_counter(&stat_config, counter);
3251 process_stat(counter, round->time);
3252 }
3253
3254 process_stat_interval(round->time);
3255 return 0;
3256}
3257
Jiri Olsa89f16882018-09-13 14:54:03 +02003258static int process_stat_config_event(struct perf_session *session __maybe_unused,
3259 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003260{
3261 perf_event__read_stat_config(&stat_config, &event->stat_config);
3262 return 0;
3263}
3264
Jiri Olsacfc88742016-01-05 22:09:06 +01003265static int set_maps(struct perf_script *script)
3266{
Jiri Olsa63503db2019-07-21 13:23:52 +02003267 struct evlist *evlist = script->session->evlist;
Jiri Olsacfc88742016-01-05 22:09:06 +01003268
3269 if (!script->cpus || !script->threads)
3270 return 0;
3271
3272 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3273 return -EINVAL;
3274
Jiri Olsa453fa032019-07-21 13:24:43 +02003275 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
Jiri Olsacfc88742016-01-05 22:09:06 +01003276
3277 if (perf_evlist__alloc_stats(evlist, true))
3278 return -ENOMEM;
3279
3280 script->allocated = true;
3281 return 0;
3282}
3283
3284static
Jiri Olsa89f16882018-09-13 14:54:03 +02003285int process_thread_map_event(struct perf_session *session,
3286 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003287{
Jiri Olsa89f16882018-09-13 14:54:03 +02003288 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003289 struct perf_script *script = container_of(tool, struct perf_script, tool);
3290
3291 if (script->threads) {
3292 pr_warning("Extra thread map event, ignoring.\n");
3293 return 0;
3294 }
3295
3296 script->threads = thread_map__new_event(&event->thread_map);
3297 if (!script->threads)
3298 return -ENOMEM;
3299
3300 return set_maps(script);
3301}
3302
3303static
Jiri Olsa89f16882018-09-13 14:54:03 +02003304int process_cpu_map_event(struct perf_session *session,
3305 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003306{
Jiri Olsa89f16882018-09-13 14:54:03 +02003307 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003308 struct perf_script *script = container_of(tool, struct perf_script, tool);
3309
3310 if (script->cpus) {
3311 pr_warning("Extra cpu map event, ignoring.\n");
3312 return 0;
3313 }
3314
3315 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3316 if (!script->cpus)
3317 return -ENOMEM;
3318
3319 return set_maps(script);
3320}
3321
Jiri Olsa89f16882018-09-13 14:54:03 +02003322static int process_feature_event(struct perf_session *session,
3323 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303324{
3325 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003326 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303327 return 0;
3328}
3329
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003330#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003331static int perf_script__process_auxtrace_info(struct perf_session *session,
3332 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003333{
Jiri Olsa89f16882018-09-13 14:54:03 +02003334 struct perf_tool *tool = session->tool;
3335
3336 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003337
3338 if (ret == 0) {
3339 struct perf_script *script = container_of(tool, struct perf_script, tool);
3340
3341 ret = perf_script__setup_per_event_dump(script);
3342 }
3343
3344 return ret;
3345}
3346#else
3347#define perf_script__process_auxtrace_info 0
3348#endif
3349
Andi Kleenb585ebd2018-09-20 11:05:36 -07003350static int parse_insn_trace(const struct option *opt __maybe_unused,
3351 const char *str __maybe_unused,
3352 int unset __maybe_unused)
3353{
3354 parse_output_fields(NULL, "+insn,-event,-period", 0);
3355 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003356 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003357 return 0;
3358}
3359
3360static int parse_xed(const struct option *opt __maybe_unused,
3361 const char *str __maybe_unused,
3362 int unset __maybe_unused)
3363{
3364 force_pager("xed -F insn: -A -64 | less");
3365 return 0;
3366}
3367
Andi Kleend1b15522018-09-20 11:05:38 -07003368static int parse_call_trace(const struct option *opt __maybe_unused,
3369 const char *str __maybe_unused,
3370 int unset __maybe_unused)
3371{
3372 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3373 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003374 symbol_conf.nanosecs = true;
Jiri Olsa1c492422019-05-08 15:20:05 +02003375 symbol_conf.pad_output_len_dso = 50;
Andi Kleend1b15522018-09-20 11:05:38 -07003376 return 0;
3377}
3378
3379static int parse_callret_trace(const struct option *opt __maybe_unused,
3380 const char *str __maybe_unused,
3381 int unset __maybe_unused)
3382{
3383 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3384 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003385 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003386 return 0;
3387}
3388
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003389int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003390{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003391 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003392 bool header = false;
3393 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003394 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003395 char *rec_script_path = NULL;
3396 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003397 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003398 struct itrace_synth_opts itrace_synth_opts = {
3399 .set = false,
3400 .default_no_sample = true,
3401 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003402 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003403 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003404 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003405 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003406 struct perf_script script = {
3407 .tool = {
3408 .sample = process_sample_event,
3409 .mmap = perf_event__process_mmap,
3410 .mmap2 = perf_event__process_mmap2,
3411 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303412 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003413 .exit = perf_event__process_exit,
3414 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003415 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003416 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003417 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303418 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003419 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003420 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003421 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003422 .auxtrace = perf_event__process_auxtrace,
3423 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003424 .stat = perf_event__process_stat_event,
3425 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003426 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003427 .thread_map = process_thread_map_event,
3428 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003429 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003430 .ordering_requires_timestamps = true,
3431 },
3432 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003433 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003434 .mode = PERF_DATA_MODE_READ,
3435 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003436 const struct option options[] = {
3437 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3438 "dump raw trace in ASCII"),
3439 OPT_INCR('v', "verbose", &verbose,
3440 "be more verbose (show symbol address, etc)"),
3441 OPT_BOOLEAN('L', "Latency", &latency_format,
3442 "show latency attributes (irqs/preemption disabled, etc)"),
3443 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3444 list_available_scripts),
3445 OPT_CALLBACK('s', "script", NULL, "name",
3446 "script file name (lang:script name, script name, or *)",
3447 parse_scriptname),
3448 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3449 "generate perf-script.xx script in specified language"),
3450 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3451 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3452 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003453 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3454 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003455 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3456 "file", "vmlinux pathname"),
3457 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3458 "file", "kallsyms pathname"),
3459 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3460 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003461 OPT_CALLBACK(0, "symfs", NULL, "directory",
3462 "Look for files with symbols relative to this directory",
3463 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003464 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003465 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003466 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003467 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003468 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303469 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3470 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
Adrian Hunter68fb45b2019-05-20 14:37:14 +03003471 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003472 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003473 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3474 "system-wide collection from all CPUs"),
3475 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3476 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003477 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3478 "Decode instructions from itrace", parse_insn_trace),
3479 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3480 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003481 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3482 "Decode calls from from itrace", parse_call_trace),
3483 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3484 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003485 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3486 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003487 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3488 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003489 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3490 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3491 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003492 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3493 "only consider symbols in these pids"),
3494 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3495 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003496 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3497 "Set the maximum stack depth when parsing the callchain, "
3498 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003499 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Andi Kleen90b10f42019-03-14 15:50:00 -07003500 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003501 OPT_BOOLEAN('I', "show-info", &show_full_info,
3502 "display extended information from perf.data file"),
3503 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3504 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003505 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3506 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003507 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3508 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003509 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3510 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303511 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3512 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003513 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3514 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003515 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3516 "Show round events (if recorded)"),
Jiri Olsa490c8cc2019-05-08 15:20:08 +02003517 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3518 "Show bpf related events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003519 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3520 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003521 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003522 OPT_INTEGER(0, "max-blocks", &max_blocks,
3523 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003524 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003525 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003526 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003527 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003528 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003529 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3530 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003531 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3532 "Enable symbol demangling"),
3533 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3534 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003535 OPT_STRING(0, "time", &script.time_str, "str",
3536 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003537 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3538 "Show inline function"),
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003539 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3540 "guest mount directory under which every guest os"
3541 " instance has a subdir"),
3542 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3543 "file", "file saving guest os vmlinux"),
3544 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3545 "file", "file saving guest os /proc/kallsyms"),
3546 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3547 "file", "file saving guest os /proc/modules"),
Arnaldo Carvalho de Meloadd3a712019-08-15 11:21:21 -03003548 OPTS_EVSWITCH(&script.evswitch),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003549 OPT_END()
3550 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003551 const char * const script_subcommands[] = { "record", "report", NULL };
3552 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003553 "perf script [<options>]",
3554 "perf script [<options>] record <script> [<record-options>] <command>",
3555 "perf script [<options>] report <script> [script-args]",
3556 "perf script [<options>] <script> [<record-options>] <command>",
3557 "perf script [<options>] <top-script> [script-args]",
3558 NULL
3559 };
Tom Zanussi38752942009-12-15 02:53:39 -06003560
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003561 perf_set_singlethreaded();
3562
Tom Zanussib5b87312010-11-10 08:16:51 -06003563 setup_scripting();
3564
Yunlong Song40cae2b2015-03-18 21:35:54 +08003565 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003566 PARSE_OPT_STOP_AT_NON_OPTION);
3567
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003568 if (symbol_conf.guestmount ||
3569 symbol_conf.default_guest_vmlinux_name ||
3570 symbol_conf.default_guest_kallsyms ||
3571 symbol_conf.default_guest_modules) {
3572 /*
3573 * Enable guest sample processing.
3574 */
3575 perf_guest = true;
3576 }
3577
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003578 data.path = input_name;
3579 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003580
Tom Zanussib5b87312010-11-10 08:16:51 -06003581 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3582 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3583 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003584 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003585 }
3586
Tom Zanussib5b87312010-11-10 08:16:51 -06003587 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3588 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3589 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003590 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003591 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003592 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003593 return -1;
3594 }
Tom Zanussi38752942009-12-15 02:53:39 -06003595 }
3596
Andi Kleen90b10f42019-03-14 15:50:00 -07003597 if (script.time_str && reltime) {
3598 fprintf(stderr, "Don't combine --reltime with --time\n");
3599 return -1;
3600 }
3601
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003602 if (itrace_synth_opts.callchain &&
3603 itrace_synth_opts.callchain_sz > scripting_max_stack)
3604 scripting_max_stack = itrace_synth_opts.callchain_sz;
3605
Ben Hutchings44e668c2010-10-10 16:10:03 +01003606 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003607 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003608
Tom Zanussib5b87312010-11-10 08:16:51 -06003609 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003610 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003611 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003612 pid_t pid;
3613
Tom Zanussib5b87312010-11-10 08:16:51 -06003614 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3615 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3616
3617 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003618 usage_with_options_msg(script_usage, options,
3619 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003620 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003621 }
3622
Tom Zanussib5b87312010-11-10 08:16:51 -06003623 if (is_top_script(argv[0])) {
3624 rep_args = argc - 1;
3625 } else {
3626 int rec_args;
3627
3628 rep_args = has_required_arg(rep_script_path);
3629 rec_args = (argc - 1) - rep_args;
3630 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003631 usage_with_options_msg(script_usage, options,
3632 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003633 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003634 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003635 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003636 }
3637
3638 if (pipe(live_pipe) < 0) {
3639 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003640 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003641 }
3642
3643 pid = fork();
3644 if (pid < 0) {
3645 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003646 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003647 }
3648
3649 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003650 j = 0;
3651
Tom Zanussia0cccc22010-04-01 23:59:25 -05003652 dup2(live_pipe[1], 1);
3653 close(live_pipe[0]);
3654
Robert Richter317df652011-11-25 15:05:25 +01003655 if (is_top_script(argv[0])) {
3656 system_wide = true;
3657 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003658 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3659 err = -1;
3660 goto out;
3661 }
Robert Richter317df652011-11-25 15:05:25 +01003662 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003663
3664 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003665 if (!__argv) {
3666 pr_err("malloc failed\n");
3667 err = -ENOMEM;
3668 goto out;
3669 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003670
Tom Zanussib5b87312010-11-10 08:16:51 -06003671 __argv[j++] = "/bin/sh";
3672 __argv[j++] = rec_script_path;
3673 if (system_wide)
3674 __argv[j++] = "-a";
3675 __argv[j++] = "-q";
3676 __argv[j++] = "-o";
3677 __argv[j++] = "-";
3678 for (i = rep_args + 1; i < argc; i++)
3679 __argv[j++] = argv[i];
3680 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003681
3682 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003683 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003684 exit(-1);
3685 }
3686
3687 dup2(live_pipe[0], 0);
3688 close(live_pipe[1]);
3689
Tom Zanussib5b87312010-11-10 08:16:51 -06003690 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003691 if (!__argv) {
3692 pr_err("malloc failed\n");
3693 err = -ENOMEM;
3694 goto out;
3695 }
3696
Tom Zanussib5b87312010-11-10 08:16:51 -06003697 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003698 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003699 __argv[j++] = rep_script_path;
3700 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003701 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003702 __argv[j++] = "-i";
3703 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003704 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003705
3706 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003707 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003708 exit(-1);
3709 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003710
Tom Zanussib5b87312010-11-10 08:16:51 -06003711 if (rec_script_path)
3712 script_path = rec_script_path;
3713 if (rep_script_path)
3714 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003715
Tom Zanussib5b87312010-11-10 08:16:51 -06003716 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003717 j = 0;
3718
Robert Richter317df652011-11-25 15:05:25 +01003719 if (!rec_script_path)
3720 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003721 else if (!system_wide) {
3722 if (have_cmd(argc - 1, &argv[1]) != 0) {
3723 err = -1;
3724 goto out;
3725 }
3726 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003727
3728 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003729 if (!__argv) {
3730 pr_err("malloc failed\n");
3731 err = -ENOMEM;
3732 goto out;
3733 }
3734
Tom Zanussib5b87312010-11-10 08:16:51 -06003735 __argv[j++] = "/bin/sh";
3736 __argv[j++] = script_path;
3737 if (system_wide)
3738 __argv[j++] = "-a";
3739 for (i = 2; i < argc; i++)
3740 __argv[j++] = argv[i];
3741 __argv[j++] = NULL;
3742
3743 execvp("/bin/sh", (char **)__argv);
3744 free(__argv);
3745 exit(-1);
3746 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003747
Milian Wolffc1c9b962018-10-21 21:14:23 +02003748 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003749 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003750 use_browser = 0;
3751 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003752
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003753 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003754 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003755 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003756
Jiri Olsae90debd2013-12-09 11:02:50 +01003757 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003758 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003759 perf_session__fprintf_info(session, stdout, show_full_info);
3760 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003761 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003762 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003763 if (show_full_info)
3764 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003765
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003766 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003767 goto out_delete;
3768
Andi Kleen3ab481a2019-03-05 06:47:45 -08003769 uname(&uts);
Song Liu9d491692019-06-20 18:44:38 -07003770 if (data.is_pipe || /* assume pipe_mode indicates native_arch */
3771 !strcmp(uts.machine, session->header.env.arch) ||
Andi Kleen3ab481a2019-03-05 06:47:45 -08003772 (!strcmp(uts.machine, "x86_64") &&
3773 !strcmp(session->header.env.arch, "i386")))
3774 native_arch = true;
3775
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003776 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003777 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003778
Andi Kleen99f753f2018-09-20 11:05:39 -07003779 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3780 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003781 itrace_synth_opts.thread_stack = true;
3782
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003783 session->itrace_synth_opts = &itrace_synth_opts;
3784
Anton Blanchard5d67be92011-07-04 21:57:50 +10003785 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003786 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3787 if (err < 0)
3788 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003789 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003790 }
3791
David Ahern1424dc92011-03-09 22:23:28 -07003792 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003793 symbol_conf.use_callchain = true;
3794 else
3795 symbol_conf.use_callchain = false;
3796
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003797 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003798 tep_set_function_resolver(session->tevent.pevent,
3799 machine__resolve_kernel_addr,
3800 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003801 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003802 err = -1;
3803 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003804 }
3805
Tom Zanussi956ffd02009-11-25 01:15:46 -06003806 if (generate_script_lang) {
3807 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003808 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003809
David Ahern2c9e45f72011-03-17 10:03:21 -06003810 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003811 fprintf(stderr,
3812 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003813 err = -EINVAL;
3814 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003815 }
3816
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003817 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003818 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003819 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003820 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003821 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003822 }
3823
3824 err = fstat(input, &perf_stat);
3825 if (err < 0) {
3826 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003827 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003828 }
3829
3830 if (!perf_stat.st_size) {
3831 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003832 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003833 }
3834
3835 scripting_ops = script_spec__lookup(generate_script_lang);
3836 if (!scripting_ops) {
3837 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003838 err = -ENOENT;
3839 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003840 }
3841
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003842 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003843 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003844 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003845 }
3846
3847 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003848 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003849 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003850 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003851 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003852 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003853 }
3854
David Ahern9cbdb702011-04-06 21:54:20 -06003855
3856 err = perf_session__check_output_opt(session);
3857 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003858 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003859
Jin Yao284c4e12019-03-01 18:13:06 +08003860 if (script.time_str) {
3861 err = perf_time__parse_for_ranges(script.time_str, session,
3862 &script.ptime_range,
3863 &script.range_size,
3864 &script.range_num);
3865 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08003866 goto out_delete;
Adrian Hunter400ae982019-06-04 16:00:00 +03003867
3868 itrace_synth_opts__set_time_range(&itrace_synth_opts,
3869 script.ptime_range,
3870 script.range_num);
David Aherna91f4c42016-11-29 10:15:43 -07003871 }
3872
Arnaldo Carvalho de Melo124e02b2019-08-15 11:31:29 -03003873 err = evswitch__init(&script.evswitch, session->evlist, stderr);
3874 if (err)
3875 goto out_delete;
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003876
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003877 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003878
Adrian Hunterd445dd22014-08-15 22:08:37 +03003879 flush_scripting();
3880
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003881out_delete:
Adrian Hunter400ae982019-06-04 16:00:00 +03003882 if (script.ptime_range) {
3883 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
Jin Yao284c4e12019-03-01 18:13:06 +08003884 zfree(&script.ptime_range);
Adrian Hunter400ae982019-06-04 16:00:00 +03003885 }
Jin Yaocc2ef582018-01-10 23:00:33 +08003886
Jiri Olsacfc88742016-01-05 22:09:06 +01003887 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003888 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003889
3890 if (script_started)
3891 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003892out:
3893 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003894}