blob: 6bae9d6edc121bd21e4652ba509ef6b6b09890bd [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"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030022#include "symbol.h"
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020023#include "header.h"
Wang Nan84c86ca2015-10-14 12:41:14 +000024#include "bpf-loader.h"
Masami Hiramatsu6e81c742014-08-14 02:22:36 +000025#include "debug.h"
Jiri Olsa592d5a62015-09-02 09:56:34 +020026#include <api/fs/tracing_path.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +020027#include <perf/cpumap.h>
Zheng Yanac20de6f2012-06-15 14:31:39 +080028#include "parse-events-bison.h"
Jiri Olsa90e2b222012-06-15 14:31:40 +080029#define YY_EXTRA_TYPE int
Jiri Olsa89812fc2012-03-15 20:09:15 +010030#include "parse-events-flex.h"
Jiri Olsa5f537a22012-03-15 20:09:18 +010031#include "pmu.h"
Namhyung Kimb41f1ce2013-08-27 11:41:53 +090032#include "thread_map.h"
Masami Hiramatsu40218da2016-06-24 18:06:46 +090033#include "probe-file.h"
Jiri Olsab39b8392015-04-22 21:10:16 +020034#include "asm/bug.h"
Andi Kleenac12f672016-10-12 14:02:06 -070035#include "util/parse-branch-options.h"
Andi Kleen71b0acc2017-08-31 12:40:32 -070036#include "metricgroup.h"
Arnaldo Carvalho de Melo95be9d12019-09-24 15:56:14 -030037#include "util/evsel_config.h"
Arnaldo Carvalho de Melo252a2fd2019-09-24 16:07:59 -030038#include "util/event.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010039
40#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020041
Jiri Olsa82ba1f22012-05-21 09:12:50 +020042#ifdef PARSER_DEBUG
43extern int parse_events_debug;
44#endif
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -030045int parse_events_parse(void *parse_state, void *scanner);
He Kuange637d172015-09-28 03:52:16 +000046static int get_config_terms(struct list_head *head_config,
47 struct list_head *head_terms __maybe_unused);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020048
Kan Liangdcb4e102014-10-07 11:08:50 -040049static struct perf_pmu_event_symbol *perf_pmu_events_list;
50/*
51 * The variable indicates the number of supported pmu event symbols.
52 * 0 means not initialized and ready to init
53 * -1 means failed to init, don't try anymore
54 * >0 is the number of supported pmu event symbols
55 */
56static int perf_pmu_events_list_num;
57
Yunlong Song705750f2015-02-27 18:21:27 +080058struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +020059 [PERF_COUNT_HW_CPU_CYCLES] = {
60 .symbol = "cpu-cycles",
61 .alias = "cycles",
62 },
63 [PERF_COUNT_HW_INSTRUCTIONS] = {
64 .symbol = "instructions",
65 .alias = "",
66 },
67 [PERF_COUNT_HW_CACHE_REFERENCES] = {
68 .symbol = "cache-references",
69 .alias = "",
70 },
71 [PERF_COUNT_HW_CACHE_MISSES] = {
72 .symbol = "cache-misses",
73 .alias = "",
74 },
75 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
76 .symbol = "branch-instructions",
77 .alias = "branches",
78 },
79 [PERF_COUNT_HW_BRANCH_MISSES] = {
80 .symbol = "branch-misses",
81 .alias = "",
82 },
83 [PERF_COUNT_HW_BUS_CYCLES] = {
84 .symbol = "bus-cycles",
85 .alias = "",
86 },
87 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
88 .symbol = "stalled-cycles-frontend",
89 .alias = "idle-cycles-frontend",
90 },
91 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
92 .symbol = "stalled-cycles-backend",
93 .alias = "idle-cycles-backend",
94 },
95 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
96 .symbol = "ref-cycles",
97 .alias = "",
98 },
99};
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200100
Yunlong Song705750f2015-02-27 18:21:27 +0800101struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
Jiri Olsa1dc127602012-07-04 00:00:44 +0200102 [PERF_COUNT_SW_CPU_CLOCK] = {
103 .symbol = "cpu-clock",
104 .alias = "",
105 },
106 [PERF_COUNT_SW_TASK_CLOCK] = {
107 .symbol = "task-clock",
108 .alias = "",
109 },
110 [PERF_COUNT_SW_PAGE_FAULTS] = {
111 .symbol = "page-faults",
112 .alias = "faults",
113 },
114 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
115 .symbol = "context-switches",
116 .alias = "cs",
117 },
118 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
119 .symbol = "cpu-migrations",
120 .alias = "migrations",
121 },
122 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
123 .symbol = "minor-faults",
124 .alias = "",
125 },
126 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
127 .symbol = "major-faults",
128 .alias = "",
129 },
130 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
131 .symbol = "alignment-faults",
132 .alias = "",
133 },
134 [PERF_COUNT_SW_EMULATION_FAULTS] = {
135 .symbol = "emulation-faults",
136 .alias = "",
137 },
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300138 [PERF_COUNT_SW_DUMMY] = {
139 .symbol = "dummy",
140 .alias = "",
141 },
Arnaldo Carvalho de Melobae9cc42015-11-27 15:54:33 -0300142 [PERF_COUNT_SW_BPF_OUTPUT] = {
143 .symbol = "bpf-output",
144 .alias = "",
145 },
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200146};
147
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200148#define __PERF_EVENT_FIELD(config, name) \
149 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +0200150
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200151#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200152#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200153#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200154#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +0200155
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300156#define for_each_subsystem(sys_dir, sys_dirent) \
157 while ((sys_dirent = readdir(sys_dir)) != NULL) \
158 if (sys_dirent->d_type == DT_DIR && \
159 (strcmp(sys_dirent->d_name, ".")) && \
160 (strcmp(sys_dirent->d_name, "..")))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400161
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300162static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
Peter Zijlstraae07b632009-08-06 16:48:54 +0200163{
164 char evt_path[MAXPATHLEN];
165 int fd;
166
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300167 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
Peter Zijlstraae07b632009-08-06 16:48:54 +0200168 fd = open(evt_path, O_RDONLY);
169 if (fd < 0)
170 return -EINVAL;
171 close(fd);
172
173 return 0;
174}
175
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300176#define for_each_event(dir_path, evt_dir, evt_dirent) \
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300177 while ((evt_dirent = readdir(evt_dir)) != NULL) \
178 if (evt_dirent->d_type == DT_DIR && \
179 (strcmp(evt_dirent->d_name, ".")) && \
180 (strcmp(evt_dirent->d_name, "..")) && \
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300181 (!tp_event_has_id(dir_path, evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400182
Li Zefan270bbbe2009-09-17 16:34:51 +0800183#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400184
Ian Rogers448d7322019-10-30 15:34:39 -0700185void parse_events__handle_error(struct parse_events_error *err, int idx,
186 char *str, char *help)
187{
188 if (WARN(!str, "WARNING: failed to provide error string\n")) {
189 free(help);
190 return;
191 }
Ian Rogersa910e462019-11-15 23:46:52 -0800192 switch (err->num_errors) {
193 case 0:
194 err->idx = idx;
195 err->str = str;
196 err->help = help;
197 break;
198 case 1:
199 err->first_idx = err->idx;
200 err->idx = idx;
201 err->first_str = err->str;
202 err->str = str;
203 err->first_help = err->help;
204 err->help = help;
205 break;
206 default:
207 WARN_ONCE(1, "WARNING: multiple event parsing errors\n");
208 free(err->str);
209 err->str = str;
210 free(err->help);
211 err->help = help;
212 break;
213 }
214 err->num_errors++;
Ian Rogers448d7322019-10-30 15:34:39 -0700215}
Jason Baronf6bdafe2009-07-21 12:20:22 -0400216
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200217struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400218{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200219 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400220 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300221 struct dirent *sys_dirent, *evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100222 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200223 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400224 u64 id;
225 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300226 char *dir_path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400227
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -0300228 sys_dir = tracing_events__opendir();
Jason Baronf6bdafe2009-07-21 12:20:22 -0400229 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200230 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400231
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300232 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300233 dir_path = get_events_file(sys_dirent->d_name);
234 if (!dir_path)
235 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200236 evt_dir = opendir(dir_path);
237 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300238 goto next;
Eric Dumazet725b1362009-09-24 15:39:09 +0200239
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -0300240 for_each_event(dir_path, evt_dir, evt_dirent) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200241
Jiri Olsa77f18152018-03-19 09:29:01 +0100242 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
243 evt_dirent->d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200244 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400245 if (fd < 0)
246 continue;
247 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
248 close(fd);
249 continue;
250 }
251 close(fd);
252 id = atoll(id_buf);
253 if (id == config) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300254 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400255 closedir(evt_dir);
256 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100257 path = zalloc(sizeof(*path));
Taeung Song5aa365f2017-02-01 21:34:05 +0900258 if (!path)
259 return NULL;
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200260 path->system = malloc(MAX_EVENT_LENGTH);
261 if (!path->system) {
262 free(path);
263 return NULL;
264 }
265 path->name = malloc(MAX_EVENT_LENGTH);
266 if (!path->name) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300267 zfree(&path->system);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200268 free(path);
269 return NULL;
270 }
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300271 strncpy(path->system, sys_dirent->d_name,
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200272 MAX_EVENT_LENGTH);
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -0300273 strncpy(path->name, evt_dirent->d_name,
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200274 MAX_EVENT_LENGTH);
275 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400276 }
277 }
278 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300279next:
280 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400281 }
282
Jason Baronf6bdafe2009-07-21 12:20:22 -0400283 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200284 return NULL;
285}
286
Namhyung Kime7c93f02013-06-26 16:14:05 +0900287struct tracepoint_path *tracepoint_name_to_path(const char *name)
288{
289 struct tracepoint_path *path = zalloc(sizeof(*path));
290 char *str = strchr(name, ':');
291
292 if (path == NULL || str == NULL) {
293 free(path);
294 return NULL;
295 }
296
297 path->system = strndup(name, str - name);
298 path->name = strdup(str+1);
299
300 if (path->system == NULL || path->name == NULL) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300301 zfree(&path->system);
302 zfree(&path->name);
Taeung Song506fde12017-02-01 21:34:06 +0900303 zfree(&path);
Namhyung Kime7c93f02013-06-26 16:14:05 +0900304 }
305
306 return path;
307}
308
David Ahern1424dc92011-03-09 22:23:28 -0700309const char *event_type(int type)
310{
311 switch (type) {
312 case PERF_TYPE_HARDWARE:
313 return "hardware";
314
315 case PERF_TYPE_SOFTWARE:
316 return "software";
317
318 case PERF_TYPE_TRACEPOINT:
319 return "tracepoint";
320
321 case PERF_TYPE_HW_CACHE:
322 return "hardware-cache";
323
324 default:
325 break;
326 }
327
328 return "unknown";
329}
330
Wang Nane814fdd2016-02-19 11:43:59 +0000331static int parse_events__is_name_term(struct parse_events_term *term)
332{
333 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
334}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800335
Wang Nane814fdd2016-02-19 11:43:59 +0000336static char *get_config_name(struct list_head *head_terms)
337{
338 struct parse_events_term *term;
339
340 if (!head_terms)
341 return NULL;
342
343 list_for_each_entry(term, head_terms, list)
344 if (parse_events__is_name_term(term))
345 return term->val.str;
346
347 return NULL;
348}
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800349
Jiri Olsa32dcd022019-07-21 13:23:51 +0200350static struct evsel *
Stephane Eranian410136f2013-11-12 17:58:49 +0100351__add_event(struct list_head *list, int *idx,
352 struct perf_event_attr *attr,
Mark Rutland66ec11912017-10-06 19:38:22 +0100353 char *name, struct perf_pmu *pmu,
Andi Kleenf0fbb112019-03-26 15:18:21 -0700354 struct list_head *config_terms, bool auto_merge_stats,
355 const char *cpu_list)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100356{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200357 struct evsel *evsel;
Jiri Olsaf8548392019-07-21 13:23:49 +0200358 struct perf_cpu_map *cpus = pmu ? pmu->cpus :
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200359 cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100360
361 event_attr_init(attr);
362
Taeung Song75fc5ae2017-02-01 21:34:04 +0900363 evsel = perf_evsel__new_idx(attr, *idx);
David Ahernc5cd8ac2013-07-02 13:27:25 -0600364 if (!evsel)
Stephane Eranian410136f2013-11-12 17:58:49 +0100365 return NULL;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100366
Taeung Song75fc5ae2017-02-01 21:34:04 +0900367 (*idx)++;
Jiri Olsad400bd32019-07-21 13:24:37 +0200368 evsel->core.cpus = perf_cpu_map__get(cpus);
Jiri Olsafe1f61b2019-07-21 13:24:38 +0200369 evsel->core.own_cpus = perf_cpu_map__get(cpus);
Jiri Olsa648b5af2019-08-06 11:35:19 +0200370 evsel->core.system_wide = pmu ? pmu->is_uncore : false;
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -0300371 evsel->auto_merge_stats = auto_merge_stats;
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200372
Arnaldo Carvalho de Melo9db17632012-06-12 13:45:00 -0300373 if (name)
374 evsel->name = strdup(name);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400375
376 if (config_terms)
377 list_splice(config_terms, &evsel->config_terms);
378
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200379 list_add_tail(&evsel->core.node, list);
Stephane Eranian410136f2013-11-12 17:58:49 +0100380 return evsel;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100381}
382
David Ahernc5cd8ac2013-07-02 13:27:25 -0600383static int add_event(struct list_head *list, int *idx,
Jiri Olsa930a2e22015-07-29 05:42:10 -0400384 struct perf_event_attr *attr, char *name,
385 struct list_head *config_terms)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800386{
Andi Kleenf0fbb112019-03-26 15:18:21 -0700387 return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
388}
389
390static int add_event_tool(struct list_head *list, int *idx,
391 enum perf_tool_event tool_event)
392{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200393 struct evsel *evsel;
Andi Kleenf0fbb112019-03-26 15:18:21 -0700394 struct perf_event_attr attr = {
395 .type = PERF_TYPE_SOFTWARE,
396 .config = PERF_COUNT_SW_DUMMY,
397 };
398
399 evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
400 if (!evsel)
401 return -ENOMEM;
402 evsel->tool_event = tool_event;
403 if (tool_event == PERF_TOOL_DURATION_TIME)
404 evsel->unit = strdup("ns");
405 return 0;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800406}
407
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300408static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200409{
410 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000411 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200412
413 for (i = 0; i < size; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300414 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000415 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100416 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000417 longest = n;
418 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100419 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000420 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200421 }
422
Ingo Molnar89536452009-06-06 21:04:17 +0200423 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200424}
425
Wang Nan43d0b972016-02-19 11:44:01 +0000426typedef int config_term_func_t(struct perf_event_attr *attr,
427 struct parse_events_term *term,
428 struct parse_events_error *err);
429static int config_term_common(struct perf_event_attr *attr,
430 struct parse_events_term *term,
431 struct parse_events_error *err);
432static int config_attr(struct perf_event_attr *attr,
433 struct list_head *head,
434 struct parse_events_error *err,
435 config_term_func_t config_term);
436
David Ahernc5cd8ac2013-07-02 13:27:25 -0600437int parse_events_add_cache(struct list_head *list, int *idx,
Wang Nan43d0b972016-02-19 11:44:01 +0000438 char *type, char *op_result1, char *op_result2,
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300439 struct parse_events_error *err,
Wang Nan43d0b972016-02-19 11:44:01 +0000440 struct list_head *head_config)
Ingo Molnar8326f442009-06-05 20:22:46 +0200441{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100442 struct perf_event_attr attr;
Wang Nan43d0b972016-02-19 11:44:01 +0000443 LIST_HEAD(config_terms);
444 char name[MAX_NAME_LEN], *config_name;
Paul Mackerras61c45982009-07-01 13:04:34 +1000445 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100446 char *op_result[2] = { op_result1, op_result2 };
447 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200448
Ingo Molnar8326f442009-06-05 20:22:46 +0200449 /*
450 * No fallback - if we cannot get a clear cache type
451 * then bail out:
452 */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300453 cache_type = parse_aliases(type, perf_evsel__hw_cache,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100454 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200455 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100456 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200457
Wang Nan43d0b972016-02-19 11:44:01 +0000458 config_name = get_config_name(head_config);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100459 n = snprintf(name, MAX_NAME_LEN, "%s", type);
460
461 for (i = 0; (i < 2) && (op_result[i]); i++) {
462 char *str = op_result[i];
463
Jiri Olsa275ef382012-09-05 19:51:33 +0200464 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000465
466 if (cache_op == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300467 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100468 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000469 if (cache_op >= 0) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300470 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100471 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000472 continue;
473 }
474 }
475
476 if (cache_result == -1) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300477 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
478 PERF_COUNT_HW_CACHE_RESULT_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000479 if (cache_result >= 0)
480 continue;
481 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000482 }
483
Ingo Molnar8326f442009-06-05 20:22:46 +0200484 /*
485 * Fall back to reads:
486 */
Ingo Molnar89536452009-06-06 21:04:17 +0200487 if (cache_op == -1)
488 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200489
Ingo Molnar8326f442009-06-05 20:22:46 +0200490 /*
491 * Fall back to accesses:
492 */
493 if (cache_result == -1)
494 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
495
Jiri Olsa89812fc2012-03-15 20:09:15 +0100496 memset(&attr, 0, sizeof(attr));
497 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
498 attr.type = PERF_TYPE_HW_CACHE;
Wang Nan43d0b972016-02-19 11:44:01 +0000499
500 if (head_config) {
Arnaldo Carvalho de Melo2c97b0d2016-02-19 19:47:04 -0300501 if (config_attr(&attr, head_config, err,
Wang Nan43d0b972016-02-19 11:44:01 +0000502 config_term_common))
503 return -EINVAL;
504
505 if (get_config_terms(head_config, &config_terms))
506 return -ENOMEM;
507 }
508 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
Ingo Molnar8326f442009-06-05 20:22:46 +0200509}
510
Jiri Olsa272ed292015-09-29 17:05:31 +0200511static void tracepoint_error(struct parse_events_error *e, int err,
Wang Nan8c619d62016-07-13 10:44:03 +0000512 const char *sys, const char *name)
Jiri Olsa19658172015-09-07 10:38:07 +0200513{
514 char help[BUFSIZ];
515
Adrian Hunterec183d22016-01-26 14:05:20 +0200516 if (!e)
517 return;
518
Jiri Olsa19658172015-09-07 10:38:07 +0200519 /*
520 * We get error directly from syscall errno ( > 0),
521 * or from encoded pointer's error ( < 0).
522 */
523 err = abs(err);
524
525 switch (err) {
526 case EACCES:
Jiri Olsa272ed292015-09-29 17:05:31 +0200527 e->str = strdup("can't access trace events");
Jiri Olsa19658172015-09-07 10:38:07 +0200528 break;
529 case ENOENT:
Jiri Olsa272ed292015-09-29 17:05:31 +0200530 e->str = strdup("unknown tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200531 break;
532 default:
Jiri Olsa272ed292015-09-29 17:05:31 +0200533 e->str = strdup("failed to add tracepoint");
Jiri Olsa19658172015-09-07 10:38:07 +0200534 break;
535 }
536
537 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
Jiri Olsa272ed292015-09-29 17:05:31 +0200538 e->help = strdup(help);
Jiri Olsa19658172015-09-07 10:38:07 +0200539}
540
David Ahernc5cd8ac2013-07-02 13:27:25 -0600541static int add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000542 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200543 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000544 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200545{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200546 struct evsel *evsel;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200547
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300548 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
Jiri Olsa19658172015-09-07 10:38:07 +0200549 if (IS_ERR(evsel)) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200550 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200551 return PTR_ERR(evsel);
Jiri Olsa19658172015-09-07 10:38:07 +0200552 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200553
He Kuange637d172015-09-28 03:52:16 +0000554 if (head_config) {
555 LIST_HEAD(config_terms);
556
557 if (get_config_terms(head_config, &config_terms))
558 return -ENOMEM;
559 list_splice(&config_terms, &evsel->config_terms);
560 }
561
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200562 list_add_tail(&evsel->core.node, list);
Arnaldo Carvalho de Melo82fe1c22012-09-26 17:13:07 -0300563 return 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200564}
565
David Ahernc5cd8ac2013-07-02 13:27:25 -0600566static int add_tracepoint_multi_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000567 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200568 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000569 struct list_head *head_config)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200570{
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300571 char *evt_path;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200572 struct dirent *evt_ent;
573 DIR *evt_dir;
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200574 int ret = 0, found = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200575
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300576 evt_path = get_events_file(sys_name);
577 if (!evt_path) {
578 tracepoint_error(err, errno, sys_name, evt_name);
579 return -1;
580 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200581 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200582 if (!evt_dir) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300583 put_events_file(evt_path);
Jiri Olsa272ed292015-09-29 17:05:31 +0200584 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100585 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200586 }
587
Jiri Olsa89812fc2012-03-15 20:09:15 +0100588 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200589 if (!strcmp(evt_ent->d_name, ".")
590 || !strcmp(evt_ent->d_name, "..")
591 || !strcmp(evt_ent->d_name, "enable")
592 || !strcmp(evt_ent->d_name, "filter"))
593 continue;
594
Jiri Olsa89812fc2012-03-15 20:09:15 +0100595 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500596 continue;
597
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200598 found++;
599
He Kuange637d172015-09-28 03:52:16 +0000600 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200601 err, head_config);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200602 }
603
Jiri Olsa27bf90b2015-10-05 21:31:17 +0200604 if (!found) {
605 tracepoint_error(err, ENOENT, sys_name, evt_name);
606 ret = -1;
607 }
608
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -0300609 put_events_file(evt_path);
Jiri Olsa0bd3f082012-12-17 14:08:36 +0100610 closedir(evt_dir);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100611 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200612}
613
David Ahernc5cd8ac2013-07-02 13:27:25 -0600614static int add_tracepoint_event(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000615 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200616 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000617 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100618{
619 return strpbrk(evt_name, "*?") ?
He Kuange637d172015-09-28 03:52:16 +0000620 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200621 err, head_config) :
He Kuange637d172015-09-28 03:52:16 +0000622 add_tracepoint(list, idx, sys_name, evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200623 err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100624}
625
David Ahernc5cd8ac2013-07-02 13:27:25 -0600626static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +0000627 const char *sys_name, const char *evt_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200628 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +0000629 struct list_head *head_config)
Jiri Olsaf35488f2012-12-17 14:08:37 +0100630{
631 struct dirent *events_ent;
632 DIR *events_dir;
633 int ret = 0;
634
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -0300635 events_dir = tracing_events__opendir();
Jiri Olsaf35488f2012-12-17 14:08:37 +0100636 if (!events_dir) {
Jiri Olsa272ed292015-09-29 17:05:31 +0200637 tracepoint_error(err, errno, sys_name, evt_name);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100638 return -1;
639 }
640
641 while (!ret && (events_ent = readdir(events_dir))) {
642 if (!strcmp(events_ent->d_name, ".")
643 || !strcmp(events_ent->d_name, "..")
644 || !strcmp(events_ent->d_name, "enable")
645 || !strcmp(events_ent->d_name, "header_event")
646 || !strcmp(events_ent->d_name, "header_page"))
647 continue;
648
649 if (!strglobmatch(events_ent->d_name, sys_name))
650 continue;
651
652 ret = add_tracepoint_event(list, idx, events_ent->d_name,
Jiri Olsa272ed292015-09-29 17:05:31 +0200653 evt_name, err, head_config);
Jiri Olsaf35488f2012-12-17 14:08:37 +0100654 }
655
656 closedir(events_dir);
657 return ret;
658}
659
Wang Nan4edf30e2015-10-14 12:41:17 +0000660struct __add_bpf_event_param {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300661 struct parse_events_state *parse_state;
Wang Nan4edf30e2015-10-14 12:41:17 +0000662 struct list_head *list;
Wang Nan95088a52016-02-22 09:10:36 +0000663 struct list_head *head_config;
Wang Nan4edf30e2015-10-14 12:41:17 +0000664};
665
Arnaldo Carvalho de Meloaf4a0992019-07-15 16:22:57 -0300666static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
Wang Nan4edf30e2015-10-14 12:41:17 +0000667 void *_param)
668{
669 LIST_HEAD(new_evsels);
670 struct __add_bpf_event_param *param = _param;
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300671 struct parse_events_state *parse_state = param->parse_state;
Wang Nan4edf30e2015-10-14 12:41:17 +0000672 struct list_head *list = param->list;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200673 struct evsel *pos;
Wang Nan4edf30e2015-10-14 12:41:17 +0000674 int err;
Arnaldo Carvalho de Melo2620b7e2019-07-15 15:29:34 -0300675 /*
676 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
677 * then don't add the tracepoint, this will be used for something else, like
678 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
679 *
680 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
681 */
682 if (group[0] == '!')
683 return 0;
Wang Nan4edf30e2015-10-14 12:41:17 +0000684
685 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
Wang Nancd102d72016-07-13 10:44:04 +0000686 group, event, fd);
Wang Nan4edf30e2015-10-14 12:41:17 +0000687
Arnaldo Carvalho de Melo5d369a72017-08-17 14:13:25 -0300688 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
689 event, parse_state->error,
Wang Nan95088a52016-02-22 09:10:36 +0000690 param->head_config);
Wang Nan4edf30e2015-10-14 12:41:17 +0000691 if (err) {
Jiri Olsa32dcd022019-07-21 13:23:51 +0200692 struct evsel *evsel, *tmp;
Wang Nan4edf30e2015-10-14 12:41:17 +0000693
694 pr_debug("Failed to add BPF event %s:%s\n",
Wang Nancd102d72016-07-13 10:44:04 +0000695 group, event);
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200696 list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
697 list_del_init(&evsel->core.node);
Jiri Olsa5eb2dd22019-07-21 13:23:57 +0200698 evsel__delete(evsel);
Wang Nan4edf30e2015-10-14 12:41:17 +0000699 }
700 return err;
701 }
Wang Nancd102d72016-07-13 10:44:04 +0000702 pr_debug("adding %s:%s\n", group, event);
Wang Nan4edf30e2015-10-14 12:41:17 +0000703
Jiri Olsab27c4ec2019-07-21 13:24:22 +0200704 list_for_each_entry(pos, &new_evsels, core.node) {
Wang Nan1f45b1d2015-10-14 12:41:18 +0000705 pr_debug("adding %s:%s to %p\n",
Wang Nancd102d72016-07-13 10:44:04 +0000706 group, event, pos);
Wang Nan1f45b1d2015-10-14 12:41:18 +0000707 pos->bpf_fd = fd;
Arnaldo Carvalho de Meloaf4a0992019-07-15 16:22:57 -0300708 pos->bpf_obj = obj;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000709 }
Wang Nan4edf30e2015-10-14 12:41:17 +0000710 list_splice(&new_evsels, list);
711 return 0;
712}
713
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300714int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
Wang Nan84c86ca2015-10-14 12:41:14 +0000715 struct list_head *list,
Wang Nan95088a52016-02-22 09:10:36 +0000716 struct bpf_object *obj,
717 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000718{
719 int err;
720 char errbuf[BUFSIZ];
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300721 struct __add_bpf_event_param param = {parse_state, list, head_config};
Wang Nanaa3abf32015-10-14 12:41:15 +0000722 static bool registered_unprobe_atexit = false;
Wang Nan84c86ca2015-10-14 12:41:14 +0000723
724 if (IS_ERR(obj) || !obj) {
725 snprintf(errbuf, sizeof(errbuf),
726 "Internal error: load bpf obj with NULL");
727 err = -EINVAL;
728 goto errout;
729 }
730
731 /*
Wang Nanaa3abf32015-10-14 12:41:15 +0000732 * Register atexit handler before calling bpf__probe() so
733 * bpf__probe() don't need to unprobe probe points its already
734 * created when failure.
735 */
736 if (!registered_unprobe_atexit) {
737 atexit(bpf__clear);
738 registered_unprobe_atexit = true;
739 }
740
741 err = bpf__probe(obj);
742 if (err) {
743 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
744 goto errout;
745 }
746
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000747 err = bpf__load(obj);
748 if (err) {
749 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
750 goto errout;
751 }
752
Wang Nancd102d72016-07-13 10:44:04 +0000753 err = bpf__foreach_event(obj, add_bpf_event, &param);
Wang Nan4edf30e2015-10-14 12:41:17 +0000754 if (err) {
755 snprintf(errbuf, sizeof(errbuf),
756 "Attach events in BPF object failed");
757 goto errout;
758 }
759
760 return 0;
Wang Nan84c86ca2015-10-14 12:41:14 +0000761errout:
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300762 parse_state->error->help = strdup("(add -v to see detail)");
763 parse_state->error->str = strdup(errbuf);
Wang Nan84c86ca2015-10-14 12:41:14 +0000764 return err;
765}
766
Wang Nana34f3be2016-02-22 09:10:31 +0000767static int
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300768parse_events_config_bpf(struct parse_events_state *parse_state,
Wang Nana34f3be2016-02-22 09:10:31 +0000769 struct bpf_object *obj,
770 struct list_head *head_config)
771{
772 struct parse_events_term *term;
773 int error_pos;
774
775 if (!head_config || list_empty(head_config))
776 return 0;
777
778 list_for_each_entry(term, head_config, list) {
779 char errbuf[BUFSIZ];
780 int err;
781
782 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
783 snprintf(errbuf, sizeof(errbuf),
784 "Invalid config term for BPF object");
785 errbuf[BUFSIZ - 1] = '\0';
786
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300787 parse_state->error->idx = term->err_term;
788 parse_state->error->str = strdup(errbuf);
Wang Nana34f3be2016-02-22 09:10:31 +0000789 return -EINVAL;
790 }
791
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300792 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
Wang Nana34f3be2016-02-22 09:10:31 +0000793 if (err) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300794 bpf__strerror_config_obj(obj, term, parse_state->evlist,
Wang Nana34f3be2016-02-22 09:10:31 +0000795 &error_pos, err, errbuf,
796 sizeof(errbuf));
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300797 parse_state->error->help = strdup(
Wang Nan7630b3e2016-02-22 09:10:33 +0000798"Hint:\tValid config terms:\n"
Wang Nane571e022016-02-22 09:10:35 +0000799" \tmap:[<arraymap>].value<indices>=[value]\n"
800" \tmap:[<eventmap>].event<indices>=[event]\n"
Wang Nan7630b3e2016-02-22 09:10:33 +0000801"\n"
Wang Nane571e022016-02-22 09:10:35 +0000802" \twhere <indices> is something like [0,3...5] or [all]\n"
Wang Nana34f3be2016-02-22 09:10:31 +0000803" \t(add -v to see detail)");
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300804 parse_state->error->str = strdup(errbuf);
Wang Nana34f3be2016-02-22 09:10:31 +0000805 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300806 parse_state->error->idx = term->err_val;
Wang Nana34f3be2016-02-22 09:10:31 +0000807 else
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300808 parse_state->error->idx = term->err_term + error_pos;
Wang Nana34f3be2016-02-22 09:10:31 +0000809 return err;
810 }
811 }
812 return 0;
813}
814
Wang Nan95088a52016-02-22 09:10:36 +0000815/*
816 * Split config terms:
817 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
818 * 'call-graph=fp' is 'evt config', should be applied to each
819 * events in bpf.c.
820 * 'map:array.value[0]=1' is 'obj config', should be processed
821 * with parse_events_config_bpf.
822 *
823 * Move object config terms from the first list to obj_head_config.
824 */
825static void
826split_bpf_config_terms(struct list_head *evt_head_config,
827 struct list_head *obj_head_config)
828{
829 struct parse_events_term *term, *temp;
830
831 /*
832 * Currectly, all possible user config term
833 * belong to bpf object. parse_events__is_hardcoded_term()
834 * happends to be a good flag.
835 *
836 * See parse_events_config_bpf() and
837 * config_term_tracepoint().
838 */
839 list_for_each_entry_safe(term, temp, evt_head_config, list)
840 if (!parse_events__is_hardcoded_term(term))
841 list_move_tail(&term->list, obj_head_config);
842}
843
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300844int parse_events_load_bpf(struct parse_events_state *parse_state,
Wang Nan84c86ca2015-10-14 12:41:14 +0000845 struct list_head *list,
Wang Nand509db02015-10-14 12:41:20 +0000846 char *bpf_file_name,
Wang Nana34f3be2016-02-22 09:10:31 +0000847 bool source,
848 struct list_head *head_config)
Wang Nan84c86ca2015-10-14 12:41:14 +0000849{
Wang Nana34f3be2016-02-22 09:10:31 +0000850 int err;
Wang Nan95088a52016-02-22 09:10:36 +0000851 struct bpf_object *obj;
852 LIST_HEAD(obj_head_config);
853
854 if (head_config)
855 split_bpf_config_terms(head_config, &obj_head_config);
Wang Nan84c86ca2015-10-14 12:41:14 +0000856
Wang Nand509db02015-10-14 12:41:20 +0000857 obj = bpf__prepare_load(bpf_file_name, source);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000858 if (IS_ERR(obj)) {
Wang Nan84c86ca2015-10-14 12:41:14 +0000859 char errbuf[BUFSIZ];
Wang Nan84c86ca2015-10-14 12:41:14 +0000860
Wang Nan6371ca3b2015-11-06 13:49:37 +0000861 err = PTR_ERR(obj);
Wang Nan84c86ca2015-10-14 12:41:14 +0000862
863 if (err == -ENOTSUP)
864 snprintf(errbuf, sizeof(errbuf),
865 "BPF support is not compiled");
866 else
Wang Nand3e0ce32015-11-06 13:58:09 +0000867 bpf__strerror_prepare_load(bpf_file_name,
868 source,
869 -err, errbuf,
870 sizeof(errbuf));
Wang Nan84c86ca2015-10-14 12:41:14 +0000871
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300872 parse_state->error->help = strdup("(add -v to see detail)");
873 parse_state->error->str = strdup(errbuf);
Wang Nan84c86ca2015-10-14 12:41:14 +0000874 return err;
875 }
876
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300877 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
Wang Nana34f3be2016-02-22 09:10:31 +0000878 if (err)
879 return err;
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300880 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
Wang Nan95088a52016-02-22 09:10:36 +0000881
882 /*
883 * Caller doesn't know anything about obj_head_config,
884 * so combine them together again before returnning.
885 */
886 if (head_config)
887 list_splice_tail(&obj_head_config, head_config);
888 return err;
Wang Nan84c86ca2015-10-14 12:41:14 +0000889}
890
Jiri Olsa89812fc2012-03-15 20:09:15 +0100891static int
892parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100893{
894 int i;
895
896 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100897 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100898 break;
899
Jiri Olsa75827322012-06-29 09:22:54 +0200900#define CHECK_SET_TYPE(bit) \
901do { \
902 if (attr->bp_type & bit) \
903 return -EINVAL; \
904 else \
905 attr->bp_type |= bit; \
906} while (0)
907
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100908 switch (type[i]) {
909 case 'r':
Jiri Olsa75827322012-06-29 09:22:54 +0200910 CHECK_SET_TYPE(HW_BREAKPOINT_R);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100911 break;
912 case 'w':
Jiri Olsa75827322012-06-29 09:22:54 +0200913 CHECK_SET_TYPE(HW_BREAKPOINT_W);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100914 break;
915 case 'x':
Jiri Olsa75827322012-06-29 09:22:54 +0200916 CHECK_SET_TYPE(HW_BREAKPOINT_X);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100917 break;
918 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100919 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100920 }
921 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100922
Jiri Olsa75827322012-06-29 09:22:54 +0200923#undef CHECK_SET_TYPE
924
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100925 if (!attr->bp_type) /* Default */
926 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
927
Jiri Olsa89812fc2012-03-15 20:09:15 +0100928 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100929}
930
David Ahernc5cd8ac2013-07-02 13:27:25 -0600931int parse_events_add_breakpoint(struct list_head *list, int *idx,
Jacob Shin3741eb92014-05-29 17:26:51 +0200932 void *ptr, char *type, u64 len)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100933{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100934 struct perf_event_attr attr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100935
Jiri Olsa89812fc2012-03-15 20:09:15 +0100936 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100937 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100938
Jiri Olsa89812fc2012-03-15 20:09:15 +0100939 if (parse_breakpoint_type(type, &attr))
940 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100941
Jacob Shin3741eb92014-05-29 17:26:51 +0200942 /* Provide some defaults if len is not specified */
943 if (!len) {
944 if (attr.bp_type == HW_BREAKPOINT_X)
945 len = sizeof(long);
946 else
947 len = HW_BREAKPOINT_LEN_4;
948 }
949
950 attr.bp_len = len;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200951
Jiri Olsa89812fc2012-03-15 20:09:15 +0100952 attr.type = PERF_TYPE_BREAKPOINT;
Jovi Zhang4a841d62012-07-15 03:03:10 +0800953 attr.sample_period = 1;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100954
Jiri Olsa930a2e22015-07-29 05:42:10 -0400955 return add_event(list, idx, &attr, NULL, NULL);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100956}
957
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200958static int check_type_val(struct parse_events_term *term,
959 struct parse_events_error *err,
960 int type)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100961{
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200962 if (type == term->type_val)
963 return 0;
964
965 if (err) {
Ian Rogers448d7322019-10-30 15:34:39 -0700966 parse_events__handle_error(err, term->err_val,
967 type == PARSE_EVENTS__TERM_TYPE_NUM
968 ? strdup("expected numeric value")
969 : strdup("expected string value"),
970 NULL);
Jiri Olsa3b0e3712015-04-22 21:10:22 +0200971 }
972 return -EINVAL;
973}
974
Wang Nan17cb5f82016-02-19 11:43:57 +0000975/*
976 * Update according to parse-events.l
977 */
978static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
979 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
980 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
981 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
982 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
983 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
984 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
985 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
986 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
987 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
988 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
989 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
990 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
991 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300992 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -0300993 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr",
Wang Nan626a6b72016-07-14 08:34:45 +0000994 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
995 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
Mathieu Poirierdd60fba2016-09-06 10:37:15 -0600996 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
Jin Yao064b4e82019-04-12 21:59:47 +0800997 [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore",
Adrian Hunter1b992152019-08-06 11:46:05 +0300998 [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output",
Wang Nan17cb5f82016-02-19 11:43:57 +0000999};
1000
Wang Nan1669e502016-02-19 11:43:58 +00001001static bool config_term_shrinked;
1002
1003static bool
1004config_term_avail(int term_type, struct parse_events_error *err)
1005{
Ian Rogers448d7322019-10-30 15:34:39 -07001006 char *err_str;
1007
Wang Nan1669e502016-02-19 11:43:58 +00001008 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
Ian Rogers448d7322019-10-30 15:34:39 -07001009 parse_events__handle_error(err, -1,
1010 strdup("Invalid term_type"), NULL);
Wang Nan1669e502016-02-19 11:43:58 +00001011 return false;
1012 }
1013 if (!config_term_shrinked)
1014 return true;
1015
1016 switch (term_type) {
1017 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1018 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1019 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1020 case PARSE_EVENTS__TERM_TYPE_NAME:
Sukadev Bhattiprolu06835542016-09-15 15:24:56 -07001021 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jin Yao064b4e82019-04-12 21:59:47 +08001022 case PARSE_EVENTS__TERM_TYPE_PERCORE:
Wang Nan1669e502016-02-19 11:43:58 +00001023 return true;
1024 default:
1025 if (!err)
1026 return false;
1027
1028 /* term_type is validated so indexing is safe */
Ian Rogers448d7322019-10-30 15:34:39 -07001029 if (asprintf(&err_str, "'%s' is not usable in 'perf stat'",
1030 config_term_names[term_type]) >= 0)
1031 parse_events__handle_error(err, -1, err_str, NULL);
Wang Nan1669e502016-02-19 11:43:58 +00001032 return false;
1033 }
1034}
1035
1036void parse_events__shrink_config_terms(void)
1037{
1038 config_term_shrinked = true;
1039}
1040
He Kuang0b8891a2015-09-28 03:52:13 +00001041static int config_term_common(struct perf_event_attr *attr,
1042 struct parse_events_term *term,
1043 struct parse_events_error *err)
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001044{
1045#define CHECK_TYPE_VAL(type) \
1046do { \
1047 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1048 return -EINVAL; \
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001049} while (0)
1050
1051 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001052 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001053 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001054 attr->config = term->val.num;
1055 break;
1056 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001057 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001058 attr->config1 = term->val.num;
1059 break;
1060 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001061 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001062 attr->config2 = term->val.num;
1063 break;
1064 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001065 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +01001066 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001067 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1068 CHECK_TYPE_VAL(NUM);
1069 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001070 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
Andi Kleenac12f672016-10-12 14:02:06 -07001071 CHECK_TYPE_VAL(STR);
1072 if (strcmp(term->val.str, "no") &&
Ian Rogers448d7322019-10-30 15:34:39 -07001073 parse_branch_str(term->val.str,
1074 &attr->branch_sample_type)) {
1075 parse_events__handle_error(err, term->err_val,
1076 strdup("invalid branch sample type"),
1077 NULL);
Andi Kleenac12f672016-10-12 14:02:06 -07001078 return -EINVAL;
1079 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01001080 break;
Kan Liang32067712015-08-04 04:30:19 -04001081 case PARSE_EVENTS__TERM_TYPE_TIME:
1082 CHECK_TYPE_VAL(NUM);
1083 if (term->val.num > 1) {
Ian Rogers448d7322019-10-30 15:34:39 -07001084 parse_events__handle_error(err, term->err_val,
1085 strdup("expected 0 or 1"),
1086 NULL);
Kan Liang32067712015-08-04 04:30:19 -04001087 return -EINVAL;
1088 }
1089 break;
Kan Liangd457c962015-08-11 06:30:47 -04001090 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1091 CHECK_TYPE_VAL(STR);
1092 break;
1093 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1094 CHECK_TYPE_VAL(NUM);
1095 break;
Wang Nan374ce932015-10-28 10:55:02 +00001096 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1097 CHECK_TYPE_VAL(NUM);
1098 break;
1099 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1100 CHECK_TYPE_VAL(NUM);
1101 break;
Wang Nan626a6b72016-07-14 08:34:45 +00001102 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1103 CHECK_TYPE_VAL(NUM);
1104 break;
1105 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1106 CHECK_TYPE_VAL(NUM);
1107 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +02001108 case PARSE_EVENTS__TERM_TYPE_NAME:
1109 CHECK_TYPE_VAL(STR);
1110 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001111 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1112 CHECK_TYPE_VAL(NUM);
1113 break;
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001114 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1115 CHECK_TYPE_VAL(NUM);
1116 break;
Jin Yao064b4e82019-04-12 21:59:47 +08001117 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1118 CHECK_TYPE_VAL(NUM);
1119 if ((unsigned int)term->val.num > 1) {
Ian Rogers448d7322019-10-30 15:34:39 -07001120 parse_events__handle_error(err, term->err_val,
1121 strdup("expected 0 or 1"),
1122 NULL);
Jin Yao064b4e82019-04-12 21:59:47 +08001123 return -EINVAL;
1124 }
1125 break;
Adrian Hunter1b992152019-08-06 11:46:05 +03001126 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1127 CHECK_TYPE_VAL(NUM);
1128 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001129 default:
Ian Rogers448d7322019-10-30 15:34:39 -07001130 parse_events__handle_error(err, term->err_term,
1131 strdup("unknown term"),
1132 parse_events_formats_error_string(NULL));
Jiri Olsa8f707d82012-03-15 20:09:16 +01001133 return -EINVAL;
1134 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001135
Wang Nan1669e502016-02-19 11:43:58 +00001136 /*
1137 * Check term availbility after basic checking so
1138 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1139 *
1140 * If check availbility at the entry of this function,
1141 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1142 * if an invalid config term is provided for legacy events
1143 * (for example, instructions/badterm/...), which is confusing.
1144 */
1145 if (!config_term_avail(term->type_term, err))
1146 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001147 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001148#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +01001149}
1150
He Kuang0b8891a2015-09-28 03:52:13 +00001151static int config_term_pmu(struct perf_event_attr *attr,
1152 struct parse_events_term *term,
1153 struct parse_events_error *err)
1154{
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001155 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1156 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
He Kuang0b8891a2015-09-28 03:52:13 +00001157 /*
1158 * Always succeed for sysfs terms, as we dont know
1159 * at this point what type they need to have.
1160 */
1161 return 0;
1162 else
1163 return config_term_common(attr, term, err);
1164}
1165
He Kuange637d172015-09-28 03:52:16 +00001166static int config_term_tracepoint(struct perf_event_attr *attr,
1167 struct parse_events_term *term,
1168 struct parse_events_error *err)
1169{
1170 switch (term->type_term) {
1171 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1172 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
Wang Nan374ce932015-10-28 10:55:02 +00001173 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1174 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001175 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001176 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
Wang Nan626a6b72016-07-14 08:34:45 +00001177 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1178 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
Adrian Hunter1b992152019-08-06 11:46:05 +03001179 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
He Kuange637d172015-09-28 03:52:16 +00001180 return config_term_common(attr, term, err);
1181 default:
1182 if (err) {
Ian Rogers448d7322019-10-30 15:34:39 -07001183 parse_events__handle_error(err, term->err_term,
1184 strdup("unknown term"),
1185 strdup("valid terms: call-graph,stack-size\n"));
He Kuange637d172015-09-28 03:52:16 +00001186 }
1187 return -EINVAL;
1188 }
1189
1190 return 0;
1191}
1192
Jiri Olsa8f707d82012-03-15 20:09:16 +01001193static int config_attr(struct perf_event_attr *attr,
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001194 struct list_head *head,
He Kuang0b8891a2015-09-28 03:52:13 +00001195 struct parse_events_error *err,
1196 config_term_func_t config_term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001197{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03001198 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001199
1200 list_for_each_entry(term, head, list)
Jiri Olsa3b0e3712015-04-22 21:10:22 +02001201 if (config_term(attr, term, err))
Jiri Olsa8f707d82012-03-15 20:09:16 +01001202 return -EINVAL;
1203
1204 return 0;
1205}
1206
Jiri Olsa930a2e22015-07-29 05:42:10 -04001207static int get_config_terms(struct list_head *head_config,
1208 struct list_head *head_terms __maybe_unused)
1209{
1210#define ADD_CONFIG_TERM(__type, __name, __val) \
1211do { \
1212 struct perf_evsel_config_term *__t; \
1213 \
1214 __t = zalloc(sizeof(*__t)); \
1215 if (!__t) \
1216 return -ENOMEM; \
1217 \
1218 INIT_LIST_HEAD(&__t->list); \
1219 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1220 __t->val.__name = __val; \
Andi Kleenc2f1cea2017-10-20 13:27:55 -07001221 __t->weak = term->weak; \
Jiri Olsa930a2e22015-07-29 05:42:10 -04001222 list_add_tail(&__t->list, head_terms); \
1223} while (0)
1224
1225 struct parse_events_term *term;
1226
1227 list_for_each_entry(term, head_config, list) {
1228 switch (term->type_term) {
Jiri Olsaee4c7582015-07-29 05:42:11 -04001229 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1230 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
Kan Liang32067712015-08-04 04:30:19 -04001231 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +09001232 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1233 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1234 break;
Kan Liang32067712015-08-04 04:30:19 -04001235 case PARSE_EVENTS__TERM_TYPE_TIME:
1236 ADD_CONFIG_TERM(TIME, time, term->val.num);
1237 break;
Kan Liangd457c962015-08-11 06:30:47 -04001238 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1239 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1240 break;
Andi Kleenac12f672016-10-12 14:02:06 -07001241 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1242 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1243 break;
Kan Liangd457c962015-08-11 06:30:47 -04001244 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1245 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1246 break;
Wang Nan374ce932015-10-28 10:55:02 +00001247 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1248 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1249 break;
1250 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1251 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1252 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001253 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1254 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1255 break;
Arnaldo Carvalho de Melo2fda5ad2018-10-19 15:47:34 -03001256 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1257 ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num);
1258 break;
Wang Nan626a6b72016-07-14 08:34:45 +00001259 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1260 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1261 break;
1262 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1263 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1264 break;
Mathieu Poirierdd60fba2016-09-06 10:37:15 -06001265 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1266 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1267 break;
Jin Yao064b4e82019-04-12 21:59:47 +08001268 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1269 ADD_CONFIG_TERM(PERCORE, percore,
1270 term->val.num ? true : false);
1271 break;
Adrian Hunter1b992152019-08-06 11:46:05 +03001272 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1273 ADD_CONFIG_TERM(AUX_OUTPUT, aux_output, term->val.num ? 1 : 0);
1274 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001275 default:
1276 break;
1277 }
1278 }
1279#undef ADD_EVSEL_CONFIG
1280 return 0;
1281}
1282
He Kuange637d172015-09-28 03:52:16 +00001283int parse_events_add_tracepoint(struct list_head *list, int *idx,
Wang Nan8c619d62016-07-13 10:44:03 +00001284 const char *sys, const char *event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001285 struct parse_events_error *err,
He Kuange637d172015-09-28 03:52:16 +00001286 struct list_head *head_config)
1287{
1288 if (head_config) {
1289 struct perf_event_attr attr;
1290
Jiri Olsa272ed292015-09-29 17:05:31 +02001291 if (config_attr(&attr, head_config, err,
He Kuange637d172015-09-28 03:52:16 +00001292 config_term_tracepoint))
1293 return -EINVAL;
1294 }
1295
1296 if (strpbrk(sys, "*?"))
1297 return add_tracepoint_multi_sys(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001298 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001299 else
1300 return add_tracepoint_event(list, idx, sys, event,
Jiri Olsa272ed292015-09-29 17:05:31 +02001301 err, head_config);
He Kuange637d172015-09-28 03:52:16 +00001302}
1303
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001304int parse_events_add_numeric(struct parse_events_state *parse_state,
Jiri Olsa87d650b2015-04-22 21:10:24 +02001305 struct list_head *list,
Robert Richterb527bab2012-08-07 19:43:13 +02001306 u32 type, u64 config,
Jiri Olsa8f707d82012-03-15 20:09:16 +01001307 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301308{
Jiri Olsa89812fc2012-03-15 20:09:15 +01001309 struct perf_event_attr attr;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001310 LIST_HEAD(config_terms);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301311
Jiri Olsa89812fc2012-03-15 20:09:15 +01001312 memset(&attr, 0, sizeof(attr));
1313 attr.type = type;
1314 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001315
Jiri Olsa930a2e22015-07-29 05:42:10 -04001316 if (head_config) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001317 if (config_attr(&attr, head_config, parse_state->error,
He Kuang0b8891a2015-09-28 03:52:13 +00001318 config_term_common))
Jiri Olsa930a2e22015-07-29 05:42:10 -04001319 return -EINVAL;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001320
Jiri Olsa930a2e22015-07-29 05:42:10 -04001321 if (get_config_terms(head_config, &config_terms))
1322 return -ENOMEM;
1323 }
1324
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001325 return add_event(list, &parse_state->idx, &attr,
Wang Nan10bf3582016-02-19 11:44:00 +00001326 get_config_name(head_config), &config_terms);
Paul Mackerras61c45982009-07-01 13:04:34 +10001327}
1328
Andi Kleenf0fbb112019-03-26 15:18:21 -07001329int parse_events_add_tool(struct parse_events_state *parse_state,
1330 struct list_head *list,
1331 enum perf_tool_event tool_event)
1332{
1333 return add_event_tool(list, &parse_state->idx, tool_event);
1334}
1335
Jin Yao064b4e82019-04-12 21:59:47 +08001336static bool config_term_percore(struct list_head *config_terms)
1337{
1338 struct perf_evsel_config_term *term;
1339
1340 list_for_each_entry(term, config_terms, list) {
1341 if (term->type == PERF_EVSEL__CONFIG_TERM_PERCORE)
1342 return term->val.percore;
1343 }
1344
1345 return false;
1346}
1347
Agustin Vega-Friasc199c112018-03-06 09:04:44 -05001348int parse_events_add_pmu(struct parse_events_state *parse_state,
Jiri Olsa36adec82015-04-22 21:10:19 +02001349 struct list_head *list, char *name,
Kan Liang3cdc5c22018-05-07 14:13:43 -07001350 struct list_head *head_config,
1351 bool auto_merge_stats,
1352 bool use_alias)
Jiri Olsa5f537a22012-03-15 20:09:18 +01001353{
1354 struct perf_event_attr attr;
Matt Fleming46441bd2014-09-24 15:04:06 +01001355 struct perf_pmu_info info;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001356 struct perf_pmu *pmu;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001357 struct evsel *evsel;
Andi Kleen333b5662017-09-13 14:50:06 -07001358 struct parse_events_error *err = parse_state->error;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001359 bool use_uncore_alias;
Jiri Olsa930a2e22015-07-29 05:42:10 -04001360 LIST_HEAD(config_terms);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001361
1362 pmu = perf_pmu__find(name);
Andi Kleen333b5662017-09-13 14:50:06 -07001363 if (!pmu) {
Ian Rogers448d7322019-10-30 15:34:39 -07001364 char *err_str;
1365
1366 if (asprintf(&err_str,
Andi Kleen333b5662017-09-13 14:50:06 -07001367 "Cannot find PMU `%s'. Missing kernel support?",
Ian Rogers448d7322019-10-30 15:34:39 -07001368 name) >= 0)
Ian Rogersa910e462019-11-15 23:46:52 -08001369 parse_events__handle_error(err, 0, err_str, NULL);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001370 return -EINVAL;
Andi Kleen333b5662017-09-13 14:50:06 -07001371 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001372
Adrian Hunterdc0a6202014-07-31 09:00:49 +03001373 if (pmu->default_config) {
1374 memcpy(&attr, pmu->default_config,
1375 sizeof(struct perf_event_attr));
1376 } else {
1377 memset(&attr, 0, sizeof(attr));
1378 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001379
Kan Liang3cdc5c22018-05-07 14:13:43 -07001380 use_uncore_alias = (pmu->is_uncore && use_alias);
1381
Adrian Hunterad962272014-08-15 22:08:40 +03001382 if (!head_config) {
1383 attr.type = pmu->type;
Andi Kleenf0fbb112019-03-26 15:18:21 -07001384 evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
1385 auto_merge_stats, NULL);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001386 if (evsel) {
1387 evsel->pmu_name = name;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001388 evsel->use_uncore_alias = use_uncore_alias;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001389 return 0;
1390 } else {
1391 return -ENOMEM;
1392 }
Adrian Hunterad962272014-08-15 22:08:40 +03001393 }
1394
Matt Fleming46441bd2014-09-24 15:04:06 +01001395 if (perf_pmu__check_alias(pmu, head_config, &info))
Zheng Yana6146d52012-06-15 14:31:41 +08001396 return -EINVAL;
1397
Jiri Olsa5f537a22012-03-15 20:09:18 +01001398 /*
1399 * Configure hardcoded terms first, no need to check
1400 * return value when called with fail == 0 ;)
1401 */
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001402 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
Jiri Olsac056ba6a2015-04-22 21:10:18 +02001403 return -EINVAL;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001404
Jiri Olsa930a2e22015-07-29 05:42:10 -04001405 if (get_config_terms(head_config, &config_terms))
1406 return -ENOMEM;
1407
Ian Rogers38f2c422019-10-30 15:34:46 -07001408 if (perf_pmu__config(pmu, &attr, head_config, parse_state->error)) {
1409 struct perf_evsel_config_term *pos, *tmp;
1410
1411 list_for_each_entry_safe(pos, tmp, &config_terms, list) {
1412 list_del_init(&pos->list);
1413 free(pos);
1414 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001415 return -EINVAL;
Ian Rogers38f2c422019-10-30 15:34:46 -07001416 }
Jiri Olsa5f537a22012-03-15 20:09:18 +01001417
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001418 evsel = __add_event(list, &parse_state->idx, &attr,
Mark Rutland66ec11912017-10-06 19:38:22 +01001419 get_config_name(head_config), pmu,
Andi Kleenf0fbb112019-03-26 15:18:21 -07001420 &config_terms, auto_merge_stats, NULL);
Stephane Eranian410136f2013-11-12 17:58:49 +01001421 if (evsel) {
Matt Fleming46441bd2014-09-24 15:04:06 +01001422 evsel->unit = info.unit;
1423 evsel->scale = info.scale;
Matt Fleming044330c2014-11-21 10:31:12 +01001424 evsel->per_pkg = info.per_pkg;
Jiri Olsa1d9e4462014-11-21 10:31:13 +01001425 evsel->snapshot = info.snapshot;
Andi Kleen37932c12017-03-20 13:17:08 -07001426 evsel->metric_expr = info.metric_expr;
Andi Kleen96284812017-03-20 13:17:10 -07001427 evsel->metric_name = info.metric_name;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001428 evsel->pmu_name = name;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001429 evsel->use_uncore_alias = use_uncore_alias;
Jin Yao064b4e82019-04-12 21:59:47 +08001430 evsel->percore = config_term_percore(&evsel->config_terms);
Stephane Eranian410136f2013-11-12 17:58:49 +01001431 }
1432
1433 return evsel ? 0 : -ENOMEM;
Jiri Olsa5f537a22012-03-15 20:09:18 +01001434}
1435
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001436int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
Andi Kleen2073ad32017-03-20 13:17:02 -07001437 char *str, struct list_head **listp)
1438{
Andi Kleen2073ad32017-03-20 13:17:02 -07001439 struct parse_events_term *term;
1440 struct list_head *list;
1441 struct perf_pmu *pmu = NULL;
1442 int ok = 0;
1443
1444 *listp = NULL;
1445 /* Add it for all PMUs that support the alias */
1446 list = malloc(sizeof(struct list_head));
1447 if (!list)
1448 return -1;
1449 INIT_LIST_HEAD(list);
1450 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1451 struct perf_pmu_alias *alias;
1452
1453 list_for_each_entry(alias, &pmu->aliases, list) {
1454 if (!strcasecmp(alias->name, str)) {
Ian Rogersb6645a72019-10-30 15:34:43 -07001455 struct list_head *head;
1456 char *config;
1457
Andi Kleen2073ad32017-03-20 13:17:02 -07001458 head = malloc(sizeof(struct list_head));
1459 if (!head)
1460 return -1;
1461 INIT_LIST_HEAD(head);
Ian Rogersb6645a72019-10-30 15:34:43 -07001462 config = strdup(str);
1463 if (!config)
Andi Kleen2073ad32017-03-20 13:17:02 -07001464 return -1;
Ian Rogersb6645a72019-10-30 15:34:43 -07001465 if (parse_events_term__num(&term,
1466 PARSE_EVENTS__TERM_TYPE_USER,
1467 config, 1, false, &config,
1468 NULL) < 0) {
1469 free(list);
1470 free(config);
1471 return -1;
1472 }
Andi Kleen2073ad32017-03-20 13:17:02 -07001473 list_add_tail(&term->list, head);
1474
Agustin Vega-Friasc199c112018-03-06 09:04:44 -05001475 if (!parse_events_add_pmu(parse_state, list,
Kan Liang3cdc5c22018-05-07 14:13:43 -07001476 pmu->name, head,
1477 true, true)) {
Ian Rogersb6645a72019-10-30 15:34:43 -07001478 pr_debug("%s -> %s/%s/\n", config,
Andi Kleen2073ad32017-03-20 13:17:02 -07001479 pmu->name, alias->str);
1480 ok++;
1481 }
1482
1483 parse_events_terms__delete(head);
1484 }
1485 }
1486 }
Ian Rogersb6645a72019-10-30 15:34:43 -07001487 if (!ok) {
1488 free(list);
Andi Kleen2073ad32017-03-20 13:17:02 -07001489 return -1;
Ian Rogersb6645a72019-10-30 15:34:43 -07001490 }
Andi Kleen2073ad32017-03-20 13:17:02 -07001491 *listp = list;
1492 return 0;
1493}
1494
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001495int parse_events__modifier_group(struct list_head *list,
1496 char *event_mod)
Jiri Olsa89efb022012-08-08 12:14:14 +02001497{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001498 return parse_events__modifier_event(list, event_mod, true);
1499}
1500
Kan Liang3cdc5c22018-05-07 14:13:43 -07001501/*
1502 * Check if the two uncore PMUs are from the same uncore block
1503 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1504 */
1505static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
1506{
1507 char *end_a, *end_b;
1508
1509 end_a = strrchr(pmu_name_a, '_');
1510 end_b = strrchr(pmu_name_b, '_');
1511
1512 if (!end_a || !end_b)
1513 return false;
1514
1515 if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
1516 return false;
1517
1518 return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
1519}
1520
1521static int
1522parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
1523 struct parse_events_state *parse_state)
1524{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001525 struct evsel *evsel, *leader;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001526 uintptr_t *leaders;
1527 bool is_leader = true;
1528 int i, nr_pmu = 0, total_members, ret = 0;
1529
Jiri Olsab27c4ec2019-07-21 13:24:22 +02001530 leader = list_first_entry(list, struct evsel, core.node);
1531 evsel = list_last_entry(list, struct evsel, core.node);
Kan Liang3cdc5c22018-05-07 14:13:43 -07001532 total_members = evsel->idx - leader->idx + 1;
1533
1534 leaders = calloc(total_members, sizeof(uintptr_t));
1535 if (WARN_ON(!leaders))
1536 return 0;
1537
1538 /*
1539 * Going through the whole group and doing sanity check.
1540 * All members must use alias, and be from the same uncore block.
1541 * Also, storing the leader events in an array.
1542 */
1543 __evlist__for_each_entry(list, evsel) {
1544
1545 /* Only split the uncore group which members use alias */
1546 if (!evsel->use_uncore_alias)
1547 goto out;
1548
1549 /* The events must be from the same uncore block */
1550 if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
1551 goto out;
1552
1553 if (!is_leader)
1554 continue;
1555 /*
1556 * If the event's PMU name starts to repeat, it must be a new
1557 * event. That can be used to distinguish the leader from
1558 * other members, even they have the same event name.
1559 */
1560 if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
1561 is_leader = false;
1562 continue;
1563 }
1564 /* The name is always alias name */
1565 WARN_ON(strcmp(leader->name, evsel->name));
1566
1567 /* Store the leader event for each PMU */
1568 leaders[nr_pmu++] = (uintptr_t) evsel;
1569 }
1570
1571 /* only one event alias */
1572 if (nr_pmu == total_members) {
1573 parse_state->nr_groups--;
1574 goto handled;
1575 }
1576
1577 /*
1578 * An uncore event alias is a joint name which means the same event
1579 * runs on all PMUs of a block.
1580 * Perf doesn't support mixed events from different PMUs in the same
1581 * group. The big group has to be split into multiple small groups
1582 * which only include the events from the same PMU.
1583 *
1584 * Here the uncore event aliases must be from the same uncore block.
1585 * The number of PMUs must be same for each alias. The number of new
1586 * small groups equals to the number of PMUs.
1587 * Setting the leader event for corresponding members in each group.
1588 */
1589 i = 0;
1590 __evlist__for_each_entry(list, evsel) {
1591 if (i >= nr_pmu)
1592 i = 0;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001593 evsel->leader = (struct evsel *) leaders[i++];
Kan Liang3cdc5c22018-05-07 14:13:43 -07001594 }
1595
1596 /* The number of members and group name are same for each group */
1597 for (i = 0; i < nr_pmu; i++) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02001598 evsel = (struct evsel *) leaders[i];
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001599 evsel->core.nr_members = total_members / nr_pmu;
Kan Liang3cdc5c22018-05-07 14:13:43 -07001600 evsel->group_name = name ? strdup(name) : NULL;
1601 }
1602
1603 /* Take the new small groups into account */
1604 parse_state->nr_groups += nr_pmu - 1;
1605
1606handled:
1607 ret = 1;
1608out:
1609 free(leaders);
1610 return ret;
1611}
1612
1613void parse_events__set_leader(char *name, struct list_head *list,
1614 struct parse_events_state *parse_state)
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001615{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001616 struct evsel *leader;
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001617
Wang Nan854f7362015-09-06 07:13:17 +00001618 if (list_empty(list)) {
1619 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1620 return;
1621 }
1622
Kan Liang3cdc5c22018-05-07 14:13:43 -07001623 if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
1624 return;
1625
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001626 __perf_evlist__set_leader(list);
Jiri Olsab27c4ec2019-07-21 13:24:22 +02001627 leader = list_entry(list->next, struct evsel, core.node);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001628 leader->group_name = name ? strdup(name) : NULL;
Jiri Olsa89efb022012-08-08 12:14:14 +02001629}
1630
David Ahernc5cd8ac2013-07-02 13:27:25 -06001631/* list_event is assumed to point to malloc'ed memory */
Jiri Olsa5d7be902012-03-20 19:15:40 +01001632void parse_events_update_lists(struct list_head *list_event,
1633 struct list_head *list_all)
1634{
1635 /*
1636 * Called for single event definition. Update the
Jiri Olsa89efb022012-08-08 12:14:14 +02001637 * 'all event' list, and reinit the 'single event'
Jiri Olsa5d7be902012-03-20 19:15:40 +01001638 * list, for next event definition.
1639 */
1640 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +02001641 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +01001642}
1643
Jiri Olsaf5b11352012-08-08 12:21:54 +02001644struct event_modifier {
1645 int eu;
1646 int ek;
1647 int eh;
1648 int eH;
1649 int eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001650 int eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001651 int precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001652 int precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001653 int exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001654 int sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001655 int pinned;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001656 int weak;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001657};
Paul Mackerras61c45982009-07-01 13:04:34 +10001658
Jiri Olsaf5b11352012-08-08 12:21:54 +02001659static int get_event_modifier(struct event_modifier *mod, char *str,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001660 struct evsel *evsel)
Jiri Olsaf5b11352012-08-08 12:21:54 +02001661{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001662 int eu = evsel ? evsel->core.attr.exclude_user : 0;
1663 int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
1664 int eh = evsel ? evsel->core.attr.exclude_hv : 0;
1665 int eH = evsel ? evsel->core.attr.exclude_host : 0;
1666 int eG = evsel ? evsel->core.attr.exclude_guest : 0;
1667 int eI = evsel ? evsel->core.attr.exclude_idle : 0;
1668 int precise = evsel ? evsel->core.attr.precise_ip : 0;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001669 int precise_max = 0;
Jiri Olsa3c176312012-10-10 17:39:03 +02001670 int sample_read = 0;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001671 int pinned = evsel ? evsel->core.attr.pinned : 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001672
1673 int exclude = eu | ek | eh;
1674 int exclude_GH = evsel ? evsel->exclude_GH : 0;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001675 int weak = 0;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001676
Jiri Olsaf5b11352012-08-08 12:21:54 +02001677 memset(mod, 0, sizeof(*mod));
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001678
Paul Mackerras61c45982009-07-01 13:04:34 +10001679 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +02001680 if (*str == 'u') {
1681 if (!exclude)
1682 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001683 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001684 } else if (*str == 'k') {
1685 if (!exclude)
1686 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001687 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +02001688 } else if (*str == 'h') {
1689 if (!exclude)
1690 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +10001691 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +01001692 } else if (*str == 'G') {
1693 if (!exclude_GH)
1694 exclude_GH = eG = eH = 1;
1695 eG = 0;
1696 } else if (*str == 'H') {
1697 if (!exclude_GH)
1698 exclude_GH = eG = eH = 1;
1699 eH = 0;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001700 } else if (*str == 'I') {
1701 eI = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001702 } else if (*str == 'p') {
1703 precise++;
David Ahern13427982012-09-13 14:59:13 -06001704 /* use of precise requires exclude_guest */
1705 if (!exclude_GH)
1706 eG = 1;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001707 } else if (*str == 'P') {
1708 precise_max = 1;
Jiri Olsa3c176312012-10-10 17:39:03 +02001709 } else if (*str == 'S') {
1710 sample_read = 1;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001711 } else if (*str == 'D') {
1712 pinned = 1;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001713 } else if (*str == 'W') {
1714 weak = 1;
Peter Zijlstraab608342010-04-08 23:03:20 +02001715 } else
Paul Mackerras61c45982009-07-01 13:04:34 +10001716 break;
Peter Zijlstraab608342010-04-08 23:03:20 +02001717
Paul Mackerras61c45982009-07-01 13:04:34 +10001718 ++str;
1719 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001720
Jiri Olsa89812fc2012-03-15 20:09:15 +01001721 /*
1722 * precise ip:
1723 *
1724 * 0 - SAMPLE_IP can have arbitrary skid
1725 * 1 - SAMPLE_IP must have constant skid
1726 * 2 - SAMPLE_IP requested to have 0 skid
1727 * 3 - SAMPLE_IP must have 0 skid
1728 *
1729 * See also PERF_RECORD_MISC_EXACT_IP
1730 */
1731 if (precise > 3)
1732 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001733
Jiri Olsaf5b11352012-08-08 12:21:54 +02001734 mod->eu = eu;
1735 mod->ek = ek;
1736 mod->eh = eh;
1737 mod->eH = eH;
1738 mod->eG = eG;
Jiri Olsaa1e12da2015-04-07 23:25:14 +02001739 mod->eI = eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001740 mod->precise = precise;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001741 mod->precise_max = precise_max;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001742 mod->exclude_GH = exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001743 mod->sample_read = sample_read;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001744 mod->pinned = pinned;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001745 mod->weak = weak;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001746
Jiri Olsaf5b11352012-08-08 12:21:54 +02001747 return 0;
1748}
1749
Jiri Olsa534123f2012-11-13 15:32:58 +01001750/*
1751 * Basic modifier sanity check to validate it contains only one
1752 * instance of any modifier (apart from 'p') present.
1753 */
1754static int check_modifier(char *str)
1755{
1756 char *p = str;
1757
1758 /* The sizeof includes 0 byte as well. */
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001759 if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
Jiri Olsa534123f2012-11-13 15:32:58 +01001760 return -1;
1761
1762 while (*p) {
1763 if (*p != 'p' && strchr(p + 1, *p))
1764 return -1;
1765 p++;
1766 }
1767
1768 return 0;
1769}
1770
Jiri Olsaf5b11352012-08-08 12:21:54 +02001771int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1772{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001773 struct evsel *evsel;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001774 struct event_modifier mod;
1775
1776 if (str == NULL)
1777 return 0;
1778
Jiri Olsa534123f2012-11-13 15:32:58 +01001779 if (check_modifier(str))
1780 return -EINVAL;
1781
Jiri Olsaf5b11352012-08-08 12:21:54 +02001782 if (!add && get_event_modifier(&mod, str, NULL))
1783 return -EINVAL;
1784
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001785 __evlist__for_each_entry(list, evsel) {
Jiri Olsaf5b11352012-08-08 12:21:54 +02001786 if (add && get_event_modifier(&mod, str, evsel))
1787 return -EINVAL;
1788
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001789 evsel->core.attr.exclude_user = mod.eu;
1790 evsel->core.attr.exclude_kernel = mod.ek;
1791 evsel->core.attr.exclude_hv = mod.eh;
1792 evsel->core.attr.precise_ip = mod.precise;
1793 evsel->core.attr.exclude_host = mod.eH;
1794 evsel->core.attr.exclude_guest = mod.eG;
1795 evsel->core.attr.exclude_idle = mod.eI;
Jiri Olsaf5b11352012-08-08 12:21:54 +02001796 evsel->exclude_GH = mod.exclude_GH;
Jiri Olsa3c176312012-10-10 17:39:03 +02001797 evsel->sample_read = mod.sample_read;
Jiri Olsa7f94af72015-10-05 20:06:05 +02001798 evsel->precise_max = mod.precise_max;
Andi Kleen5a5dfe42017-08-31 12:40:26 -07001799 evsel->weak_group = mod.weak;
Michael Ellermane9a7c412013-08-06 23:28:05 +10001800
1801 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001802 evsel->core.attr.pinned = mod.pinned;
Jiri Olsa89812fc2012-03-15 20:09:15 +01001803 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +02001804
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301805 return 0;
1806}
1807
Robert Richterac2ba9f2012-08-16 21:10:21 +02001808int parse_events_name(struct list_head *list, char *name)
1809{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001810 struct evsel *evsel;
Robert Richterac2ba9f2012-08-16 21:10:21 +02001811
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001812 __evlist__for_each_entry(list, evsel) {
Robert Richterac2ba9f2012-08-16 21:10:21 +02001813 if (!evsel->name)
1814 evsel->name = strdup(name);
1815 }
1816
1817 return 0;
1818}
1819
Kan Liangdcb4e102014-10-07 11:08:50 -04001820static int
1821comp_pmu(const void *p1, const void *p2)
1822{
1823 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1824 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1825
Andi Kleene312bcf2016-09-15 15:24:53 -07001826 return strcasecmp(pmu1->symbol, pmu2->symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001827}
1828
1829static void perf_pmu__parse_cleanup(void)
1830{
1831 if (perf_pmu_events_list_num > 0) {
1832 struct perf_pmu_event_symbol *p;
1833 int i;
1834
1835 for (i = 0; i < perf_pmu_events_list_num; i++) {
1836 p = perf_pmu_events_list + i;
Taeung Song360e0712017-02-01 21:34:07 +09001837 zfree(&p->symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001838 }
Taeung Song506fde12017-02-01 21:34:06 +09001839 zfree(&perf_pmu_events_list);
Kan Liangdcb4e102014-10-07 11:08:50 -04001840 perf_pmu_events_list_num = 0;
1841 }
1842}
1843
1844#define SET_SYMBOL(str, stype) \
1845do { \
1846 p->symbol = str; \
1847 if (!p->symbol) \
1848 goto err; \
1849 p->type = stype; \
1850} while (0)
1851
1852/*
1853 * Read the pmu events list from sysfs
1854 * Save it into perf_pmu_events_list
1855 */
1856static void perf_pmu__parse_init(void)
1857{
1858
1859 struct perf_pmu *pmu = NULL;
1860 struct perf_pmu_alias *alias;
1861 int len = 0;
1862
Andi Kleen231bb2a2017-01-27 18:03:39 -08001863 pmu = NULL;
1864 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1865 list_for_each_entry(alias, &pmu->aliases, list) {
1866 if (strchr(alias->name, '-'))
1867 len++;
1868 len++;
1869 }
1870 }
1871
1872 if (len == 0) {
Kan Liangdcb4e102014-10-07 11:08:50 -04001873 perf_pmu_events_list_num = -1;
1874 return;
1875 }
Kan Liangdcb4e102014-10-07 11:08:50 -04001876 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1877 if (!perf_pmu_events_list)
1878 return;
1879 perf_pmu_events_list_num = len;
1880
1881 len = 0;
Andi Kleen231bb2a2017-01-27 18:03:39 -08001882 pmu = NULL;
1883 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1884 list_for_each_entry(alias, &pmu->aliases, list) {
1885 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1886 char *tmp = strchr(alias->name, '-');
Kan Liangdcb4e102014-10-07 11:08:50 -04001887
Andi Kleen231bb2a2017-01-27 18:03:39 -08001888 if (tmp != NULL) {
1889 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1890 PMU_EVENT_SYMBOL_PREFIX);
1891 p++;
1892 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1893 len += 2;
1894 } else {
1895 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1896 len++;
1897 }
Kan Liangdcb4e102014-10-07 11:08:50 -04001898 }
1899 }
1900 qsort(perf_pmu_events_list, len,
1901 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1902
1903 return;
1904err:
1905 perf_pmu__parse_cleanup();
1906}
1907
1908enum perf_pmu_event_symbol_type
1909perf_pmu__parse_check(const char *name)
1910{
1911 struct perf_pmu_event_symbol p, *r;
1912
1913 /* scan kernel pmu events from sysfs if needed */
1914 if (perf_pmu_events_list_num == 0)
1915 perf_pmu__parse_init();
1916 /*
1917 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1918 * cpu-cycles has been handled by hardcode.
1919 * So it must be cpu// events, not kernel pmu event.
1920 */
1921 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1922 return PMU_EVENT_SYMBOL_ERR;
1923
1924 p.symbol = strdup(name);
1925 r = bsearch(&p, perf_pmu_events_list,
1926 (size_t) perf_pmu_events_list_num,
1927 sizeof(struct perf_pmu_event_symbol), comp_pmu);
Taeung Song360e0712017-02-01 21:34:07 +09001928 zfree(&p.symbol);
Kan Liangdcb4e102014-10-07 11:08:50 -04001929 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1930}
1931
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001932static int parse_events__scanner(const char *str, void *parse_state, int start_token)
Zheng Yanac20de6f2012-06-15 14:31:39 +08001933{
1934 YY_BUFFER_STATE buffer;
1935 void *scanner;
1936 int ret;
1937
Jiri Olsa90e2b222012-06-15 14:31:40 +08001938 ret = parse_events_lex_init_extra(start_token, &scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001939 if (ret)
1940 return ret;
1941
1942 buffer = parse_events__scan_string(str, scanner);
1943
1944#ifdef PARSER_DEBUG
1945 parse_events_debug = 1;
1946#endif
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001947 ret = parse_events_parse(parse_state, scanner);
Zheng Yanac20de6f2012-06-15 14:31:39 +08001948
1949 parse_events__flush_buffer(buffer, scanner);
1950 parse_events__delete_buffer(buffer, scanner);
1951 parse_events_lex_destroy(scanner);
1952 return ret;
1953}
1954
Jiri Olsa90e2b222012-06-15 14:31:40 +08001955/*
1956 * parse event config string, return a list of event terms.
1957 */
1958int parse_events_terms(struct list_head *terms, const char *str)
1959{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001960 struct parse_events_state parse_state = {
Jiri Olsa90e2b222012-06-15 14:31:40 +08001961 .terms = NULL,
1962 };
1963 int ret;
1964
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001965 ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001966 if (!ret) {
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001967 list_splice(parse_state.terms, terms);
1968 zfree(&parse_state.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001969 return 0;
1970 }
1971
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001972 parse_events_terms__delete(parse_state.terms);
Jiri Olsa90e2b222012-06-15 14:31:40 +08001973 return ret;
1974}
1975
Jiri Olsa63503db2019-07-21 13:23:52 +02001976int parse_events(struct evlist *evlist, const char *str,
Jiri Olsab39b8392015-04-22 21:10:16 +02001977 struct parse_events_error *err)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001978{
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001979 struct parse_events_state parse_state = {
1980 .list = LIST_HEAD_INIT(parse_state.list),
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001981 .idx = evlist->core.nr_entries,
Wang Nan7630b3e2016-02-22 09:10:33 +00001982 .error = err,
1983 .evlist = evlist,
Jiri Olsa46010ab2012-06-15 14:31:38 +08001984 };
Jiri Olsa46010ab2012-06-15 14:31:38 +08001985 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001986
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03001987 ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
Kan Liangdcb4e102014-10-07 11:08:50 -04001988 perf_pmu__parse_cleanup();
Ian Rogers8e8714c2019-10-25 11:08:22 -07001989
1990 if (!ret && list_empty(&parse_state.list)) {
1991 WARN_ONCE(true, "WARNING: event parser found nothing\n");
1992 return -1;
1993 }
1994
1995 /*
1996 * Add list to the evlist even with errors to allow callers to clean up.
1997 */
1998 perf_evlist__splice_list_tail(evlist, &parse_state.list);
1999
Jiri Olsa89812fc2012-03-15 20:09:15 +01002000 if (!ret) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002001 struct evsel *last;
Wang Nan15bfd2c2015-07-10 07:36:09 +00002002
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002003 evlist->nr_groups += parse_state.nr_groups;
Jiri Olsa515dbe42019-09-03 10:39:52 +02002004 last = evlist__last(evlist);
Wang Nan15bfd2c2015-07-10 07:36:09 +00002005 last->cmdline_group_boundary = true;
2006
Jiri Olsa89812fc2012-03-15 20:09:15 +01002007 return 0;
2008 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02002009
Jiri Olsa5d7be902012-03-20 19:15:40 +01002010 /*
2011 * There are 2 users - builtin-record and builtin-test objects.
Jiri Olsac12995a2019-07-21 13:23:56 +02002012 * Both call evlist__delete in case of error, so we dont
Jiri Olsa5d7be902012-03-20 19:15:40 +01002013 * need to bother.
2014 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +02002015 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002016}
2017
Jiri Olsab39b8392015-04-22 21:10:16 +02002018#define MAX_WIDTH 1000
2019static int get_term_width(void)
2020{
2021 struct winsize ws;
2022
2023 get_term_dimensions(&ws);
2024 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
2025}
2026
Ian Rogersa910e462019-11-15 23:46:52 -08002027static void __parse_events_print_error(int err_idx, const char *err_str,
2028 const char *err_help, const char *event)
Jiri Olsab39b8392015-04-22 21:10:16 +02002029{
2030 const char *str = "invalid or unsupported event: ";
2031 char _buf[MAX_WIDTH];
2032 char *buf = (char *) event;
2033 int idx = 0;
Ian Rogersa910e462019-11-15 23:46:52 -08002034 if (err_str) {
Jiri Olsab39b8392015-04-22 21:10:16 +02002035 /* -2 for extra '' in the final fprintf */
2036 int width = get_term_width() - 2;
2037 int len_event = strlen(event);
2038 int len_str, max_len, cut = 0;
2039
2040 /*
2041 * Maximum error index indent, we will cut
2042 * the event string if it's bigger.
2043 */
Adrian Hunter141b2d32015-07-17 19:33:51 +03002044 int max_err_idx = 13;
Jiri Olsab39b8392015-04-22 21:10:16 +02002045
2046 /*
2047 * Let's be specific with the message when
2048 * we have the precise error.
2049 */
2050 str = "event syntax error: ";
2051 len_str = strlen(str);
2052 max_len = width - len_str;
2053
2054 buf = _buf;
2055
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -08002056 /* We're cutting from the beginning. */
Ian Rogersa910e462019-11-15 23:46:52 -08002057 if (err_idx > max_err_idx)
2058 cut = err_idx - max_err_idx;
Jiri Olsab39b8392015-04-22 21:10:16 +02002059
2060 strncpy(buf, event + cut, max_len);
2061
2062 /* Mark cut parts with '..' on both sides. */
2063 if (cut)
2064 buf[0] = buf[1] = '.';
2065
2066 if ((len_event - cut) > max_len) {
2067 buf[max_len - 1] = buf[max_len - 2] = '.';
2068 buf[max_len] = 0;
2069 }
2070
Ian Rogersa910e462019-11-15 23:46:52 -08002071 idx = len_str + err_idx - cut;
Jiri Olsab39b8392015-04-22 21:10:16 +02002072 }
2073
2074 fprintf(stderr, "%s'%s'\n", str, buf);
2075 if (idx) {
Ian Rogersa910e462019-11-15 23:46:52 -08002076 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str);
2077 if (err_help)
2078 fprintf(stderr, "\n%s\n", err_help);
2079 }
2080}
2081
2082void parse_events_print_error(struct parse_events_error *err,
2083 const char *event)
2084{
2085 if (!err->num_errors)
2086 return;
2087
2088 __parse_events_print_error(err->idx, err->str, err->help, event);
2089 zfree(&err->str);
2090 zfree(&err->help);
2091
2092 if (err->num_errors > 1) {
2093 fputs("\nInitial error:\n", stderr);
2094 __parse_events_print_error(err->first_idx, err->first_str,
2095 err->first_help, event);
2096 zfree(&err->first_str);
2097 zfree(&err->first_help);
Jiri Olsab39b8392015-04-22 21:10:16 +02002098 }
Jiri Olsab39b8392015-04-22 21:10:16 +02002099}
2100
2101#undef MAX_WIDTH
2102
Jiri Olsaf120f9d2011-07-14 11:25:32 +02002103int parse_events_option(const struct option *opt, const char *str,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002104 int unset __maybe_unused)
Jiri Olsaf120f9d2011-07-14 11:25:32 +02002105{
Jiri Olsa63503db2019-07-21 13:23:52 +02002106 struct evlist *evlist = *(struct evlist **)opt->value;
Ian Rogersa910e462019-11-15 23:46:52 -08002107 struct parse_events_error err;
2108 int ret;
2109
2110 bzero(&err, sizeof(err));
2111 ret = parse_events(evlist, str, &err);
Andi Kleen9175ce12012-10-26 13:30:06 -07002112
Andi Kleen333b5662017-09-13 14:50:06 -07002113 if (ret) {
Jiri Olsab39b8392015-04-22 21:10:16 +02002114 parse_events_print_error(&err, str);
Andi Kleen333b5662017-09-13 14:50:06 -07002115 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2116 }
Jiri Olsab39b8392015-04-22 21:10:16 +02002117
Andi Kleen9175ce12012-10-26 13:30:06 -07002118 return ret;
Jiri Olsaf120f9d2011-07-14 11:25:32 +02002119}
2120
Wang Nan4ba1faa2015-07-10 07:36:10 +00002121static int
Jiri Olsa63503db2019-07-21 13:23:52 +02002122foreach_evsel_in_last_glob(struct evlist *evlist,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002123 int (*func)(struct evsel *evsel,
Wang Nan4ba1faa2015-07-10 07:36:10 +00002124 const void *arg),
2125 const void *arg)
Li Zefanc171b552009-10-15 11:22:07 +08002126{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002127 struct evsel *last = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002128 int err;
Li Zefanc171b552009-10-15 11:22:07 +08002129
Wang Nan854f7362015-09-06 07:13:17 +00002130 /*
2131 * Don't return when list_empty, give func a chance to report
2132 * error when it found last == NULL.
2133 *
2134 * So no need to WARN here, let *func do this.
2135 */
Jiri Olsa6484d2f2019-07-21 13:24:28 +02002136 if (evlist->core.nr_entries > 0)
Jiri Olsa515dbe42019-09-03 10:39:52 +02002137 last = evlist__last(evlist);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02002138
Wang Nan15bfd2c2015-07-10 07:36:09 +00002139 do {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002140 err = (*func)(last, arg);
2141 if (err)
Wang Nan15bfd2c2015-07-10 07:36:09 +00002142 return -1;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002143 if (!last)
2144 return 0;
Wang Nan15bfd2c2015-07-10 07:36:09 +00002145
Jiri Olsace9036a2019-07-21 13:24:23 +02002146 if (last->core.node.prev == &evlist->core.entries)
Wang Nan15bfd2c2015-07-10 07:36:09 +00002147 return 0;
Jiri Olsab27c4ec2019-07-21 13:24:22 +02002148 last = list_entry(last->core.node.prev, struct evsel, core.node);
Wang Nan15bfd2c2015-07-10 07:36:09 +00002149 } while (!last->cmdline_group_boundary);
Li Zefanc171b552009-10-15 11:22:07 +08002150
2151 return 0;
2152}
2153
Jiri Olsa32dcd022019-07-21 13:23:51 +02002154static int set_filter(struct evsel *evsel, const void *arg)
Wang Nan4ba1faa2015-07-10 07:36:10 +00002155{
2156 const char *str = arg;
Mathieu Poirier1e857482016-09-16 08:44:05 -06002157 bool found = false;
2158 int nr_addr_filters = 0;
2159 struct perf_pmu *pmu = NULL;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002160
Jack Henschel49836f72018-07-04 14:13:45 +02002161 if (evsel == NULL) {
2162 fprintf(stderr,
2163 "--filter option should follow a -e tracepoint or HW tracer option\n");
2164 return -1;
2165 }
Mathieu Poirier1e857482016-09-16 08:44:05 -06002166
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002167 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
Mathieu Poirier1e857482016-09-16 08:44:05 -06002168 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
2169 fprintf(stderr,
2170 "not enough memory to hold filter string\n");
2171 return -1;
2172 }
2173
2174 return 0;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002175 }
2176
Mathieu Poirier1e857482016-09-16 08:44:05 -06002177 while ((pmu = perf_pmu__scan(pmu)) != NULL)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002178 if (pmu->type == evsel->core.attr.type) {
Mathieu Poirier1e857482016-09-16 08:44:05 -06002179 found = true;
2180 break;
2181 }
2182
2183 if (found)
2184 perf_pmu__scan_file(pmu, "nr_addr_filters",
2185 "%d", &nr_addr_filters);
2186
Jack Henschel49836f72018-07-04 14:13:45 +02002187 if (!nr_addr_filters) {
2188 fprintf(stderr,
2189 "This CPU does not support address filtering\n");
2190 return -1;
2191 }
Mathieu Poirier1e857482016-09-16 08:44:05 -06002192
2193 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002194 fprintf(stderr,
2195 "not enough memory to hold filter string\n");
2196 return -1;
2197 }
2198
2199 return 0;
2200}
2201
2202int parse_filter(const struct option *opt, const char *str,
2203 int unset __maybe_unused)
2204{
Jiri Olsa63503db2019-07-21 13:23:52 +02002205 struct evlist *evlist = *(struct evlist **)opt->value;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002206
2207 return foreach_evsel_in_last_glob(evlist, set_filter,
2208 (const void *)str);
2209}
2210
Jiri Olsa32dcd022019-07-21 13:23:51 +02002211static int add_exclude_perf_filter(struct evsel *evsel,
Wang Nan4ba1faa2015-07-10 07:36:10 +00002212 const void *arg __maybe_unused)
2213{
2214 char new_filter[64];
2215
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002216 if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002217 fprintf(stderr,
2218 "--exclude-perf option should follow a -e tracepoint option\n");
2219 return -1;
2220 }
2221
2222 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2223
Mathieu Poirier3541c032016-09-16 08:44:04 -06002224 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
Wang Nan4ba1faa2015-07-10 07:36:10 +00002225 fprintf(stderr,
2226 "not enough memory to hold filter string\n");
2227 return -1;
2228 }
2229
2230 return 0;
2231}
2232
2233int exclude_perf(const struct option *opt,
2234 const char *arg __maybe_unused,
2235 int unset __maybe_unused)
2236{
Jiri Olsa63503db2019-07-21 13:23:52 +02002237 struct evlist *evlist = *(struct evlist **)opt->value;
Wang Nan4ba1faa2015-07-10 07:36:10 +00002238
2239 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2240 NULL);
2241}
2242
Thomas Gleixner86847b62009-06-06 12:24:17 +02002243static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +02002244 "Hardware event",
2245 "Software event",
2246 "Tracepoint event",
2247 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +08002248 "Raw hardware event descriptor",
2249 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +02002250};
2251
Yunlong Songab0e4802015-02-27 18:21:25 +08002252static int cmp_string(const void *a, const void *b)
2253{
2254 const char * const *as = a;
2255 const char * const *bs = b;
2256
2257 return strcmp(*as, *bs);
2258}
2259
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002260/*
Jason Baronf6bdafe2009-07-21 12:20:22 -04002261 * Print the events from <debugfs_mount_point>/tracing/events
2262 */
2263
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002264void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2265 bool name_only)
Jason Baronf6bdafe2009-07-21 12:20:22 -04002266{
2267 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002268 struct dirent *sys_dirent, *evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002269 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002270 char *dir_path;
Yunlong Songab0e4802015-02-27 18:21:25 +08002271 char **evt_list = NULL;
2272 unsigned int evt_i = 0, evt_num = 0;
2273 bool evt_num_known = false;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002274
Yunlong Songab0e4802015-02-27 18:21:25 +08002275restart:
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -03002276 sys_dir = tracing_events__opendir();
Jason Baronf6bdafe2009-07-21 12:20:22 -04002277 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +02002278 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002279
Yunlong Songab0e4802015-02-27 18:21:25 +08002280 if (evt_num_known) {
2281 evt_list = zalloc(sizeof(char *) * evt_num);
2282 if (!evt_list)
2283 goto out_close_sys_dir;
2284 }
2285
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002286 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03002287 if (subsys_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002288 !strglobmatch(sys_dirent->d_name, subsys_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002289 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02002290
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002291 dir_path = get_events_file(sys_dirent->d_name);
2292 if (!dir_path)
2293 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +02002294 evt_dir = opendir(dir_path);
2295 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002296 goto next;
Eric Dumazet725b1362009-09-24 15:39:09 +02002297
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -03002298 for_each_event(dir_path, evt_dir, evt_dirent) {
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03002299 if (event_glob != NULL &&
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002300 !strglobmatch(evt_dirent->d_name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002301 continue;
2302
Yunlong Songab0e4802015-02-27 18:21:25 +08002303 if (!evt_num_known) {
2304 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002305 continue;
2306 }
2307
Jason Baronf6bdafe2009-07-21 12:20:22 -04002308 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002309 sys_dirent->d_name, evt_dirent->d_name);
Yunlong Songab0e4802015-02-27 18:21:25 +08002310
2311 evt_list[evt_i] = strdup(evt_path);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002312 if (evt_list[evt_i] == NULL) {
2313 put_events_file(dir_path);
Yunlong Songab0e4802015-02-27 18:21:25 +08002314 goto out_close_evt_dir;
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002315 }
Yunlong Songab0e4802015-02-27 18:21:25 +08002316 evt_i++;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002317 }
2318 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002319next:
2320 put_events_file(dir_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -04002321 }
Jason Baronf6bdafe2009-07-21 12:20:22 -04002322 closedir(sys_dir);
Yunlong Songab0e4802015-02-27 18:21:25 +08002323
2324 if (!evt_num_known) {
2325 evt_num_known = true;
2326 goto restart;
2327 }
2328 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2329 evt_i = 0;
2330 while (evt_i < evt_num) {
2331 if (name_only) {
2332 printf("%s ", evt_list[evt_i++]);
2333 continue;
2334 }
2335 printf(" %-50s [%s]\n", evt_list[evt_i++],
2336 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2337 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002338 if (evt_num && pager_in_use())
Yunlong Songab0e4802015-02-27 18:21:25 +08002339 printf("\n");
2340
2341out_free:
2342 evt_num = evt_i;
2343 for (evt_i = 0; evt_i < evt_num; evt_i++)
2344 zfree(&evt_list[evt_i]);
2345 zfree(&evt_list);
2346 return;
2347
2348out_close_evt_dir:
2349 closedir(evt_dir);
2350out_close_sys_dir:
2351 closedir(sys_dir);
2352
2353 printf("FATAL: not enough memory to print %s\n",
2354 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2355 if (evt_list)
2356 goto out_free;
Jason Baronf6bdafe2009-07-21 12:20:22 -04002357}
2358
2359/*
Thomas Renninger20c457b2011-01-03 17:50:45 +01002360 * Check whether event is in <debugfs_mount_point>/tracing/events
2361 */
2362
2363int is_valid_tracepoint(const char *event_string)
2364{
2365 DIR *sys_dir, *evt_dir;
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002366 struct dirent *sys_dirent, *evt_dirent;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002367 char evt_path[MAXPATHLEN];
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002368 char *dir_path;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002369
Arnaldo Carvalho de Melo7014e0e2018-05-17 14:42:39 -03002370 sys_dir = tracing_events__opendir();
Thomas Renninger20c457b2011-01-03 17:50:45 +01002371 if (!sys_dir)
2372 return 0;
2373
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002374 for_each_subsystem(sys_dir, sys_dirent) {
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002375 dir_path = get_events_file(sys_dirent->d_name);
2376 if (!dir_path)
2377 continue;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002378 evt_dir = opendir(dir_path);
2379 if (!evt_dir)
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002380 goto next;
Thomas Renninger20c457b2011-01-03 17:50:45 +01002381
Arnaldo Carvalho de Meloc02cab22018-05-17 14:22:37 -03002382 for_each_event(dir_path, evt_dir, evt_dirent) {
Thomas Renninger20c457b2011-01-03 17:50:45 +01002383 snprintf(evt_path, MAXPATHLEN, "%s:%s",
Arnaldo Carvalho de Melo22a9f412016-04-08 11:53:02 -03002384 sys_dirent->d_name, evt_dirent->d_name);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002385 if (!strcmp(evt_path, event_string)) {
2386 closedir(evt_dir);
2387 closedir(sys_dir);
2388 return 1;
2389 }
2390 }
2391 closedir(evt_dir);
Arnaldo Carvalho de Melo25a7d9142018-05-17 14:27:29 -03002392next:
2393 put_events_file(dir_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +01002394 }
2395 closedir(sys_dir);
2396 return 0;
2397}
2398
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002399static bool is_event_supported(u8 type, unsigned config)
2400{
2401 bool ret = true;
Vince Weaver88fee522013-12-30 15:39:45 -05002402 int open_return;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002403 struct evsel *evsel;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002404 struct perf_event_attr attr = {
2405 .type = type,
2406 .config = config,
2407 .disabled = 1,
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002408 };
Jiri Olsa9749b902019-07-21 13:23:50 +02002409 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
Arnaldo Carvalho de Melo89896052017-02-14 10:59:04 -03002410
2411 if (tmap == NULL)
2412 return false;
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002413
Jiri Olsa365c3ae2019-07-21 13:23:58 +02002414 evsel = evsel__new(&attr);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002415 if (evsel) {
Jiri Olsa5972d1e2019-07-21 13:24:01 +02002416 open_return = evsel__open(evsel, NULL, tmap);
Vince Weaver88fee522013-12-30 15:39:45 -05002417 ret = open_return >= 0;
2418
2419 if (open_return == -EACCES) {
2420 /*
2421 * This happens if the paranoid value
2422 * /proc/sys/kernel/perf_event_paranoid is set to 2
2423 * Re-run with exclude_kernel set; we don't do that
2424 * by default as some ARM machines do not support it.
2425 *
2426 */
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002427 evsel->core.attr.exclude_kernel = 1;
Jiri Olsa5972d1e2019-07-21 13:24:01 +02002428 ret = evsel__open(evsel, NULL, tmap) >= 0;
Vince Weaver88fee522013-12-30 15:39:45 -05002429 }
Jiri Olsa5eb2dd22019-07-21 13:23:57 +02002430 evsel__delete(evsel);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002431 }
2432
Jiri Olsa7836e522019-07-21 13:24:20 +02002433 perf_thread_map__put(tmap);
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002434 return ret;
2435}
2436
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002437void print_sdt_events(const char *subsys_glob, const char *event_glob,
2438 bool name_only)
2439{
2440 struct probe_cache *pcache;
2441 struct probe_cache_entry *ent;
2442 struct strlist *bidlist, *sdtlist;
2443 struct strlist_config cfg = {.dont_dupstr = true};
2444 struct str_node *nd, *nd2;
2445 char *buf, *path, *ptr = NULL;
2446 bool show_detail = false;
2447 int ret;
2448
2449 sdtlist = strlist__new(NULL, &cfg);
2450 if (!sdtlist) {
2451 pr_debug("Failed to allocate new strlist for SDT\n");
2452 return;
2453 }
2454 bidlist = build_id_cache__list_all(true);
2455 if (!bidlist) {
2456 pr_debug("Failed to get buildids: %d\n", errno);
2457 return;
2458 }
2459 strlist__for_each_entry(nd, bidlist) {
Krister Johansenf045b8c2017-07-05 18:48:11 -07002460 pcache = probe_cache__new(nd->s, NULL);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002461 if (!pcache)
2462 continue;
2463 list_for_each_entry(ent, &pcache->entries, node) {
2464 if (!ent->sdt)
2465 continue;
2466 if (subsys_glob &&
2467 !strglobmatch(ent->pev.group, subsys_glob))
2468 continue;
2469 if (event_glob &&
2470 !strglobmatch(ent->pev.event, event_glob))
2471 continue;
2472 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2473 ent->pev.event, nd->s);
2474 if (ret > 0)
2475 strlist__add(sdtlist, buf);
2476 }
2477 probe_cache__delete(pcache);
2478 }
2479 strlist__delete(bidlist);
2480
2481 strlist__for_each_entry(nd, sdtlist) {
2482 buf = strchr(nd->s, '@');
2483 if (buf)
2484 *(buf++) = '\0';
2485 if (name_only) {
2486 printf("%s ", nd->s);
2487 continue;
2488 }
2489 nd2 = strlist__next(nd);
2490 if (nd2) {
2491 ptr = strchr(nd2->s, '@');
2492 if (ptr)
2493 *ptr = '\0';
2494 if (strcmp(nd->s, nd2->s) == 0)
2495 show_detail = true;
2496 }
2497 if (show_detail) {
2498 path = build_id_cache__origname(buf);
2499 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2500 if (ret > 0) {
2501 printf(" %-50s [%s]\n", buf, "SDT event");
2502 free(buf);
2503 }
Changbin Du8bde8512019-03-16 16:05:46 +08002504 free(path);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002505 } else
2506 printf(" %-50s [%s]\n", nd->s, "SDT event");
2507 if (nd2) {
2508 if (strcmp(nd->s, nd2->s) != 0)
2509 show_detail = false;
2510 if (ptr)
2511 *ptr = '@';
2512 }
2513 }
2514 strlist__delete(sdtlist);
2515}
2516
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002517int print_hwcache_events(const char *event_glob, bool name_only)
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002518{
Yunlong Songab0e4802015-02-27 18:21:25 +08002519 unsigned int type, op, i, evt_i = 0, evt_num = 0;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002520 char name[64];
Yunlong Songab0e4802015-02-27 18:21:25 +08002521 char **evt_list = NULL;
2522 bool evt_num_known = false;
2523
2524restart:
2525 if (evt_num_known) {
2526 evt_list = zalloc(sizeof(char *) * evt_num);
2527 if (!evt_list)
2528 goto out_enomem;
2529 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002530
2531 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2532 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2533 /* skip invalid cache type */
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002534 if (!perf_evsel__is_cache_op_valid(type, op))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002535 continue;
2536
2537 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -03002538 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2539 name, sizeof(name));
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002540 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002541 continue;
2542
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002543 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2544 type | (op << 8) | (i << 16)))
2545 continue;
2546
Yunlong Songab0e4802015-02-27 18:21:25 +08002547 if (!evt_num_known) {
2548 evt_num++;
2549 continue;
2550 }
2551
2552 evt_list[evt_i] = strdup(name);
2553 if (evt_list[evt_i] == NULL)
2554 goto out_enomem;
2555 evt_i++;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002556 }
2557 }
2558 }
2559
Yunlong Songab0e4802015-02-27 18:21:25 +08002560 if (!evt_num_known) {
2561 evt_num_known = true;
2562 goto restart;
2563 }
2564 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2565 evt_i = 0;
2566 while (evt_i < evt_num) {
2567 if (name_only) {
2568 printf("%s ", evt_list[evt_i++]);
2569 continue;
2570 }
2571 printf(" %-50s [%s]\n", evt_list[evt_i++],
2572 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2573 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002574 if (evt_num && pager_in_use())
Andi Kleendc098b32013-04-20 11:02:29 -07002575 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002576
2577out_free:
2578 evt_num = evt_i;
2579 for (evt_i = 0; evt_i < evt_num; evt_i++)
2580 zfree(&evt_list[evt_i]);
2581 zfree(&evt_list);
2582 return evt_num;
2583
2584out_enomem:
2585 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2586 if (evt_list)
2587 goto out_free;
2588 return evt_num;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002589}
2590
Andi Kleen5e0861b2019-03-26 15:18:23 -07002591static void print_tool_event(const char *name, const char *event_glob,
2592 bool name_only)
2593{
2594 if (event_glob && !strglobmatch(name, event_glob))
2595 return;
2596 if (name_only)
2597 printf("%s ", name);
2598 else
2599 printf(" %-50s [%s]\n", name, "Tool event");
2600
2601}
2602
2603void print_tool_events(const char *event_glob, bool name_only)
2604{
2605 print_tool_event("duration_time", event_glob, name_only);
2606 if (pager_in_use())
2607 printf("\n");
2608}
2609
Yunlong Song705750f2015-02-27 18:21:27 +08002610void print_symbol_events(const char *event_glob, unsigned type,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002611 struct event_symbol *syms, unsigned max,
2612 bool name_only)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002613{
Yunlong Songab0e4802015-02-27 18:21:25 +08002614 unsigned int i, evt_i = 0, evt_num = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002615 char name[MAX_NAME_LEN];
Yunlong Songab0e4802015-02-27 18:21:25 +08002616 char **evt_list = NULL;
2617 bool evt_num_known = false;
2618
2619restart:
2620 if (evt_num_known) {
2621 evt_list = zalloc(sizeof(char *) * evt_num);
2622 if (!evt_list)
2623 goto out_enomem;
2624 syms -= max;
2625 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002626
Jiri Olsa1dc127602012-07-04 00:00:44 +02002627 for (i = 0; i < max; i++, syms++) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002628
Arnaldo Carvalho de Meloe37df6c2015-11-27 16:04:58 -03002629 if (event_glob != NULL && syms->symbol != NULL &&
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002630 !(strglobmatch(syms->symbol, event_glob) ||
2631 (syms->alias && strglobmatch(syms->alias, event_glob))))
2632 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002633
Namhyung Kimb41f1ce2013-08-27 11:41:53 +09002634 if (!is_event_supported(type, i))
2635 continue;
2636
Yunlong Songab0e4802015-02-27 18:21:25 +08002637 if (!evt_num_known) {
2638 evt_num++;
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002639 continue;
2640 }
2641
Yunlong Songab0e4802015-02-27 18:21:25 +08002642 if (!name_only && strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02002643 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05302644 else
Arnaldo Carvalho de Melobd8d57f2018-12-06 13:52:13 -03002645 strlcpy(name, syms->symbol, MAX_NAME_LEN);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002646
Yunlong Songab0e4802015-02-27 18:21:25 +08002647 evt_list[evt_i] = strdup(name);
2648 if (evt_list[evt_i] == NULL)
2649 goto out_enomem;
2650 evt_i++;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002651 }
2652
Yunlong Songab0e4802015-02-27 18:21:25 +08002653 if (!evt_num_known) {
2654 evt_num_known = true;
2655 goto restart;
2656 }
2657 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2658 evt_i = 0;
2659 while (evt_i < evt_num) {
2660 if (name_only) {
2661 printf("%s ", evt_list[evt_i++]);
2662 continue;
2663 }
2664 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2665 }
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002666 if (evt_num && pager_in_use())
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002667 printf("\n");
Yunlong Songab0e4802015-02-27 18:21:25 +08002668
2669out_free:
2670 evt_num = evt_i;
2671 for (evt_i = 0; evt_i < evt_num; evt_i++)
2672 zfree(&evt_list[evt_i]);
2673 zfree(&evt_list);
2674 return;
2675
2676out_enomem:
2677 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2678 if (evt_list)
2679 goto out_free;
Jiri Olsa1dc127602012-07-04 00:00:44 +02002680}
2681
2682/*
2683 * Print the help text for the event symbols:
2684 */
Sukadev Bhattiproluc8d68282016-09-15 15:24:48 -07002685void print_events(const char *event_glob, bool name_only, bool quiet_flag,
Jin Yaoa7f6c8c2019-10-15 10:53:57 +08002686 bool long_desc, bool details_flag, bool deprecated)
Jiri Olsa1dc127602012-07-04 00:00:44 +02002687{
Jiri Olsa1dc127602012-07-04 00:00:44 +02002688 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002689 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002690
2691 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002692 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
Andi Kleen5e0861b2019-03-26 15:18:23 -07002693 print_tool_events(event_glob, name_only);
Jiri Olsa1dc127602012-07-04 00:00:44 +02002694
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002695 print_hwcache_events(event_glob, name_only);
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002696
Andi Kleenbf874fc2017-03-20 13:17:11 -07002697 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
Jin Yaoa7f6c8c2019-10-15 10:53:57 +08002698 details_flag, deprecated);
Andi Kleendc098b32013-04-20 11:02:29 -07002699
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02002700 if (event_glob != NULL)
2701 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05302702
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002703 if (!name_only) {
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002704 printf(" %-50s [%s]\n",
2705 "rNNN",
2706 event_type_descriptors[PERF_TYPE_RAW]);
2707 printf(" %-50s [%s]\n",
2708 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2709 event_type_descriptors[PERF_TYPE_RAW]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002710 if (pager_in_use())
2711 printf(" (see 'man perf-list' on how to encode it)\n\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02002712
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002713 printf(" %-50s [%s]\n",
Jacob Shin3741eb92014-05-29 17:26:51 +02002714 "mem:<addr>[/len][:access]",
Liming Wang41bdcb22009-12-29 16:37:07 +08002715 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Arnaldo Carvalho de Melodfc431c2015-09-30 17:13:26 -03002716 if (pager_in_use())
2717 printf("\n");
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002718 }
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01002719
Frederic Weisbeckera3277d22012-08-09 16:31:52 +02002720 print_tracepoint_events(NULL, NULL, name_only);
Masami Hiramatsu40218da2016-06-24 18:06:46 +09002721
2722 print_sdt_events(NULL, NULL, name_only);
Andi Kleen71b0acc2017-08-31 12:40:32 -07002723
Jiri Olsa33bbc572019-02-13 13:32:41 +01002724 metricgroup__print(true, true, NULL, name_only, details_flag);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002725}
Jiri Olsa8f707d82012-03-15 20:09:16 +01002726
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002727int parse_events__is_hardcoded_term(struct parse_events_term *term)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002728{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002729 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002730}
2731
Jiri Olsa67b49b32017-02-17 15:00:55 +01002732static int new_term(struct parse_events_term **_term,
2733 struct parse_events_term *temp,
2734 char *str, u64 num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002735{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002736 struct parse_events_term *term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002737
Jiri Olsa67b49b32017-02-17 15:00:55 +01002738 term = malloc(sizeof(*term));
Jiri Olsa8f707d82012-03-15 20:09:16 +01002739 if (!term)
2740 return -ENOMEM;
2741
Jiri Olsa67b49b32017-02-17 15:00:55 +01002742 *term = *temp;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002743 INIT_LIST_HEAD(&term->list);
Andi Kleenc2f1cea2017-10-20 13:27:55 -07002744 term->weak = false;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002745
Jiri Olsa67b49b32017-02-17 15:00:55 +01002746 switch (term->type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01002747 case PARSE_EVENTS__TERM_TYPE_NUM:
2748 term->val.num = num;
2749 break;
2750 case PARSE_EVENTS__TERM_TYPE_STR:
2751 term->val.str = str;
2752 break;
2753 default:
Adrian Hunter4be8be62013-07-04 16:20:24 +03002754 free(term);
Jiri Olsa8f707d82012-03-15 20:09:16 +01002755 return -EINVAL;
2756 }
2757
2758 *_term = term;
2759 return 0;
2760}
2761
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002762int parse_events_term__num(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002763 int type_term, char *config, u64 num,
Jiri Olsa99e71382017-02-17 15:00:56 +01002764 bool no_value,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002765 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002766{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002767 YYLTYPE *loc_term = loc_term_;
2768 YYLTYPE *loc_val = loc_val_;
2769
Jiri Olsa67b49b32017-02-17 15:00:55 +01002770 struct parse_events_term temp = {
2771 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2772 .type_term = type_term,
2773 .config = config,
Jiri Olsa99e71382017-02-17 15:00:56 +01002774 .no_value = no_value,
Jiri Olsa67b49b32017-02-17 15:00:55 +01002775 .err_term = loc_term ? loc_term->first_column : 0,
2776 .err_val = loc_val ? loc_val->first_column : 0,
2777 };
2778
2779 return new_term(term, &temp, NULL, num);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002780}
2781
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002782int parse_events_term__str(struct parse_events_term **term,
Jiri Olsacecf3a2e2015-04-22 21:10:20 +02002783 int type_term, char *config, char *str,
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002784 void *loc_term_, void *loc_val_)
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002785{
Adrian Hunterbb78ce72015-05-19 16:05:42 +03002786 YYLTYPE *loc_term = loc_term_;
2787 YYLTYPE *loc_val = loc_val_;
2788
Jiri Olsa67b49b32017-02-17 15:00:55 +01002789 struct parse_events_term temp = {
2790 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2791 .type_term = type_term,
2792 .config = config,
2793 .err_term = loc_term ? loc_term->first_column : 0,
2794 .err_val = loc_val ? loc_val->first_column : 0,
2795 };
2796
2797 return new_term(term, &temp, str, 0);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02002798}
2799
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002800int parse_events_term__sym_hw(struct parse_events_term **term,
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002801 char *config, unsigned idx)
2802{
2803 struct event_symbol *sym;
Ian Rogersb6645a72019-10-30 15:34:43 -07002804 char *str;
Jiri Olsa67b49b32017-02-17 15:00:55 +01002805 struct parse_events_term temp = {
2806 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2807 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
Ian Rogersb6645a72019-10-30 15:34:43 -07002808 .config = config,
Jiri Olsa67b49b32017-02-17 15:00:55 +01002809 };
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002810
Ian Rogersb6645a72019-10-30 15:34:43 -07002811 if (!temp.config) {
2812 temp.config = strdup("event");
2813 if (!temp.config)
2814 return -ENOMEM;
2815 }
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002816 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2817 sym = &event_symbols_hw[idx];
2818
Ian Rogersb6645a72019-10-30 15:34:43 -07002819 str = strdup(sym->symbol);
2820 if (!str)
2821 return -ENOMEM;
2822 return new_term(term, &temp, str, 0);
Jiri Olsa1d33d6d2012-10-10 14:53:17 +02002823}
2824
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002825int parse_events_term__clone(struct parse_events_term **new,
2826 struct parse_events_term *term)
Zheng Yana6146d52012-06-15 14:31:41 +08002827{
Ian Rogersb6645a72019-10-30 15:34:43 -07002828 char *str;
Jiri Olsa67b49b32017-02-17 15:00:55 +01002829 struct parse_events_term temp = {
2830 .type_val = term->type_val,
2831 .type_term = term->type_term,
Ian Rogersb6645a72019-10-30 15:34:43 -07002832 .config = NULL,
Jiri Olsa67b49b32017-02-17 15:00:55 +01002833 .err_term = term->err_term,
2834 .err_val = term->err_val,
2835 };
2836
Ian Rogersb6645a72019-10-30 15:34:43 -07002837 if (term->config) {
2838 temp.config = strdup(term->config);
2839 if (!temp.config)
2840 return -ENOMEM;
2841 }
2842 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
2843 return new_term(new, &temp, NULL, term->val.num);
2844
2845 str = strdup(term->val.str);
2846 if (!str)
2847 return -ENOMEM;
2848 return new_term(new, &temp, str, 0);
Zheng Yana6146d52012-06-15 14:31:41 +08002849}
2850
Ian Rogers1dc92552019-10-30 15:34:47 -07002851void parse_events_term__delete(struct parse_events_term *term)
2852{
2853 if (term->array.nr_ranges)
2854 zfree(&term->array.ranges);
2855
2856 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
2857 zfree(&term->val.str);
2858
2859 zfree(&term->config);
2860 free(term);
2861}
2862
Andi Kleen82557182017-03-20 13:17:03 -07002863int parse_events_copy_term_list(struct list_head *old,
2864 struct list_head **new)
2865{
2866 struct parse_events_term *term, *n;
2867 int ret;
2868
2869 if (!old) {
2870 *new = NULL;
2871 return 0;
2872 }
2873
2874 *new = malloc(sizeof(struct list_head));
2875 if (!*new)
2876 return -ENOMEM;
2877 INIT_LIST_HEAD(*new);
2878
2879 list_for_each_entry (term, old, list) {
2880 ret = parse_events_term__clone(&n, term);
2881 if (ret)
2882 return ret;
2883 list_add_tail(&n->list, *new);
2884 }
2885 return 0;
2886}
2887
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002888void parse_events_terms__purge(struct list_head *terms)
Jiri Olsa8f707d82012-03-15 20:09:16 +01002889{
Arnaldo Carvalho de Melo6cee6cd2013-01-18 16:29:49 -03002890 struct parse_events_term *term, *h;
Jiri Olsa8f707d82012-03-15 20:09:16 +01002891
Wang Nana8adfce2016-02-12 16:31:23 -03002892 list_for_each_entry_safe(term, h, terms, list) {
2893 list_del_init(&term->list);
Ian Rogers1dc92552019-10-30 15:34:47 -07002894 parse_events_term__delete(term);
Wang Nana8adfce2016-02-12 16:31:23 -03002895 }
Jiri Olsa8f707d82012-03-15 20:09:16 +01002896}
Jiri Olsab39b8392015-04-22 21:10:16 +02002897
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002898void parse_events_terms__delete(struct list_head *terms)
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002899{
Arnaldo Carvalho de Melo2146afc2016-02-12 17:09:17 -03002900 if (!terms)
2901 return;
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002902 parse_events_terms__purge(terms);
Wang Nand20a5f22016-02-12 17:01:17 -03002903 free(terms);
Arnaldo Carvalho de Melofc0a2c12016-02-12 16:43:02 -03002904}
2905
Wang Nan2d055bf2016-02-22 09:10:34 +00002906void parse_events__clear_array(struct parse_events_array *a)
2907{
Taeung Song360e0712017-02-01 21:34:07 +09002908 zfree(&a->ranges);
Wang Nan2d055bf2016-02-22 09:10:34 +00002909}
2910
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -03002911void parse_events_evlist_error(struct parse_events_state *parse_state,
Jiri Olsab39b8392015-04-22 21:10:16 +02002912 int idx, const char *str)
2913{
Ian Rogers448d7322019-10-30 15:34:39 -07002914 if (!parse_state->error)
Adrian Huntera6ced2b2015-05-19 16:05:44 +03002915 return;
Ian Rogers448d7322019-10-30 15:34:39 -07002916
2917 parse_events__handle_error(parse_state->error, idx, strdup(str), NULL);
Jiri Olsab39b8392015-04-22 21:10:16 +02002918}
He Kuangffeb8832015-09-28 03:52:14 +00002919
Wang Nan17cb5f82016-02-19 11:43:57 +00002920static void config_terms_list(char *buf, size_t buf_sz)
2921{
2922 int i;
2923 bool first = true;
2924
2925 buf[0] = '\0';
2926 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2927 const char *name = config_term_names[i];
2928
Wang Nan1669e502016-02-19 11:43:58 +00002929 if (!config_term_avail(i, NULL))
2930 continue;
Wang Nan17cb5f82016-02-19 11:43:57 +00002931 if (!name)
2932 continue;
2933 if (name[0] == '<')
2934 continue;
2935
2936 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2937 return;
2938
2939 if (!first)
2940 strcat(buf, ",");
2941 else
2942 first = false;
2943 strcat(buf, name);
2944 }
2945}
2946
He Kuangffeb8832015-09-28 03:52:14 +00002947/*
2948 * Return string contains valid config terms of an event.
2949 * @additional_terms: For terms such as PMU sysfs terms.
2950 */
2951char *parse_events_formats_error_string(char *additional_terms)
2952{
2953 char *str;
Wang Nan626a6b72016-07-14 08:34:45 +00002954 /* "no-overwrite" is the longest name */
Wang Nan17cb5f82016-02-19 11:43:57 +00002955 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
Wang Nan626a6b72016-07-14 08:34:45 +00002956 (sizeof("no-overwrite") - 1)];
He Kuangffeb8832015-09-28 03:52:14 +00002957
Wang Nan17cb5f82016-02-19 11:43:57 +00002958 config_terms_list(static_terms, sizeof(static_terms));
He Kuangffeb8832015-09-28 03:52:14 +00002959 /* valid terms */
2960 if (additional_terms) {
Wang Nan26dee022016-02-19 11:43:52 +00002961 if (asprintf(&str, "valid terms: %s,%s",
2962 additional_terms, static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002963 goto fail;
2964 } else {
Wang Nan26dee022016-02-19 11:43:52 +00002965 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
He Kuangffeb8832015-09-28 03:52:14 +00002966 goto fail;
2967 }
2968 return str;
2969
2970fail:
2971 return NULL;
2972}