blob: 9739b05b999eb0de8fa4a28cac452881bd3f5974 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
David Howellsd2709c72012-11-19 22:21:03 +00002#include <linux/hw_breakpoint.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +02003#include <linux/err.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -03004#include <linux/zalloc.h>
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -03005#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03006#include <errno.h>
Arnaldo Carvalho de Melo86a5e0c2017-04-19 19:03:14 -03007#include <sys/ioctl.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -03008#include <sys/types.h>
9#include <sys/stat.h>
10#include <fcntl.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030011#include <sys/param.h>
Arnaldo Carvalho de Melob0742e92017-04-18 11:08:10 -030012#include "term.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -030013#include "build-id.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020014#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020015#include "evsel.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030016#include <subcmd/pager.h>
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060017#include <subcmd/parse-options.h>
Ingo Molnar8ad8db32009-05-26 11:10:09 +020018#include "parse-events.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060019#include <subcmd/exec-cmd.h>
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030020#include "string2.h"
Arnaldo Carvalho de Melo8ec20b12017-04-18 10:57:25 -030021#include "strlist.h"
Wang Nan84c86ca2015-10-14 12:41:14 +000022#include "bpf-loader.h"
Masami Hiramatsu6e81c742014-08-14 02:22:36 +000023#include "debug.h"
Jiri Olsa592d5a62015-09-02 09:56:34 +020024#include <api/fs/tracing_path.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +020025#include <perf/cpumap.h>
Zheng Yanac20de6f2012-06-15 14:31:39 +080026#include "parse-events-bison.h"
Jiri Olsa1244a322020-05-25 00:42:08 +020027#define YY_EXTRA_TYPE void*
Jiri Olsa89812fc2012-03-15 20:09:15 +010028#include "parse-events-flex.h"
Jiri Olsa5f537a22012-03-15 20:09:18 +010029#include "pmu.h"
Namhyung Kimb41f1ce2013-08-27 11:41:53 +090030#include "thread_map.h"
Masami Hiramatsu40218da2016-06-24 18:06:46 +090031#include "probe-file.h"
Jiri Olsab39b8392015-04-22 21:10:16 +020032#include "asm/bug.h"
Andi Kleenac12f672016-10-12 14:02:06 -070033#include "util/parse-branch-options.h"
Andi Kleen71b0acc2017-08-31 12:40:32 -070034#include "metricgroup.h"
Arnaldo Carvalho de Melo95be9d12019-09-24 15:56:14 -030035#include "util/evsel_config.h"
Arnaldo Carvalho de Melo252a2fd2019-09-24 16:07:59 -030036#include "util/event.h"
Stephane Eranian70943492020-05-05 11:29:43 -070037#include "util/pfm.h"
Jin Yao9cbfa2f2021-04-27 15:01:21 +080038#include "util/parse-events-hybrid.h"
Jin Yao5e4edd12021-04-27 15:01:25 +080039#include "util/pmu-hybrid.h"
Jin Yao943b69a2020-08-14 09:21:20 +080040#include "perf.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010041
42#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020043
Jiri Olsa82ba1f22012-05-21 09:12:50 +020044#ifdef PARSER_DEBUG
45extern int parse_events_debug;
46#endif
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -030047int parse_events_parse(void *parse_state, void *scanner);
He Kuange637d172015-09-28 03:52:16 +000048static int get_config_terms(struct list_head *head_config,
49 struct list_head *head_terms __maybe_unused);
Jin Yao5e4edd12021-04-27 15:01:25 +080050static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state,
51 const char *str, char *pmu_name,
52 struct list_head *list);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020053
Kan Liangdcb4e102014-10-07 11:08:50 -040054static struct perf_pmu_event_symbol *perf_pmu_events_list;
55/*
56 * The variable indicates the number of supported pmu event symbols.
57 * 0 means not initialized and ready to init
58 * -1 means failed to init, don't try anymore
59 * >0 is the number of supported pmu event symbols
60 */
61static int perf_pmu_events_list_num;
62
Yunlong Song705750f2015-02-27 18:21:27 +080063struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020064 [PERF_COUNT_HW_CPU_CYCLES] = {
65 .symbol = "cpu-cycles",
66 .alias = "cycles",
67 },
68 [PERF_COUNT_HW_INSTRUCTIONS] = {
69 .symbol = "instructions",
70 .alias = "",
71 },
72 [PERF_COUNT_HW_CACHE_REFERENCES] = {
73 .symbol = "cache-references",
74 .alias = "",
75 },
76 [PERF_COUNT_HW_CACHE_MISSES] = {
77 .symbol = "cache-misses",
78 .alias = "",
79 },
80 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
81 .symbol = "branch-instructions",
82 .alias = "branches",
83 },
84 [PERF_COUNT_HW_BRANCH_MISSES] = {
85 .symbol = "branch-misses",
86 .alias = "",
87 },
88 [PERF_COUNT_HW_BUS_CYCLES] = {
89 .symbol = "bus-cycles",
90 .alias = "",
91 },
92 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
93 .symbol = "stalled-cycles-frontend",
94 .alias = "idle-cycles-frontend",
95 },
96 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
97 .symbol = "stalled-cycles-backend",
98 .alias = "idle-cycles-backend",
99 },
100 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
101 .symbol = "ref-cycles",
102 .alias = "",
103 },
104};
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200105
Yunlong Song705750f2015-02-27 18:21:27 +0800106struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +0200107 [PERF_COUNT_SW_CPU_CLOCK] = {
108 .symbol = "cpu-clock",
109 .alias = "",
110 },
111 [PERF_COUNT_SW_TASK_CLOCK] = {
112 .symbol = "task-clock",
113 .alias = "",
114 },
115 [PERF_COUNT_SW_PAGE_FAULTS] = {
116 .symbol = "page-faults",
117 .alias = "faults",
118 },
119 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
120 .symbol = "context-switches",
121 .alias = "cs",
122 },
123 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
124 .symbol = "cpu-migrations",
125 .alias = "migrations",
126 },
127 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
128 .symbol = "minor-faults",
129 .alias = "",
130 },
131 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
132 .symbol = "major-faults",
133 .alias = "",
134 },
135 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
136 .symbol = "alignment-faults",
137 .alias = "",
138 },
139 [PERF_COUNT_SW_EMULATION_FAULTS] = {
140 .symbol = "emulation-faults",
141 .alias = "",
142 },
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300143 [PERF_COUNT_SW_DUMMY] = {
144 .symbol = "dummy",
145 .alias = "",
146 },
Arnaldo Carvalho de Melobae9cc42015-11-27 15:54:33 -0300147 [PERF_COUNT_SW_BPF_OUTPUT] = {
148 .symbol = "bpf-output",
149 .alias = "",
150 },
Namhyung Kimfb6c79d2021-02-10 17:33:27 +0900151 [PERF_COUNT_SW_CGROUP_SWITCHES] = {
152 .symbol = "cgroup-switches",
153 .alias = "",
154 },
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200155};
156
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200157#define __PERF_EVENT_FIELD(config, name) \
158 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +0200159
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200160#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200161#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200162#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200163#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +0200164
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300165#define for_each_subsystem(sys_dir, sys_dirent) \
166 while ((sys_dirent = readdir(sys_dir)) != NULL) \
167 if (sys_dirent->d_type == DT_DIR && \
168 (strcmp(sys_dirent->d_name, ".")) && \
169 (strcmp(sys_dirent->d_name, "..")))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400170
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300171static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
Peter Zijlstraae07b632009-08-06 16:48:54 +0200172{
173 char evt_path[MAXPATHLEN];
174 int fd;
175
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300176 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
Peter Zijlstraae07b632009-08-06 16:48:54 +0200177 fd = open(evt_path, O_RDONLY);
178 if (fd < 0)
179 return -EINVAL;
180 close(fd);
181
182 return 0;
183}
184
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300185#define for_each_event(dir_path, evt_dir, evt_dirent) \
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300186 while ((evt_dirent = readdir(evt_dir)) != NULL) \
187 if (evt_dirent->d_type == DT_DIR && \
188 (strcmp(evt_dirent->d_name, ".")) && \
189 (strcmp(evt_dirent->d_name, "..")) && \
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300190 (!tp_event_has_id(dir_path, evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400191
Li Zefan270bbbe2009-09-17 16:34:51 +0800192#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400193
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200194struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400195{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200196 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400197 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300198 struct dirent *sys_dirent, *evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100199 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200200 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400201 u64 id;
202 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300203 char *dir_path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400204
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -0300205 sys_dir = tracing_events__opendir();
Jason Baronf6bdafe2009-07-21 12:20:22 -0400206 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200207 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400208
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300209 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300210 dir_path = get_events_file(sys_dirent->d_name);
211 if (!dir_path)
212 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200213 evt_dir = opendir(dir_path);
214 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300215 goto next;
Eric Dumazet725b1362009-09-24 15:39:09 +0200216
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300217 for_each_event(dir_path, evt_dir, evt_dirent) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200218
Jiri Olsa77f18152018-03-19 09:29:01 +0100219 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
220 evt_dirent->d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200221 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400222 if (fd < 0)
223 continue;
224 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
225 close(fd);
226 continue;
227 }
228 close(fd);
229 id = atoll(id_buf);
230 if (id == config) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300231 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400232 closedir(evt_dir);
233 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100234 path = zalloc(sizeof(*path));
Taeung Song5aa365f2017-02-01 21:34:05 +0900235 if (!path)
236 return NULL;
Arnaldo Carvalho de Melo7125f202020-03-02 11:55:47 -0300237 if (asprintf(&path->system, "%.*s", MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) {
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200238 free(path);
239 return NULL;
240 }
Arnaldo Carvalho de Melo7125f202020-03-02 11:55:47 -0300241 if (asprintf(&path->name, "%.*s", MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300242 zfree(&path->system);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200243 free(path);
244 return NULL;
245 }
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200246 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400247 }
248 }
249 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300250next:
251 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400252 }
253
Jason Baronf6bdafe2009-07-21 12:20:22 -0400254 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200255 return NULL;
256}
257
Namhyung Kime7c93f02013-06-26 16:14:05 +0900258struct tracepoint_path *tracepoint_name_to_path(const char *name)
259{
260 struct tracepoint_path *path = zalloc(sizeof(*path));
261 char *str = strchr(name, ':');
262
263 if (path == NULL || str == NULL) {
264 free(path);
265 return NULL;
266 }
267
268 path->system = strndup(name, str - name);
269 path->name = strdup(str+1);
270
271 if (path->system == NULL || path->name == NULL) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300272 zfree(&path->system);
273 zfree(&path->name);
Taeung Song506fde12017-02-01 21:34:06 +0900274 zfree(&path);
Namhyung Kime7c93f02013-06-26 16:14:05 +0900275 }
276
277 return path;
278}
279
David Ahern1424dc92011-03-09 22:23:28 -0700280const char *event_type(int type)
281{
282 switch (type) {
283 case PERF_TYPE_HARDWARE:
284 return "hardware";
285
286 case PERF_TYPE_SOFTWARE:
287 return "software";
288
289 case PERF_TYPE_TRACEPOINT:
290 return "tracepoint";
291
292 case PERF_TYPE_HW_CACHE:
293 return "hardware-cache";
294
295 default:
296 break;
297 }
298
299 return "unknown";
300}
301
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700302static char *get_config_str(struct list_head *head_terms, int type_term)
Wang Nane814fdd2016-02-19 11:43:59 +0000303{
304 struct parse_events_term *term;
305
306 if (!head_terms)
307 return NULL;
308
309 list_for_each_entry(term, head_terms, list)
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700310 if (term->type_term == type_term)
Wang Nane814fdd2016-02-19 11:43:59 +0000311 return term->val.str;
312
313 return NULL;
314}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800315
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700316static char *get_config_metric_id(struct list_head *head_terms)
317{
318 return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID);
319}
320
321static char *get_config_name(struct list_head *head_terms)
322{
323 return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME);
324}
325
Jiri Olsa32dcd022019-07-21 13:23:51 +0200326static struct evsel *
Stephane Eranian410136f2013-11-12 17:58:49 +0100327__add_event(struct list_head *list, int *idx,
328 struct perf_event_attr *attr,
Stephane Eranian70943492020-05-05 11:29:43 -0700329 bool init_attr,
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700330 const char *name, const char *metric_id, struct perf_pmu *pmu,
Andi Kleenf0fbb112019-03-26 15:18:21 -0700331 struct list_head *config_terms, bool auto_merge_stats,
332 const char *cpu_list)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100333{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200334 struct evsel *evsel;
Namhyung Kim5d680be2020-09-17 15:02:19 +0900335 struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) :
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200336 cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100337
Jin Yaoe4064772021-03-10 13:11:38 +0800338 if (pmu && attr->type == PERF_TYPE_RAW)
339 perf_pmu__warn_invalid_config(pmu, attr->config, name);
340
Stephane Eranian70943492020-05-05 11:29:43 -0700341 if (init_attr)
342 event_attr_init(attr);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100343
Arnaldo Carvalho de Melo8f6725a2020-05-06 13:27:04 -0300344 evsel = evsel__new_idx(attr, *idx);
Namhyung Kim0f1b5502020-09-17 15:02:18 +0900345 if (!evsel) {
346 perf_cpu_map__put(cpus);
Stephane Eranian410136f2013-11-12 17:58:49 +0100347 return NULL;
Namhyung Kim0f1b5502020-09-17 15:02:18 +0900348 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100349
Taeung Song75fc5ae2017-02-01 21:34:04 +0900350 (*idx)++;
Namhyung Kim5d680be2020-09-17 15:02:19 +0900351 evsel->core.cpus = cpus;
Jiri Olsafe1f61b2019-07-21 13:24:38 +0200352 evsel->core.own_cpus = perf_cpu_map__get(cpus);
Jiri Olsa648b5af2019-08-06 11:35:19 +0200353 evsel->core.system_wide = pmu ? pmu->is_uncore : false;
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -0300354 evsel->auto_merge_stats = auto_merge_stats;
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200355
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300356 if (name)
357 evsel->name = strdup(name);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400358
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700359 if (metric_id)
360 evsel->metric_id = strdup(metric_id);
361
Jiri Olsa930a2e22015-07-29 05:42:10 -0400362 if (config_terms)
Adrian Hunter99fc5942021-09-09 15:55:08 +0300363 list_splice_init(config_terms, &evsel->config_terms);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400364
Stephane Eranian70943492020-05-05 11:29:43 -0700365 if (list)
366 list_add_tail(&evsel->core.node, list);
367
Stephane Eranian410136f2013-11-12 17:58:49 +0100368 return evsel;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100369}
370
Stephane Eranian70943492020-05-05 11:29:43 -0700371struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700372 const char *name, const char *metric_id,
373 struct perf_pmu *pmu)
Stephane Eranian70943492020-05-05 11:29:43 -0700374{
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700375 return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name,
376 metric_id, pmu, /*config_terms=*/NULL,
377 /*auto_merge_stats=*/false, /*cpu_list=*/NULL);
Stephane Eranian70943492020-05-05 11:29:43 -0700378}
379
David Ahernc5cd8ac2013-07-02 13:27:25 -0600380static int add_event(struct list_head *list, int *idx,
Ian Rogers8e8bbfb2021-10-15 10:21:24 -0700381 struct perf_event_attr *attr, const char *name,
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700382 const char *metric_id, struct list_head *config_terms)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800383{
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700384 return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id,
385 /*pmu=*/NULL, config_terms,
386 /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM;
Andi Kleenf0fbb112019-03-26 15:18:21 -0700387}
388
389static int add_event_tool(struct list_head *list, int *idx,
390 enum perf_tool_event tool_event)
391{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200392 struct evsel *evsel;
Andi Kleenf0fbb112019-03-26 15:18:21 -0700393 struct perf_event_attr attr = {
394 .type = PERF_TYPE_SOFTWARE,
395 .config = PERF_COUNT_SW_DUMMY,
396 };
397
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700398 evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL,
399 /*metric_id=*/NULL, /*pmu=*/NULL,
400 /*config_terms=*/NULL, /*auto_merge_stats=*/false,
401 /*cpu_list=*/"0");
Andi Kleenf0fbb112019-03-26 15:18:21 -0700402 if (!evsel)
403 return -ENOMEM;
404 evsel->tool_event = tool_event;
Ian Rogersb194c9c2021-11-18 00:47:49 -0800405 if (tool_event == PERF_TOOL_DURATION_TIME) {
406 free((char *)evsel->unit);
407 evsel->unit = strdup("ns");
408 }
Andi Kleenf0fbb112019-03-26 15:18:21 -0700409 return 0;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800410}
411
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300412static int parse_aliases(char *str, const char *names[][EVSEL__MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200413{
414 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000415 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200416
417 for (i = 0; i < size; i++) {
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300418 for (j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000419 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100420 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000421 longest = n;
422 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100423 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000424 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200425 }
426
Ingo Molnar89536452009-06-06 21:04:17 +0200427 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200428}
429
Wang Nan43d0b972016-02-19 11:44:01 +0000430typedef int config_term_func_t(struct perf_event_attr *attr,
431 struct parse_events_term *term,
432 struct parse_events_error *err);
433static int config_term_common(struct perf_event_attr *attr,
434 struct parse_events_term *term,
435 struct parse_events_error *err);
436static int config_attr(struct perf_event_attr *attr,
437 struct list_head *head,
438 struct parse_events_error *err,
439 config_term_func_t config_term);
440
David Ahernc5cd8ac2013-07-02 13:27:25 -0600441int parse_events_add_cache(struct list_head *list, int *idx,
Wang Nan43d0b972016-02-19 11:44:01 +0000442 char *type, char *op_result1, char *op_result2,
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300443 struct parse_events_error *err,
Jin Yaoc93afad2021-04-27 15:01:24 +0800444 struct list_head *head_config,
445 struct parse_events_state *parse_state)
Ingo Molnar8326f442009-06-05 20:22:46 +0200446{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100447 struct perf_event_attr attr;
Wang Nan43d0b972016-02-19 11:44:01 +0000448 LIST_HEAD(config_terms);
Ian Rogers8e8bbfb2021-10-15 10:21:24 -0700449 char name[MAX_NAME_LEN];
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700450 const char *config_name, *metric_id;
Paul Mackerras61c45982009-07-01 13:04:34 +1000451 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100452 char *op_result[2] = { op_result1, op_result2 };
Jin Yao30def612021-04-27 15:01:22 +0800453 int i, n, ret;
454 bool hybrid;
Ingo Molnar8326f442009-06-05 20:22:46 +0200455
Ingo Molnar8326f442009-06-05 20:22:46 +0200456 /*
457 * No fallback - if we cannot get a clear cache type
458 * then bail out:
459 */
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300460 cache_type = parse_aliases(type, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200461 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100462 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200463
Wang Nan43d0b972016-02-19 11:44:01 +0000464 config_name = get_config_name(head_config);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100465 n = snprintf(name, MAX_NAME_LEN, "%s", type);
466
467 for (i = 0; (i < 2) && (op_result[i]); i++) {
468 char *str = op_result[i];
469
Jiri Olsa275ef382012-09-05 19:51:33 +0200470 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000471
472 if (cache_op == -1) {
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300473 cache_op = parse_aliases(str, evsel__hw_cache_op,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100474 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000475 if (cache_op >= 0) {
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300476 if (!evsel__is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100477 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000478 continue;
479 }
480 }
481
482 if (cache_result == -1) {
Arnaldo Carvalho de Meloc64e85e2020-05-06 13:32:23 -0300483 cache_result = parse_aliases(str, evsel__hw_cache_result,
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300484 PERF_COUNT_HW_CACHE_RESULT_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000485 if (cache_result >= 0)
486 continue;
487 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000488 }
489
Ingo Molnar8326f442009-06-05 20:22:46 +0200490 /*
491 * Fall back to reads:
492 */
Ingo Molnar89536452009-06-06 21:04:17 +0200493 if (cache_op == -1)
494 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200495
Ingo Molnar8326f442009-06-05 20:22:46 +0200496 /*
497 * Fall back to accesses:
498 */
499 if (cache_result == -1)
500 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
501
Jiri Olsa89812fc2012-03-15 20:09:15 +0100502 memset(&attr, 0, sizeof(attr));
503 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
504 attr.type = PERF_TYPE_HW_CACHE;
Wang Nan43d0b972016-02-19 11:44:01 +0000505
506 if (head_config) {
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300507 if (config_attr(&attr, head_config, err,
Wang Nan43d0b972016-02-19 11:44:01 +0000508 config_term_common))
509 return -EINVAL;
510
511 if (get_config_terms(head_config, &config_terms))
512 return -ENOMEM;
513 }
Jin Yao30def612021-04-27 15:01:22 +0800514
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700515 metric_id = get_config_metric_id(head_config);
Jin Yao30def612021-04-27 15:01:22 +0800516 ret = parse_events__add_cache_hybrid(list, idx, &attr,
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700517 config_name ? : name,
518 metric_id,
519 &config_terms,
Jin Yaoc93afad2021-04-27 15:01:24 +0800520 &hybrid, parse_state);
Jin Yao30def612021-04-27 15:01:22 +0800521 if (hybrid)
Adrian Hunter99fc5942021-09-09 15:55:08 +0300522 goto out_free_terms;
Jin Yao30def612021-04-27 15:01:22 +0800523
Ian Rogers2b62b3a2021-10-15 10:21:25 -0700524 ret = add_event(list, idx, &attr, config_name ? : name, metric_id,
525 &config_terms);
Adrian Hunter99fc5942021-09-09 15:55:08 +0300526out_free_terms:
527 free_config_terms(&config_terms);
528 return ret;
Ingo Molnar8326f442009-06-05 20:22:46 +0200529}
530
Jiri Olsa272ed292015-09-29 17:05:31 +0200531static void tracepoint_error(struct parse_events_error *e, int err,
Wang Nan8c619d62016-07-13 10:44:03 +0000532 const char *sys, const char *name)
Jiri Olsa19658172015-09-07 10:38:07 +0200533{
Ian Rogers4584f082019-11-20 10:09:25 -0800534 const char *str;
Jiri Olsa19658172015-09-07 10:38:07 +0200535 char help[BUFSIZ];
536
Adrian Hunterec183d22016-01-26 14:05:20 +0200537 if (!e)
538 return;
539
Jiri Olsa19658172015-09-07 10:38:07 +0200540 /*
541 * We get error directly from syscall errno ( > 0),
542 * or from encoded pointer's error ( < 0).
543 */
544 err = abs(err);
545
546 switch (err) {
547 case EACCES:
Ian Rogers4584f082019-11-20 10:09:25 -0800548 str = "can't access trace events";
Jiri Olsa19658172015-09-07 10:38:07 +0200549 break;
550 case ENOENT:
Ian Rogers4584f082019-11-20 10:09:25 -0800551 str = "unknown tracepoint";
Jiri Olsa19658172015-09-07 10:38:07 +0200552 break;
553 default:
Ian Rogers4584f082019-11-20 10:09:25 -0800554 str = "failed to add tracepoint";
Jiri Olsa19658172015-09-07 10:38:07 +0200555 break;
556 }
557
558 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
Ian Rogers6c191282021-11-07 01:00:00 -0800559 parse_events_error__handle(e, 0, strdup(str), strdup(help));
Jiri Olsa19658172015-09-07 10:38:07 +0200560}
561
David Ahernc5cd8ac2013-07-02 13:27:25 -0600562static int add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000563 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200564 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000565 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200566{
Arnaldo Carvalho de Melo8f6725a2020-05-06 13:27:04 -0300567 struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200568
Jiri Olsa19658172015-09-07 10:38:07 +0200569 if (IS_ERR(evsel)) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200570 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200571 return PTR_ERR(evsel);
Jiri Olsa19658172015-09-07 10:38:07 +0200572 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200573
He Kuange637d172015-09-28 03:52:16 +0000574 if (head_config) {
575 LIST_HEAD(config_terms);
576
577 if (get_config_terms(head_config, &config_terms))
578 return -ENOMEM;
579 list_splice(&config_terms, &evsel->config_terms);
580 }
581
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200582 list_add_tail(&evsel->core.node, list);
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300583 return 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200584}
585
David Ahernc5cd8ac2013-07-02 13:27:25 -0600586static int add_tracepoint_multi_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000587 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200588 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000589 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200590{
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300591 char *evt_path;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200592 struct dirent *evt_ent;
593 DIR *evt_dir;
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200594 int ret = 0, found = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200595
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300596 evt_path = get_events_file(sys_name);
597 if (!evt_path) {
598 tracepoint_error(err, errno, sys_name, evt_name);
599 return -1;
600 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200601 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200602 if (!evt_dir) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300603 put_events_file(evt_path);
Jiri Olsa272ed292015-09-29 17:05:31 +0200604 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100605 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200606 }
607
Jiri Olsa89812fc2012-03-15 20:09:15 +0100608 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200609 if (!strcmp(evt_ent->d_name, ".")
610 || !strcmp(evt_ent->d_name, "..")
611 || !strcmp(evt_ent->d_name, "enable")
612 || !strcmp(evt_ent->d_name, "filter"))
613 continue;
614
Jiri Olsa89812fc2012-03-15 20:09:15 +0100615 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500616 continue;
617
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200618 found++;
619
He Kuange637d172015-09-28 03:52:16 +0000620 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200621 err, head_config);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200622 }
623
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200624 if (!found) {
625 tracepoint_error(err, ENOENT, sys_name, evt_name);
626 ret = -1;
627 }
628
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300629 put_events_file(evt_path);
Jiri Olsa0bd3f082012-12-17 14:08:36 +0100630 closedir(evt_dir);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100631 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200632}
633
David Ahernc5cd8ac2013-07-02 13:27:25 -0600634static int add_tracepoint_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000635 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200636 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000637 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100638{
639 return strpbrk(evt_name, "*?") ?
He Kuange637d172015-09-28 03:52:16 +0000640 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200641 err, head_config) :
He Kuange637d172015-09-28 03:52:16 +0000642 add_tracepoint(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200643 err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100644}
645
David Ahernc5cd8ac2013-07-02 13:27:25 -0600646static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000647 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200648 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000649 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100650{
651 struct dirent *events_ent;
652 DIR *events_dir;
653 int ret = 0;
654
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -0300655 events_dir = tracing_events__opendir();
Jiri Olsaf35488f2012-12-17 14:08:37 +0100656 if (!events_dir) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200657 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100658 return -1;
659 }
660
661 while (!ret && (events_ent = readdir(events_dir))) {
662 if (!strcmp(events_ent->d_name, ".")
663 || !strcmp(events_ent->d_name, "..")
664 || !strcmp(events_ent->d_name, "enable")
665 || !strcmp(events_ent->d_name, "header_event")
666 || !strcmp(events_ent->d_name, "header_page"))
667 continue;
668
669 if (!strglobmatch(events_ent->d_name, sys_name))
670 continue;
671
672 ret = add_tracepoint_event(list, idx, events_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200673 evt_name, err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100674 }
675
676 closedir(events_dir);
677 return ret;
678}
679
Arnaldo Carvalho de Meloc18cf782020-10-20 14:12:37 -0300680#ifdef HAVE_LIBBPF_SUPPORT
Wang Nan4edf30e2015-10-14 12:41:17 +0000681struct __add_bpf_event_param {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300682 struct parse_events_state *parse_state;
Wang Nan4edf30e2015-10-14 12:41:17 +0000683 struct list_head *list;
Wang Nan95088a52016-02-22 09:10:36 +0000684 struct list_head *head_config;
Wang Nan4edf30e2015-10-14 12:41:17 +0000685};
686
Arnaldo Carvalho de Meloaf4a0992019-07-15 16:22:57 -0300687static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
Wang Nan4edf30e2015-10-14 12:41:17 +0000688 void *_param)
689{
690 LIST_HEAD(new_evsels);
691 struct __add_bpf_event_param *param = _param;
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300692 struct parse_events_state *parse_state = param->parse_state;
Wang Nan4edf30e2015-10-14 12:41:17 +0000693 struct list_head *list = param->list;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200694 struct evsel *pos;
Wang Nan4edf30e2015-10-14 12:41:17 +0000695 int err;
Arnaldo Carvalho de Melo2620b7e2019-07-15 15:29:34 -0300696 /*
697 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
698 * then don't add the tracepoint, this will be used for something else, like
699 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
700 *
701 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
702 */
703 if (group[0] == '!')
704 return 0;
Wang Nan4edf30e2015-10-14 12:41:17 +0000705
706 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
Wang Nancd102d72016-07-13 10:44:04 +0000707 group, event, fd);
Wang Nan4edf30e2015-10-14 12:41:17 +0000708
Arnaldo Carvalho de Melo5d369a72017-08-17 14:13:25 -0300709 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
710 event, parse_state->error,
Wang Nan95088a52016-02-22 09:10:36 +0000711 param->head_config);
Wang Nan4edf30e2015-10-14 12:41:17 +0000712 if (err) {
Jiri Olsa32dcd022019-07-21 13:23:51 +0200713 struct evsel *evsel, *tmp;
Wang Nan4edf30e2015-10-14 12:41:17 +0000714
715 pr_debug("Failed to add BPF event %s:%s\n",
Wang Nancd102d72016-07-13 10:44:04 +0000716 group, event);
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200717 list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
718 list_del_init(&evsel->core.node);
Jiri Olsa5eb2dd22019-07-21 13:23:57 +0200719 evsel__delete(evsel);
Wang Nan4edf30e2015-10-14 12:41:17 +0000720 }
721 return err;
722 }
Wang Nancd102d72016-07-13 10:44:04 +0000723 pr_debug("adding %s:%s\n", group, event);
Wang Nan4edf30e2015-10-14 12:41:17 +0000724
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200725 list_for_each_entry(pos, &new_evsels, core.node) {
Wang Nan1f45b1d2015-10-14 12:41:18 +0000726 pr_debug("adding %s:%s to %p\n",
Wang Nancd102d72016-07-13 10:44:04 +0000727 group, event, pos);
Wang Nan1f45b1d2015-10-14 12:41:18 +0000728 pos->bpf_fd = fd;
Arnaldo Carvalho de Meloaf4a0992019-07-15 16:22:57 -0300729 pos->bpf_obj = obj;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000730 }
Wang Nan4edf30e2015-10-14 12:41:17 +0000731 list_splice(&new_evsels, list);
732 return 0;
733}
734
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300735int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
Wang Nan84c86ca2015-10-14 12:41:14 +0000736 struct list_head *list,
Wang Nan95088a52016-02-22 09:10:36 +0000737 struct bpf_object *obj,
738 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000739{
740 int err;
741 char errbuf[BUFSIZ];
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300742 struct __add_bpf_event_param param = {parse_state, list, head_config};
Wang Nanaa3abf32015-10-14 12:41:15 +0000743 static bool registered_unprobe_atexit = false;
Wang Nan84c86ca2015-10-14 12:41:14 +0000744
745 if (IS_ERR(obj) || !obj) {
746 snprintf(errbuf, sizeof(errbuf),
747 "Internal error: load bpf obj with NULL");
748 err = -EINVAL;
749 goto errout;
750 }
751
752 /*
Wang Nanaa3abf32015-10-14 12:41:15 +0000753 * Register atexit handler before calling bpf__probe() so
754 * bpf__probe() don't need to unprobe probe points its already
755 * created when failure.
756 */
757 if (!registered_unprobe_atexit) {
758 atexit(bpf__clear);
759 registered_unprobe_atexit = true;
760 }
761
762 err = bpf__probe(obj);
763 if (err) {
764 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
765 goto errout;
766 }
767
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000768 err = bpf__load(obj);
769 if (err) {
770 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
771 goto errout;
772 }
773
Wang Nancd102d72016-07-13 10:44:04 +0000774 err = bpf__foreach_event(obj, add_bpf_event, &param);
Wang Nan4edf30e2015-10-14 12:41:17 +0000775 if (err) {
776 snprintf(errbuf, sizeof(errbuf),
777 "Attach events in BPF object failed");
778 goto errout;
779 }
780
781 return 0;
Wang Nan84c86ca2015-10-14 12:41:14 +0000782errout:
Ian Rogers6c191282021-11-07 01:00:00 -0800783 parse_events_error__handle(parse_state->error, 0,
Ian Rogers5f634c8e2020-07-07 14:14:49 -0700784 strdup(errbuf), strdup("(add -v to see detail)"));
Wang Nan84c86ca2015-10-14 12:41:14 +0000785 return err;
786}
787
Wang Nana34f3be2016-02-22 09:10:31 +0000788static int
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300789parse_events_config_bpf(struct parse_events_state *parse_state,
Wang Nana34f3be2016-02-22 09:10:31 +0000790 struct bpf_object *obj,
791 struct list_head *head_config)
792{
793 struct parse_events_term *term;
794 int error_pos;
795
796 if (!head_config || list_empty(head_config))
797 return 0;
798
799 list_for_each_entry(term, head_config, list) {
Wang Nana34f3be2016-02-22 09:10:31 +0000800 int err;
801
802 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
Ian Rogers6c191282021-11-07 01:00:00 -0800803 parse_events_error__handle(parse_state->error, term->err_term,
Ian Rogers5f634c8e2020-07-07 14:14:49 -0700804 strdup("Invalid config term for BPF object"),
805 NULL);
Wang Nana34f3be2016-02-22 09:10:31 +0000806 return -EINVAL;
807 }
808
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300809 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
Wang Nana34f3be2016-02-22 09:10:31 +0000810 if (err) {
Ian Rogers5f634c8e2020-07-07 14:14:49 -0700811 char errbuf[BUFSIZ];
812 int idx;
813
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300814 bpf__strerror_config_obj(obj, term, parse_state->evlist,
Wang Nana34f3be2016-02-22 09:10:31 +0000815 &error_pos, err, errbuf,
816 sizeof(errbuf));
Ian Rogers5f634c8e2020-07-07 14:14:49 -0700817
818 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
819 idx = term->err_val;
820 else
821 idx = term->err_term + error_pos;
822
Ian Rogers6c191282021-11-07 01:00:00 -0800823 parse_events_error__handle(parse_state->error, idx,
Ian Rogers5f634c8e2020-07-07 14:14:49 -0700824 strdup(errbuf),
825 strdup(
Wang Nan7630b3e2016-02-22 09:10:33 +0000826"Hint:\tValid config terms:\n"
Wang Nane571e022016-02-22 09:10:35 +0000827" \tmap:[<arraymap>].value<indices>=[value]\n"
828" \tmap:[<eventmap>].event<indices>=[event]\n"
Wang Nan7630b3e2016-02-22 09:10:33 +0000829"\n"
Wang Nane571e022016-02-22 09:10:35 +0000830" \twhere <indices> is something like [0,3...5] or [all]\n"
Ian Rogers5f634c8e2020-07-07 14:14:49 -0700831" \t(add -v to see detail)"));
Wang Nana34f3be2016-02-22 09:10:31 +0000832 return err;
833 }
834 }
835 return 0;
836}
837
Wang Nan95088a52016-02-22 09:10:36 +0000838/*
839 * Split config terms:
840 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
841 * 'call-graph=fp' is 'evt config', should be applied to each
842 * events in bpf.c.
843 * 'map:array.value[0]=1' is 'obj config', should be processed
844 * with parse_events_config_bpf.
845 *
846 * Move object config terms from the first list to obj_head_config.
847 */
848static void
849split_bpf_config_terms(struct list_head *evt_head_config,
850 struct list_head *obj_head_config)
851{
852 struct parse_events_term *term, *temp;
853
854 /*
Ingo Molnar4d39c892021-03-23 17:09:15 +0100855 * Currently, all possible user config term
Wang Nan95088a52016-02-22 09:10:36 +0000856 * belong to bpf object. parse_events__is_hardcoded_term()
Ingo Molnar4d39c892021-03-23 17:09:15 +0100857 * happens to be a good flag.
Wang Nan95088a52016-02-22 09:10:36 +0000858 *
859 * See parse_events_config_bpf() and
860 * config_term_tracepoint().
861 */
862 list_for_each_entry_safe(term, temp, evt_head_config, list)
863 if (!parse_events__is_hardcoded_term(term))
864 list_move_tail(&term->list, obj_head_config);
865}
866
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300867int parse_events_load_bpf(struct parse_events_state *parse_state,
Wang Nan84c86ca2015-10-14 12:41:14 +0000868 struct list_head *list,
Wang Nand509db02015-10-14 12:41:20 +0000869 char *bpf_file_name,
Wang Nana34f3be2016-02-22 09:10:31 +0000870 bool source,
871 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000872{
Wang Nana34f3be2016-02-22 09:10:31 +0000873 int err;
Wang Nan95088a52016-02-22 09:10:36 +0000874 struct bpf_object *obj;
875 LIST_HEAD(obj_head_config);
876
877 if (head_config)
878 split_bpf_config_terms(head_config, &obj_head_config);
Wang Nan84c86ca2015-10-14 12:41:14 +0000879
Wang Nand509db02015-10-14 12:41:20 +0000880 obj = bpf__prepare_load(bpf_file_name, source);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000881 if (IS_ERR(obj)) {
Wang Nan84c86ca2015-10-14 12:41:14 +0000882 char errbuf[BUFSIZ];
Wang Nan84c86ca2015-10-14 12:41:14 +0000883
Wang Nan6371ca3b2015-11-06 13:49:37 +0000884 err = PTR_ERR(obj);
Wang Nan84c86ca2015-10-14 12:41:14 +0000885
886 if (err == -ENOTSUP)
887 snprintf(errbuf, sizeof(errbuf),
888 "BPF support is not compiled");
889 else
Wang Nand3e0ce32015-11-06 13:58:09 +0000890 bpf__strerror_prepare_load(bpf_file_name,
891 source,
892 -err, errbuf,
893 sizeof(errbuf));
Wang Nan84c86ca2015-10-14 12:41:14 +0000894
Ian Rogers6c191282021-11-07 01:00:00 -0800895 parse_events_error__handle(parse_state->error, 0,
Ian Rogers5f634c8e2020-07-07 14:14:49 -0700896 strdup(errbuf), strdup("(add -v to see detail)"));
Wang Nan84c86ca2015-10-14 12:41:14 +0000897 return err;
898 }
899
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300900 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
Wang Nana34f3be2016-02-22 09:10:31 +0000901 if (err)
902 return err;
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300903 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
Wang Nan95088a52016-02-22 09:10:36 +0000904
905 /*
906 * Caller doesn't know anything about obj_head_config,
Ingo Molnar4d39c892021-03-23 17:09:15 +0100907 * so combine them together again before returning.
Wang Nan95088a52016-02-22 09:10:36 +0000908 */
909 if (head_config)
910 list_splice_tail(&obj_head_config, head_config);
911 return err;
Wang Nan84c86ca2015-10-14 12:41:14 +0000912}
Arnaldo Carvalho de Meloc18cf782020-10-20 14:12:37 -0300913#else // HAVE_LIBBPF_SUPPORT
914int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
915 struct list_head *list __maybe_unused,
916 struct bpf_object *obj __maybe_unused,
917 struct list_head *head_config __maybe_unused)
918{
Ian Rogers6c191282021-11-07 01:00:00 -0800919 parse_events_error__handle(parse_state->error, 0,
Arnaldo Carvalho de Meloc18cf782020-10-20 14:12:37 -0300920 strdup("BPF support is not compiled"),
921 strdup("Make sure libbpf-devel is available at build time."));
922 return -ENOTSUP;
923}
924
925int parse_events_load_bpf(struct parse_events_state *parse_state,
926 struct list_head *list __maybe_unused,
927 char *bpf_file_name __maybe_unused,
928 bool source __maybe_unused,
929 struct list_head *head_config __maybe_unused)
930{
Ian Rogers6c191282021-11-07 01:00:00 -0800931 parse_events_error__handle(parse_state->error, 0,
Arnaldo Carvalho de Meloc18cf782020-10-20 14:12:37 -0300932 strdup("BPF support is not compiled"),
933 strdup("Make sure libbpf-devel is available at build time."));
934 return -ENOTSUP;
935}
936#endif // HAVE_LIBBPF_SUPPORT
Wang Nan84c86ca2015-10-14 12:41:14 +0000937
Jiri Olsa89812fc2012-03-15 20:09:15 +0100938static int
939parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100940{
941 int i;
942
943 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100944 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100945 break;
946
Jiri Olsa75827322012-06-29 09:22:54 +0200947#define CHECK_SET_TYPE(bit) \
948do { \
949 if (attr->bp_type & bit) \
950 return -EINVAL; \
951 else \
952 attr->bp_type |= bit; \
953} while (0)
954
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100955 switch (type[i]) {
956 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200957 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100958 break;
959 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200960 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100961 break;
962 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200963 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100964 break;
965 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100966 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100967 }
968 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100969
Jiri Olsa75827322012-06-29 09:22:54 +0200970#undef CHECK_SET_TYPE
971
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100972 if (!attr->bp_type) /* Default */
973 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
974
Jiri Olsa89812fc2012-03-15 20:09:15 +0100975 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100976}
977
David Ahernc5cd8ac2013-07-02 13:27:25 -0600978int parse_events_add_breakpoint(struct list_head *list, int *idx,
Ian Rogersaa98d842020-09-24 17:39:03 -0700979 u64 addr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100980{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100981 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100982
Jiri Olsa89812fc2012-03-15 20:09:15 +0100983 memset(&attr, 0, sizeof(attr));
Ian Rogersaa98d842020-09-24 17:39:03 -0700984 attr.bp_addr = addr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100985
Jiri Olsa89812fc2012-03-15 20:09:15 +0100986 if (parse_breakpoint_type(type, &attr))
987 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100988
Jacob Shin3741eb92014-05-29 17:26:51 +0200989 /* Provide some defaults if len is not specified */
990 if (!len) {
991 if (attr.bp_type == HW_BREAKPOINT_X)
992 len = sizeof(long);
993 else
994 len = HW_BREAKPOINT_LEN_4;
995 }
996
997 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200998
Jiri Olsa89812fc2012-03-15 20:09:15 +0100999 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +08001000 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001001
Ian Rogers2b62b3a2021-10-15 10:21:25 -07001002 return add_event(list, idx, &attr, /*name=*/NULL, /*mertic_id=*/NULL,
1003 /*config_terms=*/NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001004}
1005
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001006static int check_type_val(struct parse_events_term *term,
1007 struct parse_events_error *err,
1008 int type)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001009{
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001010 if (type == term->type_val)
1011 return 0;
1012
1013 if (err) {
Ian Rogers6c191282021-11-07 01:00:00 -08001014 parse_events_error__handle(err, term->err_val,
Ian Rogers448d7322019-10-30 15:34:39 -07001015 type == PARSE_EVENTS__TERM_TYPE_NUM
1016 ? strdup("expected numeric value")
1017 : strdup("expected string value"),
1018 NULL);
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001019 }
1020 return -EINVAL;
1021}
1022
Wang Nan17cb5f82016-02-19 11:43:57 +00001023/*
1024 * Update according to parse-events.l
1025 */
1026static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
1027 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
1028 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
1029 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
1030 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
1031 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
1032 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
1033 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
1034 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
1035 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
1036 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
1037 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
1038 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
1039 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001040 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001041 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr",
Wang Nan626a6b72016-07-14 08:34:45 +00001042 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
1043 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001044 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
Jin Yao064b4e82019-04-12 21:59:47 +08001045 [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore",
Adrian Hunter1b992152019-08-06 11:46:05 +03001046 [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output",
Adrian Huntereb7a52d2019-11-15 14:42:17 +02001047 [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE] = "aux-sample-size",
Ian Rogers2b62b3a2021-10-15 10:21:25 -07001048 [PARSE_EVENTS__TERM_TYPE_METRIC_ID] = "metric-id",
Wang Nan17cb5f82016-02-19 11:43:57 +00001049};
1050
Wang Nan1669e502016-02-19 11:43:58 +00001051static bool config_term_shrinked;
1052
1053static bool
1054config_term_avail(int term_type, struct parse_events_error *err)
1055{
Ian Rogers448d7322019-10-30 15:34:39 -07001056 char *err_str;
1057
Wang Nan1669e502016-02-19 11:43:58 +00001058 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
Ian Rogers6c191282021-11-07 01:00:00 -08001059 parse_events_error__handle(err, -1,
Ian Rogers448d7322019-10-30 15:34:39 -07001060 strdup("Invalid term_type"), NULL);
Wang Nan1669e502016-02-19 11:43:58 +00001061 return false;
1062 }
1063 if (!config_term_shrinked)
1064 return true;
1065
1066 switch (term_type) {
1067 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1068 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1069 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1070 case PARSE_EVENTS__TERM_TYPE_NAME:
Ian Rogers2b62b3a2021-10-15 10:21:25 -07001071 case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
Sukadev Bhattiprolu06835542016-09-15 15:24:56 -07001072 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jin Yao064b4e82019-04-12 21:59:47 +08001073 case PARSE_EVENTS__TERM_TYPE_PERCORE:
Wang Nan1669e502016-02-19 11:43:58 +00001074 return true;
1075 default:
1076 if (!err)
1077 return false;
1078
1079 /* term_type is validated so indexing is safe */
Ian Rogers448d7322019-10-30 15:34:39 -07001080 if (asprintf(&err_str, "'%s' is not usable in 'perf stat'",
1081 config_term_names[term_type]) >= 0)
Ian Rogers6c191282021-11-07 01:00:00 -08001082 parse_events_error__handle(err, -1, err_str, NULL);
Wang Nan1669e502016-02-19 11:43:58 +00001083 return false;
1084 }
1085}
1086
1087void parse_events__shrink_config_terms(void)
1088{
1089 config_term_shrinked = true;
1090}
1091
He Kuang0b8891a2015-09-28 03:52:13 +00001092static int config_term_common(struct perf_event_attr *attr,
1093 struct parse_events_term *term,
1094 struct parse_events_error *err)
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001095{
1096#define CHECK_TYPE_VAL(type) \
1097do { \
1098 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1099 return -EINVAL; \
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001100} while (0)
1101
1102 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001103 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001104 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001105 attr->config = term->val.num;
1106 break;
1107 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001108 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001109 attr->config1 = term->val.num;
1110 break;
1111 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001112 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001113 attr->config2 = term->val.num;
1114 break;
1115 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001116 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001117 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001118 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1119 CHECK_TYPE_VAL(NUM);
1120 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001121 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
Andi Kleenac12f672016-10-12 14:02:06 -07001122 CHECK_TYPE_VAL(STR);
1123 if (strcmp(term->val.str, "no") &&
Ian Rogers448d7322019-10-30 15:34:39 -07001124 parse_branch_str(term->val.str,
1125 &attr->branch_sample_type)) {
Ian Rogers6c191282021-11-07 01:00:00 -08001126 parse_events_error__handle(err, term->err_val,
Ian Rogers448d7322019-10-30 15:34:39 -07001127 strdup("invalid branch sample type"),
1128 NULL);
Andi Kleenac12f672016-10-12 14:02:06 -07001129 return -EINVAL;
1130 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01001131 break;
Kan Liang32067712015-08-04 04:30:19 -04001132 case PARSE_EVENTS__TERM_TYPE_TIME:
1133 CHECK_TYPE_VAL(NUM);
1134 if (term->val.num > 1) {
Ian Rogers6c191282021-11-07 01:00:00 -08001135 parse_events_error__handle(err, term->err_val,
Ian Rogers448d7322019-10-30 15:34:39 -07001136 strdup("expected 0 or 1"),
1137 NULL);
Kan Liang32067712015-08-04 04:30:19 -04001138 return -EINVAL;
1139 }
1140 break;
Kan Liangd457c962015-08-11 06:30:47 -04001141 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1142 CHECK_TYPE_VAL(STR);
1143 break;
1144 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1145 CHECK_TYPE_VAL(NUM);
1146 break;
Wang Nan374ce932015-10-28 10:55:02 +00001147 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1148 CHECK_TYPE_VAL(NUM);
1149 break;
1150 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1151 CHECK_TYPE_VAL(NUM);
1152 break;
Wang Nan626a6b72016-07-14 08:34:45 +00001153 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1154 CHECK_TYPE_VAL(NUM);
1155 break;
1156 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1157 CHECK_TYPE_VAL(NUM);
1158 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +02001159 case PARSE_EVENTS__TERM_TYPE_NAME:
1160 CHECK_TYPE_VAL(STR);
1161 break;
Ian Rogers2b62b3a2021-10-15 10:21:25 -07001162 case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
1163 CHECK_TYPE_VAL(STR);
1164 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001165 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1166 CHECK_TYPE_VAL(NUM);
1167 break;
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001168 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1169 CHECK_TYPE_VAL(NUM);
1170 break;
Jin Yao064b4e82019-04-12 21:59:47 +08001171 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1172 CHECK_TYPE_VAL(NUM);
1173 if ((unsigned int)term->val.num > 1) {
Ian Rogers6c191282021-11-07 01:00:00 -08001174 parse_events_error__handle(err, term->err_val,
Ian Rogers448d7322019-10-30 15:34:39 -07001175 strdup("expected 0 or 1"),
1176 NULL);
Jin Yao064b4e82019-04-12 21:59:47 +08001177 return -EINVAL;
1178 }
1179 break;
Adrian Hunter1b992152019-08-06 11:46:05 +03001180 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1181 CHECK_TYPE_VAL(NUM);
1182 break;
Adrian Huntereb7a52d2019-11-15 14:42:17 +02001183 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1184 CHECK_TYPE_VAL(NUM);
1185 if (term->val.num > UINT_MAX) {
Ian Rogers6c191282021-11-07 01:00:00 -08001186 parse_events_error__handle(err, term->err_val,
Adrian Huntereb7a52d2019-11-15 14:42:17 +02001187 strdup("too big"),
1188 NULL);
1189 return -EINVAL;
1190 }
1191 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001192 default:
Ian Rogers6c191282021-11-07 01:00:00 -08001193 parse_events_error__handle(err, term->err_term,
Ian Rogers448d7322019-10-30 15:34:39 -07001194 strdup("unknown term"),
1195 parse_events_formats_error_string(NULL));
Jiri Olsa8f707d82012-03-15 20:09:16 +01001196 return -EINVAL;
1197 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001198
Wang Nan1669e502016-02-19 11:43:58 +00001199 /*
Ingo Molnar4d39c892021-03-23 17:09:15 +01001200 * Check term availability after basic checking so
Wang Nan1669e502016-02-19 11:43:58 +00001201 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1202 *
Ingo Molnar4d39c892021-03-23 17:09:15 +01001203 * If check availability at the entry of this function,
Wang Nan1669e502016-02-19 11:43:58 +00001204 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1205 * if an invalid config term is provided for legacy events
1206 * (for example, instructions/badterm/...), which is confusing.
1207 */
1208 if (!config_term_avail(term->type_term, err))
1209 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001210 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001211#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +01001212}
1213
He Kuang0b8891a2015-09-28 03:52:13 +00001214static int config_term_pmu(struct perf_event_attr *attr,
1215 struct parse_events_term *term,
1216 struct parse_events_error *err)
1217{
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001218 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1219 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
He Kuang0b8891a2015-09-28 03:52:13 +00001220 /*
1221 * Always succeed for sysfs terms, as we dont know
1222 * at this point what type they need to have.
1223 */
1224 return 0;
1225 else
1226 return config_term_common(attr, term, err);
1227}
1228
He Kuange637d172015-09-28 03:52:16 +00001229static int config_term_tracepoint(struct perf_event_attr *attr,
1230 struct parse_events_term *term,
1231 struct parse_events_error *err)
1232{
1233 switch (term->type_term) {
1234 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1235 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
Wang Nan374ce932015-10-28 10:55:02 +00001236 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1237 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001238 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001239 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
Wang Nan626a6b72016-07-14 08:34:45 +00001240 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1241 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
Adrian Hunter1b992152019-08-06 11:46:05 +03001242 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
Adrian Huntereb7a52d2019-11-15 14:42:17 +02001243 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
He Kuange637d172015-09-28 03:52:16 +00001244 return config_term_common(attr, term, err);
1245 default:
1246 if (err) {
Ian Rogers6c191282021-11-07 01:00:00 -08001247 parse_events_error__handle(err, term->err_term,
Ian Rogers448d7322019-10-30 15:34:39 -07001248 strdup("unknown term"),
1249 strdup("valid terms: call-graph,stack-size\n"));
He Kuange637d172015-09-28 03:52:16 +00001250 }
1251 return -EINVAL;
1252 }
1253
1254 return 0;
1255}
1256
Jiri Olsa8f707d82012-03-15 20:09:16 +01001257static int config_attr(struct perf_event_attr *attr,
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001258 struct list_head *head,
He Kuang0b8891a2015-09-28 03:52:13 +00001259 struct parse_events_error *err,
1260 config_term_func_t config_term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001261{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001262 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001263
1264 list_for_each_entry(term, head, list)
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001265 if (config_term(attr, term, err))
Jiri Olsa8f707d82012-03-15 20:09:16 +01001266 return -EINVAL;
1267
1268 return 0;
1269}
1270
Jiri Olsa930a2e22015-07-29 05:42:10 -04001271static int get_config_terms(struct list_head *head_config,
1272 struct list_head *head_terms __maybe_unused)
1273{
Ian Rogers05e54e22020-03-06 23:31:21 -08001274#define ADD_CONFIG_TERM(__type, __weak) \
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -03001275 struct evsel_config_term *__t; \
Jiri Olsa930a2e22015-07-29 05:42:10 -04001276 \
1277 __t = zalloc(sizeof(*__t)); \
1278 if (!__t) \
1279 return -ENOMEM; \
1280 \
1281 INIT_LIST_HEAD(&__t->list); \
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -03001282 __t->type = EVSEL__CONFIG_TERM_ ## __type; \
Ian Rogers05e54e22020-03-06 23:31:21 -08001283 __t->weak = __weak; \
Leo Yane8846022020-01-17 13:52:50 +08001284 list_add_tail(&__t->list, head_terms)
1285
Ian Rogers05e54e22020-03-06 23:31:21 -08001286#define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak) \
Leo Yane8846022020-01-17 13:52:50 +08001287do { \
Ian Rogers05e54e22020-03-06 23:31:21 -08001288 ADD_CONFIG_TERM(__type, __weak); \
Leo Yane8846022020-01-17 13:52:50 +08001289 __t->val.__name = __val; \
1290} while (0)
1291
Ian Rogers05e54e22020-03-06 23:31:21 -08001292#define ADD_CONFIG_TERM_STR(__type, __val, __weak) \
Leo Yane8846022020-01-17 13:52:50 +08001293do { \
Ian Rogers05e54e22020-03-06 23:31:21 -08001294 ADD_CONFIG_TERM(__type, __weak); \
Leo Yan3220fb82020-01-17 13:52:51 +08001295 __t->val.str = strdup(__val); \
1296 if (!__t->val.str) { \
1297 zfree(&__t); \
1298 return -ENOMEM; \
1299 } \
1300 __t->free_str = true; \
Jiri Olsa930a2e22015-07-29 05:42:10 -04001301} while (0)
1302
1303 struct parse_events_term *term;
1304
1305 list_for_each_entry(term, head_config, list) {
1306 switch (term->type_term) {
Jiri Olsaee4c7582015-07-29 05:42:11 -04001307 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Ian Rogers05e54e22020-03-06 23:31:21 -08001308 ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak);
Kan Liang32067712015-08-04 04:30:19 -04001309 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001310 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
Ian Rogers05e54e22020-03-06 23:31:21 -08001311 ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak);
Namhyung Kim09af2a52015-08-09 15:45:23 +09001312 break;
Kan Liang32067712015-08-04 04:30:19 -04001313 case PARSE_EVENTS__TERM_TYPE_TIME:
Ian Rogers05e54e22020-03-06 23:31:21 -08001314 ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak);
Kan Liang32067712015-08-04 04:30:19 -04001315 break;
Kan Liangd457c962015-08-11 06:30:47 -04001316 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
Ian Rogers05e54e22020-03-06 23:31:21 -08001317 ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak);
Kan Liangd457c962015-08-11 06:30:47 -04001318 break;
Andi Kleenac12f672016-10-12 14:02:06 -07001319 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
Ian Rogers05e54e22020-03-06 23:31:21 -08001320 ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak);
Andi Kleenac12f672016-10-12 14:02:06 -07001321 break;
Kan Liangd457c962015-08-11 06:30:47 -04001322 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
Leo Yane8846022020-01-17 13:52:50 +08001323 ADD_CONFIG_TERM_VAL(STACK_USER, stack_user,
Ian Rogers05e54e22020-03-06 23:31:21 -08001324 term->val.num, term->weak);
Kan Liangd457c962015-08-11 06:30:47 -04001325 break;
Wang Nan374ce932015-10-28 10:55:02 +00001326 case PARSE_EVENTS__TERM_TYPE_INHERIT:
Leo Yane8846022020-01-17 13:52:50 +08001327 ADD_CONFIG_TERM_VAL(INHERIT, inherit,
Ian Rogers05e54e22020-03-06 23:31:21 -08001328 term->val.num ? 1 : 0, term->weak);
Wang Nan374ce932015-10-28 10:55:02 +00001329 break;
1330 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
Leo Yane8846022020-01-17 13:52:50 +08001331 ADD_CONFIG_TERM_VAL(INHERIT, inherit,
Ian Rogers05e54e22020-03-06 23:31:21 -08001332 term->val.num ? 0 : 1, term->weak);
Wang Nan374ce932015-10-28 10:55:02 +00001333 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001334 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
Leo Yane8846022020-01-17 13:52:50 +08001335 ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack,
Ian Rogers05e54e22020-03-06 23:31:21 -08001336 term->val.num, term->weak);
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001337 break;
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001338 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
Leo Yane8846022020-01-17 13:52:50 +08001339 ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events,
Ian Rogers05e54e22020-03-06 23:31:21 -08001340 term->val.num, term->weak);
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001341 break;
Wang Nan626a6b72016-07-14 08:34:45 +00001342 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
Leo Yane8846022020-01-17 13:52:50 +08001343 ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
Ian Rogers05e54e22020-03-06 23:31:21 -08001344 term->val.num ? 1 : 0, term->weak);
Wang Nan626a6b72016-07-14 08:34:45 +00001345 break;
1346 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
Leo Yane8846022020-01-17 13:52:50 +08001347 ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
Ian Rogers05e54e22020-03-06 23:31:21 -08001348 term->val.num ? 0 : 1, term->weak);
Wang Nan626a6b72016-07-14 08:34:45 +00001349 break;
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001350 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
Ian Rogers05e54e22020-03-06 23:31:21 -08001351 ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak);
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001352 break;
Jin Yao064b4e82019-04-12 21:59:47 +08001353 case PARSE_EVENTS__TERM_TYPE_PERCORE:
Leo Yane8846022020-01-17 13:52:50 +08001354 ADD_CONFIG_TERM_VAL(PERCORE, percore,
Ian Rogers05e54e22020-03-06 23:31:21 -08001355 term->val.num ? true : false, term->weak);
Jin Yao064b4e82019-04-12 21:59:47 +08001356 break;
Adrian Hunter1b992152019-08-06 11:46:05 +03001357 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
Leo Yane8846022020-01-17 13:52:50 +08001358 ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output,
Ian Rogers05e54e22020-03-06 23:31:21 -08001359 term->val.num ? 1 : 0, term->weak);
Adrian Hunter1b992152019-08-06 11:46:05 +03001360 break;
Adrian Huntereb7a52d2019-11-15 14:42:17 +02001361 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
Leo Yane8846022020-01-17 13:52:50 +08001362 ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size,
Ian Rogers05e54e22020-03-06 23:31:21 -08001363 term->val.num, term->weak);
Adrian Huntereb7a52d2019-11-15 14:42:17 +02001364 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001365 default:
1366 break;
1367 }
1368 }
Adrian Huntera1ac7de2019-11-15 14:42:22 +02001369 return 0;
1370}
1371
1372/*
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -03001373 * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for
Adrian Huntera1ac7de2019-11-15 14:42:22 +02001374 * each bit of attr->config that the user has changed.
1375 */
1376static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config,
1377 struct list_head *head_terms)
1378{
1379 struct parse_events_term *term;
1380 u64 bits = 0;
1381 int type;
1382
1383 list_for_each_entry(term, head_config, list) {
1384 switch (term->type_term) {
1385 case PARSE_EVENTS__TERM_TYPE_USER:
1386 type = perf_pmu__format_type(&pmu->format, term->config);
1387 if (type != PERF_PMU_FORMAT_VALUE_CONFIG)
1388 continue;
1389 bits |= perf_pmu__format_bits(&pmu->format, term->config);
1390 break;
1391 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1392 bits = ~(u64)0;
1393 break;
1394 default:
1395 break;
1396 }
1397 }
1398
1399 if (bits)
Ian Rogers05e54e22020-03-06 23:31:21 -08001400 ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false);
Adrian Huntera1ac7de2019-11-15 14:42:22 +02001401
1402#undef ADD_CONFIG_TERM
Jiri Olsa930a2e22015-07-29 05:42:10 -04001403 return 0;
1404}
1405
He Kuange637d172015-09-28 03:52:16 +00001406int parse_events_add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +00001407 const char *sys, const char *event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001408 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +00001409 struct list_head *head_config)
1410{
1411 if (head_config) {
1412 struct perf_event_attr attr;
1413
Jiri Olsa272ed292015-09-29 17:05:31 +02001414 if (config_attr(&attr, head_config, err,
He Kuange637d172015-09-28 03:52:16 +00001415 config_term_tracepoint))
1416 return -EINVAL;
1417 }
1418
1419 if (strpbrk(sys, "*?"))
1420 return add_tracepoint_multi_sys(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001421 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001422 else
1423 return add_tracepoint_event(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001424 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001425}
1426
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001427int parse_events_add_numeric(struct parse_events_state *parse_state,
Jiri Olsa87d650b2015-04-22 21:10:24 +02001428 struct list_head *list,
Robert Richterb527bab2012-08-07 19:43:13 +02001429 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +01001430 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301431{
Jiri Olsa89812fc2012-03-15 20:09:15 +01001432 struct perf_event_attr attr;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001433 LIST_HEAD(config_terms);
Ian Rogers2b62b3a2021-10-15 10:21:25 -07001434 const char *name, *metric_id;
Jin Yao9cbfa2f2021-04-27 15:01:21 +08001435 bool hybrid;
1436 int ret;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301437
Jiri Olsa89812fc2012-03-15 20:09:15 +01001438 memset(&attr, 0, sizeof(attr));
1439 attr.type = type;
1440 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001441
Jiri Olsa930a2e22015-07-29 05:42:10 -04001442 if (head_config) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001443 if (config_attr(&attr, head_config, parse_state->error,
He Kuang0b8891a2015-09-28 03:52:13 +00001444 config_term_common))
Jiri Olsa930a2e22015-07-29 05:42:10 -04001445 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001446
Jiri Olsa930a2e22015-07-29 05:42:10 -04001447 if (get_config_terms(head_config, &config_terms))
1448 return -ENOMEM;
1449 }
1450
Ian Rogers2b62b3a2021-10-15 10:21:25 -07001451 name = get_config_name(head_config);
1452 metric_id = get_config_metric_id(head_config);
Jin Yao9cbfa2f2021-04-27 15:01:21 +08001453 ret = parse_events__add_numeric_hybrid(parse_state, list, &attr,
Ian Rogers2b62b3a2021-10-15 10:21:25 -07001454 name, metric_id,
Jin Yao9cbfa2f2021-04-27 15:01:21 +08001455 &config_terms, &hybrid);
1456 if (hybrid)
Adrian Hunter99fc5942021-09-09 15:55:08 +03001457 goto out_free_terms;
Jin Yao9cbfa2f2021-04-27 15:01:21 +08001458
Ian Rogers2b62b3a2021-10-15 10:21:25 -07001459 ret = add_event(list, &parse_state->idx, &attr, name, metric_id,
1460 &config_terms);
Adrian Hunter99fc5942021-09-09 15:55:08 +03001461out_free_terms:
1462 free_config_terms(&config_terms);
1463 return ret;
Paul Mackerras61c45982009-07-01 13:04:34 +10001464}
1465
Andi Kleenf0fbb112019-03-26 15:18:21 -07001466int parse_events_add_tool(struct parse_events_state *parse_state,
1467 struct list_head *list,
Ian Rogers8228e932021-09-15 14:14:28 -07001468 int tool_event)
Andi Kleenf0fbb112019-03-26 15:18:21 -07001469{
1470 return add_event_tool(list, &parse_state->idx, tool_event);
1471}
1472
Jin Yao064b4e82019-04-12 21:59:47 +08001473static bool config_term_percore(struct list_head *config_terms)
1474{
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -03001475 struct evsel_config_term *term;
Jin Yao064b4e82019-04-12 21:59:47 +08001476
1477 list_for_each_entry(term, config_terms, list) {
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -03001478 if (term->type == EVSEL__CONFIG_TERM_PERCORE)
Jin Yao064b4e82019-04-12 21:59:47 +08001479 return term->val.percore;
1480 }
1481
1482 return false;
1483}
1484
Jin Yao5e4edd12021-04-27 15:01:25 +08001485static int parse_events__inside_hybrid_pmu(struct parse_events_state *parse_state,
1486 struct list_head *list, char *name,
1487 struct list_head *head_config)
1488{
1489 struct parse_events_term *term;
1490 int ret = -1;
1491
1492 if (parse_state->fake_pmu || !head_config || list_empty(head_config) ||
1493 !perf_pmu__is_hybrid(name)) {
1494 return -1;
1495 }
1496
1497 /*
1498 * More than one term in list.
1499 */
1500 if (head_config->next && head_config->next->next != head_config)
1501 return -1;
1502
1503 term = list_first_entry(head_config, struct parse_events_term, list);
1504 if (term && term->config && strcmp(term->config, "event")) {
1505 ret = parse_events__with_hybrid_pmu(parse_state, term->config,
1506 name, list);
1507 }
1508
1509 return ret;
1510}
1511
Agustin Vega-Friasc199c112018-03-06 09:04:44 -05001512int parse_events_add_pmu(struct parse_events_state *parse_state,
Jiri Olsa36adec82015-04-22 21:10:19 +02001513 struct list_head *list, char *name,
Kan Liang3cdc5c22018-05-07 14:13:43 -07001514 struct list_head *head_config,
1515 bool auto_merge_stats,
1516 bool use_alias)
Jiri Olsa5f537a22012-03-15 20:09:18 +01001517{
1518 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +01001519 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001520 struct perf_pmu *pmu;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001521 struct evsel *evsel;
Andi Kleen333b5662017-09-13 14:50:06 -07001522 struct parse_events_error *err = parse_state->error;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001523 bool use_uncore_alias;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001524 LIST_HEAD(config_terms);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001525
Ian Rogers4ac22b42020-05-13 15:06:35 -07001526 if (verbose > 1) {
1527 fprintf(stderr, "Attempting to add event pmu '%s' with '",
1528 name);
1529 if (head_config) {
1530 struct parse_events_term *term;
1531
1532 list_for_each_entry(term, head_config, list) {
1533 fprintf(stderr, "%s,", term->config);
1534 }
1535 }
1536 fprintf(stderr, "' that may result in non-fatal errors\n");
1537 }
1538
Jiri Olsa387ad332020-06-02 23:47:29 +02001539 pmu = parse_state->fake_pmu ?: perf_pmu__find(name);
Andi Kleen333b5662017-09-13 14:50:06 -07001540 if (!pmu) {
Ian Rogers448d7322019-10-30 15:34:39 -07001541 char *err_str;
1542
1543 if (asprintf(&err_str,
Andi Kleen333b5662017-09-13 14:50:06 -07001544 "Cannot find PMU `%s'. Missing kernel support?",
Ian Rogers448d7322019-10-30 15:34:39 -07001545 name) >= 0)
Ian Rogers6c191282021-11-07 01:00:00 -08001546 parse_events_error__handle(err, 0, err_str, NULL);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001547 return -EINVAL;
Andi Kleen333b5662017-09-13 14:50:06 -07001548 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001549
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001550 if (pmu->default_config) {
1551 memcpy(&attr, pmu->default_config,
1552 sizeof(struct perf_event_attr));
1553 } else {
1554 memset(&attr, 0, sizeof(attr));
1555 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001556
Kan Liang3cdc5c22018-05-07 14:13:43 -07001557 use_uncore_alias = (pmu->is_uncore && use_alias);
1558
Adrian Hunterad962272014-08-15 22:08:40 +03001559 if (!head_config) {
1560 attr.type = pmu->type;
Ian Rogers2b62b3a2021-10-15 10:21:25 -07001561 evsel = __add_event(list, &parse_state->idx, &attr,
1562 /*init_attr=*/true, /*name=*/NULL,
1563 /*metric_id=*/NULL, pmu,
1564 /*config_terms=*/NULL, auto_merge_stats,
1565 /*cpu_list=*/NULL);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001566 if (evsel) {
Ian Rogersd4953f72020-03-14 10:03:56 -07001567 evsel->pmu_name = name ? strdup(name) : NULL;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001568 evsel->use_uncore_alias = use_uncore_alias;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001569 return 0;
1570 } else {
1571 return -ENOMEM;
1572 }
Adrian Hunterad962272014-08-15 22:08:40 +03001573 }
1574
Jiri Olsa387ad332020-06-02 23:47:29 +02001575 if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +08001576 return -EINVAL;
1577
Ian Rogers4ac22b42020-05-13 15:06:35 -07001578 if (verbose > 1) {
1579 fprintf(stderr, "After aliases, add event pmu '%s' with '",
1580 name);
1581 if (head_config) {
1582 struct parse_events_term *term;
1583
1584 list_for_each_entry(term, head_config, list) {
1585 fprintf(stderr, "%s,", term->config);
1586 }
1587 }
1588 fprintf(stderr, "' that may result in non-fatal errors\n");
1589 }
1590
Jiri Olsa5f537a22012-03-15 20:09:18 +01001591 /*
1592 * Configure hardcoded terms first, no need to check
1593 * return value when called with fail == 0 ;)
1594 */
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001595 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
Jiri Olsac056ba6a2015-04-22 21:10:18 +02001596 return -EINVAL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001597
Jiri Olsa930a2e22015-07-29 05:42:10 -04001598 if (get_config_terms(head_config, &config_terms))
1599 return -ENOMEM;
1600
Adrian Huntera1ac7de2019-11-15 14:42:22 +02001601 /*
1602 * When using default config, record which bits of attr->config were
1603 * changed by the user.
1604 */
1605 if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms))
1606 return -ENOMEM;
1607
Jin Yao5e4edd12021-04-27 15:01:25 +08001608 if (!parse_events__inside_hybrid_pmu(parse_state, list, name,
1609 head_config)) {
1610 return 0;
1611 }
1612
Jiri Olsa387ad332020-06-02 23:47:29 +02001613 if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) {
Adrian Huntera7d212f2021-09-09 15:55:07 +03001614 free_config_terms(&config_terms);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001615 return -EINVAL;
Ian Rogers38f2c422019-10-30 15:34:46 -07001616 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001617
Ian Rogers2b62b3a2021-10-15 10:21:25 -07001618 evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true,
1619 get_config_name(head_config),
1620 get_config_metric_id(head_config), pmu,
1621 &config_terms, auto_merge_stats, /*cpu_list=*/NULL);
Ian Rogers33321a02020-08-25 21:29:10 -07001622 if (!evsel)
1623 return -ENOMEM;
Stephane Eranian410136f2013-11-12 17:58:49 +01001624
Jin Yao12279422021-04-27 15:01:20 +08001625 if (evsel->name)
1626 evsel->use_config_name = true;
1627
Ian Rogers33321a02020-08-25 21:29:10 -07001628 evsel->pmu_name = name ? strdup(name) : NULL;
1629 evsel->use_uncore_alias = use_uncore_alias;
1630 evsel->percore = config_term_percore(&evsel->config_terms);
1631
1632 if (parse_state->fake_pmu)
1633 return 0;
1634
Ian Rogersb194c9c2021-11-18 00:47:49 -08001635 free((char *)evsel->unit);
1636 evsel->unit = strdup(info.unit);
Ian Rogers33321a02020-08-25 21:29:10 -07001637 evsel->scale = info.scale;
1638 evsel->per_pkg = info.per_pkg;
1639 evsel->snapshot = info.snapshot;
1640 evsel->metric_expr = info.metric_expr;
1641 evsel->metric_name = info.metric_name;
1642 return 0;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001643}
1644
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001645int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
Ian Rogersfb081152021-10-15 10:21:26 -07001646 char *str, struct list_head *head,
1647 struct list_head **listp)
Andi Kleen2073ad32017-03-20 13:17:02 -07001648{
Andi Kleen2073ad32017-03-20 13:17:02 -07001649 struct parse_events_term *term;
Ian Rogersfb081152021-10-15 10:21:26 -07001650 struct list_head *list = NULL;
Andi Kleen2073ad32017-03-20 13:17:02 -07001651 struct perf_pmu *pmu = NULL;
1652 int ok = 0;
Ian Rogersfb081152021-10-15 10:21:26 -07001653 char *config;
Andi Kleen2073ad32017-03-20 13:17:02 -07001654
1655 *listp = NULL;
Ian Rogersfb081152021-10-15 10:21:26 -07001656
1657 if (!head) {
1658 head = malloc(sizeof(struct list_head));
1659 if (!head)
1660 goto out_err;
1661
1662 INIT_LIST_HEAD(head);
1663 }
1664 config = strdup(str);
1665 if (!config)
1666 goto out_err;
1667
1668 if (parse_events_term__num(&term,
1669 PARSE_EVENTS__TERM_TYPE_USER,
1670 config, 1, false, &config,
1671 NULL) < 0) {
1672 free(config);
1673 goto out_err;
1674 }
1675 list_add_tail(&term->list, head);
1676
1677
Andi Kleen2073ad32017-03-20 13:17:02 -07001678 /* Add it for all PMUs that support the alias */
1679 list = malloc(sizeof(struct list_head));
1680 if (!list)
Ian Rogersfb081152021-10-15 10:21:26 -07001681 goto out_err;
1682
Andi Kleen2073ad32017-03-20 13:17:02 -07001683 INIT_LIST_HEAD(list);
Ian Rogersfb081152021-10-15 10:21:26 -07001684
Andi Kleen2073ad32017-03-20 13:17:02 -07001685 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1686 struct perf_pmu_alias *alias;
1687
1688 list_for_each_entry(alias, &pmu->aliases, list) {
1689 if (!strcasecmp(alias->name, str)) {
Agustin Vega-Friasc199c112018-03-06 09:04:44 -05001690 if (!parse_events_add_pmu(parse_state, list,
Kan Liang3cdc5c22018-05-07 14:13:43 -07001691 pmu->name, head,
1692 true, true)) {
Ian Rogersd4953f72020-03-14 10:03:56 -07001693 pr_debug("%s -> %s/%s/\n", str,
Andi Kleen2073ad32017-03-20 13:17:02 -07001694 pmu->name, alias->str);
1695 ok++;
1696 }
Andi Kleen2073ad32017-03-20 13:17:02 -07001697 }
1698 }
1699 }
John Garryb4a72762022-01-17 23:10:15 +08001700
1701 if (parse_state->fake_pmu) {
1702 if (!parse_events_add_pmu(parse_state, list, str, head,
1703 true, true)) {
1704 pr_debug("%s -> %s/%s/\n", str, "fake_pmu", str);
1705 ok++;
1706 }
1707 }
1708
Ian Rogersfb081152021-10-15 10:21:26 -07001709out_err:
1710 if (ok)
1711 *listp = list;
1712 else
Ian Rogersb6645a72019-10-30 15:34:43 -07001713 free(list);
Ian Rogersfb081152021-10-15 10:21:26 -07001714
1715 parse_events_terms__delete(head);
1716 return ok ? 0 : -1;
Andi Kleen2073ad32017-03-20 13:17:02 -07001717}
1718
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001719int parse_events__modifier_group(struct list_head *list,
1720 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +02001721{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001722 return parse_events__modifier_event(list, event_mod, true);
1723}
1724
Kan Liang3cdc5c22018-05-07 14:13:43 -07001725/*
1726 * Check if the two uncore PMUs are from the same uncore block
1727 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1728 */
1729static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
1730{
1731 char *end_a, *end_b;
1732
1733 end_a = strrchr(pmu_name_a, '_');
1734 end_b = strrchr(pmu_name_b, '_');
1735
1736 if (!end_a || !end_b)
1737 return false;
1738
1739 if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
1740 return false;
1741
1742 return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
1743}
1744
1745static int
1746parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
1747 struct parse_events_state *parse_state)
1748{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001749 struct evsel *evsel, *leader;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001750 uintptr_t *leaders;
1751 bool is_leader = true;
1752 int i, nr_pmu = 0, total_members, ret = 0;
1753
Jiri Olsab27c4ec2019-07-21 13:24:22 +02001754 leader = list_first_entry(list, struct evsel, core.node);
1755 evsel = list_last_entry(list, struct evsel, core.node);
Jiri Olsa38fe0e02021-07-06 17:16:59 +02001756 total_members = evsel->core.idx - leader->core.idx + 1;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001757
1758 leaders = calloc(total_members, sizeof(uintptr_t));
1759 if (WARN_ON(!leaders))
1760 return 0;
1761
1762 /*
1763 * Going through the whole group and doing sanity check.
1764 * All members must use alias, and be from the same uncore block.
1765 * Also, storing the leader events in an array.
1766 */
1767 __evlist__for_each_entry(list, evsel) {
1768
1769 /* Only split the uncore group which members use alias */
1770 if (!evsel->use_uncore_alias)
1771 goto out;
1772
1773 /* The events must be from the same uncore block */
1774 if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
1775 goto out;
1776
1777 if (!is_leader)
1778 continue;
1779 /*
1780 * If the event's PMU name starts to repeat, it must be a new
1781 * event. That can be used to distinguish the leader from
1782 * other members, even they have the same event name.
1783 */
Jin Yao85108952020-04-30 08:36:18 +08001784 if ((leader != evsel) &&
1785 !strcmp(leader->pmu_name, evsel->pmu_name)) {
Kan Liang3cdc5c22018-05-07 14:13:43 -07001786 is_leader = false;
1787 continue;
1788 }
Kan Liang3cdc5c22018-05-07 14:13:43 -07001789
1790 /* Store the leader event for each PMU */
1791 leaders[nr_pmu++] = (uintptr_t) evsel;
1792 }
1793
1794 /* only one event alias */
1795 if (nr_pmu == total_members) {
1796 parse_state->nr_groups--;
1797 goto handled;
1798 }
1799
1800 /*
1801 * An uncore event alias is a joint name which means the same event
1802 * runs on all PMUs of a block.
1803 * Perf doesn't support mixed events from different PMUs in the same
1804 * group. The big group has to be split into multiple small groups
1805 * which only include the events from the same PMU.
1806 *
1807 * Here the uncore event aliases must be from the same uncore block.
1808 * The number of PMUs must be same for each alias. The number of new
1809 * small groups equals to the number of PMUs.
1810 * Setting the leader event for corresponding members in each group.
1811 */
1812 i = 0;
1813 __evlist__for_each_entry(list, evsel) {
1814 if (i >= nr_pmu)
1815 i = 0;
Jiri Olsafba7c862021-07-06 17:17:00 +02001816 evsel__set_leader(evsel, (struct evsel *) leaders[i++]);
Kan Liang3cdc5c22018-05-07 14:13:43 -07001817 }
1818
1819 /* The number of members and group name are same for each group */
1820 for (i = 0; i < nr_pmu; i++) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02001821 evsel = (struct evsel *) leaders[i];
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001822 evsel->core.nr_members = total_members / nr_pmu;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001823 evsel->group_name = name ? strdup(name) : NULL;
1824 }
1825
1826 /* Take the new small groups into account */
1827 parse_state->nr_groups += nr_pmu - 1;
1828
1829handled:
1830 ret = 1;
1831out:
1832 free(leaders);
1833 return ret;
1834}
1835
Ian Rogers94dbfd62021-11-30 09:49:45 -08001836__weak struct evsel *arch_evlist__leader(struct list_head *list)
1837{
1838 return list_first_entry(list, struct evsel, core.node);
1839}
1840
Kan Liang3cdc5c22018-05-07 14:13:43 -07001841void parse_events__set_leader(char *name, struct list_head *list,
1842 struct parse_events_state *parse_state)
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001843{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001844 struct evsel *leader;
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001845
Wang Nan854f7362015-09-06 07:13:17 +00001846 if (list_empty(list)) {
1847 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1848 return;
1849 }
1850
Kan Liang3cdc5c22018-05-07 14:13:43 -07001851 if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
1852 return;
1853
Ian Rogers94dbfd62021-11-30 09:49:45 -08001854 leader = arch_evlist__leader(list);
Ian Rogersecdcf632021-11-30 09:49:44 -08001855 __perf_evlist__set_leader(list, &leader->core);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001856 leader->group_name = name ? strdup(name) : NULL;
Ian Rogers94dbfd62021-11-30 09:49:45 -08001857 list_move(&leader->core.node, list);
Jiri Olsa89efb022012-08-08 12:14:14 +02001858}
1859
David Ahernc5cd8ac2013-07-02 13:27:25 -06001860/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +01001861void parse_events_update_lists(struct list_head *list_event,
1862 struct list_head *list_all)
1863{
1864 /*
1865 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +02001866 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +01001867 * list, for next event definition.
1868 */
1869 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +02001870 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +01001871}
1872
Jiri Olsaf5b11352012-08-08 12:21:54 +02001873struct event_modifier {
1874 int eu;
1875 int ek;
1876 int eh;
1877 int eH;
1878 int eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001879 int eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001880 int precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001881 int precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001882 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001883 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001884 int pinned;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001885 int weak;
Andi Kleen0997a262020-10-14 07:42:55 -07001886 int exclusive;
Song Liu01bd8efc2021-04-25 14:43:32 -07001887 int bpf_counter;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001888};
Paul Mackerras61c45982009-07-01 13:04:34 +10001889
Jiri Olsaf5b11352012-08-08 12:21:54 +02001890static int get_event_modifier(struct event_modifier *mod, char *str,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001891 struct evsel *evsel)
Jiri Olsaf5b11352012-08-08 12:21:54 +02001892{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001893 int eu = evsel ? evsel->core.attr.exclude_user : 0;
1894 int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
1895 int eh = evsel ? evsel->core.attr.exclude_hv : 0;
1896 int eH = evsel ? evsel->core.attr.exclude_host : 0;
1897 int eG = evsel ? evsel->core.attr.exclude_guest : 0;
1898 int eI = evsel ? evsel->core.attr.exclude_idle : 0;
1899 int precise = evsel ? evsel->core.attr.precise_ip : 0;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001900 int precise_max = 0;
Jiri Olsa3c176312012-10-10 17:39:03 +02001901 int sample_read = 0;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001902 int pinned = evsel ? evsel->core.attr.pinned : 0;
Andi Kleen0997a262020-10-14 07:42:55 -07001903 int exclusive = evsel ? evsel->core.attr.exclusive : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001904
1905 int exclude = eu | ek | eh;
1906 int exclude_GH = evsel ? evsel->exclude_GH : 0;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001907 int weak = 0;
Song Liu01bd8efc2021-04-25 14:43:32 -07001908 int bpf_counter = 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001909
Jiri Olsaf5b11352012-08-08 12:21:54 +02001910 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001911
Paul Mackerras61c45982009-07-01 13:04:34 +10001912 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +02001913 if (*str == 'u') {
1914 if (!exclude)
1915 exclude = eu = ek = eh = 1;
Jin Yao943b69a2020-08-14 09:21:20 +08001916 if (!exclude_GH && !perf_guest)
1917 eG = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001918 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001919 } else if (*str == 'k') {
1920 if (!exclude)
1921 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001922 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001923 } else if (*str == 'h') {
1924 if (!exclude)
1925 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001926 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +01001927 } else if (*str == 'G') {
1928 if (!exclude_GH)
1929 exclude_GH = eG = eH = 1;
1930 eG = 0;
1931 } else if (*str == 'H') {
1932 if (!exclude_GH)
1933 exclude_GH = eG = eH = 1;
1934 eH = 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001935 } else if (*str == 'I') {
1936 eI = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001937 } else if (*str == 'p') {
1938 precise++;
David Ahern13427982012-09-13 14:59:13 -06001939 /* use of precise requires exclude_guest */
1940 if (!exclude_GH)
1941 eG = 1;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001942 } else if (*str == 'P') {
1943 precise_max = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +02001944 } else if (*str == 'S') {
1945 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001946 } else if (*str == 'D') {
1947 pinned = 1;
Andi Kleen0997a262020-10-14 07:42:55 -07001948 } else if (*str == 'e') {
1949 exclusive = 1;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001950 } else if (*str == 'W') {
1951 weak = 1;
Song Liu01bd8efc2021-04-25 14:43:32 -07001952 } else if (*str == 'b') {
1953 bpf_counter = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001954 } else
Paul Mackerras61c45982009-07-01 13:04:34 +10001955 break;
Peter Zijlstraab608342010-04-08 23:03:20 +02001956
Paul Mackerras61c45982009-07-01 13:04:34 +10001957 ++str;
1958 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001959
Jiri Olsa89812fc2012-03-15 20:09:15 +01001960 /*
1961 * precise ip:
1962 *
1963 * 0 - SAMPLE_IP can have arbitrary skid
1964 * 1 - SAMPLE_IP must have constant skid
1965 * 2 - SAMPLE_IP requested to have 0 skid
1966 * 3 - SAMPLE_IP must have 0 skid
1967 *
1968 * See also PERF_RECORD_MISC_EXACT_IP
1969 */
1970 if (precise > 3)
1971 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001972
Jiri Olsaf5b11352012-08-08 12:21:54 +02001973 mod->eu = eu;
1974 mod->ek = ek;
1975 mod->eh = eh;
1976 mod->eH = eH;
1977 mod->eG = eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001978 mod->eI = eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001979 mod->precise = precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001980 mod->precise_max = precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001981 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001982 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001983 mod->pinned = pinned;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001984 mod->weak = weak;
Song Liu01bd8efc2021-04-25 14:43:32 -07001985 mod->bpf_counter = bpf_counter;
Andi Kleen0997a262020-10-14 07:42:55 -07001986 mod->exclusive = exclusive;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001987
Jiri Olsaf5b11352012-08-08 12:21:54 +02001988 return 0;
1989}
1990
Jiri Olsa534123f2012-11-13 15:32:58 +01001991/*
1992 * Basic modifier sanity check to validate it contains only one
1993 * instance of any modifier (apart from 'p') present.
1994 */
1995static int check_modifier(char *str)
1996{
1997 char *p = str;
1998
1999 /* The sizeof includes 0 byte as well. */
Song Liu01bd8efc2021-04-25 14:43:32 -07002000 if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +01002001 return -1;
2002
2003 while (*p) {
2004 if (*p != 'p' && strchr(p + 1, *p))
2005 return -1;
2006 p++;
2007 }
2008
2009 return 0;
2010}
2011
Jiri Olsaf5b11352012-08-08 12:21:54 +02002012int parse_events__modifier_event(struct list_head *list, char *str, bool add)
2013{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002014 struct evsel *evsel;
Jiri Olsaf5b11352012-08-08 12:21:54 +02002015 struct event_modifier mod;
2016
2017 if (str == NULL)
2018 return 0;
2019
Jiri Olsa534123f2012-11-13 15:32:58 +01002020 if (check_modifier(str))
2021 return -EINVAL;
2022
Jiri Olsaf5b11352012-08-08 12:21:54 +02002023 if (!add && get_event_modifier(&mod, str, NULL))
2024 return -EINVAL;
2025
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002026 __evlist__for_each_entry(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +02002027 if (add && get_event_modifier(&mod, str, evsel))
2028 return -EINVAL;
2029
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002030 evsel->core.attr.exclude_user = mod.eu;
2031 evsel->core.attr.exclude_kernel = mod.ek;
2032 evsel->core.attr.exclude_hv = mod.eh;
2033 evsel->core.attr.precise_ip = mod.precise;
2034 evsel->core.attr.exclude_host = mod.eH;
2035 evsel->core.attr.exclude_guest = mod.eG;
2036 evsel->core.attr.exclude_idle = mod.eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02002037 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02002038 evsel->sample_read = mod.sample_read;
Jiri Olsa7f94af72015-10-05 20:06:05 +02002039 evsel->precise_max = mod.precise_max;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07002040 evsel->weak_group = mod.weak;
Song Liu01bd8efc2021-04-25 14:43:32 -07002041 evsel->bpf_counter = mod.bpf_counter;
Michael Ellermane9a7c412013-08-06 23:28:05 +10002042
Andi Kleen0997a262020-10-14 07:42:55 -07002043 if (evsel__is_group_leader(evsel)) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002044 evsel->core.attr.pinned = mod.pinned;
Andi Kleen0997a262020-10-14 07:42:55 -07002045 evsel->core.attr.exclusive = mod.exclusive;
2046 }
Jiri Olsa89812fc2012-03-15 20:09:15 +01002047 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02002048
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05302049 return 0;
2050}
2051
Ian Rogers8e8bbfb2021-10-15 10:21:24 -07002052int parse_events_name(struct list_head *list, const char *name)
Robert Richterac2ba9f2012-08-16 21:10:21 +02002053{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002054 struct evsel *evsel;
Robert Richterac2ba9f2012-08-16 21:10:21 +02002055
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002056 __evlist__for_each_entry(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +02002057 if (!evsel->name)
2058 evsel->name = strdup(name);
2059 }
2060
2061 return 0;
2062}
2063
Kan Liangdcb4e102014-10-07 11:08:50 -04002064static int
2065comp_pmu(const void *p1, const void *p2)
2066{
2067 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
2068 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
2069
Andi Kleene312bcf2016-09-15 15:24:53 -07002070 return strcasecmp(pmu1->symbol, pmu2->symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04002071}
2072
2073static void perf_pmu__parse_cleanup(void)
2074{
2075 if (perf_pmu_events_list_num > 0) {
2076 struct perf_pmu_event_symbol *p;
2077 int i;
2078
2079 for (i = 0; i < perf_pmu_events_list_num; i++) {
2080 p = perf_pmu_events_list + i;
Taeung Song360e0712017-02-01 21:34:07 +09002081 zfree(&p->symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04002082 }
Taeung Song506fde12017-02-01 21:34:06 +09002083 zfree(&perf_pmu_events_list);
Kan Liangdcb4e102014-10-07 11:08:50 -04002084 perf_pmu_events_list_num = 0;
2085 }
2086}
2087
2088#define SET_SYMBOL(str, stype) \
2089do { \
2090 p->symbol = str; \
2091 if (!p->symbol) \
2092 goto err; \
2093 p->type = stype; \
2094} while (0)
2095
2096/*
2097 * Read the pmu events list from sysfs
2098 * Save it into perf_pmu_events_list
2099 */
2100static void perf_pmu__parse_init(void)
2101{
2102
2103 struct perf_pmu *pmu = NULL;
2104 struct perf_pmu_alias *alias;
2105 int len = 0;
2106
Andi Kleen231bb2a2017-01-27 18:03:39 -08002107 pmu = NULL;
2108 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2109 list_for_each_entry(alias, &pmu->aliases, list) {
John Garry864bc8c2022-01-17 23:10:13 +08002110 char *tmp = strchr(alias->name, '-');
2111
2112 if (tmp) {
2113 char *tmp2 = NULL;
2114
2115 tmp2 = strchr(tmp + 1, '-');
Andi Kleen231bb2a2017-01-27 18:03:39 -08002116 len++;
John Garry864bc8c2022-01-17 23:10:13 +08002117 if (tmp2)
2118 len++;
2119 }
2120
Andi Kleen231bb2a2017-01-27 18:03:39 -08002121 len++;
2122 }
2123 }
2124
2125 if (len == 0) {
Kan Liangdcb4e102014-10-07 11:08:50 -04002126 perf_pmu_events_list_num = -1;
2127 return;
2128 }
Kan Liangdcb4e102014-10-07 11:08:50 -04002129 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
2130 if (!perf_pmu_events_list)
2131 return;
2132 perf_pmu_events_list_num = len;
2133
2134 len = 0;
Andi Kleen231bb2a2017-01-27 18:03:39 -08002135 pmu = NULL;
2136 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2137 list_for_each_entry(alias, &pmu->aliases, list) {
2138 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
2139 char *tmp = strchr(alias->name, '-');
John Garry864bc8c2022-01-17 23:10:13 +08002140 char *tmp2 = NULL;
Kan Liangdcb4e102014-10-07 11:08:50 -04002141
John Garry864bc8c2022-01-17 23:10:13 +08002142 if (tmp)
2143 tmp2 = strchr(tmp + 1, '-');
2144 if (tmp2) {
2145 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
2146 PMU_EVENT_SYMBOL_PREFIX);
2147 p++;
2148 tmp++;
2149 SET_SYMBOL(strndup(tmp, tmp2 - tmp), PMU_EVENT_SYMBOL_SUFFIX);
2150 p++;
2151 SET_SYMBOL(strdup(++tmp2), PMU_EVENT_SYMBOL_SUFFIX2);
2152 len += 3;
2153 } else if (tmp) {
Andi Kleen231bb2a2017-01-27 18:03:39 -08002154 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
2155 PMU_EVENT_SYMBOL_PREFIX);
2156 p++;
2157 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
2158 len += 2;
2159 } else {
2160 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
2161 len++;
2162 }
Kan Liangdcb4e102014-10-07 11:08:50 -04002163 }
2164 }
2165 qsort(perf_pmu_events_list, len,
2166 sizeof(struct perf_pmu_event_symbol), comp_pmu);
2167
2168 return;
2169err:
2170 perf_pmu__parse_cleanup();
2171}
2172
Jiri Olsa4929e952020-07-26 09:52:44 +02002173/*
2174 * This function injects special term in
2175 * perf_pmu_events_list so the test code
2176 * can check on this functionality.
2177 */
2178int perf_pmu__test_parse_init(void)
2179{
John Garryb4a72762022-01-17 23:10:15 +08002180 struct perf_pmu_event_symbol *list, *tmp, symbols[] = {
2181 {(char *)"read", PMU_EVENT_SYMBOL},
2182 {(char *)"event", PMU_EVENT_SYMBOL_PREFIX},
2183 {(char *)"two", PMU_EVENT_SYMBOL_SUFFIX},
2184 {(char *)"hyphen", PMU_EVENT_SYMBOL_SUFFIX},
2185 {(char *)"hyph", PMU_EVENT_SYMBOL_SUFFIX2},
2186 };
2187 unsigned long i, j;
Jiri Olsa4929e952020-07-26 09:52:44 +02002188
John Garryb4a72762022-01-17 23:10:15 +08002189 tmp = list = malloc(sizeof(*list) * ARRAY_SIZE(symbols));
Jiri Olsa4929e952020-07-26 09:52:44 +02002190 if (!list)
2191 return -ENOMEM;
2192
John Garryb4a72762022-01-17 23:10:15 +08002193 for (i = 0; i < ARRAY_SIZE(symbols); i++, tmp++) {
2194 tmp->type = symbols[i].type;
2195 tmp->symbol = strdup(symbols[i].symbol);
2196 if (!list->symbol)
2197 goto err_free;
Jiri Olsa4929e952020-07-26 09:52:44 +02002198 }
2199
2200 perf_pmu_events_list = list;
John Garryb4a72762022-01-17 23:10:15 +08002201 perf_pmu_events_list_num = ARRAY_SIZE(symbols);
2202
2203 qsort(perf_pmu_events_list, ARRAY_SIZE(symbols),
2204 sizeof(struct perf_pmu_event_symbol), comp_pmu);
Jiri Olsa4929e952020-07-26 09:52:44 +02002205 return 0;
John Garryb4a72762022-01-17 23:10:15 +08002206
2207err_free:
2208 for (j = 0, tmp = list; j < i; j++, tmp++)
2209 free(tmp->symbol);
2210 free(list);
2211 return -ENOMEM;
Jiri Olsa4929e952020-07-26 09:52:44 +02002212}
2213
Kan Liangdcb4e102014-10-07 11:08:50 -04002214enum perf_pmu_event_symbol_type
2215perf_pmu__parse_check(const char *name)
2216{
2217 struct perf_pmu_event_symbol p, *r;
2218
2219 /* scan kernel pmu events from sysfs if needed */
2220 if (perf_pmu_events_list_num == 0)
2221 perf_pmu__parse_init();
2222 /*
2223 * name "cpu" could be prefix of cpu-cycles or cpu// events.
2224 * cpu-cycles has been handled by hardcode.
2225 * So it must be cpu// events, not kernel pmu event.
2226 */
2227 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
2228 return PMU_EVENT_SYMBOL_ERR;
2229
2230 p.symbol = strdup(name);
2231 r = bsearch(&p, perf_pmu_events_list,
2232 (size_t) perf_pmu_events_list_num,
2233 sizeof(struct perf_pmu_event_symbol), comp_pmu);
Taeung Song360e0712017-02-01 21:34:07 +09002234 zfree(&p.symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04002235 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
2236}
2237
Jiri Olsa1244a322020-05-25 00:42:08 +02002238static int parse_events__scanner(const char *str,
2239 struct parse_events_state *parse_state)
Zheng Yanac20de6f2012-06-15 14:31:39 +08002240{
2241 YY_BUFFER_STATE buffer;
2242 void *scanner;
2243 int ret;
2244
Jiri Olsa1244a322020-05-25 00:42:08 +02002245 ret = parse_events_lex_init_extra(parse_state, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08002246 if (ret)
2247 return ret;
2248
2249 buffer = parse_events__scan_string(str, scanner);
2250
2251#ifdef PARSER_DEBUG
2252 parse_events_debug = 1;
Jiri Olsaae762642020-05-20 09:40:50 +02002253 parse_events_set_debug(1, scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08002254#endif
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002255 ret = parse_events_parse(parse_state, scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08002256
2257 parse_events__flush_buffer(buffer, scanner);
2258 parse_events__delete_buffer(buffer, scanner);
2259 parse_events_lex_destroy(scanner);
2260 return ret;
2261}
2262
Jiri Olsa90e2b222012-06-15 14:31:40 +08002263/*
2264 * parse event config string, return a list of event terms.
2265 */
2266int parse_events_terms(struct list_head *terms, const char *str)
2267{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002268 struct parse_events_state parse_state = {
Jiri Olsa1244a322020-05-25 00:42:08 +02002269 .terms = NULL,
2270 .stoken = PE_START_TERMS,
Jiri Olsa90e2b222012-06-15 14:31:40 +08002271 };
2272 int ret;
2273
Jiri Olsa1244a322020-05-25 00:42:08 +02002274 ret = parse_events__scanner(str, &parse_state);
Jiri Olsa4929e952020-07-26 09:52:44 +02002275 perf_pmu__parse_cleanup();
2276
Jiri Olsa90e2b222012-06-15 14:31:40 +08002277 if (!ret) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002278 list_splice(parse_state.terms, terms);
2279 zfree(&parse_state.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08002280 return 0;
2281 }
2282
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002283 parse_events_terms__delete(parse_state.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08002284 return ret;
2285}
2286
Jin Yao5e4edd12021-04-27 15:01:25 +08002287static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state,
2288 const char *str, char *pmu_name,
2289 struct list_head *list)
2290{
2291 struct parse_events_state ps = {
2292 .list = LIST_HEAD_INIT(ps.list),
2293 .stoken = PE_START_EVENTS,
2294 .hybrid_pmu_name = pmu_name,
2295 .idx = parse_state->idx,
2296 };
2297 int ret;
2298
2299 ret = parse_events__scanner(str, &ps);
2300 perf_pmu__parse_cleanup();
2301
2302 if (!ret) {
2303 if (!list_empty(&ps.list)) {
2304 list_splice(&ps.list, list);
2305 parse_state->idx = ps.idx;
2306 return 0;
2307 } else
2308 return -1;
2309 }
2310
2311 return ret;
2312}
2313
Arnaldo Carvalho de Melo3bf91aa2020-06-03 12:32:55 -03002314int __parse_events(struct evlist *evlist, const char *str,
2315 struct parse_events_error *err, struct perf_pmu *fake_pmu)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002316{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002317 struct parse_events_state parse_state = {
Arnaldo Carvalho de Melo3bf91aa2020-06-03 12:32:55 -03002318 .list = LIST_HEAD_INIT(parse_state.list),
2319 .idx = evlist->core.nr_entries,
2320 .error = err,
2321 .evlist = evlist,
2322 .stoken = PE_START_EVENTS,
2323 .fake_pmu = fake_pmu,
Jiri Olsa46010ab2012-06-15 14:31:38 +08002324 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08002325 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002326
Jiri Olsa1244a322020-05-25 00:42:08 +02002327 ret = parse_events__scanner(str, &parse_state);
Kan Liangdcb4e102014-10-07 11:08:50 -04002328 perf_pmu__parse_cleanup();
Ian Rogers8e8714c2019-10-25 11:08:22 -07002329
2330 if (!ret && list_empty(&parse_state.list)) {
2331 WARN_ONCE(true, "WARNING: event parser found nothing\n");
2332 return -1;
2333 }
2334
2335 /*
2336 * Add list to the evlist even with errors to allow callers to clean up.
2337 */
Arnaldo Carvalho de Meloe414fd12020-11-30 14:52:44 -03002338 evlist__splice_list_tail(evlist, &parse_state.list);
Ian Rogers8e8714c2019-10-25 11:08:22 -07002339
Jiri Olsa89812fc2012-03-15 20:09:15 +01002340 if (!ret) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002341 struct evsel *last;
Wang Nan15bfd2c2015-07-10 07:36:09 +00002342
Jiri Olsa3a683122021-07-06 17:17:01 +02002343 evlist->core.nr_groups += parse_state.nr_groups;
Jiri Olsa515dbe42019-09-03 10:39:52 +02002344 last = evlist__last(evlist);
Wang Nan15bfd2c2015-07-10 07:36:09 +00002345 last->cmdline_group_boundary = true;
2346
Jiri Olsa89812fc2012-03-15 20:09:15 +01002347 return 0;
2348 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02002349
Jiri Olsa5d7be902012-03-20 19:15:40 +01002350 /*
2351 * There are 2 users - builtin-record and builtin-test objects.
Jiri Olsac12995a2019-07-21 13:23:56 +02002352 * Both call evlist__delete in case of error, so we dont
Jiri Olsa5d7be902012-03-20 19:15:40 +01002353 * need to bother.
2354 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02002355 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002356}
2357
Ian Rogers07eafd42021-11-07 01:00:01 -08002358void parse_events_error__init(struct parse_events_error *err)
2359{
2360 bzero(err, sizeof(*err));
2361}
2362
2363void parse_events_error__exit(struct parse_events_error *err)
2364{
2365 zfree(&err->str);
2366 zfree(&err->help);
2367 zfree(&err->first_str);
2368 zfree(&err->first_help);
2369}
2370
Ian Rogers6c191282021-11-07 01:00:00 -08002371void parse_events_error__handle(struct parse_events_error *err, int idx,
2372 char *str, char *help)
2373{
2374 if (WARN(!str, "WARNING: failed to provide error string\n")) {
2375 free(help);
2376 return;
2377 }
2378 switch (err->num_errors) {
2379 case 0:
2380 err->idx = idx;
2381 err->str = str;
2382 err->help = help;
2383 break;
2384 case 1:
2385 err->first_idx = err->idx;
2386 err->idx = idx;
2387 err->first_str = err->str;
2388 err->str = str;
2389 err->first_help = err->help;
2390 err->help = help;
2391 break;
2392 default:
2393 pr_debug("Multiple errors dropping message: %s (%s)\n",
2394 err->str, err->help);
2395 free(err->str);
2396 err->str = str;
2397 free(err->help);
2398 err->help = help;
2399 break;
2400 }
2401 err->num_errors++;
2402}
2403
Jiri Olsab39b8392015-04-22 21:10:16 +02002404#define MAX_WIDTH 1000
2405static int get_term_width(void)
2406{
2407 struct winsize ws;
2408
2409 get_term_dimensions(&ws);
2410 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
2411}
2412
Ian Rogers6c191282021-11-07 01:00:00 -08002413static void __parse_events_error__print(int err_idx, const char *err_str,
2414 const char *err_help, const char *event)
Jiri Olsab39b8392015-04-22 21:10:16 +02002415{
2416 const char *str = "invalid or unsupported event: ";
2417 char _buf[MAX_WIDTH];
2418 char *buf = (char *) event;
2419 int idx = 0;
Ian Rogersa910e462019-11-15 23:46:52 -08002420 if (err_str) {
Jiri Olsab39b8392015-04-22 21:10:16 +02002421 /* -2 for extra '' in the final fprintf */
2422 int width = get_term_width() - 2;
2423 int len_event = strlen(event);
2424 int len_str, max_len, cut = 0;
2425
2426 /*
2427 * Maximum error index indent, we will cut
2428 * the event string if it's bigger.
2429 */
Adrian Hunter141b2d32015-07-17 19:33:51 +03002430 int max_err_idx = 13;
Jiri Olsab39b8392015-04-22 21:10:16 +02002431
2432 /*
2433 * Let's be specific with the message when
2434 * we have the precise error.
2435 */
2436 str = "event syntax error: ";
2437 len_str = strlen(str);
2438 max_len = width - len_str;
2439
2440 buf = _buf;
2441
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -08002442 /* We're cutting from the beginning. */
Ian Rogersa910e462019-11-15 23:46:52 -08002443 if (err_idx > max_err_idx)
2444 cut = err_idx - max_err_idx;
Jiri Olsab39b8392015-04-22 21:10:16 +02002445
2446 strncpy(buf, event + cut, max_len);
2447
2448 /* Mark cut parts with '..' on both sides. */
2449 if (cut)
2450 buf[0] = buf[1] = '.';
2451
2452 if ((len_event - cut) > max_len) {
2453 buf[max_len - 1] = buf[max_len - 2] = '.';
2454 buf[max_len] = 0;
2455 }
2456
Ian Rogersa910e462019-11-15 23:46:52 -08002457 idx = len_str + err_idx - cut;
Jiri Olsab39b8392015-04-22 21:10:16 +02002458 }
2459
2460 fprintf(stderr, "%s'%s'\n", str, buf);
2461 if (idx) {
Ian Rogersa910e462019-11-15 23:46:52 -08002462 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str);
2463 if (err_help)
2464 fprintf(stderr, "\n%s\n", err_help);
2465 }
2466}
2467
Ian Rogers6c191282021-11-07 01:00:00 -08002468void parse_events_error__print(struct parse_events_error *err,
2469 const char *event)
Ian Rogersa910e462019-11-15 23:46:52 -08002470{
2471 if (!err->num_errors)
2472 return;
2473
Ian Rogers6c191282021-11-07 01:00:00 -08002474 __parse_events_error__print(err->idx, err->str, err->help, event);
Ian Rogersa910e462019-11-15 23:46:52 -08002475
2476 if (err->num_errors > 1) {
2477 fputs("\nInitial error:\n", stderr);
Ian Rogers6c191282021-11-07 01:00:00 -08002478 __parse_events_error__print(err->first_idx, err->first_str,
Ian Rogersa910e462019-11-15 23:46:52 -08002479 err->first_help, event);
Jiri Olsab39b8392015-04-22 21:10:16 +02002480 }
Jiri Olsab39b8392015-04-22 21:10:16 +02002481}
2482
2483#undef MAX_WIDTH
2484
Jiri Olsaf120f9d2011-07-14 11:25:32 +02002485int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002486 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02002487{
Jiri Olsa63503db2019-07-21 13:23:52 +02002488 struct evlist *evlist = *(struct evlist **)opt->value;
Ian Rogersa910e462019-11-15 23:46:52 -08002489 struct parse_events_error err;
2490 int ret;
2491
Ian Rogers07eafd42021-11-07 01:00:01 -08002492 parse_events_error__init(&err);
Ian Rogersa910e462019-11-15 23:46:52 -08002493 ret = parse_events(evlist, str, &err);
Andi Kleen9175ce12012-10-26 13:30:06 -07002494
Andi Kleen333b5662017-09-13 14:50:06 -07002495 if (ret) {
Ian Rogers6c191282021-11-07 01:00:00 -08002496 parse_events_error__print(&err, str);
Andi Kleen333b5662017-09-13 14:50:06 -07002497 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2498 }
Ian Rogers07eafd42021-11-07 01:00:01 -08002499 parse_events_error__exit(&err);
Jiri Olsab39b8392015-04-22 21:10:16 +02002500
Andi Kleen9175ce12012-10-26 13:30:06 -07002501 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02002502}
2503
Arnaldo Carvalho de Melod0abbc32020-04-27 13:58:11 -03002504int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset)
2505{
2506 struct evlist **evlistp = opt->value;
2507 int ret;
2508
2509 if (*evlistp == NULL) {
2510 *evlistp = evlist__new();
2511
2512 if (*evlistp == NULL) {
2513 fprintf(stderr, "Not enough memory to create evlist\n");
2514 return -1;
2515 }
2516 }
2517
2518 ret = parse_events_option(opt, str, unset);
2519 if (ret) {
2520 evlist__delete(*evlistp);
2521 *evlistp = NULL;
2522 }
2523
2524 return ret;
2525}
2526
Wang Nan4ba1faa2015-07-10 07:36:10 +00002527static int
Jiri Olsa63503db2019-07-21 13:23:52 +02002528foreach_evsel_in_last_glob(struct evlist *evlist,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002529 int (*func)(struct evsel *evsel,
Wang Nan4ba1faa2015-07-10 07:36:10 +00002530 const void *arg),
2531 const void *arg)
Li Zefanc171b552009-10-15 11:22:07 +08002532{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002533 struct evsel *last = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002534 int err;
Li Zefanc171b552009-10-15 11:22:07 +08002535
Wang Nan854f7362015-09-06 07:13:17 +00002536 /*
2537 * Don't return when list_empty, give func a chance to report
2538 * error when it found last == NULL.
2539 *
2540 * So no need to WARN here, let *func do this.
2541 */
Jiri Olsa6484d2f2019-07-21 13:24:28 +02002542 if (evlist->core.nr_entries > 0)
Jiri Olsa515dbe42019-09-03 10:39:52 +02002543 last = evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02002544
Wang Nan15bfd2c2015-07-10 07:36:09 +00002545 do {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002546 err = (*func)(last, arg);
2547 if (err)
Wang Nan15bfd2c2015-07-10 07:36:09 +00002548 return -1;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002549 if (!last)
2550 return 0;
Wang Nan15bfd2c2015-07-10 07:36:09 +00002551
Jiri Olsace9036a2019-07-21 13:24:23 +02002552 if (last->core.node.prev == &evlist->core.entries)
Wang Nan15bfd2c2015-07-10 07:36:09 +00002553 return 0;
Jiri Olsab27c4ec2019-07-21 13:24:22 +02002554 last = list_entry(last->core.node.prev, struct evsel, core.node);
Wang Nan15bfd2c2015-07-10 07:36:09 +00002555 } while (!last->cmdline_group_boundary);
Li Zefanc171b552009-10-15 11:22:07 +08002556
2557 return 0;
2558}
2559
Jiri Olsa32dcd022019-07-21 13:23:51 +02002560static int set_filter(struct evsel *evsel, const void *arg)
Wang Nan4ba1faa2015-07-10 07:36:10 +00002561{
2562 const char *str = arg;
Mathieu Poirier1e857482016-09-16 08:44:05 -06002563 bool found = false;
2564 int nr_addr_filters = 0;
2565 struct perf_pmu *pmu = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002566
Jack Henschel49836f72018-07-04 14:13:45 +02002567 if (evsel == NULL) {
2568 fprintf(stderr,
2569 "--filter option should follow a -e tracepoint or HW tracer option\n");
2570 return -1;
2571 }
Mathieu Poirier1e857482016-09-16 08:44:05 -06002572
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002573 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03002574 if (evsel__append_tp_filter(evsel, str) < 0) {
Mathieu Poirier1e857482016-09-16 08:44:05 -06002575 fprintf(stderr,
2576 "not enough memory to hold filter string\n");
2577 return -1;
2578 }
2579
2580 return 0;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002581 }
2582
Mathieu Poirier1e857482016-09-16 08:44:05 -06002583 while ((pmu = perf_pmu__scan(pmu)) != NULL)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002584 if (pmu->type == evsel->core.attr.type) {
Mathieu Poirier1e857482016-09-16 08:44:05 -06002585 found = true;
2586 break;
2587 }
2588
2589 if (found)
2590 perf_pmu__scan_file(pmu, "nr_addr_filters",
2591 "%d", &nr_addr_filters);
2592
Jack Henschel49836f72018-07-04 14:13:45 +02002593 if (!nr_addr_filters) {
2594 fprintf(stderr,
2595 "This CPU does not support address filtering\n");
2596 return -1;
2597 }
Mathieu Poirier1e857482016-09-16 08:44:05 -06002598
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03002599 if (evsel__append_addr_filter(evsel, str) < 0) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002600 fprintf(stderr,
2601 "not enough memory to hold filter string\n");
2602 return -1;
2603 }
2604
2605 return 0;
2606}
2607
2608int parse_filter(const struct option *opt, const char *str,
2609 int unset __maybe_unused)
2610{
Jiri Olsa63503db2019-07-21 13:23:52 +02002611 struct evlist *evlist = *(struct evlist **)opt->value;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002612
2613 return foreach_evsel_in_last_glob(evlist, set_filter,
2614 (const void *)str);
2615}
2616
Jiri Olsa32dcd022019-07-21 13:23:51 +02002617static int add_exclude_perf_filter(struct evsel *evsel,
Wang Nan4ba1faa2015-07-10 07:36:10 +00002618 const void *arg __maybe_unused)
2619{
2620 char new_filter[64];
2621
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002622 if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002623 fprintf(stderr,
2624 "--exclude-perf option should follow a -e tracepoint option\n");
2625 return -1;
2626 }
2627
2628 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2629
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03002630 if (evsel__append_tp_filter(evsel, new_filter) < 0) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002631 fprintf(stderr,
2632 "not enough memory to hold filter string\n");
2633 return -1;
2634 }
2635
2636 return 0;
2637}
2638
2639int exclude_perf(const struct option *opt,
2640 const char *arg __maybe_unused,
2641 int unset __maybe_unused)
2642{
Jiri Olsa63503db2019-07-21 13:23:52 +02002643 struct evlist *evlist = *(struct evlist **)opt->value;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002644
2645 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2646 NULL);
2647}
2648
Thomas Gleixner86847b62009-06-06 12:24:17 +02002649static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02002650 "Hardware event",
2651 "Software event",
2652 "Tracepoint event",
2653 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08002654 "Raw hardware event descriptor",
2655 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02002656};
2657
Yunlong Songab0e4802015-02-27 18:21:25 +08002658static int cmp_string(const void *a, const void *b)
2659{
2660 const char * const *as = a;
2661 const char * const *bs = b;
2662
2663 return strcmp(*as, *bs);
2664}
2665
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002666/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04002667 * Print the events from <debugfs_mount_point>/tracing/events
2668 */
2669
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002670void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2671 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04002672{
2673 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002674 struct dirent *sys_dirent, *evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002675 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002676 char *dir_path;
Yunlong Songab0e4802015-02-27 18:21:25 +08002677 char **evt_list = NULL;
2678 unsigned int evt_i = 0, evt_num = 0;
2679 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002680
Yunlong Songab0e4802015-02-27 18:21:25 +08002681restart:
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -03002682 sys_dir = tracing_events__opendir();
Jason Baronf6bdafe2009-07-21 12:20:22 -04002683 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02002684 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002685
Yunlong Songab0e4802015-02-27 18:21:25 +08002686 if (evt_num_known) {
2687 evt_list = zalloc(sizeof(char *) * evt_num);
2688 if (!evt_list)
2689 goto out_close_sys_dir;
2690 }
2691
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002692 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03002693 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002694 !strglobmatch(sys_dirent->d_name, subsys_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002695 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02002696
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002697 dir_path = get_events_file(sys_dirent->d_name);
2698 if (!dir_path)
2699 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02002700 evt_dir = opendir(dir_path);
2701 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002702 goto next;
Eric Dumazet725b1362009-09-24 15:39:09 +02002703
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -03002704 for_each_event(dir_path, evt_dir, evt_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03002705 if (event_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002706 !strglobmatch(evt_dirent->d_name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002707 continue;
2708
Yunlong Songab0e4802015-02-27 18:21:25 +08002709 if (!evt_num_known) {
2710 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002711 continue;
2712 }
2713
Jason Baronf6bdafe2009-07-21 12:20:22 -04002714 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002715 sys_dirent->d_name, evt_dirent->d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08002716
2717 evt_list[evt_i] = strdup(evt_path);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002718 if (evt_list[evt_i] == NULL) {
2719 put_events_file(dir_path);
Yunlong Songab0e4802015-02-27 18:21:25 +08002720 goto out_close_evt_dir;
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002721 }
Yunlong Songab0e4802015-02-27 18:21:25 +08002722 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002723 }
2724 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002725next:
2726 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04002727 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04002728 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08002729
2730 if (!evt_num_known) {
2731 evt_num_known = true;
2732 goto restart;
2733 }
2734 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2735 evt_i = 0;
2736 while (evt_i < evt_num) {
2737 if (name_only) {
2738 printf("%s ", evt_list[evt_i++]);
2739 continue;
2740 }
2741 printf(" %-50s [%s]\n", evt_list[evt_i++],
2742 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2743 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002744 if (evt_num && pager_in_use())
Yunlong Songab0e4802015-02-27 18:21:25 +08002745 printf("\n");
2746
2747out_free:
2748 evt_num = evt_i;
2749 for (evt_i = 0; evt_i < evt_num; evt_i++)
2750 zfree(&evt_list[evt_i]);
2751 zfree(&evt_list);
2752 return;
2753
2754out_close_evt_dir:
2755 closedir(evt_dir);
2756out_close_sys_dir:
2757 closedir(sys_dir);
2758
2759 printf("FATAL: not enough memory to print %s\n",
2760 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2761 if (evt_list)
2762 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002763}
2764
2765/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01002766 * Check whether event is in <debugfs_mount_point>/tracing/events
2767 */
2768
2769int is_valid_tracepoint(const char *event_string)
2770{
2771 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002772 struct dirent *sys_dirent, *evt_dirent;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002773 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002774 char *dir_path;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002775
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -03002776 sys_dir = tracing_events__opendir();
Thomas Renninger20c457b2011-01-03 17:50:45 +01002777 if (!sys_dir)
2778 return 0;
2779
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002780 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002781 dir_path = get_events_file(sys_dirent->d_name);
2782 if (!dir_path)
2783 continue;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002784 evt_dir = opendir(dir_path);
2785 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002786 goto next;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002787
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -03002788 for_each_event(dir_path, evt_dir, evt_dirent) {
Thomas Renninger20c457b2011-01-03 17:50:45 +01002789 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002790 sys_dirent->d_name, evt_dirent->d_name);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002791 if (!strcmp(evt_path, event_string)) {
2792 closedir(evt_dir);
2793 closedir(sys_dir);
2794 return 1;
2795 }
2796 }
2797 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002798next:
2799 put_events_file(dir_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002800 }
2801 closedir(sys_dir);
2802 return 0;
2803}
2804
Jin Yao6c93f392021-09-09 14:18:44 +08002805static bool is_event_supported(u8 type, u64 config)
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002806{
2807 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05002808 int open_return;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002809 struct evsel *evsel;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002810 struct perf_event_attr attr = {
2811 .type = type,
2812 .config = config,
2813 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002814 };
Jiri Olsa9749b902019-07-21 13:23:50 +02002815 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
Arnaldo Carvalho de Melo89896052017-02-14 10:59:04 -03002816
2817 if (tmap == NULL)
2818 return false;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002819
Jiri Olsa365c3ae2019-07-21 13:23:58 +02002820 evsel = evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002821 if (evsel) {
Jiri Olsa5972d1e2019-07-21 13:24:01 +02002822 open_return = evsel__open(evsel, NULL, tmap);
Vince Weaver88fee522013-12-30 15:39:45 -05002823 ret = open_return >= 0;
2824
2825 if (open_return == -EACCES) {
2826 /*
2827 * This happens if the paranoid value
2828 * /proc/sys/kernel/perf_event_paranoid is set to 2
2829 * Re-run with exclude_kernel set; we don't do that
2830 * by default as some ARM machines do not support it.
2831 *
2832 */
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002833 evsel->core.attr.exclude_kernel = 1;
Jiri Olsa5972d1e2019-07-21 13:24:01 +02002834 ret = evsel__open(evsel, NULL, tmap) >= 0;
Vince Weaver88fee522013-12-30 15:39:45 -05002835 }
Jiri Olsa5eb2dd22019-07-21 13:23:57 +02002836 evsel__delete(evsel);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002837 }
2838
Jiri Olsa7836e522019-07-21 13:24:20 +02002839 perf_thread_map__put(tmap);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002840 return ret;
2841}
2842
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002843void print_sdt_events(const char *subsys_glob, const char *event_glob,
2844 bool name_only)
2845{
2846 struct probe_cache *pcache;
2847 struct probe_cache_entry *ent;
2848 struct strlist *bidlist, *sdtlist;
2849 struct strlist_config cfg = {.dont_dupstr = true};
2850 struct str_node *nd, *nd2;
2851 char *buf, *path, *ptr = NULL;
2852 bool show_detail = false;
2853 int ret;
2854
2855 sdtlist = strlist__new(NULL, &cfg);
2856 if (!sdtlist) {
2857 pr_debug("Failed to allocate new strlist for SDT\n");
2858 return;
2859 }
2860 bidlist = build_id_cache__list_all(true);
2861 if (!bidlist) {
2862 pr_debug("Failed to get buildids: %d\n", errno);
2863 return;
2864 }
2865 strlist__for_each_entry(nd, bidlist) {
Krister Johansenf045b8c2017-07-05 18:48:11 -07002866 pcache = probe_cache__new(nd->s, NULL);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002867 if (!pcache)
2868 continue;
2869 list_for_each_entry(ent, &pcache->entries, node) {
2870 if (!ent->sdt)
2871 continue;
2872 if (subsys_glob &&
2873 !strglobmatch(ent->pev.group, subsys_glob))
2874 continue;
2875 if (event_glob &&
2876 !strglobmatch(ent->pev.event, event_glob))
2877 continue;
2878 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2879 ent->pev.event, nd->s);
2880 if (ret > 0)
2881 strlist__add(sdtlist, buf);
2882 }
2883 probe_cache__delete(pcache);
2884 }
2885 strlist__delete(bidlist);
2886
2887 strlist__for_each_entry(nd, sdtlist) {
2888 buf = strchr(nd->s, '@');
2889 if (buf)
2890 *(buf++) = '\0';
2891 if (name_only) {
2892 printf("%s ", nd->s);
2893 continue;
2894 }
2895 nd2 = strlist__next(nd);
2896 if (nd2) {
2897 ptr = strchr(nd2->s, '@');
2898 if (ptr)
2899 *ptr = '\0';
2900 if (strcmp(nd->s, nd2->s) == 0)
2901 show_detail = true;
2902 }
2903 if (show_detail) {
2904 path = build_id_cache__origname(buf);
2905 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2906 if (ret > 0) {
2907 printf(" %-50s [%s]\n", buf, "SDT event");
2908 free(buf);
2909 }
Changbin Du8bde8512019-03-16 16:05:46 +08002910 free(path);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002911 } else
2912 printf(" %-50s [%s]\n", nd->s, "SDT event");
2913 if (nd2) {
2914 if (strcmp(nd->s, nd2->s) != 0)
2915 show_detail = false;
2916 if (ptr)
2917 *ptr = '@';
2918 }
2919 }
2920 strlist__delete(sdtlist);
2921}
2922
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002923int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002924{
Jin Yao6c93f392021-09-09 14:18:44 +08002925 unsigned int type, op, i, evt_i = 0, evt_num = 0, npmus = 0;
2926 char name[64], new_name[128];
2927 char **evt_list = NULL, **evt_pmus = NULL;
Yunlong Songab0e4802015-02-27 18:21:25 +08002928 bool evt_num_known = false;
Jin Yao6c93f392021-09-09 14:18:44 +08002929 struct perf_pmu *pmu = NULL;
2930
2931 if (perf_pmu__has_hybrid()) {
2932 npmus = perf_pmu__hybrid_pmu_num();
2933 evt_pmus = zalloc(sizeof(char *) * npmus);
2934 if (!evt_pmus)
2935 goto out_enomem;
2936 }
Yunlong Songab0e4802015-02-27 18:21:25 +08002937
2938restart:
2939 if (evt_num_known) {
2940 evt_list = zalloc(sizeof(char *) * evt_num);
2941 if (!evt_list)
2942 goto out_enomem;
2943 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002944
2945 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2946 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2947 /* skip invalid cache type */
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03002948 if (!evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002949 continue;
2950
2951 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Jin Yao6c93f392021-09-09 14:18:44 +08002952 unsigned int hybrid_supported = 0, j;
2953 bool supported;
2954
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002955 __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002956 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002957 continue;
2958
Jin Yao6c93f392021-09-09 14:18:44 +08002959 if (!perf_pmu__has_hybrid()) {
2960 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2961 type | (op << 8) | (i << 16))) {
2962 continue;
2963 }
2964 } else {
2965 perf_pmu__for_each_hybrid_pmu(pmu) {
2966 if (!evt_num_known) {
2967 evt_num++;
2968 continue;
2969 }
2970
2971 supported = is_event_supported(
2972 PERF_TYPE_HW_CACHE,
2973 type | (op << 8) | (i << 16) |
2974 ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT));
2975 if (supported) {
2976 snprintf(new_name, sizeof(new_name), "%s/%s/",
2977 pmu->name, name);
2978 evt_pmus[hybrid_supported] = strdup(new_name);
2979 hybrid_supported++;
2980 }
2981 }
2982
2983 if (hybrid_supported == 0)
2984 continue;
2985 }
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002986
Yunlong Songab0e4802015-02-27 18:21:25 +08002987 if (!evt_num_known) {
2988 evt_num++;
2989 continue;
2990 }
2991
Jin Yao6c93f392021-09-09 14:18:44 +08002992 if ((hybrid_supported == 0) ||
2993 (hybrid_supported == npmus)) {
2994 evt_list[evt_i] = strdup(name);
2995 if (npmus > 0) {
2996 for (j = 0; j < npmus; j++)
2997 zfree(&evt_pmus[j]);
2998 }
2999 } else {
3000 for (j = 0; j < hybrid_supported; j++) {
3001 evt_list[evt_i++] = evt_pmus[j];
3002 evt_pmus[j] = NULL;
3003 }
3004 continue;
3005 }
3006
Yunlong Songab0e4802015-02-27 18:21:25 +08003007 if (evt_list[evt_i] == NULL)
3008 goto out_enomem;
3009 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02003010 }
3011 }
3012 }
3013
Yunlong Songab0e4802015-02-27 18:21:25 +08003014 if (!evt_num_known) {
3015 evt_num_known = true;
3016 goto restart;
3017 }
Jin Yao6c93f392021-09-09 14:18:44 +08003018
3019 for (evt_i = 0; evt_i < evt_num; evt_i++) {
3020 if (!evt_list[evt_i])
3021 break;
3022 }
3023
3024 evt_num = evt_i;
Yunlong Songab0e4802015-02-27 18:21:25 +08003025 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
3026 evt_i = 0;
3027 while (evt_i < evt_num) {
3028 if (name_only) {
3029 printf("%s ", evt_list[evt_i++]);
3030 continue;
3031 }
3032 printf(" %-50s [%s]\n", evt_list[evt_i++],
3033 event_type_descriptors[PERF_TYPE_HW_CACHE]);
3034 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03003035 if (evt_num && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07003036 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08003037
3038out_free:
3039 evt_num = evt_i;
3040 for (evt_i = 0; evt_i < evt_num; evt_i++)
3041 zfree(&evt_list[evt_i]);
3042 zfree(&evt_list);
Jin Yao6c93f392021-09-09 14:18:44 +08003043
3044 for (evt_i = 0; evt_i < npmus; evt_i++)
3045 zfree(&evt_pmus[evt_i]);
3046 zfree(&evt_pmus);
Yunlong Songab0e4802015-02-27 18:21:25 +08003047 return evt_num;
3048
3049out_enomem:
3050 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
3051 if (evt_list)
3052 goto out_free;
3053 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02003054}
3055
Andi Kleen5e0861b2019-03-26 15:18:23 -07003056static void print_tool_event(const char *name, const char *event_glob,
3057 bool name_only)
3058{
3059 if (event_glob && !strglobmatch(name, event_glob))
3060 return;
3061 if (name_only)
3062 printf("%s ", name);
3063 else
3064 printf(" %-50s [%s]\n", name, "Tool event");
3065
3066}
3067
3068void print_tool_events(const char *event_glob, bool name_only)
3069{
3070 print_tool_event("duration_time", event_glob, name_only);
3071 if (pager_in_use())
3072 printf("\n");
3073}
3074
Yunlong Song705750f2015-02-27 18:21:27 +08003075void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02003076 struct event_symbol *syms, unsigned max,
3077 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02003078{
Yunlong Songab0e4802015-02-27 18:21:25 +08003079 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02003080 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08003081 char **evt_list = NULL;
3082 bool evt_num_known = false;
3083
3084restart:
3085 if (evt_num_known) {
3086 evt_list = zalloc(sizeof(char *) * evt_num);
3087 if (!evt_list)
3088 goto out_enomem;
3089 syms -= max;
3090 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02003091
Jiri Olsa1dc127602012-07-04 00:00:44 +02003092 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo3b2f17a2021-05-19 13:50:31 -03003093 /*
3094 * New attr.config still not supported here, the latest
3095 * example was PERF_COUNT_SW_CGROUP_SWITCHES
3096 */
3097 if (syms->symbol == NULL)
3098 continue;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02003099
Arnaldo Carvalho de Melo3b2f17a2021-05-19 13:50:31 -03003100 if (event_glob != NULL && !(strglobmatch(syms->symbol, event_glob) ||
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02003101 (syms->alias && strglobmatch(syms->alias, event_glob))))
3102 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02003103
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09003104 if (!is_event_supported(type, i))
3105 continue;
3106
Yunlong Songab0e4802015-02-27 18:21:25 +08003107 if (!evt_num_known) {
3108 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02003109 continue;
3110 }
3111
Yunlong Songab0e4802015-02-27 18:21:25 +08003112 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02003113 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05303114 else
Arnaldo Carvalho de Melobd8d57f2018-12-06 13:52:13 -03003115 strlcpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02003116
Yunlong Songab0e4802015-02-27 18:21:25 +08003117 evt_list[evt_i] = strdup(name);
3118 if (evt_list[evt_i] == NULL)
3119 goto out_enomem;
3120 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02003121 }
3122
Yunlong Songab0e4802015-02-27 18:21:25 +08003123 if (!evt_num_known) {
3124 evt_num_known = true;
3125 goto restart;
3126 }
3127 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
3128 evt_i = 0;
3129 while (evt_i < evt_num) {
3130 if (name_only) {
3131 printf("%s ", evt_list[evt_i++]);
3132 continue;
3133 }
3134 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
3135 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03003136 if (evt_num && pager_in_use())
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02003137 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08003138
3139out_free:
3140 evt_num = evt_i;
3141 for (evt_i = 0; evt_i < evt_num; evt_i++)
3142 zfree(&evt_list[evt_i]);
3143 zfree(&evt_list);
3144 return;
3145
3146out_enomem:
3147 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
3148 if (evt_list)
3149 goto out_free;
Jiri Olsa1dc127602012-07-04 00:00:44 +02003150}
3151
3152/*
3153 * Print the help text for the event symbols:
3154 */
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07003155void print_events(const char *event_glob, bool name_only, bool quiet_flag,
Jin Yao0e0ae872021-09-03 10:52:39 +08003156 bool long_desc, bool details_flag, bool deprecated,
3157 const char *pmu_name)
Jiri Olsa1dc127602012-07-04 00:00:44 +02003158{
Jiri Olsa1dc127602012-07-04 00:00:44 +02003159 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02003160 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02003161
3162 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02003163 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Andi Kleen5e0861b2019-03-26 15:18:23 -07003164 print_tool_events(event_glob, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02003165
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02003166 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02003167
Andi Kleenbf874fc2017-03-20 13:17:11 -07003168 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
Jin Yao0e0ae872021-09-03 10:52:39 +08003169 details_flag, deprecated, pmu_name);
Andi Kleendc098b32013-04-20 11:02:29 -07003170
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02003171 if (event_glob != NULL)
3172 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05303173
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02003174 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02003175 printf(" %-50s [%s]\n",
3176 "rNNN",
3177 event_type_descriptors[PERF_TYPE_RAW]);
3178 printf(" %-50s [%s]\n",
3179 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
3180 event_type_descriptors[PERF_TYPE_RAW]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03003181 if (pager_in_use())
3182 printf(" (see 'man perf-list' on how to encode it)\n\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02003183
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02003184 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02003185 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08003186 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03003187 if (pager_in_use())
3188 printf("\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02003189 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01003190
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02003191 print_tracepoint_events(NULL, NULL, name_only);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09003192
3193 print_sdt_events(NULL, NULL, name_only);
Andi Kleen71b0acc2017-08-31 12:40:32 -07003194
Jin Yao0e0ae872021-09-03 10:52:39 +08003195 metricgroup__print(true, true, NULL, name_only, details_flag,
3196 pmu_name);
Stephane Eranian70943492020-05-05 11:29:43 -07003197
3198 print_libpfm_events(name_only, long_desc);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02003199}
Jiri Olsa8f707d82012-03-15 20:09:16 +01003200
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03003201int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01003202{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02003203 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01003204}
3205
Jiri Olsa67b49b32017-02-17 15:00:55 +01003206static int new_term(struct parse_events_term **_term,
3207 struct parse_events_term *temp,
3208 char *str, u64 num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01003209{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03003210 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01003211
Jiri Olsa67b49b32017-02-17 15:00:55 +01003212 term = malloc(sizeof(*term));
Jiri Olsa8f707d82012-03-15 20:09:16 +01003213 if (!term)
3214 return -ENOMEM;
3215
Jiri Olsa67b49b32017-02-17 15:00:55 +01003216 *term = *temp;
Jiri Olsa8f707d82012-03-15 20:09:16 +01003217 INIT_LIST_HEAD(&term->list);
Andi Kleenc2f1cea2017-10-20 13:27:55 -07003218 term->weak = false;
Jiri Olsa8f707d82012-03-15 20:09:16 +01003219
Jiri Olsa67b49b32017-02-17 15:00:55 +01003220 switch (term->type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01003221 case PARSE_EVENTS__TERM_TYPE_NUM:
3222 term->val.num = num;
3223 break;
3224 case PARSE_EVENTS__TERM_TYPE_STR:
3225 term->val.str = str;
3226 break;
3227 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03003228 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01003229 return -EINVAL;
3230 }
3231
3232 *_term = term;
3233 return 0;
3234}
3235
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03003236int parse_events_term__num(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02003237 int type_term, char *config, u64 num,
Jiri Olsa99e71382017-02-17 15:00:56 +01003238 bool no_value,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03003239 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02003240{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03003241 YYLTYPE *loc_term = loc_term_;
3242 YYLTYPE *loc_val = loc_val_;
3243
Jiri Olsa67b49b32017-02-17 15:00:55 +01003244 struct parse_events_term temp = {
3245 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
3246 .type_term = type_term,
John Garry4f9d4f82021-09-16 20:34:21 +08003247 .config = config ? : strdup(config_term_names[type_term]),
Jiri Olsa99e71382017-02-17 15:00:56 +01003248 .no_value = no_value,
Jiri Olsa67b49b32017-02-17 15:00:55 +01003249 .err_term = loc_term ? loc_term->first_column : 0,
3250 .err_val = loc_val ? loc_val->first_column : 0,
3251 };
3252
3253 return new_term(term, &temp, NULL, num);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02003254}
3255
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03003256int parse_events_term__str(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02003257 int type_term, char *config, char *str,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03003258 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02003259{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03003260 YYLTYPE *loc_term = loc_term_;
3261 YYLTYPE *loc_val = loc_val_;
3262
Jiri Olsa67b49b32017-02-17 15:00:55 +01003263 struct parse_events_term temp = {
3264 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
3265 .type_term = type_term,
3266 .config = config,
3267 .err_term = loc_term ? loc_term->first_column : 0,
3268 .err_val = loc_val ? loc_val->first_column : 0,
3269 };
3270
3271 return new_term(term, &temp, str, 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02003272}
3273
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03003274int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02003275 char *config, unsigned idx)
3276{
3277 struct event_symbol *sym;
Ian Rogersb6645a72019-10-30 15:34:43 -07003278 char *str;
Jiri Olsa67b49b32017-02-17 15:00:55 +01003279 struct parse_events_term temp = {
3280 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
3281 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Ian Rogersb6645a72019-10-30 15:34:43 -07003282 .config = config,
Jiri Olsa67b49b32017-02-17 15:00:55 +01003283 };
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02003284
Ian Rogersb6645a72019-10-30 15:34:43 -07003285 if (!temp.config) {
3286 temp.config = strdup("event");
3287 if (!temp.config)
3288 return -ENOMEM;
3289 }
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02003290 BUG_ON(idx >= PERF_COUNT_HW_MAX);
3291 sym = &event_symbols_hw[idx];
3292
Ian Rogersb6645a72019-10-30 15:34:43 -07003293 str = strdup(sym->symbol);
3294 if (!str)
3295 return -ENOMEM;
3296 return new_term(term, &temp, str, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02003297}
3298
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03003299int parse_events_term__clone(struct parse_events_term **new,
3300 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08003301{
Ian Rogersb6645a72019-10-30 15:34:43 -07003302 char *str;
Jiri Olsa67b49b32017-02-17 15:00:55 +01003303 struct parse_events_term temp = {
3304 .type_val = term->type_val,
3305 .type_term = term->type_term,
Ian Rogersb6645a72019-10-30 15:34:43 -07003306 .config = NULL,
Jiri Olsa67b49b32017-02-17 15:00:55 +01003307 .err_term = term->err_term,
3308 .err_val = term->err_val,
3309 };
3310
Ian Rogersb6645a72019-10-30 15:34:43 -07003311 if (term->config) {
3312 temp.config = strdup(term->config);
3313 if (!temp.config)
3314 return -ENOMEM;
3315 }
3316 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
3317 return new_term(new, &temp, NULL, term->val.num);
3318
3319 str = strdup(term->val.str);
3320 if (!str)
3321 return -ENOMEM;
3322 return new_term(new, &temp, str, 0);
Zheng Yana6146d52012-06-15 14:31:41 +08003323}
3324
Ian Rogers1dc92552019-10-30 15:34:47 -07003325void parse_events_term__delete(struct parse_events_term *term)
3326{
3327 if (term->array.nr_ranges)
3328 zfree(&term->array.ranges);
3329
3330 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
3331 zfree(&term->val.str);
3332
3333 zfree(&term->config);
3334 free(term);
3335}
3336
Andi Kleen82557182017-03-20 13:17:03 -07003337int parse_events_copy_term_list(struct list_head *old,
3338 struct list_head **new)
3339{
3340 struct parse_events_term *term, *n;
3341 int ret;
3342
3343 if (!old) {
3344 *new = NULL;
3345 return 0;
3346 }
3347
3348 *new = malloc(sizeof(struct list_head));
3349 if (!*new)
3350 return -ENOMEM;
3351 INIT_LIST_HEAD(*new);
3352
3353 list_for_each_entry (term, old, list) {
3354 ret = parse_events_term__clone(&n, term);
3355 if (ret)
3356 return ret;
3357 list_add_tail(&n->list, *new);
3358 }
3359 return 0;
3360}
3361
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03003362void parse_events_terms__purge(struct list_head *terms)
Jiri Olsa8f707d82012-03-15 20:09:16 +01003363{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03003364 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01003365
Wang Nana8adfce2016-02-12 16:31:23 -03003366 list_for_each_entry_safe(term, h, terms, list) {
3367 list_del_init(&term->list);
Ian Rogers1dc92552019-10-30 15:34:47 -07003368 parse_events_term__delete(term);
Wang Nana8adfce2016-02-12 16:31:23 -03003369 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01003370}
Jiri Olsab39b8392015-04-22 21:10:16 +02003371
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03003372void parse_events_terms__delete(struct list_head *terms)
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03003373{
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03003374 if (!terms)
3375 return;
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03003376 parse_events_terms__purge(terms);
Wang Nand20a5f22016-02-12 17:01:17 -03003377 free(terms);
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03003378}
3379
Wang Nan2d055bf2016-02-22 09:10:34 +00003380void parse_events__clear_array(struct parse_events_array *a)
3381{
Taeung Song360e0712017-02-01 21:34:07 +09003382 zfree(&a->ranges);
Wang Nan2d055bf2016-02-22 09:10:34 +00003383}
3384
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03003385void parse_events_evlist_error(struct parse_events_state *parse_state,
Jiri Olsab39b8392015-04-22 21:10:16 +02003386 int idx, const char *str)
3387{
Ian Rogers448d7322019-10-30 15:34:39 -07003388 if (!parse_state->error)
Adrian Huntera6ced2b2015-05-19 16:05:44 +03003389 return;
Ian Rogers448d7322019-10-30 15:34:39 -07003390
Ian Rogers6c191282021-11-07 01:00:00 -08003391 parse_events_error__handle(parse_state->error, idx, strdup(str), NULL);
Jiri Olsab39b8392015-04-22 21:10:16 +02003392}
He Kuangffeb8832015-09-28 03:52:14 +00003393
Wang Nan17cb5f82016-02-19 11:43:57 +00003394static void config_terms_list(char *buf, size_t buf_sz)
3395{
3396 int i;
3397 bool first = true;
3398
3399 buf[0] = '\0';
3400 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
3401 const char *name = config_term_names[i];
3402
Wang Nan1669e502016-02-19 11:43:58 +00003403 if (!config_term_avail(i, NULL))
3404 continue;
Wang Nan17cb5f82016-02-19 11:43:57 +00003405 if (!name)
3406 continue;
3407 if (name[0] == '<')
3408 continue;
3409
3410 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
3411 return;
3412
3413 if (!first)
3414 strcat(buf, ",");
3415 else
3416 first = false;
3417 strcat(buf, name);
3418 }
3419}
3420
He Kuangffeb8832015-09-28 03:52:14 +00003421/*
3422 * Return string contains valid config terms of an event.
3423 * @additional_terms: For terms such as PMU sysfs terms.
3424 */
3425char *parse_events_formats_error_string(char *additional_terms)
3426{
3427 char *str;
Wang Nan626a6b72016-07-14 08:34:45 +00003428 /* "no-overwrite" is the longest name */
Wang Nan17cb5f82016-02-19 11:43:57 +00003429 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
Wang Nan626a6b72016-07-14 08:34:45 +00003430 (sizeof("no-overwrite") - 1)];
He Kuangffeb8832015-09-28 03:52:14 +00003431
Wang Nan17cb5f82016-02-19 11:43:57 +00003432 config_terms_list(static_terms, sizeof(static_terms));
He Kuangffeb8832015-09-28 03:52:14 +00003433 /* valid terms */
3434 if (additional_terms) {
Wang Nan26dee022016-02-19 11:43:52 +00003435 if (asprintf(&str, "valid terms: %s,%s",
3436 additional_terms, static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00003437 goto fail;
3438 } else {
Wang Nan26dee022016-02-19 11:43:52 +00003439 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00003440 goto fail;
3441 }
3442 return str;
3443
3444fail:
3445 return NULL;
3446}
Jin Yao9cbfa2f2021-04-27 15:01:21 +08003447
3448struct evsel *parse_events__add_event_hybrid(struct list_head *list, int *idx,
3449 struct perf_event_attr *attr,
Ian Rogers2b62b3a2021-10-15 10:21:25 -07003450 const char *name,
3451 const char *metric_id,
3452 struct perf_pmu *pmu,
Jin Yao9cbfa2f2021-04-27 15:01:21 +08003453 struct list_head *config_terms)
3454{
Ian Rogers2b62b3a2021-10-15 10:21:25 -07003455 return __add_event(list, idx, attr, /*init_attr=*/true, name, metric_id,
3456 pmu, config_terms, /*auto_merge_stats=*/false,
3457 /*cpu_list=*/NULL);
Jin Yao9cbfa2f2021-04-27 15:01:21 +08003458}